| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 2 | package org.openstreetmap.josm.actions.mapmode;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.*;
|
|---|
| 5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 7 | import static org.openstreetmap.josm.tools.I18n.trn;
|
|---|
| 8 |
|
|---|
| 9 | import java.awt.event.AWTEventListener;
|
|---|
| 10 | import java.awt.event.InputEvent;
|
|---|
| 11 | import java.awt.event.KeyEvent;
|
|---|
| 12 | import java.awt.event.MouseEvent;
|
|---|
| 13 | import java.awt.geom.Point2D;
|
|---|
| 14 | import java.util.Collection;
|
|---|
| 15 | import java.util.Collections;
|
|---|
| 16 | import java.util.HashSet;
|
|---|
| 17 | import java.util.Iterator;
|
|---|
| 18 | import java.util.LinkedList;
|
|---|
| 19 | import java.util.Set;
|
|---|
| 20 |
|
|---|
| 21 | import javax.swing.JOptionPane;
|
|---|
| 22 |
|
|---|
| 23 | import org.openstreetmap.josm.Main;
|
|---|
| 24 | import org.openstreetmap.josm.actions.MergeNodesAction;
|
|---|
| 25 | import org.openstreetmap.josm.command.AddCommand;
|
|---|
| 26 | import org.openstreetmap.josm.command.ChangeCommand;
|
|---|
| 27 | import org.openstreetmap.josm.command.Command;
|
|---|
| 28 | import org.openstreetmap.josm.command.MoveCommand;
|
|---|
| 29 | import org.openstreetmap.josm.command.RotateCommand;
|
|---|
| 30 | import org.openstreetmap.josm.command.ScaleCommand;
|
|---|
| 31 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 32 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 33 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 34 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 35 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 36 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 37 | import org.openstreetmap.josm.data.osm.WaySegment;
|
|---|
| 38 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
|---|
| 39 | import org.openstreetmap.josm.data.osm.visitor.paint.WireframeMapRenderer;
|
|---|
| 40 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 41 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 42 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 43 | import org.openstreetmap.josm.gui.SelectionManager;
|
|---|
| 44 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
|---|
| 45 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 46 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 47 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 48 | import org.openstreetmap.josm.tools.Pair;
|
|---|
| 49 | import org.openstreetmap.josm.tools.PlatformHookOsx;
|
|---|
| 50 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Move is an action that can move all kind of OsmPrimitives (except keys for now).
|
|---|
| 54 | *
|
|---|
| 55 | * If an selected object is under the mouse when dragging, move all selected objects.
|
|---|
| 56 | * If an unselected object is under the mouse when dragging, it becomes selected
|
|---|
| 57 | * and will be moved.
|
|---|
| 58 | * If no object is under the mouse, move all selected objects (if any)
|
|---|
| 59 | *
|
|---|
| 60 | * @author imi
|
|---|
| 61 | */
|
|---|
| 62 | public class SelectAction extends MapMode implements AWTEventListener, SelectionEnded {
|
|---|
| 63 | // "select" means the selection rectangle and "move" means either dragging
|
|---|
| 64 | // or select if no mouse movement occurs (i.e. just clicking)
|
|---|
| 65 | enum Mode { move, rotate, scale, select }
|
|---|
| 66 |
|
|---|
| 67 | // contains all possible cases the cursor can be in the SelectAction
|
|---|
| 68 | static private enum SelectActionCursor {
|
|---|
| 69 | rect("normal", "selection"),
|
|---|
| 70 | rect_add("normal", "select_add"),
|
|---|
| 71 | rect_rm("normal", "select_remove"),
|
|---|
| 72 | way("normal", "select_way"),
|
|---|
| 73 | way_add("normal", "select_way_add"),
|
|---|
| 74 | way_rm("normal", "select_way_remove"),
|
|---|
| 75 | node("normal", "select_node"),
|
|---|
| 76 | node_add("normal", "select_node_add"),
|
|---|
| 77 | node_rm("normal", "select_node_remove"),
|
|---|
| 78 | virtual_node("normal", "addnode"),
|
|---|
| 79 | scale("scale", null),
|
|---|
| 80 | rotate("rotate", null),
|
|---|
| 81 | merge("crosshair", null),
|
|---|
| 82 | lasso("normal", "rope"),
|
|---|
| 83 | merge_to_node("crosshair", "joinnode"),
|
|---|
| 84 | move(Cursor.MOVE_CURSOR);
|
|---|
| 85 |
|
|---|
| 86 | private final Cursor c;
|
|---|
| 87 | private SelectActionCursor(String main, String sub) {
|
|---|
| 88 | c = ImageProvider.getCursor(main, sub);
|
|---|
| 89 | }
|
|---|
| 90 | private SelectActionCursor(int systemCursor) {
|
|---|
| 91 | c = Cursor.getPredefinedCursor(systemCursor);
|
|---|
| 92 | }
|
|---|
| 93 | public Cursor cursor() {
|
|---|
| 94 | return c;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | private boolean lassoMode = false;
|
|---|
| 99 |
|
|---|
| 100 | // Cache previous mouse event (needed when only the modifier keys are
|
|---|
| 101 | // pressed but the mouse isn't moved)
|
|---|
| 102 | private MouseEvent oldEvent = null;
|
|---|
| 103 |
|
|---|
| 104 | private Mode mode = null;
|
|---|
| 105 | private SelectionManager selectionManager;
|
|---|
| 106 | private boolean cancelDrawMode = false;
|
|---|
| 107 | private boolean drawTargetHighlight;
|
|---|
| 108 | private boolean didMouseDrag = false;
|
|---|
| 109 | /**
|
|---|
| 110 | * The component this SelectAction is associated with.
|
|---|
| 111 | */
|
|---|
| 112 | private final MapView mv;
|
|---|
| 113 | /**
|
|---|
| 114 | * The old cursor before the user pressed the mouse button.
|
|---|
| 115 | */
|
|---|
| 116 | private Point startingDraggingPos;
|
|---|
| 117 | /**
|
|---|
| 118 | * The last known position of the mouse.
|
|---|
| 119 | */
|
|---|
| 120 | private Point lastMousePos;
|
|---|
| 121 | /**
|
|---|
| 122 | * The time of the user mouse down event.
|
|---|
| 123 | */
|
|---|
| 124 | private long mouseDownTime = 0;
|
|---|
| 125 | /**
|
|---|
| 126 | * The pressed button of the user mouse down event.
|
|---|
| 127 | */
|
|---|
| 128 | private int mouseDownButton = 0;
|
|---|
| 129 | /**
|
|---|
| 130 | * The time of the user mouse down event.
|
|---|
| 131 | */
|
|---|
| 132 | private long mouseReleaseTime = 0;
|
|---|
| 133 | /**
|
|---|
| 134 | * The time which needs to pass between click and release before something
|
|---|
| 135 | * counts as a move, in milliseconds
|
|---|
| 136 | */
|
|---|
| 137 | private int initialMoveDelay;
|
|---|
| 138 | /**
|
|---|
| 139 | * The screen distance which needs to be travelled before something
|
|---|
| 140 | * counts as a move, in pixels
|
|---|
| 141 | */
|
|---|
| 142 | private int initialMoveThreshold;
|
|---|
| 143 | private boolean initialMoveThresholdExceeded = false;
|
|---|
| 144 |
|
|---|
| 145 | /**
|
|---|
| 146 | * elements that have been highlighted in the previous iteration. Used
|
|---|
| 147 | * to remove the highlight from them again as otherwise the whole data
|
|---|
| 148 | * set would have to be checked.
|
|---|
| 149 | */
|
|---|
| 150 | private Set<OsmPrimitive> oldHighlights = new HashSet<OsmPrimitive>();
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * Create a new SelectAction
|
|---|
| 154 | * @param mapFrame The MapFrame this action belongs to.
|
|---|
| 155 | */
|
|---|
| 156 | public SelectAction(MapFrame mapFrame) {
|
|---|
| 157 | super(tr("Select"), "move/move", tr("Select, move, scale and rotate objects"),
|
|---|
| 158 | Shortcut.registerShortcut("mapmode:select", tr("Mode: {0}", tr("Select")), KeyEvent.VK_S, Shortcut.DIRECT),
|
|---|
| 159 | mapFrame,
|
|---|
| 160 | ImageProvider.getCursor("normal", "selection"));
|
|---|
| 161 | mv = mapFrame.mapView;
|
|---|
| 162 | putValue("help", ht("/Action/Select"));
|
|---|
| 163 | selectionManager = new SelectionManager(this, false, mv);
|
|---|
| 164 | initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay", 200);
|
|---|
| 165 | initialMoveThreshold = Main.pref.getInteger("edit.initial-move-threshold", 5);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | @Override
|
|---|
| 169 | public void enterMode() {
|
|---|
| 170 | super.enterMode();
|
|---|
| 171 | mv.addMouseListener(this);
|
|---|
| 172 | mv.addMouseMotionListener(this);
|
|---|
| 173 | mv.setVirtualNodesEnabled(Main.pref.getInteger("mappaint.node.virtual-size", 8) != 0);
|
|---|
| 174 | drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true);
|
|---|
| 175 | // This is required to update the cursors when ctrl/shift/alt is pressed
|
|---|
| 176 | try {
|
|---|
| 177 | Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
|
|---|
| 178 | } catch (SecurityException ex) {}
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | @Override
|
|---|
| 182 | public void exitMode() {
|
|---|
| 183 | super.exitMode();
|
|---|
| 184 | selectionManager.unregister(mv);
|
|---|
| 185 | mv.removeMouseListener(this);
|
|---|
| 186 | mv.removeMouseMotionListener(this);
|
|---|
| 187 | mv.setVirtualNodesEnabled(false);
|
|---|
| 188 | try {
|
|---|
| 189 | Toolkit.getDefaultToolkit().removeAWTEventListener(this);
|
|---|
| 190 | } catch (SecurityException ex) {}
|
|---|
| 191 | removeHighlighting();
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /**
|
|---|
| 195 | * works out which cursor should be displayed for most of SelectAction's
|
|---|
| 196 | * features. The only exception is the "move" cursor when actually dragging
|
|---|
| 197 | * primitives.
|
|---|
| 198 | * @param nearbyStuff primitives near the cursor
|
|---|
| 199 | * @return the cursor that should be displayed
|
|---|
| 200 | */
|
|---|
| 201 | private Cursor getCursor(Collection<OsmPrimitive> nearbyStuff) {
|
|---|
| 202 | String c = "rect";
|
|---|
| 203 | switch(mode) {
|
|---|
| 204 | case move:
|
|---|
| 205 | if(virtualManager.hasVirtualNode()) {
|
|---|
| 206 | c = "virtual_node";
|
|---|
| 207 | break;
|
|---|
| 208 | }
|
|---|
| 209 | final Iterator<OsmPrimitive> it = nearbyStuff.iterator();
|
|---|
| 210 | final OsmPrimitive osm = it.hasNext() ? it.next() : null;
|
|---|
| 211 |
|
|---|
| 212 | if(dragInProgress()) {
|
|---|
| 213 | // only consider merge if ctrl is pressed and there are nodes in
|
|---|
| 214 | // the selection that could be merged
|
|---|
| 215 | if(!ctrl || getCurrentDataSet().getSelectedNodes().isEmpty()) {
|
|---|
| 216 | c = "move";
|
|---|
| 217 | break;
|
|---|
| 218 | }
|
|---|
| 219 | // only show merge to node cursor if nearby node and that node is currently
|
|---|
| 220 | // not being dragged
|
|---|
| 221 | final boolean hasTarget = osm != null && osm instanceof Node && !osm.isSelected();
|
|---|
| 222 | c = hasTarget ? "merge_to_node" : "merge";
|
|---|
| 223 | break;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | c = (osm instanceof Node) ? "node" : c;
|
|---|
| 227 | c = (osm instanceof Way) ? "way" : c;
|
|---|
| 228 | if(shift) {
|
|---|
| 229 | c += "_add";
|
|---|
| 230 | } else if(ctrl) {
|
|---|
| 231 | c += osm == null || osm.isSelected() ? "_rm" : "_add";
|
|---|
| 232 | }
|
|---|
| 233 | break;
|
|---|
| 234 | case rotate:
|
|---|
| 235 | c = "rotate";
|
|---|
| 236 | break;
|
|---|
| 237 | case scale:
|
|---|
| 238 | c = "scale";
|
|---|
| 239 | break;
|
|---|
| 240 | case select:
|
|---|
| 241 | if (lassoMode) {
|
|---|
| 242 | c = "lasso";
|
|---|
| 243 | } else {
|
|---|
| 244 | c = "rect" + (shift ? "_add" : (ctrl ? "_rm" : ""));
|
|---|
| 245 | }
|
|---|
| 246 | break;
|
|---|
| 247 | }
|
|---|
| 248 | return SelectActionCursor.valueOf(c).cursor();
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /**
|
|---|
| 252 | * Removes all existing highlights.
|
|---|
| 253 | * @return true if a repaint is required
|
|---|
| 254 | */
|
|---|
| 255 | private boolean removeHighlighting() {
|
|---|
| 256 | boolean needsRepaint = false;
|
|---|
| 257 | DataSet ds = getCurrentDataSet();
|
|---|
| 258 | if(ds != null && !ds.getHighlightedVirtualNodes().isEmpty()) {
|
|---|
| 259 | needsRepaint = true;
|
|---|
| 260 | ds.clearHighlightedVirtualNodes();
|
|---|
| 261 | }
|
|---|
| 262 | if(oldHighlights.isEmpty())
|
|---|
| 263 | return needsRepaint;
|
|---|
| 264 |
|
|---|
| 265 | for(OsmPrimitive prim : oldHighlights) {
|
|---|
| 266 | prim.setHighlighted(false);
|
|---|
| 267 | }
|
|---|
| 268 | oldHighlights = new HashSet<OsmPrimitive>();
|
|---|
| 269 | return true;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | private boolean repaintIfRequired(HashSet<OsmPrimitive> newHighlights) {
|
|---|
| 273 | if(!drawTargetHighlight)
|
|---|
| 274 | return false;
|
|---|
| 275 |
|
|---|
| 276 | boolean needsRepaint = false;
|
|---|
| 277 | for(OsmPrimitive x : newHighlights) {
|
|---|
| 278 | if(oldHighlights.contains(x)) {
|
|---|
| 279 | continue;
|
|---|
| 280 | }
|
|---|
| 281 | needsRepaint = true;
|
|---|
| 282 | x.setHighlighted(true);
|
|---|
| 283 | }
|
|---|
| 284 | oldHighlights.removeAll(newHighlights);
|
|---|
| 285 | for(OsmPrimitive x : oldHighlights) {
|
|---|
| 286 | x.setHighlighted(false);
|
|---|
| 287 | needsRepaint = true;
|
|---|
| 288 | }
|
|---|
| 289 | oldHighlights = newHighlights;
|
|---|
| 290 | return needsRepaint;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /**
|
|---|
| 294 | * handles adding highlights and updating the cursor for the given mouse event.
|
|---|
| 295 | * Please note that the highlighting for merging while moving is handled via mouseDragged.
|
|---|
| 296 | * @param MouseEvent which should be used as base for the feedback
|
|---|
| 297 | * @return true if repaint is required
|
|---|
| 298 | */
|
|---|
| 299 | private boolean giveUserFeedback(MouseEvent e) {
|
|---|
| 300 | return giveUserFeedback(e, e.getModifiers());
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /**
|
|---|
| 304 | * handles adding highlights and updating the cursor for the given mouse event.
|
|---|
| 305 | * Please note that the highlighting for merging while moving is handled via mouseDragged.
|
|---|
| 306 | * @param MouseEvent which should be used as base for the feedback
|
|---|
| 307 | * @param define custom keyboard modifiers if the ones from MouseEvent are outdated or similar
|
|---|
| 308 | * @return true if repaint is required
|
|---|
| 309 | */
|
|---|
| 310 | private boolean giveUserFeedback(MouseEvent e, int modifiers) {
|
|---|
| 311 | Collection<OsmPrimitive> c = MapView.asColl(
|
|---|
| 312 | mv.getNearestNodeOrWay(e.getPoint(), OsmPrimitive.isSelectablePredicate, true));
|
|---|
| 313 |
|
|---|
| 314 | updateKeyModifiers(modifiers);
|
|---|
| 315 | determineMapMode(!c.isEmpty());
|
|---|
| 316 |
|
|---|
| 317 | HashSet<OsmPrimitive> newHighlights = new HashSet<OsmPrimitive>();
|
|---|
| 318 |
|
|---|
| 319 | virtualManager.clear();
|
|---|
| 320 | if(mode == Mode.move && virtualManager.setupVirtual(e.getPoint())) {
|
|---|
| 321 | DataSet ds = getCurrentDataSet();
|
|---|
| 322 | if (ds != null && drawTargetHighlight) {
|
|---|
| 323 | ds.setHighlightedVirtualNodes(virtualManager.virtualWays);
|
|---|
| 324 | }
|
|---|
| 325 | mv.setNewCursor(SelectActionCursor.virtual_node.cursor(), this);
|
|---|
| 326 | // don't highlight anything else if a virtual node will be
|
|---|
| 327 | return repaintIfRequired(newHighlights);
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | mv.setNewCursor(getCursor(c), this);
|
|---|
| 331 |
|
|---|
| 332 | // return early if there can't be any highlights
|
|---|
| 333 | if(!drawTargetHighlight || mode != Mode.move || c.isEmpty())
|
|---|
| 334 | return repaintIfRequired(newHighlights);
|
|---|
| 335 |
|
|---|
| 336 | // CTRL toggles selection, but if while dragging CTRL means merge
|
|---|
| 337 | final boolean isToggleMode = ctrl && !dragInProgress();
|
|---|
| 338 | for(OsmPrimitive x : c) {
|
|---|
| 339 | // only highlight primitives that will change the selection
|
|---|
| 340 | // when clicked. I.e. don't highlight selected elements unless
|
|---|
| 341 | // we are in toggle mode.
|
|---|
| 342 | if(isToggleMode || !x.isSelected()) {
|
|---|
| 343 | newHighlights.add(x);
|
|---|
| 344 | }
|
|---|
| 345 | }
|
|---|
| 346 | return repaintIfRequired(newHighlights);
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | /**
|
|---|
| 350 | * This is called whenever the keyboard modifier status changes
|
|---|
| 351 | */
|
|---|
| 352 | public void eventDispatched(AWTEvent e) {
|
|---|
| 353 | if(oldEvent == null)
|
|---|
| 354 | return;
|
|---|
| 355 | // We don't have a mouse event, so we pass the old mouse event but the
|
|---|
| 356 | // new modifiers.
|
|---|
| 357 | if(giveUserFeedback(oldEvent, ((InputEvent) e).getModifiers())) {
|
|---|
| 358 | mv.repaint();
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /**
|
|---|
| 363 | * If the left mouse button is pressed, move all currently selected
|
|---|
| 364 | * objects (if one of them is under the mouse) or the current one under the
|
|---|
| 365 | * mouse (which will become selected).
|
|---|
| 366 | */
|
|---|
| 367 | @Override
|
|---|
| 368 | public void mouseDragged(MouseEvent e) {
|
|---|
| 369 | if (!mv.isActiveLayerVisible())
|
|---|
| 370 | return;
|
|---|
| 371 |
|
|---|
| 372 | // Swing sends random mouseDragged events when closing dialogs by double-clicking their top-left icon on Windows
|
|---|
| 373 | // Ignore such false events to prevent issues like #7078
|
|---|
| 374 | if (mouseDownButton == MouseEvent.BUTTON1 && mouseReleaseTime > mouseDownTime)
|
|---|
| 375 | return;
|
|---|
| 376 |
|
|---|
| 377 | cancelDrawMode = true;
|
|---|
| 378 | if (mode == Mode.select)
|
|---|
| 379 | return;
|
|---|
| 380 |
|
|---|
| 381 | // do not count anything as a move if it lasts less than 100 milliseconds.
|
|---|
| 382 | if ((mode == Mode.move) && (System.currentTimeMillis() - mouseDownTime < initialMoveDelay))
|
|---|
| 383 | return;
|
|---|
| 384 |
|
|---|
| 385 | if (mode != Mode.rotate && mode != Mode.scale) // button is pressed in rotate mode
|
|---|
| 386 | {
|
|---|
| 387 | if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
|
|---|
| 388 | return;
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | if (mode == Mode.move) {
|
|---|
| 392 | // If ctrl is pressed we are in merge mode. Look for a nearby node,
|
|---|
| 393 | // highlight it and adjust the cursor accordingly.
|
|---|
| 394 | final boolean canMerge = ctrl && !getCurrentDataSet().getSelectedNodes().isEmpty();
|
|---|
| 395 | final OsmPrimitive p = canMerge ? (OsmPrimitive)findNodeToMergeTo(e.getPoint()) : null;
|
|---|
| 396 | boolean needsRepaint = removeHighlighting();
|
|---|
| 397 | if(p != null) {
|
|---|
| 398 | p.setHighlighted(true);
|
|---|
| 399 | oldHighlights.add(p);
|
|---|
| 400 | needsRepaint = true;
|
|---|
| 401 | }
|
|---|
| 402 | mv.setNewCursor(getCursor(MapView.asColl(p)), this);
|
|---|
| 403 | // also update the stored mouse event, so we can display the correct cursor
|
|---|
| 404 | // when dragging a node onto another one and then press CTRL to merge
|
|---|
| 405 | oldEvent = e;
|
|---|
| 406 | if(needsRepaint) {
|
|---|
| 407 | mv.repaint();
|
|---|
| 408 | }
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | if (startingDraggingPos == null) {
|
|---|
| 412 | startingDraggingPos = new Point(e.getX(), e.getY());
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | if( lastMousePos == null ) {
|
|---|
| 416 | lastMousePos = e.getPoint();
|
|---|
| 417 | return;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | if (!initialMoveThresholdExceeded) {
|
|---|
| 421 | int dxp = lastMousePos.x - e.getX();
|
|---|
| 422 | int dyp = lastMousePos.y - e.getY();
|
|---|
| 423 | int dp = (int) Math.sqrt(dxp * dxp + dyp * dyp);
|
|---|
| 424 | if (dp < initialMoveThreshold)
|
|---|
| 425 | return;
|
|---|
| 426 | initialMoveThresholdExceeded = true;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | EastNorth currentEN = mv.getEastNorth(e.getX(), e.getY());
|
|---|
| 430 | EastNorth lastEN = mv.getEastNorth(lastMousePos.x, lastMousePos.y);
|
|---|
| 431 | //EastNorth startEN = mv.getEastNorth(startingDraggingPos.x, startingDraggingPos.y);
|
|---|
| 432 | double dx = currentEN.east() - lastEN.east();
|
|---|
| 433 | double dy = currentEN.north() - lastEN.north();
|
|---|
| 434 | if (dx == 0 && dy == 0)
|
|---|
| 435 | return;
|
|---|
| 436 |
|
|---|
| 437 | if (virtualManager.virtualWays.size() > 0) {
|
|---|
| 438 | virtualManager.processVirtualNodeMovements(dx, dy);
|
|---|
| 439 | } else {
|
|---|
| 440 | if (!updateCommandWhileDragging(dx, dy, currentEN)) return;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | mv.repaint();
|
|---|
| 444 | if (mode != Mode.scale) {
|
|---|
| 445 | lastMousePos = e.getPoint();
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | didMouseDrag = true;
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | @Override
|
|---|
| 452 | public void mouseMoved(MouseEvent e) {
|
|---|
| 453 | // Mac OSX simulates with ctrl + mouse 1 the second mouse button hence no dragging events get fired.
|
|---|
| 454 | if ((Main.platform instanceof PlatformHookOsx) && (mode == Mode.rotate || mode == Mode.scale)) {
|
|---|
| 455 | mouseDragged(e);
|
|---|
| 456 | return;
|
|---|
| 457 | }
|
|---|
| 458 | oldEvent = e;
|
|---|
| 459 | if(giveUserFeedback(e)) {
|
|---|
| 460 | mv.repaint();
|
|---|
| 461 | }
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | @Override
|
|---|
| 465 | public void mouseExited(MouseEvent e) {
|
|---|
| 466 | if(removeHighlighting()) {
|
|---|
| 467 | mv.repaint();
|
|---|
| 468 | }
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | /**
|
|---|
| 472 | * Look, whether any object is selected. If not, select the nearest node.
|
|---|
| 473 | * If there are no nodes in the dataset, do nothing.
|
|---|
| 474 | *
|
|---|
| 475 | * If the user did not press the left mouse button, do nothing.
|
|---|
| 476 | *
|
|---|
| 477 | * Also remember the starting position of the movement and change the mouse
|
|---|
| 478 | * cursor to movement.
|
|---|
| 479 | */
|
|---|
| 480 | @Override
|
|---|
| 481 | public void mousePressed(MouseEvent e) {
|
|---|
| 482 | // return early
|
|---|
| 483 | if (!mv.isActiveLayerVisible() || !(Boolean) this.getValue("active") || (mouseDownButton = e.getButton()) != MouseEvent.BUTTON1)
|
|---|
| 484 | return;
|
|---|
| 485 |
|
|---|
| 486 | // left-button mouse click only is processed here
|
|---|
| 487 |
|
|---|
| 488 | // request focus in order to enable the expected keyboard shortcuts
|
|---|
| 489 | mv.requestFocus();
|
|---|
| 490 |
|
|---|
| 491 | // update which modifiers are pressed (shift, alt, ctrl)
|
|---|
| 492 | updateKeyModifiers(e);
|
|---|
| 493 |
|
|---|
| 494 | // We don't want to change to draw tool if the user tries to (de)select
|
|---|
| 495 | // stuff but accidentally clicks in an empty area when selection is empty
|
|---|
| 496 | cancelDrawMode = (shift || ctrl);
|
|---|
| 497 | didMouseDrag = false;
|
|---|
| 498 | initialMoveThresholdExceeded = false;
|
|---|
| 499 | mouseDownTime = System.currentTimeMillis();
|
|---|
| 500 | lastMousePos = e.getPoint();
|
|---|
| 501 |
|
|---|
| 502 | // primitives under cursor are stored in c collection
|
|---|
| 503 | Collection<OsmPrimitive> c = MapView.asColl(
|
|---|
| 504 | mv.getNearestNodeOrWay(e.getPoint(), OsmPrimitive.isSelectablePredicate, true));
|
|---|
| 505 |
|
|---|
| 506 | determineMapMode(!c.isEmpty());
|
|---|
| 507 |
|
|---|
| 508 | switch(mode) {
|
|---|
| 509 | case rotate:
|
|---|
| 510 | case scale:
|
|---|
| 511 | if (getCurrentDataSet().getSelected().isEmpty()) {
|
|---|
| 512 | getCurrentDataSet().setSelected(c);
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | // Mode.select redraws when selectPrims is called
|
|---|
| 516 | // Mode.move redraws when mouseDragged is called
|
|---|
| 517 | // Mode.rotate redraws here
|
|---|
| 518 | // Mode.scale redraws here
|
|---|
| 519 | break;
|
|---|
| 520 | case move:
|
|---|
| 521 | if (!cancelDrawMode && c.iterator().next() instanceof Way) {
|
|---|
| 522 | virtualManager.setupVirtual(e.getPoint());
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | selectPrims(cycleManager.cycleSetup(c, e.getPoint()), false, false);
|
|---|
| 526 | break;
|
|---|
| 527 | case select:
|
|---|
| 528 | default:
|
|---|
| 529 | selectionManager.register(mv, lassoMode);
|
|---|
| 530 | selectionManager.mousePressed(e);
|
|---|
| 531 | break;
|
|---|
| 532 | }
|
|---|
| 533 | giveUserFeedback(e);
|
|---|
| 534 | mv.repaint();
|
|---|
| 535 | updateStatusLine();
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | @Override
|
|---|
| 539 | public void mouseReleased(MouseEvent e) {
|
|---|
| 540 | if (!mv.isActiveLayerVisible())
|
|---|
| 541 | return;
|
|---|
| 542 |
|
|---|
| 543 | startingDraggingPos = null;
|
|---|
| 544 | mouseReleaseTime = System.currentTimeMillis();
|
|---|
| 545 |
|
|---|
| 546 | if (mode == Mode.select) {
|
|---|
| 547 | selectionManager.unregister(mv);
|
|---|
| 548 |
|
|---|
| 549 | // Select Draw Tool if no selection has been made
|
|---|
| 550 | if (getCurrentDataSet().getSelected().size() == 0 && !cancelDrawMode) {
|
|---|
| 551 | Main.map.selectDrawTool(true);
|
|---|
| 552 | return;
|
|---|
| 553 | }
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | if (mode == Mode.move && e.getButton() == MouseEvent.BUTTON1) {
|
|---|
| 557 | if (!didMouseDrag) {
|
|---|
| 558 | // only built in move mode
|
|---|
| 559 | virtualManager.clear();
|
|---|
| 560 | // do nothing if the click was to short too be recognized as a drag,
|
|---|
| 561 | // but the release position is farther than 10px away from the press position
|
|---|
| 562 | if (lastMousePos == null || lastMousePos.distanceSq(e.getPoint()) < 100) {
|
|---|
| 563 | updateKeyModifiers(e);
|
|---|
| 564 | selectPrims(cycleManager.cyclePrims(), true, false);
|
|---|
| 565 |
|
|---|
| 566 | // If the user double-clicked a node, change to draw mode
|
|---|
| 567 | Collection<OsmPrimitive> c = getCurrentDataSet().getSelected();
|
|---|
| 568 | if (e.getClickCount() >= 2 && c.size() == 1 && c.iterator().next() instanceof Node) {
|
|---|
| 569 | // We need to do it like this as otherwise drawAction will see a double
|
|---|
| 570 | // click and switch back to SelectMode
|
|---|
| 571 | Main.worker.execute(new Runnable() {
|
|---|
| 572 | public void run() {
|
|---|
| 573 | Main.map.selectDrawTool(true);
|
|---|
| 574 | }
|
|---|
| 575 | });
|
|---|
| 576 | return;
|
|---|
| 577 | }
|
|---|
| 578 | }
|
|---|
| 579 | } else {
|
|---|
| 580 | confirmOrUndoMovement(e);
|
|---|
| 581 | }
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | mode = null;
|
|---|
| 585 |
|
|---|
| 586 | // simply remove any highlights if the middle click popup is active because
|
|---|
| 587 | // the highlights don't depend on the cursor position there. If something was
|
|---|
| 588 | // selected beforehand this would put us into move mode as well, which breaks
|
|---|
| 589 | // the cycling through primitives on top of each other (see #6739).
|
|---|
| 590 | if(e.getButton() == MouseEvent.BUTTON2) {
|
|---|
| 591 | removeHighlighting();
|
|---|
| 592 | } else {
|
|---|
| 593 | giveUserFeedback(e);
|
|---|
| 594 | }
|
|---|
| 595 | updateStatusLine();
|
|---|
| 596 | }
|
|---|
| 597 |
|
|---|
| 598 | @Override
|
|---|
| 599 | public void selectionEnded(Rectangle r, MouseEvent e) {
|
|---|
| 600 | updateKeyModifiers(e);
|
|---|
| 601 | mv.repaint();
|
|---|
| 602 | selectPrims(selectionManager.getSelectedObjects(alt), true, true);
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | /**
|
|---|
| 606 | * sets the mapmode according to key modifiers and if there are any
|
|---|
| 607 | * selectables nearby. Everything has to be pre-determined for this
|
|---|
| 608 | * function; its main purpose is to centralize what the modifiers do.
|
|---|
| 609 | * @param hasSelectionNearby
|
|---|
| 610 | */
|
|---|
| 611 | private void determineMapMode(boolean hasSelectionNearby) {
|
|---|
| 612 | if (shift && ctrl) {
|
|---|
| 613 | mode = Mode.rotate;
|
|---|
| 614 | } else if (alt && ctrl) {
|
|---|
| 615 | mode = Mode.scale;
|
|---|
| 616 | } else if (hasSelectionNearby || dragInProgress()) {
|
|---|
| 617 | mode = Mode.move;
|
|---|
| 618 | } else {
|
|---|
| 619 | mode = Mode.select;
|
|---|
| 620 | }
|
|---|
| 621 | }
|
|---|
| 622 |
|
|---|
| 623 | /** returns true whenever elements have been grabbed and moved (i.e. the initial
|
|---|
| 624 | * thresholds have been exceeded) and is still in progress (i.e. mouse button
|
|---|
| 625 | * still pressed)
|
|---|
| 626 | */
|
|---|
| 627 | final private boolean dragInProgress() {
|
|---|
| 628 | return didMouseDrag && startingDraggingPos != null;
|
|---|
| 629 | }
|
|---|
| 630 |
|
|---|
| 631 |
|
|---|
| 632 | /**
|
|---|
| 633 | * Create or update data modfication command whle dragging mouse - implementation of
|
|---|
| 634 | * continuous moving, scaling and rotation
|
|---|
| 635 | * @param dx, @param dy - mouse displacement
|
|---|
| 636 | * @param currentEN -
|
|---|
| 637 | * @return
|
|---|
| 638 | */
|
|---|
| 639 | private boolean updateCommandWhileDragging(double dx, double dy, EastNorth currentEN) {
|
|---|
| 640 | // Currently we support only transformations which do not affect relations.
|
|---|
| 641 | // So don't add them in the first place to make handling easier
|
|---|
| 642 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelectedNodesAndWays();
|
|---|
| 643 | Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
|
|---|
| 644 | // for these transformations, having only one node makes no sense - quit silently
|
|---|
| 645 | if (affectedNodes.size() < 2 && (mode == Mode.rotate || mode == Mode.scale)) {
|
|---|
| 646 | return false;
|
|---|
| 647 | }
|
|---|
| 648 | Command c = !Main.main.undoRedo.commands.isEmpty()
|
|---|
| 649 | ? Main.main.undoRedo.commands.getLast() : null;
|
|---|
| 650 | if (c instanceof SequenceCommand) {
|
|---|
| 651 | c = ((SequenceCommand) c).getLastCommand();
|
|---|
| 652 | }
|
|---|
| 653 | if (mode == Mode.move) {
|
|---|
| 654 | // getCurrentDataSet().beginUpdate();
|
|---|
| 655 | if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand) c).getParticipatingPrimitives())) {
|
|---|
| 656 | ((MoveCommand) c).moveAgain(dx, dy);
|
|---|
| 657 | } else {
|
|---|
| 658 | Main.main.undoRedo.add(
|
|---|
| 659 | c = new MoveCommand(selection, dx, dy));
|
|---|
| 660 | }
|
|---|
| 661 | //getCurrentDataSet().endUpdate();
|
|---|
| 662 | for (Node n : affectedNodes) {
|
|---|
| 663 | if (n.getCoor().isOutSideWorld()) {
|
|---|
| 664 | // Revert move
|
|---|
| 665 | ((MoveCommand) c).moveAgain(-dx, -dy);
|
|---|
| 666 | JOptionPane.showMessageDialog(
|
|---|
| 667 | Main.parent,
|
|---|
| 668 | tr("Cannot move objects outside of the world."),
|
|---|
| 669 | tr("Warning"),
|
|---|
| 670 | JOptionPane.WARNING_MESSAGE);
|
|---|
| 671 | mv.setNewCursor(cursor, this);
|
|---|
| 672 | return false;
|
|---|
| 673 | }
|
|---|
| 674 | }
|
|---|
| 675 | } else if (mode == Mode.rotate) {
|
|---|
| 676 | if (c instanceof RotateCommand && affectedNodes.equals(((RotateCommand) c).getTransformedNodes())) {
|
|---|
| 677 | ((RotateCommand) c).handleEvent(currentEN);
|
|---|
| 678 | } else {
|
|---|
| 679 | Main.main.undoRedo.add(new RotateCommand(selection, currentEN));
|
|---|
| 680 | }
|
|---|
| 681 | } else if (mode == Mode.scale) {
|
|---|
| 682 | if (c instanceof ScaleCommand && affectedNodes.equals(((ScaleCommand) c).getTransformedNodes())) {
|
|---|
| 683 | ((ScaleCommand) c).handleEvent(currentEN);
|
|---|
| 684 | } else {
|
|---|
| 685 | Main.main.undoRedo.add(new ScaleCommand(selection, currentEN));
|
|---|
| 686 | }
|
|---|
| 687 | }
|
|---|
| 688 | return true;
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | private void confirmOrUndoMovement(MouseEvent e) {
|
|---|
| 692 | int max = Main.pref.getInteger("warn.move.maxelements", 20), limit = max;
|
|---|
| 693 | for (OsmPrimitive osm : getCurrentDataSet().getSelected()) {
|
|---|
| 694 | if (osm instanceof Way) {
|
|---|
| 695 | limit -= ((Way) osm).getNodes().size();
|
|---|
| 696 | }
|
|---|
| 697 | if ((limit -= 1) < 0) {
|
|---|
| 698 | break;
|
|---|
| 699 | }
|
|---|
| 700 | }
|
|---|
| 701 | if (limit < 0) {
|
|---|
| 702 | ExtendedDialog ed = new ExtendedDialog(
|
|---|
| 703 | Main.parent,
|
|---|
| 704 | tr("Move elements"),
|
|---|
| 705 | new String[]{tr("Move them"), tr("Undo move")});
|
|---|
| 706 | ed.setButtonIcons(new String[]{"reorder.png", "cancel.png"});
|
|---|
| 707 | ed.setContent(tr("You moved more than {0} elements. " + "Moving a large number of elements is often an error.\n" + "Really move them?", max));
|
|---|
| 708 | ed.setCancelButton(2);
|
|---|
| 709 | ed.toggleEnable("movedManyElements");
|
|---|
| 710 | ed.showDialog();
|
|---|
| 711 |
|
|---|
| 712 | if (ed.getValue() != 1) {
|
|---|
| 713 | Main.main.undoRedo.undo();
|
|---|
| 714 | }
|
|---|
| 715 | } else {
|
|---|
| 716 | // if small number of elements were moved,
|
|---|
| 717 | updateKeyModifiers(e);
|
|---|
| 718 | if (ctrl) mergePrims(e.getPoint());
|
|---|
| 719 | }
|
|---|
| 720 | getCurrentDataSet().fireSelectionChanged();
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | /** Merges the selected nodes to the one closest to the given mouse position iff the control
|
|---|
| 724 | * key is pressed. If there is no such node, no action will be done and no error will be
|
|---|
| 725 | * reported. If there is, it will execute the merge and add it to the undo buffer. */
|
|---|
| 726 | final private void mergePrims(Point p) {
|
|---|
| 727 | Collection<Node> selNodes = getCurrentDataSet().getSelectedNodes();
|
|---|
| 728 | if (selNodes.isEmpty())
|
|---|
| 729 | return;
|
|---|
| 730 |
|
|---|
| 731 | Node target = findNodeToMergeTo(p);
|
|---|
| 732 | if (target == null)
|
|---|
| 733 | return;
|
|---|
| 734 |
|
|---|
| 735 | Collection<Node> nodesToMerge = new LinkedList<Node>(selNodes);
|
|---|
| 736 | nodesToMerge.add(target);
|
|---|
| 737 | MergeNodesAction.doMergeNodes(Main.main.getEditLayer(), nodesToMerge, target);
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | /** tries to find a node to merge to when in move-merge mode for the current mouse
|
|---|
| 741 | * position. Either returns the node or null, if no suitable one is nearby. */
|
|---|
| 742 | final private Node findNodeToMergeTo(Point p) {
|
|---|
| 743 | Collection<Node> target = mv.getNearestNodes(p,
|
|---|
| 744 | getCurrentDataSet().getSelectedNodes(),
|
|---|
| 745 | OsmPrimitive.isSelectablePredicate);
|
|---|
| 746 | return target.isEmpty() ? null : target.iterator().next();
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | private void selectPrims(Collection<OsmPrimitive> prims, boolean released, boolean area) {
|
|---|
| 750 | System.out.println("selectPrims:" +prims);
|
|---|
| 751 | DataSet ds = getCurrentDataSet();
|
|---|
| 752 |
|
|---|
| 753 | // not allowed together: do not change dataset selection, return early
|
|---|
| 754 | // Virtual Ways: if non-empty the cursor is above a virtual node. So don't highlight
|
|---|
| 755 | // anything if about to drag the virtual node (i.e. !released) but continue if the
|
|---|
| 756 | // cursor is only released above a virtual node by accident (i.e. released). See #7018
|
|---|
| 757 | if ((shift && ctrl) || (ctrl && !released) || (!virtualManager.virtualWays.isEmpty() && !released))
|
|---|
| 758 | return;
|
|---|
| 759 |
|
|---|
| 760 | if (!released) {
|
|---|
| 761 | // Don't replace the selection if the user clicked on a
|
|---|
| 762 | // selected object (it breaks moving of selected groups).
|
|---|
| 763 | // Do it later, on mouse release.
|
|---|
| 764 | shift |= getCurrentDataSet().getSelected().containsAll(prims);
|
|---|
| 765 | }
|
|---|
| 766 |
|
|---|
| 767 | if (ctrl) {
|
|---|
| 768 | // Ctrl on an item toggles its selection status,
|
|---|
| 769 | // but Ctrl on an *area* just clears those items
|
|---|
| 770 | // out of the selection.
|
|---|
| 771 | if (area) {
|
|---|
| 772 | ds.clearSelection(prims);
|
|---|
| 773 | } else {
|
|---|
| 774 | ds.toggleSelected(prims);
|
|---|
| 775 | }
|
|---|
| 776 | } else if (shift) {
|
|---|
| 777 | // add prims to an existing selection
|
|---|
| 778 | ds.addSelected(prims);
|
|---|
| 779 | } else {
|
|---|
| 780 | // clear selection, then select the prims clicked
|
|---|
| 781 | ds.setSelected(prims);
|
|---|
| 782 | }
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | @Override
|
|---|
| 786 | public String getModeHelpText() {
|
|---|
| 787 | if (mode == Mode.select)
|
|---|
| 788 | return tr("Release the mouse button to select the objects in the rectangle.");
|
|---|
| 789 | else if (mode == Mode.move) {
|
|---|
| 790 | final boolean canMerge = getCurrentDataSet()!=null && !getCurrentDataSet().getSelectedNodes().isEmpty();
|
|---|
| 791 | final String mergeHelp = canMerge ? (" " + tr("Ctrl to merge with nearest node.")) : "";
|
|---|
| 792 | return tr("Release the mouse button to stop moving.") + mergeHelp;
|
|---|
| 793 | } else if (mode == Mode.rotate)
|
|---|
| 794 | return tr("Release the mouse button to stop rotating.");
|
|---|
| 795 | else if (mode == Mode.scale)
|
|---|
| 796 | return tr("Release the mouse button to stop scaling.");
|
|---|
| 797 | else
|
|---|
| 798 | return tr("Move objects by dragging; Shift to add to selection (Ctrl to toggle); Shift-Ctrl to rotate selected; Alt-Ctrl to scale selected; or change selection");
|
|---|
| 799 | }
|
|---|
| 800 |
|
|---|
| 801 | @Override
|
|---|
| 802 | public boolean layerIsSupported(Layer l) {
|
|---|
| 803 | return l instanceof OsmDataLayer;
|
|---|
| 804 | }
|
|---|
| 805 |
|
|---|
| 806 | public void setLassoMode(boolean lassoMode) {
|
|---|
| 807 | this.selectionManager.setLassoMode(lassoMode);
|
|---|
| 808 | this.lassoMode = lassoMode;
|
|---|
| 809 | }
|
|---|
| 810 |
|
|---|
| 811 | CycleManager cycleManager = new CycleManager();
|
|---|
| 812 | VirtualManager virtualManager = new VirtualManager();
|
|---|
| 813 |
|
|---|
| 814 | private class CycleManager {
|
|---|
| 815 | private Collection<OsmPrimitive> cycleList = Collections.emptyList();
|
|---|
| 816 | private boolean cyclePrims = false;
|
|---|
| 817 | private OsmPrimitive cycleStart = null;
|
|---|
| 818 |
|
|---|
| 819 | /**
|
|---|
| 820 | *
|
|---|
| 821 | * @param osm nearest primitive found by simple method
|
|---|
| 822 | * @param e
|
|---|
| 823 | * @return
|
|---|
| 824 | */
|
|---|
| 825 | private Collection<OsmPrimitive> cycleSetup(Collection<OsmPrimitive> single, Point p) {
|
|---|
| 826 | OsmPrimitive osm = null;
|
|---|
| 827 |
|
|---|
| 828 | if (single != null && !single.isEmpty()) {
|
|---|
| 829 | osm = single.iterator().next();
|
|---|
| 830 |
|
|---|
| 831 | // Point p = e.getPoint();
|
|---|
| 832 | boolean waitForMouseUp = Main.pref.getBoolean("mappaint.select.waits-for-mouse-up", false);
|
|---|
| 833 | // updateKeyModifiers(e); // cycleSetup called only after updateModifiers !
|
|---|
| 834 | alt = alt || Main.pref.getBoolean("selectaction.cycles.multiple.matches", false);
|
|---|
| 835 |
|
|---|
| 836 | if (!alt) {
|
|---|
| 837 | cycleList = MapView.asColl(osm);
|
|---|
| 838 |
|
|---|
| 839 | if (waitForMouseUp) {
|
|---|
| 840 | // prefer a selected nearest node or way, if possible
|
|---|
| 841 | osm = mv.getNearestNodeOrWay(p, OsmPrimitive.isSelectablePredicate, true);
|
|---|
| 842 | }
|
|---|
| 843 | } else {
|
|---|
| 844 | cycleList = mv.getAllNearest(p, OsmPrimitive.isSelectablePredicate);
|
|---|
| 845 |
|
|---|
| 846 | if (cycleList.size() > 1) {
|
|---|
| 847 | cyclePrims = false;
|
|---|
| 848 |
|
|---|
| 849 | OsmPrimitive old = osm;
|
|---|
| 850 | for (OsmPrimitive o : cycleList) {
|
|---|
| 851 | if (o.isSelected()) {
|
|---|
| 852 | cyclePrims = true;
|
|---|
| 853 | osm = o;
|
|---|
| 854 | break;
|
|---|
| 855 | }
|
|---|
| 856 | }
|
|---|
| 857 |
|
|---|
| 858 | // special case: for cycle groups of 2, we can toggle to the
|
|---|
| 859 | // true nearest primitive on mousePressed right away
|
|---|
| 860 | if (cycleList.size() == 2 && !waitForMouseUp) {
|
|---|
| 861 | if (!(osm.equals(old) || osm.isNew() || ctrl)) {
|
|---|
| 862 | cyclePrims = false;
|
|---|
| 863 | osm = old;
|
|---|
| 864 | } // else defer toggling to mouseRelease time in those cases:
|
|---|
| 865 | /*
|
|---|
| 866 | * osm == old -- the true nearest node is the selected one
|
|---|
| 867 | * osm is a new node -- do not break unglue ways in ALT mode
|
|---|
| 868 | * ctrl is pressed -- ctrl generally works on mouseReleased
|
|---|
| 869 | */
|
|---|
| 870 | }
|
|---|
| 871 | }
|
|---|
| 872 | }
|
|---|
| 873 | }
|
|---|
| 874 |
|
|---|
| 875 | return MapView.asColl(osm);
|
|---|
| 876 | }
|
|---|
| 877 |
|
|---|
| 878 | /**
|
|---|
| 879 | * Modifies current selection state and returns the next element in a
|
|---|
| 880 | * selection cycle given by <code>prims</code>.
|
|---|
| 881 | * @param prims the primitives that form the selection cycle
|
|---|
| 882 | * @param mouse event
|
|---|
| 883 | * @return the next element of cycle list <code>prims</code>.
|
|---|
| 884 | */
|
|---|
| 885 | private Collection<OsmPrimitive> cyclePrims() {
|
|---|
| 886 | Collection<OsmPrimitive> prims = cycleList;
|
|---|
| 887 | OsmPrimitive nxt = null;
|
|---|
| 888 |
|
|---|
| 889 | if (prims.size() > 1) {
|
|---|
| 890 | // updateKeyModifiers(e); // already called before !
|
|---|
| 891 |
|
|---|
| 892 | DataSet ds = getCurrentDataSet();
|
|---|
| 893 | OsmPrimitive first = prims.iterator().next(), foundInDS = null;
|
|---|
| 894 | nxt = first;
|
|---|
| 895 |
|
|---|
| 896 | for (Iterator<OsmPrimitive> i = prims.iterator(); i.hasNext();) {
|
|---|
| 897 | if (cyclePrims && shift) {
|
|---|
| 898 | if (!(nxt = i.next()).isSelected()) {
|
|---|
| 899 | break; // take first primitive in prims list not in sel
|
|---|
| 900 | }
|
|---|
| 901 | } else {
|
|---|
| 902 | if ((nxt = i.next()).isSelected()) {
|
|---|
| 903 | foundInDS = nxt;
|
|---|
| 904 | if (cyclePrims || ctrl) {
|
|---|
| 905 | ds.clearSelection(foundInDS);
|
|---|
| 906 | nxt = i.hasNext() ? i.next() : first;
|
|---|
| 907 | }
|
|---|
| 908 | break; // take next primitive in prims list
|
|---|
| 909 | }
|
|---|
| 910 | }
|
|---|
| 911 | }
|
|---|
| 912 |
|
|---|
| 913 | if (ctrl) {
|
|---|
| 914 | // a member of prims was found in the current dataset selection
|
|---|
| 915 | if (foundInDS != null) {
|
|---|
| 916 | // mouse was moved to a different selection group w/ a previous sel
|
|---|
| 917 | if (!prims.contains(cycleStart)) {
|
|---|
| 918 | ds.clearSelection(prims);
|
|---|
| 919 | cycleStart = foundInDS;
|
|---|
| 920 | } else if (cycleStart.equals(nxt)) {
|
|---|
| 921 | // loop detected, insert deselect step
|
|---|
| 922 | ds.addSelected(nxt);
|
|---|
| 923 | }
|
|---|
| 924 | } else {
|
|---|
| 925 | // setup for iterating a sel group again or a new, different one..
|
|---|
| 926 | nxt = (prims.contains(cycleStart)) ? cycleStart : first;
|
|---|
| 927 | cycleStart = nxt;
|
|---|
| 928 | }
|
|---|
| 929 | } else {
|
|---|
| 930 | cycleStart = null;
|
|---|
| 931 | }
|
|---|
| 932 | }
|
|---|
| 933 |
|
|---|
| 934 | // pass on prims, if it had less than 2 elements
|
|---|
| 935 | return (nxt != null) ? MapView.asColl(nxt) : prims;
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | }
|
|---|
| 939 |
|
|---|
| 940 | private class VirtualManager {
|
|---|
| 941 |
|
|---|
| 942 | private Node virtualNode = null;
|
|---|
| 943 | private Collection<WaySegment> virtualWays = new LinkedList<WaySegment>();
|
|---|
| 944 |
|
|---|
| 945 |
|
|---|
| 946 | /**
|
|---|
| 947 | * Calculate a virtual node if there is enough visual space to draw a crosshair
|
|---|
| 948 | * node and the middle of a way segment is clicked. If the user drags the
|
|---|
| 949 | * crosshair node, it will be added to all ways in <code>virtualWays</code>.
|
|---|
| 950 | *
|
|---|
| 951 | * @param e contains the point clicked
|
|---|
| 952 | * @return whether <code>virtualNode</code> and <code>virtualWays</code> were setup.
|
|---|
| 953 | */
|
|---|
| 954 | private boolean setupVirtual(Point p) {
|
|---|
| 955 | if (Main.pref.getInteger("mappaint.node.virtual-size", 8) > 0) {
|
|---|
| 956 | int virtualSnapDistSq = Main.pref.getInteger("mappaint.node.virtual-snap-distance", 8);
|
|---|
| 957 | int virtualSpace = Main.pref.getInteger("mappaint.node.virtual-space", 70);
|
|---|
| 958 | virtualSnapDistSq *= virtualSnapDistSq;
|
|---|
| 959 |
|
|---|
| 960 | Collection<WaySegment> selVirtualWays = new LinkedList<WaySegment>();
|
|---|
| 961 | Pair<Node, Node> vnp = null, wnp = new Pair<Node, Node>(null, null);
|
|---|
| 962 |
|
|---|
| 963 | Way w = null;
|
|---|
| 964 | for (WaySegment ws : mv.getNearestWaySegments(p, OsmPrimitive.isSelectablePredicate)) {
|
|---|
| 965 | w = ws.way;
|
|---|
| 966 |
|
|---|
| 967 | Point2D p1 = mv.getPoint2D(wnp.a = w.getNode(ws.lowerIndex));
|
|---|
| 968 | Point2D p2 = mv.getPoint2D(wnp.b = w.getNode(ws.lowerIndex + 1));
|
|---|
| 969 | if (WireframeMapRenderer.isLargeSegment(p1, p2, virtualSpace)) {
|
|---|
| 970 | Point2D pc = new Point2D.Double((p1.getX() + p2.getX()) / 2, (p1.getY() + p2.getY()) / 2);
|
|---|
| 971 | if (p.distanceSq(pc) < virtualSnapDistSq) {
|
|---|
| 972 | // Check that only segments on top of each other get added to the
|
|---|
| 973 | // virtual ways list. Otherwise ways that coincidentally have their
|
|---|
| 974 | // virtual node at the same spot will be joined which is likely unwanted
|
|---|
| 975 | Pair.sort(wnp);
|
|---|
| 976 | if (vnp == null) {
|
|---|
| 977 | vnp = new Pair<Node, Node>(wnp.a, wnp.b);
|
|---|
| 978 | virtualNode = new Node(mv.getLatLon(pc.getX(), pc.getY()));
|
|---|
| 979 | }
|
|---|
| 980 | if (vnp.equals(wnp)) {
|
|---|
| 981 | (w.isSelected() ? selVirtualWays : virtualWays).add(ws);
|
|---|
| 982 | }
|
|---|
| 983 | }
|
|---|
| 984 | }
|
|---|
| 985 | }
|
|---|
| 986 |
|
|---|
| 987 | if (!selVirtualWays.isEmpty()) {
|
|---|
| 988 | virtualWays = selVirtualWays;
|
|---|
| 989 | }
|
|---|
| 990 | }
|
|---|
| 991 |
|
|---|
| 992 | return !virtualWays.isEmpty();
|
|---|
| 993 | }
|
|---|
| 994 |
|
|---|
| 995 | private void processVirtualNodeMovements(double dx, double dy) {
|
|---|
| 996 | Collection<Command> virtualCmds = new LinkedList<Command>();
|
|---|
| 997 | virtualCmds.add(new AddCommand(virtualNode));
|
|---|
| 998 | for (WaySegment virtualWay : virtualWays) {
|
|---|
| 999 | Way w = virtualWay.way;
|
|---|
| 1000 | Way wnew = new Way(w);
|
|---|
| 1001 | wnew.addNode(virtualWay.lowerIndex + 1, virtualNode);
|
|---|
| 1002 | virtualCmds.add(new ChangeCommand(w, wnew));
|
|---|
| 1003 | }
|
|---|
| 1004 | virtualCmds.add(new MoveCommand(virtualNode, dx, dy));
|
|---|
| 1005 | String text = trn("Add and move a virtual new node to way",
|
|---|
| 1006 | "Add and move a virtual new node to {0} ways", virtualWays.size(),
|
|---|
| 1007 | virtualWays.size());
|
|---|
| 1008 | Main.main.undoRedo.add(new SequenceCommand(text, virtualCmds));
|
|---|
| 1009 | getCurrentDataSet().setSelected(Collections.singleton((OsmPrimitive) virtualNode));
|
|---|
| 1010 | clear();
|
|---|
| 1011 | }
|
|---|
| 1012 |
|
|---|
| 1013 | private void clear() {
|
|---|
| 1014 | virtualWays.clear();
|
|---|
| 1015 | virtualNode = null;
|
|---|
| 1016 | }
|
|---|
| 1017 |
|
|---|
| 1018 | private boolean hasVirtualNode() {
|
|---|
| 1019 | return virtualNode != null;
|
|---|
| 1020 | }
|
|---|
| 1021 | }
|
|---|
| 1022 | }
|
|---|