001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Cursor; 007import java.awt.Point; 008import java.awt.event.ActionEvent; 009import java.awt.event.InputEvent; 010import java.awt.event.KeyEvent; 011import java.awt.event.MouseAdapter; 012import java.awt.event.MouseEvent; 013import java.awt.event.MouseWheelEvent; 014import java.util.ArrayList; 015import java.util.Optional; 016 017import javax.swing.AbstractAction; 018 019import org.openstreetmap.gui.jmapviewer.JMapViewer; 020import org.openstreetmap.josm.actions.mapmode.SelectAction; 021import org.openstreetmap.josm.data.coor.EastNorth; 022import org.openstreetmap.josm.data.preferences.BooleanProperty; 023import org.openstreetmap.josm.gui.MapViewState.MapViewPoint; 024import org.openstreetmap.josm.gui.layer.Layer; 025import org.openstreetmap.josm.spi.preferences.Config; 026import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent; 027import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener; 028import org.openstreetmap.josm.tools.Destroyable; 029import org.openstreetmap.josm.tools.Pair; 030import org.openstreetmap.josm.tools.PlatformManager; 031import org.openstreetmap.josm.tools.Shortcut; 032 033/** 034 * Enables moving of the map by holding down the right mouse button and drag 035 * the mouse. Also, enables zooming by the mouse wheel. 036 * 037 * @author imi 038 */ 039public class MapMover extends MouseAdapter implements Destroyable { 040 041 /** 042 * Zoom wheel is reversed. 043 */ 044 public static final BooleanProperty PROP_ZOOM_REVERSE_WHEEL = new BooleanProperty("zoom.reverse-wheel", false); 045 046 static { 047 new JMapViewerUpdater(); 048 } 049 050 private static class JMapViewerUpdater implements PreferenceChangedListener { 051 052 JMapViewerUpdater() { 053 Config.getPref().addPreferenceChangeListener(this); 054 updateJMapViewer(); 055 } 056 057 @Override 058 public void preferenceChanged(PreferenceChangeEvent e) { 059 if (MapMover.PROP_ZOOM_REVERSE_WHEEL.getKey().equals(e.getKey())) { 060 updateJMapViewer(); 061 } 062 } 063 064 private static void updateJMapViewer() { 065 JMapViewer.zoomReverseWheel = MapMover.PROP_ZOOM_REVERSE_WHEEL.get(); 066 } 067 } 068 069 private final class ZoomerAction extends AbstractAction { 070 private final String action; 071 072 ZoomerAction(String action) { 073 this(action, "MapMover.Zoomer." + action); 074 } 075 076 ZoomerAction(String action, String name) { 077 this.action = action; 078 putValue(NAME, name); 079 } 080 081 @Override 082 public void actionPerformed(ActionEvent e) { 083 if (".".equals(action) || ",".equals(action)) { 084 Point mouse = Optional.ofNullable(nc.getMousePosition()).orElseGet( 085 () -> new Point((int) nc.getBounds().getCenterX(), (int) nc.getBounds().getCenterY())); 086 mouseWheelMoved(new MouseWheelEvent(nc, e.getID(), e.getWhen(), e.getModifiers(), mouse.x, mouse.y, 0, false, 087 MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, ",".equals(action) ? -1 : 1)); 088 } else { 089 EastNorth center = nc.getCenter(); 090 EastNorth newcenter = nc.getEastNorth(nc.getWidth()/2+nc.getWidth()/5, nc.getHeight()/2+nc.getHeight()/5); 091 switch (action) { 092 case "left": 093 nc.zoomTo(new EastNorth(2*center.east()-newcenter.east(), center.north())); 094 break; 095 case "right": 096 nc.zoomTo(new EastNorth(newcenter.east(), center.north())); 097 break; 098 case "up": 099 nc.zoomTo(new EastNorth(center.east(), 2*center.north()-newcenter.north())); 100 break; 101 case "down": 102 nc.zoomTo(new EastNorth(center.east(), newcenter.north())); 103 break; 104 case "center": 105 Optional.ofNullable(nc.getMousePosition()).ifPresent(p -> nc.zoomTo(nc.getEastNorth(p.x, p.y))); 106 break; 107 default: // Do nothing 108 } 109 } 110 } 111 } 112 113 /** 114 * The point in the map that was the under the mouse point 115 * when moving around started. 116 * 117 * This is <code>null</code> if movement is not active 118 */ 119 private MapViewPoint mousePosMoveStart; 120 121 /** 122 * The map to move around. 123 */ 124 private final NavigatableComponent nc; 125 126 private final ArrayList<Pair<ZoomerAction, Shortcut>> registeredShortcuts = new ArrayList<>(); 127 128 /** 129 * Constructs a new {@code MapMover}. 130 * @param navComp the navigable component 131 * @since 11713 132 */ 133 public MapMover(NavigatableComponent navComp) { 134 this.nc = navComp; 135 nc.addMouseListener(this); 136 nc.addMouseMotionListener(this); 137 nc.addMouseWheelListener(this); 138 139 registerActionShortcut(new ZoomerAction("right"), 140 Shortcut.registerShortcut("system:movefocusright", tr("Map: {0}", tr("Move right")), KeyEvent.VK_RIGHT, Shortcut.CTRL)); 141 142 registerActionShortcut(new ZoomerAction("left"), 143 Shortcut.registerShortcut("system:movefocusleft", tr("Map: {0}", tr("Move left")), KeyEvent.VK_LEFT, Shortcut.CTRL)); 144 145 registerActionShortcut(new ZoomerAction("up"), 146 Shortcut.registerShortcut("system:movefocusup", tr("Map: {0}", tr("Move up")), KeyEvent.VK_UP, Shortcut.CTRL)); 147 registerActionShortcut(new ZoomerAction("down"), 148 Shortcut.registerShortcut("system:movefocusdown", tr("Map: {0}", tr("Move down")), KeyEvent.VK_DOWN, Shortcut.CTRL)); 149 registerActionShortcut(new ZoomerAction("center"), 150 Shortcut.registerShortcut("view:centeratmouse", tr("Map: {0}", tr("Center view at mouse position")), 151 KeyEvent.VK_HOME, Shortcut.CTRL)); 152 153 // see #10592 - Disable these alternate shortcuts on OS X because of conflict with system shortcut 154 if (!PlatformManager.isPlatformOsx()) { 155 registerActionShortcut(new ZoomerAction(",", "MapMover.Zoomer.in"), 156 Shortcut.registerShortcut("view:zoominalternate", tr("Map: {0}", tr("Zoom In")), KeyEvent.VK_COMMA, Shortcut.CTRL)); 157 158 registerActionShortcut(new ZoomerAction(".", "MapMover.Zoomer.out"), 159 Shortcut.registerShortcut("view:zoomoutalternate", tr("Map: {0}", tr("Zoom Out")), KeyEvent.VK_PERIOD, Shortcut.CTRL)); 160 } 161 } 162 163 private void registerActionShortcut(ZoomerAction action, Shortcut shortcut) { 164 MainApplication.registerActionShortcut(action, shortcut); 165 registeredShortcuts.add(new Pair<>(action, shortcut)); 166 } 167 168 /** 169 * Determines if a map move is in progress. 170 * @return {@code true} if a map move is in progress 171 * @since 13987 172 */ 173 public boolean movementInProgress() { 174 return mousePosMoveStart != null; 175 } 176 177 /** 178 * If the right (and only the right) mouse button is pressed, move the map. 179 */ 180 @Override 181 public void mouseDragged(MouseEvent e) { 182 int offMask = InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON2_DOWN_MASK; 183 boolean allowMovement = (e.getModifiersEx() & (InputEvent.BUTTON3_DOWN_MASK | offMask)) == InputEvent.BUTTON3_DOWN_MASK; 184 if (PlatformManager.isPlatformOsx()) { 185 MapFrame map = MainApplication.getMap(); 186 int macMouseMask = InputEvent.CTRL_DOWN_MASK | InputEvent.BUTTON1_DOWN_MASK; 187 boolean macMovement = e.getModifiersEx() == macMouseMask; 188 boolean allowedMode = !map.mapModeSelect.equals(map.mapMode) 189 || SelectAction.Mode.SELECT == map.mapModeSelect.getMode(); 190 allowMovement |= macMovement && allowedMode; 191 } 192 if (allowMovement) { 193 doMoveForDrag(e); 194 } else { 195 endMovement(); 196 } 197 } 198 199 private void doMoveForDrag(MouseEvent e) { 200 if (!movementInProgress()) { 201 startMovement(e); 202 } 203 EastNorth center = nc.getCenter(); 204 EastNorth mouseCenter = nc.getEastNorth(e.getX(), e.getY()); 205 nc.zoomTo(mousePosMoveStart.getEastNorth().add(center).subtract(mouseCenter)); 206 } 207 208 /** 209 * Start the movement, if it was the 3rd button (right button). 210 */ 211 @Override 212 public void mousePressed(MouseEvent e) { 213 int offMask = InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON2_DOWN_MASK; 214 int macMouseMask = InputEvent.CTRL_DOWN_MASK | InputEvent.BUTTON1_DOWN_MASK; 215 if ((e.getButton() == MouseEvent.BUTTON3 && (e.getModifiersEx() & offMask) == 0) || 216 (PlatformManager.isPlatformOsx() && e.getModifiersEx() == macMouseMask)) { 217 startMovement(e); 218 } 219 } 220 221 /** 222 * Change the cursor back to it's pre-move cursor. 223 */ 224 @Override 225 public void mouseReleased(MouseEvent e) { 226 if (e.getButton() == MouseEvent.BUTTON3 || (PlatformManager.isPlatformOsx() && e.getButton() == MouseEvent.BUTTON1)) { 227 endMovement(); 228 } 229 } 230 231 /** 232 * Start movement by setting a new cursor and remember the current mouse 233 * position. 234 * @param e The mouse event that leat to the movement from. 235 */ 236 private void startMovement(MouseEvent e) { 237 if (movementInProgress()) { 238 return; 239 } 240 mousePosMoveStart = nc.getState().getForView(e.getX(), e.getY()); 241 nc.setNewCursor(Cursor.MOVE_CURSOR, this); 242 } 243 244 /** 245 * End the movement. Setting back the cursor and clear the movement variables 246 */ 247 private void endMovement() { 248 if (!movementInProgress()) { 249 return; 250 } 251 nc.resetCursor(this); 252 mousePosMoveStart = null; 253 MainApplication.getLayerManager().getLayers().forEach(Layer::invalidate); 254 } 255 256 /** 257 * Zoom the map by 1/5th of current zoom per wheel-delta. 258 * @param e The wheel event. 259 */ 260 @Override 261 public void mouseWheelMoved(MouseWheelEvent e) { 262 int rotation = Boolean.TRUE.equals(PROP_ZOOM_REVERSE_WHEEL.get()) ? -e.getWheelRotation() : e.getWheelRotation(); 263 nc.zoomManyTimes(e.getX(), e.getY(), rotation); 264 } 265 266 /** 267 * Emulates dragging on Mac OSX. 268 */ 269 @Override 270 public void mouseMoved(MouseEvent e) { 271 if (!movementInProgress()) { 272 return; 273 } 274 // Mac OSX simulates with ctrl + mouse 1 the second mouse button hence no dragging events get fired. 275 // Is only the selected mouse button pressed? 276 if (PlatformManager.isPlatformOsx()) { 277 if (e.getModifiersEx() == InputEvent.CTRL_DOWN_MASK) { 278 doMoveForDrag(e); 279 } else { 280 endMovement(); 281 } 282 } 283 } 284 285 @Override 286 public void destroy() { 287 for (Pair<ZoomerAction, Shortcut> shortcut : registeredShortcuts) { 288 MainApplication.unregisterActionShortcut(shortcut.a, shortcut.b); 289 } 290 } 291}