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