1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Rectangle;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.awt.event.MouseEvent;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
12 | import org.openstreetmap.josm.gui.MapView;
|
---|
13 | import org.openstreetmap.josm.gui.SelectionManager;
|
---|
14 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
---|
15 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
16 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Enable the zoom mode within the MapFrame.
|
---|
20 | *
|
---|
21 | * Holding down the left mouse button select a rectangle with the same aspect
|
---|
22 | * ratio than the current map view.
|
---|
23 | * Holding down left and right let the user move the former selected rectangle.
|
---|
24 | * Releasing the left button zoom to the selection.
|
---|
25 | *
|
---|
26 | * Rectangle selections with either height or width smaller than 3 pixels
|
---|
27 | * are ignored.
|
---|
28 | *
|
---|
29 | * @author imi
|
---|
30 | */
|
---|
31 | public class ZoomAction extends MapMode implements SelectionEnded {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Manager that manages the selection rectangle with the aspect ratio of the
|
---|
35 | * MapView.
|
---|
36 | */
|
---|
37 | private final SelectionManager selectionManager;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Construct a ZoomAction without a label.
|
---|
41 | * @param mapFrame The MapFrame, whose zoom mode should be enabled.
|
---|
42 | */
|
---|
43 | public ZoomAction(MapFrame mapFrame) {
|
---|
44 | super(tr("Zoom"), "zoom", tr("Zoom and move map"),
|
---|
45 | Shortcut.registerShortcut("mapmode:zoom", tr("Mode: {0}", tr("Zoom")), KeyEvent.VK_Z, Shortcut.DIRECT),
|
---|
46 | mapFrame, ImageProvider.getCursor("normal", "zoom"));
|
---|
47 | selectionManager = new SelectionManager(this, true, mapFrame.mapView);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Zoom to the rectangle on the map.
|
---|
52 | */
|
---|
53 | @Override
|
---|
54 | public void selectionEnded(Rectangle r, MouseEvent e) {
|
---|
55 | if (r.width >= 3 && r.height >= 3 && Main.isDisplayingMapView()) {
|
---|
56 | MapView mv = Main.map.mapView;
|
---|
57 | mv.zoomToFactor(mv.getEastNorth(r.x+r.width/2, r.y+r.height/2), r.getWidth()/mv.getWidth());
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override public void enterMode() {
|
---|
62 | super.enterMode();
|
---|
63 | selectionManager.register(Main.map.mapView, false);
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override public void exitMode() {
|
---|
67 | super.exitMode();
|
---|
68 | selectionManager.unregister(Main.map.mapView);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override public String getModeHelpText() {
|
---|
72 | return tr("Zoom by dragging or Ctrl+. or Ctrl+,; move with Ctrl+up, left, down, right; move zoom with right button");
|
---|
73 | }
|
---|
74 | }
|
---|