source: osm/applications/editors/josm/plugins/slippy_map_chooser/src/OsmMapControl.java@ 7199

Last change on this file since 7199 was 7199, checked in by tim, 17 years ago

Initial import of slippy_map_chooser source version 1.1

File size: 7.2 KB
Line 
1// This code has been adapted and copied from code that has been written by Immanuel Scholz and others for JOSM.
2// License: GPL. Copyright 2007 by Tim Haussmann
3
4import java.awt.Cursor;
5import java.awt.Point;
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.awt.event.MouseAdapter;
9import java.awt.event.MouseEvent;
10import java.awt.event.MouseMotionListener;
11import java.awt.event.MouseWheelEvent;
12import java.awt.event.MouseWheelListener;
13
14import javax.swing.AbstractAction;
15import javax.swing.JComponent;
16import javax.swing.JPanel;
17import javax.swing.KeyStroke;
18import static org.openstreetmap.josm.tools.I18n.tr;
19
20/**
21 * This class controls the user input by listening to mouse and key events. Currently implemented is:
22 * - zooming in and out with scrollwheel
23 * - zooming in and centering by double clicking
24 * - selecting an area by clicking and dragging the mouse
25 *
26 * @author Tim Haussmann
27 */
28public class OsmMapControl extends MouseAdapter implements MouseMotionListener, MouseWheelListener {
29
30
31 //positions while moving the map
32 private int iLastX = 0;
33 private int iLastY = 0;
34
35 //start and end point of selection rectangle
36 private Point iStartSelectionPoint;
37 private Point iEndSelectionPoint;
38
39 //the SlippyMapChooserComponent
40 private final SlippyMapChooser iSlippyMapChooser;
41
42 //The old cursor when we changed it to movement cursor.
43 private Cursor iOldCursor;
44
45 private boolean isMovementInPlace = false;
46
47 private SizeButton iSizeButton = null;
48
49
50 private final class ZoomerAction extends AbstractAction {
51 private final String action;
52 public ZoomerAction(String action) {
53 this.action = action;
54 }
55 public void actionPerformed(ActionEvent e) {
56 if (action.equals(".") || action.equals(",")) {
57 Point mouse = iSlippyMapChooser.getMousePosition();
58 if (mouse == null)
59 mouse = new Point((int)iSlippyMapChooser.getBounds().getCenterX(), (int)iSlippyMapChooser.getBounds().getCenterY());
60 MouseWheelEvent we = new MouseWheelEvent(iSlippyMapChooser, e.getID(), e.getWhen(), e.getModifiers(), mouse.x, mouse.y, 0, false, MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, action.equals(",") ? -1 : 1);
61 mouseWheelMoved(we);
62 } else {
63 if (action.equals(tr("left")))
64 iSlippyMapChooser.moveMap(-30, 0);
65 else if (action.equals("right"))
66 iSlippyMapChooser.moveMap(30, 0);
67 else if (action.equals("up"))
68 iSlippyMapChooser.moveMap(0, -30);
69 else if (action.equals("down"))
70 iSlippyMapChooser.moveMap(0, 30);
71 }
72 }
73 }
74
75
76
77 /**
78 * Create a new OsmMapControl
79 */
80 public OsmMapControl(SlippyMapChooser navComp, JPanel contentPane, SizeButton sizeButton) {
81 this.iSlippyMapChooser = navComp;
82 iSlippyMapChooser.addMouseListener(this);
83 iSlippyMapChooser.addMouseMotionListener(this);
84 iSlippyMapChooser.addMouseWheelListener(this);
85
86 String[] n = {",",".","up","right","down","left"};
87 int[] k = {KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT};
88
89 if (contentPane != null) {
90 for (int i = 0; i < n.length; ++i) {
91 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK), "MapMover.Zoomer."+n[i]);
92 contentPane.getActionMap().put("MapMover.Zoomer."+n[i], new ZoomerAction(n[i]));
93 }
94 }
95
96 iSizeButton = sizeButton;
97 }
98
99 /**
100 * If the right mouse button is pressed and moved while holding, move the map.
101 * If the left mouse button is pressed start the selection rectangle and stop when left mouse button is released
102 */
103 public void mouseDragged(MouseEvent e) {
104 int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK;
105 //Moving the map
106 if ((e.getModifiersEx() & (MouseEvent.BUTTON3_DOWN_MASK | offMask)) == MouseEvent.BUTTON3_DOWN_MASK) {
107
108 startMovement(e);
109
110 //calc the moved distance since the last drag event
111 int distX = e.getX()-iLastX;
112 int distY = e.getY()-iLastY;
113
114 //change the offset for the origin of the map
115 iSlippyMapChooser.moveMap(-distX, -distY);
116
117 //update the last position of the mouse for the next dragging event
118 iLastX = e.getX();
119 iLastY = e.getY();
120 }
121 //selecting
122 else if((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK){
123 iEndSelectionPoint = e.getPoint();
124 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());
125 }
126 //end moving of the map
127 else
128 endMovement();
129 }
130
131 /**
132 * Start moving the map, if it was the 3rd button (right button).
133 * Start drawing the selection rectangle if it was the 1st button (left button)
134 */
135 @Override public void mousePressed(MouseEvent e) {
136 int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK;
137 if (e.getButton() == MouseEvent.BUTTON3 && (e.getModifiersEx() & offMask) == 0)
138 startMovement(e);
139 else if (e.getButton() == MouseEvent.BUTTON1){
140 if(!iSizeButton.hit(e.getPoint())){
141 iStartSelectionPoint = e.getPoint();
142 iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint);
143 }
144 }
145 }
146
147
148 /**
149 * When dragging the map change the cursor back to it's pre-move cursor.
150 * If a double-click occurs center and zoom the map on the clicked location.
151 */
152 @Override public void mouseReleased(MouseEvent e) {
153 if (e.getButton() == MouseEvent.BUTTON3)
154 endMovement();
155 else if (e.getButton() == MouseEvent.BUTTON1){
156 if(iSizeButton.hit(e.getPoint())){
157 iSizeButton.toggle();
158 iSlippyMapChooser.resizeSlippyMap();
159 }else{
160 if(e.getClickCount() == 1){
161 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());
162
163 //reset the selections start and end
164 iEndSelectionPoint = null;
165 iStartSelectionPoint = null;
166 }
167 else if(e.getClickCount() == 2){
168 iSlippyMapChooser.centerOnScreenPoint(e.getPoint());
169 iSlippyMapChooser.zoomIn();
170 }
171 }
172 }
173 }
174
175 /**
176 * Start movement by setting a new cursor and remember the current mouse
177 * position.
178 */
179 private void startMovement(MouseEvent e) {
180 if (isMovementInPlace)
181 return;
182 isMovementInPlace = true;
183 iLastX = e.getX();
184 iLastY = e.getY();
185 iOldCursor = iSlippyMapChooser.getCursor();
186 iSlippyMapChooser.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
187 }
188
189 /**
190 * End the movement. Setting back the cursor and clear the movement variables
191 */
192 private void endMovement() {
193 if (!isMovementInPlace)
194 return;
195 isMovementInPlace = false;
196 if (iOldCursor != null)
197 iSlippyMapChooser.setCursor(iOldCursor);
198 else
199 iSlippyMapChooser.setCursor(Cursor.getDefaultCursor());
200 iOldCursor = null;
201 }
202
203 /**
204 * Zoom the map in and out.
205 * @param e The wheel event.
206 */
207 public void mouseWheelMoved(MouseWheelEvent e) {
208 int rot = e.getWheelRotation();
209 //scroll wheel rotated away from user
210 if(rot < 0){
211 iSlippyMapChooser.zoomIn();
212 }
213 //scroll wheel rotated towards the user
214 else if(rot > 0){
215 iSlippyMapChooser.zoomOut();
216 }
217 }
218
219 /**
220 * Does nothing. Only to satisfy MouseMotionListener
221 */
222 public void mouseMoved(MouseEvent e) {}
223}
Note: See TracBrowser for help on using the repository browser.