| 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.gui.MainApplication;
|
|---|
| 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 | * @since 1
|
|---|
| 31 | */
|
|---|
| 32 | public class ZoomAction extends MapMode implements SelectionEnded {
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Manager that manages the selection rectangle with the aspect ratio of the MapView.
|
|---|
| 36 | */
|
|---|
| 37 | private final transient 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 mode"), "zoom", tr("Zoom and move map"),
|
|---|
| 45 | Shortcut.registerShortcut("mapmode:zoom", tr("Mode: {0}", tr("Zoom mode")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE),
|
|---|
| 46 | 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 && MainApplication.isDisplayingMapView()) {
|
|---|
| 56 | MapView mv = MainApplication.getMap().mapView;
|
|---|
| 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);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | @Override public void enterMode() {
|
|---|
| 68 | super.enterMode();
|
|---|
| 69 | selectionManager.register(MainApplication.getMap().mapView, false);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | @Override public void exitMode() {
|
|---|
| 73 | super.exitMode();
|
|---|
| 74 | selectionManager.unregister(MainApplication.getMap().mapView);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | @Override public String getModeHelpText() {
|
|---|
| 78 | return tr("Zoom by dragging or Ctrl+. or Ctrl+,; move with Ctrl+up, left, down, right; move zoom with right button");
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|