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