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