001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.bbox; 003 004import java.awt.KeyboardFocusManager; 005import java.awt.Point; 006import java.awt.event.ActionEvent; 007import java.awt.event.InputEvent; 008import java.awt.event.KeyEvent; 009import java.awt.event.MouseAdapter; 010import java.awt.event.MouseEvent; 011import java.util.Timer; 012import java.util.TimerTask; 013 014import javax.swing.AbstractAction; 015import javax.swing.ActionMap; 016import javax.swing.InputMap; 017import javax.swing.JComponent; 018import javax.swing.JPanel; 019import javax.swing.KeyStroke; 020import javax.swing.text.JTextComponent; 021 022import org.openstreetmap.josm.tools.PlatformManager; 023 024/** 025 * This class controls the user input by listening to mouse and key events. 026 * Currently implemented is: - zooming in and out with scrollwheel - zooming in 027 * and centering by double clicking - selecting an area by clicking and dragging 028 * the mouse 029 * 030 * @author Tim Haussmann 031 */ 032public class SlippyMapController extends MouseAdapter { 033 034 /** A Timer for smoothly moving the map area */ 035 private static final Timer TIMER = new Timer(true); 036 037 /** Does the moving */ 038 private MoveTask moveTask = new MoveTask(); 039 040 /** How often to do the moving (milliseconds) */ 041 private static final long timerInterval = 20; 042 043 /** The maximum speed (pixels per timer interval) */ 044 private static final double MAX_SPEED = 20; 045 046 /** The speed increase per timer interval when a cursor button is clicked */ 047 private static final double ACCELERATION = 0.10; 048 049 private static final int MAC_MOUSE_BUTTON3_MASK = InputEvent.CTRL_DOWN_MASK | InputEvent.BUTTON1_DOWN_MASK; 050 051 private static final String[] N = { 052 ",", ".", "up", "right", "down", "left"}; 053 private static final int[] K = { 054 KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT}; 055 056 // start and end point of selection rectangle 057 private Point iStartSelectionPoint; 058 private Point iEndSelectionPoint; 059 060 private final SlippyMapBBoxChooser iSlippyMapChooser; 061 062 private boolean isSelecting; 063 064 /** 065 * Constructs a new {@code SlippyMapController}. 066 * @param navComp navigable component 067 * @param contentPane content pane 068 */ 069 public SlippyMapController(SlippyMapBBoxChooser navComp, JPanel contentPane) { 070 iSlippyMapChooser = navComp; 071 iSlippyMapChooser.addMouseListener(this); 072 iSlippyMapChooser.addMouseMotionListener(this); 073 074 if (contentPane != null) { 075 for (int i = 0; i < N.length; ++i) { 076 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( 077 KeyStroke.getKeyStroke(K[i], InputEvent.CTRL_DOWN_MASK), "MapMover.Zoomer." + N[i]); 078 } 079 } 080 isSelecting = false; 081 082 InputMap inputMap = navComp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 083 ActionMap actionMap = navComp.getActionMap(); 084 085 // map moving 086 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "MOVE_RIGHT"); 087 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "MOVE_LEFT"); 088 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "MOVE_UP"); 089 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "MOVE_DOWN"); 090 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "STOP_MOVE_HORIZONTALLY"); 091 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), "STOP_MOVE_HORIZONTALLY"); 092 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "STOP_MOVE_VERTICALLY"); 093 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "STOP_MOVE_VERTICALLY"); 094 095 // zooming. To avoid confusion about which modifier key to use, 096 // we just add all keys left of the space bar 097 // 098 // Typing the '+', '=', or '-' character in the Overpass query field 099 // seems to trigger these key bindings, which would zoom the map 100 // (https://josm.openstreetmap.de/ticket/24392). To prevent that, those 101 // keys use a special version of the action that does nothing if a text 102 // field is focused. That feels like a hack, but we didn't find an 103 // obvious better way. 104 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_DOWN_MASK, false), "ZOOM_IN"); 105 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.META_DOWN_MASK, false), "ZOOM_IN"); 106 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.ALT_DOWN_MASK, false), "ZOOM_IN"); 107 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0, false), "ZOOM_IN_UNLESS_TYPING"); 108 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, 0, false), "ZOOM_IN_UNLESS_TYPING"); 109 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, 0, false), "ZOOM_IN_UNLESS_TYPING"); 110 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, InputEvent.SHIFT_DOWN_MASK, false), "ZOOM_IN_UNLESS_TYPING"); 111 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_DOWN_MASK, false), "ZOOM_OUT"); 112 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.META_DOWN_MASK, false), "ZOOM_OUT"); 113 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.ALT_DOWN_MASK, false), "ZOOM_OUT"); 114 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0, false), "ZOOM_OUT_UNLESS_TYPING"); 115 inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, 0, false), "ZOOM_OUT_UNLESS_TYPING"); 116 117 // action mapping 118 actionMap.put("MOVE_RIGHT", new MoveXAction(1)); 119 actionMap.put("MOVE_LEFT", new MoveXAction(-1)); 120 actionMap.put("MOVE_UP", new MoveYAction(-1)); 121 actionMap.put("MOVE_DOWN", new MoveYAction(1)); 122 actionMap.put("STOP_MOVE_HORIZONTALLY", new MoveXAction(0)); 123 actionMap.put("STOP_MOVE_VERTICALLY", new MoveYAction(0)); 124 actionMap.put("ZOOM_IN", new ZoomInAction(false)); 125 actionMap.put("ZOOM_IN_UNLESS_TYPING", new ZoomInAction(true)); 126 actionMap.put("ZOOM_OUT", new ZoomOutAction(false)); 127 actionMap.put("ZOOM_OUT_UNLESS_TYPING", new ZoomOutAction(true)); 128 } 129 130 /** 131 * Start drawing the selection rectangle if it was the 1st button (left button) 132 */ 133 @Override 134 public void mousePressed(MouseEvent e) { 135 if (e.getButton() == MouseEvent.BUTTON1 && !(PlatformManager.isPlatformOsx() && e.getModifiersEx() == MAC_MOUSE_BUTTON3_MASK)) { 136 iStartSelectionPoint = e.getPoint(); 137 iEndSelectionPoint = e.getPoint(); 138 } 139 } 140 141 @Override 142 public void mouseDragged(MouseEvent e) { 143 if (iStartSelectionPoint != null && (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK 144 && !(PlatformManager.isPlatformOsx() && e.getModifiersEx() == MAC_MOUSE_BUTTON3_MASK)) { 145 iEndSelectionPoint = e.getPoint(); 146 iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint); 147 isSelecting = true; 148 } 149 } 150 151 /** 152 * When dragging the map change the cursor back to it's pre-move cursor. If 153 * a double-click occurs center and zoom the map on the clicked location. 154 */ 155 @Override 156 public void mouseReleased(MouseEvent e) { 157 if (e.getButton() == MouseEvent.BUTTON1) { 158 159 if (isSelecting && e.getClickCount() == 1) { 160 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint()); 161 162 // reset the selections start and end 163 iEndSelectionPoint = null; 164 iStartSelectionPoint = null; 165 isSelecting = false; 166 167 } else { 168 iSlippyMapChooser.handleAttribution(e.getPoint(), true); 169 } 170 } 171 } 172 173 @Override 174 public void mouseMoved(MouseEvent e) { 175 iSlippyMapChooser.handleMouseMoved(e.getPoint()); 176 iSlippyMapChooser.handleAttribution(e.getPoint(), false); 177 } 178 179 private class MoveXAction extends AbstractAction { 180 181 private final int direction; 182 183 MoveXAction(int direction) { 184 this.direction = direction; 185 } 186 187 @Override 188 public void actionPerformed(ActionEvent e) { 189 moveTask.setDirectionX(direction); 190 } 191 } 192 193 private class MoveYAction extends AbstractAction { 194 195 private final int direction; 196 197 MoveYAction(int direction) { 198 this.direction = direction; 199 } 200 201 @Override 202 public void actionPerformed(ActionEvent e) { 203 moveTask.setDirectionY(direction); 204 } 205 } 206 207 /** Moves the map depending on which cursor keys are pressed (or not) */ 208 private final class MoveTask extends TimerTask { 209 /** The current x speed (pixels per timer interval) */ 210 private double speedX = 1; 211 212 /** The current y speed (pixels per timer interval) */ 213 private double speedY = 1; 214 215 /** The horizontal direction of movement, -1:left, 0:stop, 1:right */ 216 private int directionX; 217 218 /** The vertical direction of movement, -1:up, 0:stop, 1:down */ 219 private int directionY; 220 221 /** 222 * Indicated if <code>moveTask</code> is currently enabled (periodically 223 * executed via timer) or disabled 224 */ 225 boolean scheduled; 226 227 void setDirectionX(int directionX) { 228 this.directionX = directionX; 229 updateScheduleStatus(); 230 } 231 232 void setDirectionY(int directionY) { 233 this.directionY = directionY; 234 updateScheduleStatus(); 235 } 236 237 private void updateScheduleStatus() { 238 boolean newMoveTaskState = !(directionX == 0 && directionY == 0); 239 240 if (newMoveTaskState != scheduled) { 241 scheduled = newMoveTaskState; 242 if (newMoveTaskState) { 243 TIMER.schedule(this, 0, timerInterval); 244 } else { 245 // We have to create a new instance because rescheduling a 246 // once canceled TimerTask is not possible 247 moveTask = new MoveTask(); 248 cancel(); // Stop this TimerTask 249 } 250 } 251 } 252 253 @Override 254 public void run() { 255 // update the x speed 256 switch (directionX) { 257 case -1: 258 if (speedX > -1) { 259 speedX = -1; 260 } 261 if (speedX > -1 * MAX_SPEED) { 262 speedX -= ACCELERATION; 263 } 264 break; 265 case 0: 266 speedX = 0; 267 break; 268 case 1: 269 if (speedX < 1) { 270 speedX = 1; 271 } 272 if (speedX < MAX_SPEED) { 273 speedX += ACCELERATION; 274 } 275 break; 276 default: 277 throw new IllegalStateException(Integer.toString(directionX)); 278 } 279 280 // update the y speed 281 switch (directionY) { 282 case -1: 283 if (speedY > -1) { 284 speedY = -1; 285 } 286 if (speedY > -1 * MAX_SPEED) { 287 speedY -= ACCELERATION; 288 } 289 break; 290 case 0: 291 speedY = 0; 292 break; 293 case 1: 294 if (speedY < 1) { 295 speedY = 1; 296 } 297 if (speedY < MAX_SPEED) { 298 speedY += ACCELERATION; 299 } 300 break; 301 default: 302 throw new IllegalStateException(Integer.toString(directionY)); 303 } 304 305 // move the map 306 int moveX = (int) Math.floor(speedX); 307 int moveY = (int) Math.floor(speedY); 308 if (moveX != 0 || moveY != 0) { 309 iSlippyMapChooser.moveMap(moveX, moveY); 310 } 311 } 312 } 313 314 static boolean isTyping() { 315 return KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() instanceof JTextComponent; 316 } 317 318 private final class ZoomInAction extends AbstractAction { 319 320 private boolean suppressWhileTyping; 321 322 ZoomInAction(boolean suppressWhileTyping) { 323 this.suppressWhileTyping = suppressWhileTyping; 324 } 325 326 @Override 327 public void actionPerformed(ActionEvent e) { 328 if (suppressWhileTyping && isTyping()) 329 return; 330 iSlippyMapChooser.zoomIn(); 331 } 332 } 333 334 private final class ZoomOutAction extends AbstractAction { 335 336 private boolean suppressWhileTyping; 337 338 ZoomOutAction(boolean suppressWhileTyping) { 339 this.suppressWhileTyping = suppressWhileTyping; 340 } 341 342 @Override 343 public void actionPerformed(ActionEvent e) { 344 if (suppressWhileTyping && isTyping()) 345 return; 346 iSlippyMapChooser.zoomOut(); 347 } 348 } 349}