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 javax.swing.KeyStroke;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
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 | */
|
---|
27 | public class MapMover extends MouseAdapter implements MouseMotionListener, MouseWheelListener {
|
---|
28 |
|
---|
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) {
|
---|
35 | if (action.equals(".") || action.equals(",")) {
|
---|
36 | Point mouse = nc.getMousePosition();
|
---|
37 | if (mouse == null)
|
---|
38 | mouse = new Point((int)nc.getBounds().getCenterX(), (int)nc.getBounds().getCenterY());
|
---|
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);
|
---|
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 |
|
---|
56 | /**
|
---|
57 | * The point in the map that was the under the mouse point
|
---|
58 | * when moving around started.
|
---|
59 | */
|
---|
60 | private EastNorth mousePosMove;
|
---|
61 | /**
|
---|
62 | * The map to move around.
|
---|
63 | */
|
---|
64 | private final NavigatableComponent nc;
|
---|
65 | /**
|
---|
66 | * The old cursor when we changed it to movement cursor.
|
---|
67 | */
|
---|
68 | private Cursor oldCursor;
|
---|
69 |
|
---|
70 | private boolean movementInPlace = false;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Create a new MapMover
|
---|
74 | */
|
---|
75 | public MapMover(NavigatableComponent navComp, JPanel contentPane) {
|
---|
76 | this.nc = navComp;
|
---|
77 | nc.addMouseListener(this);
|
---|
78 | nc.addMouseMotionListener(this);
|
---|
79 | nc.addMouseWheelListener(this);
|
---|
80 |
|
---|
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};
|
---|
83 |
|
---|
84 | if (contentPane != null) {
|
---|
85 | for (int i = 0; i < n.length; ++i) {
|
---|
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]));
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * If the right (and only the right) mouse button is pressed, move the map
|
---|
94 | */
|
---|
95 | public void mouseDragged(MouseEvent e) {
|
---|
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);
|
---|
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());
|
---|
105 | nc.zoomTo(p, nc.getScale());
|
---|
106 | } else
|
---|
107 | endMovement();
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Start the movement, if it was the 3rd button (right button).
|
---|
112 | */
|
---|
113 | @Override public void mousePressed(MouseEvent e) {
|
---|
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 | */
|
---|
122 | @Override public void mouseReleased(MouseEvent e) {
|
---|
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.
|
---|
130 | * @param e The mouse event that leat to the movement from.
|
---|
131 | */
|
---|
132 | private void startMovement(MouseEvent e) {
|
---|
133 | if (movementInPlace)
|
---|
134 | return;
|
---|
135 | movementInPlace = true;
|
---|
136 | mousePosMove = nc.getEastNorth(e.getX(), e.getY());
|
---|
137 | oldCursor = nc.getCursor();
|
---|
138 | nc.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * End the movement. Setting back the cursor and clear the movement variables
|
---|
143 | */
|
---|
144 | private void endMovement() {
|
---|
145 | if (!movementInPlace)
|
---|
146 | return;
|
---|
147 | movementInPlace = false;
|
---|
148 | if (oldCursor != null)
|
---|
149 | nc.setCursor(oldCursor);
|
---|
150 | else
|
---|
151 | nc.setCursor(Cursor.getDefaultCursor());
|
---|
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 | */
|
---|
160 | public void mouseWheelMoved(MouseWheelEvent e) {
|
---|
161 | int w = nc.getWidth();
|
---|
162 | int h = nc.getHeight();
|
---|
163 |
|
---|
164 | double zoom = Math.max(0.1, 1 + e.getWheelRotation()/5.0);
|
---|
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);
|
---|
172 |
|
---|
173 | nc.zoomTo(newCenter, nc.getScale()*zoom);
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Does nothing. Only to satisfy MouseMotionListener
|
---|
178 | */
|
---|
179 | public void mouseMoved(MouseEvent e) {}
|
---|
180 | }
|
---|