[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[626] | 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;
|
---|
[3594] | 8 | import java.awt.event.MouseEvent;
|
---|
[626] | 9 |
|
---|
[12630] | 10 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[626] | 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;
|
---|
[1084] | 16 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[626] | 17 |
|
---|
| 18 | /**
|
---|
[1023] | 19 | * Enable the zoom mode within the MapFrame.
|
---|
| 20 | *
|
---|
| 21 | * Holding down the left mouse button select a rectangle with the same aspect
|
---|
[626] | 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.
|
---|
[1023] | 25 | *
|
---|
| 26 | * Rectangle selections with either height or width smaller than 3 pixels
|
---|
[626] | 27 | * are ignored.
|
---|
[1023] | 28 | *
|
---|
[626] | 29 | * @author imi
|
---|
[10241] | 30 | * @since 1
|
---|
[626] | 31 | */
|
---|
| 32 | public class ZoomAction extends MapMode implements SelectionEnded {
|
---|
| 33 |
|
---|
[1169] | 34 | /**
|
---|
[8308] | 35 | * Manager that manages the selection rectangle with the aspect ratio of the MapView.
|
---|
[1169] | 36 | */
|
---|
[8308] | 37 | private final transient SelectionManager selectionManager;
|
---|
[626] | 38 |
|
---|
[1169] | 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) {
|
---|
[15046] | 44 | super(tr("Zoom mode"), "zoom", tr("Zoom and move map"),
|
---|
| 45 | Shortcut.registerShortcut("mapmode:zoom", tr("Mode: {0}", tr("Zoom mode")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
|
---|
[11713] | 46 | ImageProvider.getCursor("normal", "zoom"));
|
---|
[3443] | 47 | selectionManager = new SelectionManager(this, true, mapFrame.mapView);
|
---|
[1169] | 48 | }
|
---|
[626] | 49 |
|
---|
[1169] | 50 | /**
|
---|
| 51 | * Zoom to the rectangle on the map.
|
---|
| 52 | */
|
---|
[6084] | 53 | @Override
|
---|
[3594] | 54 | public void selectionEnded(Rectangle r, MouseEvent e) {
|
---|
[12630] | 55 | if (r.width >= 3 && r.height >= 3 && MainApplication.isDisplayingMapView()) {
|
---|
| 56 | MapView mv = MainApplication.getMap().mapView;
|
---|
[16900] | 57 | final double factor;
|
---|
| 58 | if (r.x == e.getPoint().x || r.y == e.getPoint().y) {
|
---|
| 59 | factor = mv.getWidth() / r.getWidth(); // zoom out
|
---|
| 60 | } else {
|
---|
| 61 | factor = r.getWidth() / mv.getWidth(); // zoom in
|
---|
| 62 | }
|
---|
| 63 | mv.zoomToFactor(mv.getEastNorth(r.x + r.width / 2, r.y + r.height / 2), factor);
|
---|
[1814] | 64 | }
|
---|
[1169] | 65 | }
|
---|
[626] | 66 |
|
---|
[1169] | 67 | @Override public void enterMode() {
|
---|
| 68 | super.enterMode();
|
---|
[12630] | 69 | selectionManager.register(MainApplication.getMap().mapView, false);
|
---|
[1169] | 70 | }
|
---|
[626] | 71 |
|
---|
[1169] | 72 | @Override public void exitMode() {
|
---|
| 73 | super.exitMode();
|
---|
[12630] | 74 | selectionManager.unregister(MainApplication.getMap().mapView);
|
---|
[1169] | 75 | }
|
---|
[1023] | 76 |
|
---|
[1169] | 77 | @Override public String getModeHelpText() {
|
---|
[1195] | 78 | return tr("Zoom by dragging or Ctrl+. or Ctrl+,; move with Ctrl+up, left, down, right; move zoom with right button");
|
---|
[1169] | 79 | }
|
---|
[626] | 80 | }
|
---|