[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[3] | 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
| 4 | import java.awt.Cursor;
|
---|
[102] | 5 | import java.awt.Point;
|
---|
[101] | 6 | import java.awt.event.ActionEvent;
|
---|
| 7 | import java.awt.event.KeyEvent;
|
---|
[3] | 8 | import java.awt.event.MouseAdapter;
|
---|
| 9 | import java.awt.event.MouseEvent;
|
---|
| 10 | import java.awt.event.MouseMotionListener;
|
---|
| 11 | import java.awt.event.MouseWheelEvent;
|
---|
| 12 | import java.awt.event.MouseWheelListener;
|
---|
| 13 |
|
---|
[101] | 14 | import javax.swing.AbstractAction;
|
---|
| 15 | import javax.swing.JComponent;
|
---|
[237] | 16 | import javax.swing.JPanel;
|
---|
[101] | 17 | import javax.swing.KeyStroke;
|
---|
| 18 |
|
---|
[71] | 19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[3] | 20 |
|
---|
| 21 | /**
|
---|
| 22 | * Enables moving of the map by holding down the right mouse button and drag
|
---|
| 23 | * the mouse. Also, enables zooming by the mouse wheel.
|
---|
| 24 | *
|
---|
| 25 | * @author imi
|
---|
| 26 | */
|
---|
[237] | 27 | public class MapMover extends MouseAdapter implements MouseMotionListener, MouseWheelListener {
|
---|
[3] | 28 |
|
---|
[101] | 29 | private final class ZoomerAction extends AbstractAction {
|
---|
| 30 | private final String action;
|
---|
| 31 | public ZoomerAction(String action) {
|
---|
| 32 | this.action = action;
|
---|
| 33 | }
|
---|
| 34 | public void actionPerformed(ActionEvent e) {
|
---|
[135] | 35 | if (action.equals(".") || action.equals(",")) {
|
---|
[102] | 36 | Point mouse = nc.getMousePosition();
|
---|
| 37 | if (mouse == null)
|
---|
| 38 | mouse = new Point((int)nc.getBounds().getCenterX(), (int)nc.getBounds().getCenterY());
|
---|
[135] | 39 | MouseWheelEvent we = new MouseWheelEvent(nc, e.getID(), e.getWhen(), e.getModifiers(), mouse.x, mouse.y, 0, false, MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, action.equals(",") ? -1 : 1);
|
---|
[101] | 40 | mouseWheelMoved(we);
|
---|
| 41 | } else {
|
---|
| 42 | EastNorth center = nc.getCenter();
|
---|
| 43 | EastNorth newcenter = nc.getEastNorth(nc.getWidth()/2+nc.getWidth()/5, nc.getHeight()/2+nc.getHeight()/5);
|
---|
| 44 | if (action.equals("left"))
|
---|
| 45 | nc.zoomTo(new EastNorth(2*center.east()-newcenter.east(), center.north()), nc.getScale());
|
---|
| 46 | else if (action.equals("right"))
|
---|
| 47 | nc.zoomTo(new EastNorth(newcenter.east(), center.north()), nc.getScale());
|
---|
| 48 | else if (action.equals("up"))
|
---|
| 49 | nc.zoomTo(new EastNorth(center.east(), 2*center.north()-newcenter.north()), nc.getScale());
|
---|
| 50 | else if (action.equals("down"))
|
---|
| 51 | nc.zoomTo(new EastNorth(center.east(), newcenter.north()), nc.getScale());
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[3] | 56 | /**
|
---|
| 57 | * The point in the map that was the under the mouse point
|
---|
| 58 | * when moving around started.
|
---|
| 59 | */
|
---|
[71] | 60 | private EastNorth mousePosMove;
|
---|
[3] | 61 | /**
|
---|
| 62 | * The map to move around.
|
---|
| 63 | */
|
---|
[41] | 64 | private final NavigatableComponent nc;
|
---|
[3] | 65 | /**
|
---|
| 66 | * The old cursor when we changed it to movement cursor.
|
---|
| 67 | */
|
---|
| 68 | private Cursor oldCursor;
|
---|
| 69 |
|
---|
[100] | 70 | private boolean movementInPlace = false;
|
---|
[101] | 71 |
|
---|
[3] | 72 | /**
|
---|
| 73 | * Create a new MapMover
|
---|
| 74 | */
|
---|
[237] | 75 | public MapMover(NavigatableComponent navComp, JPanel contentPane) {
|
---|
[42] | 76 | this.nc = navComp;
|
---|
[41] | 77 | nc.addMouseListener(this);
|
---|
| 78 | nc.addMouseMotionListener(this);
|
---|
| 79 | nc.addMouseWheelListener(this);
|
---|
[101] | 80 |
|
---|
[135] | 81 | String[] n = {",",".","up","right","down","left"};
|
---|
| 82 | int[] k = {KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT};
|
---|
[101] | 83 |
|
---|
[237] | 84 | if (contentPane != null) {
|
---|
[206] | 85 | for (int i = 0; i < n.length; ++i) {
|
---|
[237] | 86 | contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK), "MapMover.Zoomer."+n[i]);
|
---|
| 87 | contentPane.getActionMap().put("MapMover.Zoomer."+n[i], new ZoomerAction(n[i]));
|
---|
[206] | 88 | }
|
---|
[101] | 89 | }
|
---|
[3] | 90 | }
|
---|
[101] | 91 |
|
---|
[3] | 92 | /**
|
---|
| 93 | * If the right (and only the right) mouse button is pressed, move the map
|
---|
| 94 | */
|
---|
[391] | 95 | public void mouseDragged(MouseEvent e) {
|
---|
[3] | 96 | int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK;
|
---|
| 97 | if ((e.getModifiersEx() & (MouseEvent.BUTTON3_DOWN_MASK | offMask)) == MouseEvent.BUTTON3_DOWN_MASK) {
|
---|
| 98 | if (mousePosMove == null)
|
---|
| 99 | startMovement(e);
|
---|
[71] | 100 | EastNorth center = nc.getCenter();
|
---|
| 101 | EastNorth mouseCenter = nc.getEastNorth(e.getX(), e.getY());
|
---|
| 102 | EastNorth p = new EastNorth(
|
---|
| 103 | mousePosMove.east() + center.east() - mouseCenter.east(),
|
---|
| 104 | mousePosMove.north() + center.north() - mouseCenter.north());
|
---|
[41] | 105 | nc.zoomTo(p, nc.getScale());
|
---|
[3] | 106 | } else
|
---|
| 107 | endMovement();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Start the movement, if it was the 3rd button (right button).
|
---|
| 112 | */
|
---|
[86] | 113 | @Override public void mousePressed(MouseEvent e) {
|
---|
[3] | 114 | int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK;
|
---|
| 115 | if (e.getButton() == MouseEvent.BUTTON3 && (e.getModifiersEx() & offMask) == 0)
|
---|
| 116 | startMovement(e);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Change the cursor back to it's pre-move cursor.
|
---|
| 121 | */
|
---|
[86] | 122 | @Override public void mouseReleased(MouseEvent e) {
|
---|
[3] | 123 | if (e.getButton() == MouseEvent.BUTTON3)
|
---|
| 124 | endMovement();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Start movement by setting a new cursor and remember the current mouse
|
---|
| 129 | * position.
|
---|
[66] | 130 | * @param e The mouse event that leat to the movement from.
|
---|
[3] | 131 | */
|
---|
| 132 | private void startMovement(MouseEvent e) {
|
---|
[100] | 133 | if (movementInPlace)
|
---|
| 134 | return;
|
---|
| 135 | movementInPlace = true;
|
---|
[71] | 136 | mousePosMove = nc.getEastNorth(e.getX(), e.getY());
|
---|
[41] | 137 | oldCursor = nc.getCursor();
|
---|
| 138 | nc.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
---|
[3] | 139 | }
|
---|
[101] | 140 |
|
---|
[3] | 141 | /**
|
---|
| 142 | * End the movement. Setting back the cursor and clear the movement variables
|
---|
| 143 | */
|
---|
| 144 | private void endMovement() {
|
---|
[100] | 145 | if (!movementInPlace)
|
---|
| 146 | return;
|
---|
| 147 | movementInPlace = false;
|
---|
[3] | 148 | if (oldCursor != null)
|
---|
[41] | 149 | nc.setCursor(oldCursor);
|
---|
[3] | 150 | else
|
---|
[41] | 151 | nc.setCursor(Cursor.getDefaultCursor());
|
---|
[3] | 152 | mousePosMove = null;
|
---|
| 153 | oldCursor = null;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * Zoom the map by 1/5th of current zoom per wheel-delta.
|
---|
| 158 | * @param e The wheel event.
|
---|
| 159 | */
|
---|
[391] | 160 | public void mouseWheelMoved(MouseWheelEvent e) {
|
---|
[71] | 161 | int w = nc.getWidth();
|
---|
| 162 | int h = nc.getHeight();
|
---|
| 163 |
|
---|
[3] | 164 | double zoom = Math.max(0.1, 1 + e.getWheelRotation()/5.0);
|
---|
[71] | 165 | double zoomfactor = (zoom-1)/2+1;
|
---|
| 166 |
|
---|
| 167 | double newHalfWidth = w*zoomfactor - w/2;
|
---|
| 168 | double newHalfHeight = h*zoomfactor - h/2;
|
---|
| 169 | double centerx = e.getX() - (e.getX()-w/2)*newHalfWidth*2/w;
|
---|
| 170 | double centery = e.getY() - (e.getY()-h/2)*newHalfHeight*2/h;
|
---|
| 171 | EastNorth newCenter = nc.getEastNorth((int)centerx, (int)centery);
|
---|
[101] | 172 |
|
---|
[65] | 173 | nc.zoomTo(newCenter, nc.getScale()*zoom);
|
---|
[3] | 174 | }
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * Does nothing. Only to satisfy MouseMotionListener
|
---|
| 178 | */
|
---|
[391] | 179 | public void mouseMoved(MouseEvent e) {}
|
---|
[3] | 180 | }
|
---|