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