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