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.marktr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
8 |
|
---|
9 | import java.awt.BasicStroke;
|
---|
10 | import java.awt.Color;
|
---|
11 | import java.awt.Cursor;
|
---|
12 | import java.awt.Graphics2D;
|
---|
13 | import java.awt.Point;
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.awt.event.KeyEvent;
|
---|
16 | import java.awt.event.MouseEvent;
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.Collections;
|
---|
20 | import java.util.HashMap;
|
---|
21 | import java.util.HashSet;
|
---|
22 | import java.util.Iterator;
|
---|
23 | import java.util.LinkedList;
|
---|
24 | import java.util.List;
|
---|
25 | import java.util.Map;
|
---|
26 | import java.util.Set;
|
---|
27 |
|
---|
28 | import javax.swing.AbstractAction;
|
---|
29 | import javax.swing.JCheckBoxMenuItem;
|
---|
30 | import javax.swing.JMenuItem;
|
---|
31 | import javax.swing.JOptionPane;
|
---|
32 |
|
---|
33 | import org.openstreetmap.josm.Main;
|
---|
34 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
35 | import org.openstreetmap.josm.command.AddCommand;
|
---|
36 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
37 | import org.openstreetmap.josm.command.Command;
|
---|
38 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
39 | import org.openstreetmap.josm.data.Bounds;
|
---|
40 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
41 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
42 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
43 | import org.openstreetmap.josm.data.osm.Node;
|
---|
44 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
45 | import org.openstreetmap.josm.data.osm.Way;
|
---|
46 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
47 | import org.openstreetmap.josm.data.osm.visitor.paint.ArrowPaintHelper;
|
---|
48 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
49 | import org.openstreetmap.josm.data.preferences.AbstractToStringProperty;
|
---|
50 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
51 | import org.openstreetmap.josm.data.preferences.CachingProperty;
|
---|
52 | import org.openstreetmap.josm.data.preferences.ColorProperty;
|
---|
53 | import org.openstreetmap.josm.data.preferences.DoubleProperty;
|
---|
54 | import org.openstreetmap.josm.data.preferences.StrokeProperty;
|
---|
55 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
56 | import org.openstreetmap.josm.gui.MapView;
|
---|
57 | import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
|
---|
58 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
59 | import org.openstreetmap.josm.gui.draw.MapPath2D;
|
---|
60 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
61 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
---|
62 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
63 | import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
|
---|
64 | import org.openstreetmap.josm.gui.util.ModifierListener;
|
---|
65 | import org.openstreetmap.josm.tools.Geometry;
|
---|
66 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
67 | import org.openstreetmap.josm.tools.Pair;
|
---|
68 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
69 | import org.openstreetmap.josm.tools.Utils;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Mapmode to add nodes, create and extend ways.
|
---|
73 | */
|
---|
74 | public class DrawAction extends MapMode implements MapViewPaintable, SelectionChangedListener, KeyPressReleaseListener, ModifierListener {
|
---|
75 |
|
---|
76 | private static final Color ORANGE_TRANSPARENT = new Color(Color.ORANGE.getRed(), Color.ORANGE.getGreen(), Color.ORANGE.getBlue(), 128);
|
---|
77 |
|
---|
78 | private static final ArrowPaintHelper START_WAY_INDICATOR = new ArrowPaintHelper(Math.toRadians(90), 8);
|
---|
79 |
|
---|
80 | static final CachingProperty<Boolean> USE_REPEATED_SHORTCUT
|
---|
81 | = new BooleanProperty("draw.anglesnap.toggleOnRepeatedA", true).cached();
|
---|
82 | static final CachingProperty<BasicStroke> RUBBER_LINE_STROKE
|
---|
83 | = new StrokeProperty("draw.stroke.helper-line", "3").cached();
|
---|
84 |
|
---|
85 | static final CachingProperty<BasicStroke> HIGHLIGHT_STROKE
|
---|
86 | = new StrokeProperty("draw.anglesnap.stroke.highlight", "10").cached();
|
---|
87 | static final CachingProperty<BasicStroke> HELPER_STROKE
|
---|
88 | = new StrokeProperty("draw.anglesnap.stroke.helper", "1 4").cached();
|
---|
89 |
|
---|
90 | static final CachingProperty<Double> SNAP_ANGLE_TOLERANCE
|
---|
91 | = new DoubleProperty("draw.anglesnap.tolerance", 5.0).cached();
|
---|
92 | static final CachingProperty<Boolean> DRAW_CONSTRUCTION_GEOMETRY
|
---|
93 | = new BooleanProperty("draw.anglesnap.drawConstructionGeometry", true).cached();
|
---|
94 | static final CachingProperty<Boolean> SHOW_PROJECTED_POINT
|
---|
95 | = new BooleanProperty("draw.anglesnap.drawProjectedPoint", true).cached();
|
---|
96 | static final CachingProperty<Boolean> SNAP_TO_PROJECTIONS
|
---|
97 | = new BooleanProperty("draw.anglesnap.projectionsnap", true).cached();
|
---|
98 |
|
---|
99 | static final CachingProperty<Boolean> SHOW_ANGLE
|
---|
100 | = new BooleanProperty("draw.anglesnap.showAngle", true).cached();
|
---|
101 |
|
---|
102 | static final CachingProperty<Color> SNAP_HELPER_COLOR
|
---|
103 | = new ColorProperty(marktr("draw angle snap"), Color.ORANGE).cached();
|
---|
104 |
|
---|
105 | static final CachingProperty<Color> HIGHLIGHT_COLOR
|
---|
106 | = new ColorProperty(marktr("draw angle snap highlight"), ORANGE_TRANSPARENT).cached();
|
---|
107 |
|
---|
108 | static final AbstractToStringProperty<Color> RUBBER_LINE_COLOR
|
---|
109 | = PaintColors.SELECTED.getProperty().getChildColor(marktr("helper line"));
|
---|
110 |
|
---|
111 | static final CachingProperty<Boolean> DRAW_HELPER_LINE
|
---|
112 | = new BooleanProperty("draw.helper-line", true).cached();
|
---|
113 | static final CachingProperty<Boolean> DRAW_TARGET_HIGHLIGHT
|
---|
114 | = new BooleanProperty("draw.target-highlight", true).cached();
|
---|
115 | static final CachingProperty<Double> SNAP_TO_INTERSECTION_THRESHOLD
|
---|
116 | = new DoubleProperty("edit.snap-intersection-threshold", 10).cached();
|
---|
117 |
|
---|
118 | private final Cursor cursorJoinNode;
|
---|
119 | private final Cursor cursorJoinWay;
|
---|
120 |
|
---|
121 | private transient Node lastUsedNode;
|
---|
122 | private double toleranceMultiplier;
|
---|
123 |
|
---|
124 | private transient Node mouseOnExistingNode;
|
---|
125 | private transient Set<Way> mouseOnExistingWays = new HashSet<>();
|
---|
126 | // old highlights store which primitives are currently highlighted. This
|
---|
127 | // is true, even if target highlighting is disabled since the status bar
|
---|
128 | // derives its information from this list as well.
|
---|
129 | private transient Set<OsmPrimitive> oldHighlights = new HashSet<>();
|
---|
130 | // new highlights contains a list of primitives that should be highlighted
|
---|
131 | // but haven't been so far. The idea is to compare old and new and only
|
---|
132 | // repaint if there are changes.
|
---|
133 | private transient Set<OsmPrimitive> newHighlights = new HashSet<>();
|
---|
134 | private boolean wayIsFinished;
|
---|
135 | private Point mousePos;
|
---|
136 | private Point oldMousePos;
|
---|
137 |
|
---|
138 | private transient Node currentBaseNode;
|
---|
139 | private transient Node previousNode;
|
---|
140 | private EastNorth currentMouseEastNorth;
|
---|
141 |
|
---|
142 | private final transient DrawSnapHelper snapHelper = new DrawSnapHelper(this);
|
---|
143 |
|
---|
144 | private final transient Shortcut backspaceShortcut;
|
---|
145 | private final BackSpaceAction backspaceAction;
|
---|
146 | private final transient Shortcut snappingShortcut;
|
---|
147 | private boolean ignoreNextKeyRelease;
|
---|
148 |
|
---|
149 | private final SnapChangeAction snapChangeAction;
|
---|
150 | private final JCheckBoxMenuItem snapCheckboxMenuItem;
|
---|
151 | private static final BasicStroke BASIC_STROKE = new BasicStroke(1);
|
---|
152 |
|
---|
153 | private Point rightClickPressPos;
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Constructs a new {@code DrawAction}.
|
---|
157 | * @since 11713
|
---|
158 | */
|
---|
159 | public DrawAction() {
|
---|
160 | super(tr("Draw"), "node/autonode", tr("Draw nodes"),
|
---|
161 | Shortcut.registerShortcut("mapmode:draw", tr("Mode: {0}", tr("Draw")), KeyEvent.VK_A, Shortcut.DIRECT),
|
---|
162 | ImageProvider.getCursor("crosshair", null));
|
---|
163 |
|
---|
164 | snappingShortcut = Shortcut.registerShortcut("mapmode:drawanglesnapping",
|
---|
165 | tr("Mode: Draw Angle snapping"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
|
---|
166 | snapChangeAction = new SnapChangeAction();
|
---|
167 | snapCheckboxMenuItem = addMenuItem();
|
---|
168 | snapHelper.setMenuCheckBox(snapCheckboxMenuItem);
|
---|
169 | backspaceShortcut = Shortcut.registerShortcut("mapmode:backspace",
|
---|
170 | tr("Backspace in Add mode"), KeyEvent.VK_BACK_SPACE, Shortcut.DIRECT);
|
---|
171 | backspaceAction = new BackSpaceAction();
|
---|
172 | cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode");
|
---|
173 | cursorJoinWay = ImageProvider.getCursor("crosshair", "joinway");
|
---|
174 |
|
---|
175 | snapHelper.init();
|
---|
176 | }
|
---|
177 |
|
---|
178 | private JCheckBoxMenuItem addMenuItem() {
|
---|
179 | int n = Main.main.menu.editMenu.getItemCount();
|
---|
180 | for (int i = n-1; i > 0; i--) {
|
---|
181 | JMenuItem item = Main.main.menu.editMenu.getItem(i);
|
---|
182 | if (item != null && item.getAction() != null && item.getAction() instanceof SnapChangeAction) {
|
---|
183 | Main.main.menu.editMenu.remove(i);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return MainMenu.addWithCheckbox(Main.main.menu.editMenu, snapChangeAction, MainMenu.WINDOW_MENU_GROUP.VOLATILE);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Checks if a map redraw is required and does so if needed. Also updates the status bar.
|
---|
191 | * @return true if a repaint is needed
|
---|
192 | */
|
---|
193 | private boolean redrawIfRequired() {
|
---|
194 | updateStatusLine();
|
---|
195 | // repaint required if the helper line is active.
|
---|
196 | boolean needsRepaint = DRAW_HELPER_LINE.get() && !wayIsFinished;
|
---|
197 | if (DRAW_TARGET_HIGHLIGHT.get()) {
|
---|
198 | // move newHighlights to oldHighlights; only update changed primitives
|
---|
199 | for (OsmPrimitive x : newHighlights) {
|
---|
200 | if (oldHighlights.contains(x)) {
|
---|
201 | continue;
|
---|
202 | }
|
---|
203 | x.setHighlighted(true);
|
---|
204 | needsRepaint = true;
|
---|
205 | }
|
---|
206 | oldHighlights.removeAll(newHighlights);
|
---|
207 | for (OsmPrimitive x : oldHighlights) {
|
---|
208 | x.setHighlighted(false);
|
---|
209 | needsRepaint = true;
|
---|
210 | }
|
---|
211 | }
|
---|
212 | // required in order to print correct help text
|
---|
213 | oldHighlights = newHighlights;
|
---|
214 |
|
---|
215 | if (!needsRepaint && !DRAW_TARGET_HIGHLIGHT.get())
|
---|
216 | return false;
|
---|
217 |
|
---|
218 | // update selection to reflect which way being modified
|
---|
219 | OsmDataLayer editLayer = getLayerManager().getEditLayer();
|
---|
220 | if (editLayer != null && getCurrentBaseNode() != null && !editLayer.data.selectionEmpty()) {
|
---|
221 | DataSet currentDataSet = editLayer.data;
|
---|
222 | Way continueFrom = getWayForNode(getCurrentBaseNode());
|
---|
223 | if (alt && continueFrom != null && (!getCurrentBaseNode().isSelected() || continueFrom.isSelected())) {
|
---|
224 | addRemoveSelection(currentDataSet, getCurrentBaseNode(), continueFrom);
|
---|
225 | needsRepaint = true;
|
---|
226 | } else if (!alt && continueFrom != null && !continueFrom.isSelected()) {
|
---|
227 | currentDataSet.addSelected(continueFrom);
|
---|
228 | needsRepaint = true;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (needsRepaint && editLayer != null) {
|
---|
233 | editLayer.invalidate();
|
---|
234 | }
|
---|
235 | return needsRepaint;
|
---|
236 | }
|
---|
237 |
|
---|
238 | private static void addRemoveSelection(DataSet ds, OsmPrimitive toAdd, OsmPrimitive toRemove) {
|
---|
239 | ds.beginUpdate(); // to prevent the selection listener to screw around with the state
|
---|
240 | ds.addSelected(toAdd);
|
---|
241 | ds.clearSelection(toRemove);
|
---|
242 | ds.endUpdate();
|
---|
243 | }
|
---|
244 |
|
---|
245 | @Override
|
---|
246 | public void enterMode() {
|
---|
247 | if (!isEnabled())
|
---|
248 | return;
|
---|
249 | super.enterMode();
|
---|
250 | readPreferences();
|
---|
251 |
|
---|
252 | // determine if selection is suitable to continue drawing. If it
|
---|
253 | // isn't, set wayIsFinished to true to avoid superfluous repaints.
|
---|
254 | determineCurrentBaseNodeAndPreviousNode(getLayerManager().getEditDataSet().getSelected());
|
---|
255 | wayIsFinished = getCurrentBaseNode() == null;
|
---|
256 |
|
---|
257 | toleranceMultiplier = 0.01 * NavigatableComponent.PROP_SNAP_DISTANCE.get();
|
---|
258 |
|
---|
259 | snapHelper.init();
|
---|
260 | snapCheckboxMenuItem.getAction().setEnabled(true);
|
---|
261 |
|
---|
262 | Main.map.statusLine.getAnglePanel().addMouseListener(snapHelper.anglePopupListener);
|
---|
263 | Main.registerActionShortcut(backspaceAction, backspaceShortcut);
|
---|
264 |
|
---|
265 | Main.map.mapView.addMouseListener(this);
|
---|
266 | Main.map.mapView.addMouseMotionListener(this);
|
---|
267 | Main.map.mapView.addTemporaryLayer(this);
|
---|
268 | DataSet.addSelectionListener(this);
|
---|
269 |
|
---|
270 | Main.map.keyDetector.addKeyListener(this);
|
---|
271 | Main.map.keyDetector.addModifierListener(this);
|
---|
272 | ignoreNextKeyRelease = true;
|
---|
273 | }
|
---|
274 |
|
---|
275 | @Override
|
---|
276 | public void exitMode() {
|
---|
277 | super.exitMode();
|
---|
278 | Main.map.mapView.removeMouseListener(this);
|
---|
279 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
280 | Main.map.mapView.removeTemporaryLayer(this);
|
---|
281 | DataSet.removeSelectionListener(this);
|
---|
282 | Main.unregisterActionShortcut(backspaceAction, backspaceShortcut);
|
---|
283 | snapHelper.unsetFixedMode();
|
---|
284 | snapCheckboxMenuItem.getAction().setEnabled(false);
|
---|
285 |
|
---|
286 | Main.map.statusLine.getAnglePanel().removeMouseListener(snapHelper.anglePopupListener);
|
---|
287 | Main.map.statusLine.activateAnglePanel(false);
|
---|
288 |
|
---|
289 | removeHighlighting();
|
---|
290 | Main.map.keyDetector.removeKeyListener(this);
|
---|
291 | Main.map.keyDetector.removeModifierListener(this);
|
---|
292 |
|
---|
293 | // when exiting we let everybody know about the currently selected
|
---|
294 | // primitives
|
---|
295 | //
|
---|
296 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
297 | if (ds != null) {
|
---|
298 | ds.fireSelectionChanged();
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * redraw to (possibly) get rid of helper line if selection changes.
|
---|
304 | */
|
---|
305 | @Override
|
---|
306 | public void modifiersChanged(int modifiers) {
|
---|
307 | if (!Main.isDisplayingMapView() || !Main.map.mapView.isActiveLayerDrawable())
|
---|
308 | return;
|
---|
309 | updateKeyModifiers(modifiers);
|
---|
310 | computeHelperLine();
|
---|
311 | addHighlighting();
|
---|
312 | }
|
---|
313 |
|
---|
314 | @Override
|
---|
315 | public void doKeyPressed(KeyEvent e) {
|
---|
316 | if (!snappingShortcut.isEvent(e) && !(USE_REPEATED_SHORTCUT.get() && getShortcut().isEvent(e)))
|
---|
317 | return;
|
---|
318 | snapHelper.setFixedMode();
|
---|
319 | computeHelperLine();
|
---|
320 | redrawIfRequired();
|
---|
321 | }
|
---|
322 |
|
---|
323 | @Override
|
---|
324 | public void doKeyReleased(KeyEvent e) {
|
---|
325 | if (!snappingShortcut.isEvent(e) && !(USE_REPEATED_SHORTCUT.get() && getShortcut().isEvent(e)))
|
---|
326 | return;
|
---|
327 | if (ignoreNextKeyRelease) {
|
---|
328 | ignoreNextKeyRelease = false;
|
---|
329 | return;
|
---|
330 | }
|
---|
331 | snapHelper.unFixOrTurnOff();
|
---|
332 | computeHelperLine();
|
---|
333 | redrawIfRequired();
|
---|
334 | }
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * redraw to (possibly) get rid of helper line if selection changes.
|
---|
338 | */
|
---|
339 | @Override
|
---|
340 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
341 | if (!Main.map.mapView.isActiveLayerDrawable())
|
---|
342 | return;
|
---|
343 | computeHelperLine();
|
---|
344 | addHighlighting();
|
---|
345 | }
|
---|
346 |
|
---|
347 | private void tryAgain(MouseEvent e) {
|
---|
348 | getLayerManager().getEditDataSet().setSelected();
|
---|
349 | mouseReleased(e);
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * This function should be called when the user wishes to finish his current draw action.
|
---|
354 | * If Potlatch Style is enabled, it will switch to select tool, otherwise simply disable
|
---|
355 | * the helper line until the user chooses to draw something else.
|
---|
356 | */
|
---|
357 | private void finishDrawing() {
|
---|
358 | // let everybody else know about the current selection
|
---|
359 | //
|
---|
360 | Main.getLayerManager().getEditDataSet().fireSelectionChanged();
|
---|
361 | lastUsedNode = null;
|
---|
362 | wayIsFinished = true;
|
---|
363 | Main.map.selectSelectTool(true);
|
---|
364 | snapHelper.noSnapNow();
|
---|
365 |
|
---|
366 | // Redraw to remove the helper line stub
|
---|
367 | computeHelperLine();
|
---|
368 | removeHighlighting();
|
---|
369 | }
|
---|
370 |
|
---|
371 | @Override
|
---|
372 | public void mousePressed(MouseEvent e) {
|
---|
373 | if (e.getButton() == MouseEvent.BUTTON3) {
|
---|
374 | rightClickPressPos = e.getPoint();
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * If user clicked with the left button, add a node at the current mouse
|
---|
380 | * position.
|
---|
381 | *
|
---|
382 | * If in nodeway mode, insert the node into the way.
|
---|
383 | */
|
---|
384 | @Override
|
---|
385 | public void mouseReleased(MouseEvent e) {
|
---|
386 | if (e.getButton() == MouseEvent.BUTTON3) {
|
---|
387 | Point curMousePos = e.getPoint();
|
---|
388 | if (curMousePos.equals(rightClickPressPos)) {
|
---|
389 | tryToSetBaseSegmentForAngleSnap();
|
---|
390 | }
|
---|
391 | return;
|
---|
392 | }
|
---|
393 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
394 | return;
|
---|
395 | if (!Main.map.mapView.isActiveLayerDrawable())
|
---|
396 | return;
|
---|
397 | // request focus in order to enable the expected keyboard shortcuts
|
---|
398 | //
|
---|
399 | Main.map.mapView.requestFocus();
|
---|
400 |
|
---|
401 | if (e.getClickCount() > 1 && mousePos != null && mousePos.equals(oldMousePos)) {
|
---|
402 | // A double click equals "user clicked last node again, finish way"
|
---|
403 | // Change draw tool only if mouse position is nearly the same, as
|
---|
404 | // otherwise fast clicks will count as a double click
|
---|
405 | finishDrawing();
|
---|
406 | return;
|
---|
407 | }
|
---|
408 | oldMousePos = mousePos;
|
---|
409 |
|
---|
410 | // we copy ctrl/alt/shift from the event just in case our global
|
---|
411 | // keyDetector didn't make it through the security manager. Unclear
|
---|
412 | // if that can ever happen but better be safe.
|
---|
413 | updateKeyModifiers(e);
|
---|
414 | mousePos = e.getPoint();
|
---|
415 |
|
---|
416 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
417 | Collection<OsmPrimitive> selection = new ArrayList<>(ds.getSelected());
|
---|
418 |
|
---|
419 | boolean newNode = false;
|
---|
420 | Node n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive::isSelectable);
|
---|
421 | if (ctrl) {
|
---|
422 | Iterator<Way> it = ds.getSelectedWays().iterator();
|
---|
423 | if (it.hasNext()) {
|
---|
424 | // ctrl-click on node of selected way = reuse node despite of ctrl
|
---|
425 | if (!it.next().containsNode(n)) n = null;
|
---|
426 | } else {
|
---|
427 | n = null; // ctrl-click + no selected way = new node
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | if (n != null && !snapHelper.isActive()) {
|
---|
432 | // user clicked on node
|
---|
433 | if (selection.isEmpty() || wayIsFinished) {
|
---|
434 | // select the clicked node and do nothing else
|
---|
435 | // (this is just a convenience option so that people don't
|
---|
436 | // have to switch modes)
|
---|
437 |
|
---|
438 | ds.setSelected(n);
|
---|
439 | // If we extend/continue an existing way, select it already now to make it obvious
|
---|
440 | Way continueFrom = getWayForNode(n);
|
---|
441 | if (continueFrom != null) {
|
---|
442 | ds.addSelected(continueFrom);
|
---|
443 | }
|
---|
444 |
|
---|
445 | // The user explicitly selected a node, so let him continue drawing
|
---|
446 | wayIsFinished = false;
|
---|
447 | return;
|
---|
448 | }
|
---|
449 | } else {
|
---|
450 | EastNorth newEN;
|
---|
451 | if (n != null) {
|
---|
452 | EastNorth foundPoint = n.getEastNorth();
|
---|
453 | // project found node to snapping line
|
---|
454 | newEN = snapHelper.getSnapPoint(foundPoint);
|
---|
455 | // do not add new node if there is some node within snapping distance
|
---|
456 | double tolerance = Main.map.mapView.getDist100Pixel() * toleranceMultiplier;
|
---|
457 | if (foundPoint.distance(newEN) > tolerance) {
|
---|
458 | n = new Node(newEN); // point != projected, so we create new node
|
---|
459 | newNode = true;
|
---|
460 | }
|
---|
461 | } else { // n==null, no node found in clicked area
|
---|
462 | EastNorth mouseEN = Main.map.mapView.getEastNorth(e.getX(), e.getY());
|
---|
463 | newEN = snapHelper.isSnapOn() ? snapHelper.getSnapPoint(mouseEN) : mouseEN;
|
---|
464 | n = new Node(newEN); //create node at clicked point
|
---|
465 | newNode = true;
|
---|
466 | }
|
---|
467 | snapHelper.unsetFixedMode();
|
---|
468 | }
|
---|
469 |
|
---|
470 | Collection<Command> cmds = new LinkedList<>();
|
---|
471 | Collection<OsmPrimitive> newSelection = new LinkedList<>(ds.getSelected());
|
---|
472 | List<Way> reuseWays = new ArrayList<>();
|
---|
473 | List<Way> replacedWays = new ArrayList<>();
|
---|
474 |
|
---|
475 | if (newNode) {
|
---|
476 | if (n.getCoor().isOutSideWorld()) {
|
---|
477 | JOptionPane.showMessageDialog(
|
---|
478 | Main.parent,
|
---|
479 | tr("Cannot add a node outside of the world."),
|
---|
480 | tr("Warning"),
|
---|
481 | JOptionPane.WARNING_MESSAGE
|
---|
482 | );
|
---|
483 | return;
|
---|
484 | }
|
---|
485 | cmds.add(new AddCommand(n));
|
---|
486 |
|
---|
487 | if (!ctrl) {
|
---|
488 | // Insert the node into all the nearby way segments
|
---|
489 | List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(
|
---|
490 | Main.map.mapView.getPoint(n), OsmPrimitive::isSelectable);
|
---|
491 | if (snapHelper.isActive()) {
|
---|
492 | tryToMoveNodeOnIntersection(wss, n);
|
---|
493 | }
|
---|
494 | insertNodeIntoAllNearbySegments(wss, n, newSelection, cmds, replacedWays, reuseWays);
|
---|
495 | }
|
---|
496 | }
|
---|
497 | // now "n" is newly created or reused node that shoud be added to some way
|
---|
498 |
|
---|
499 | // This part decides whether or not a "segment" (i.e. a connection) is made to an existing node.
|
---|
500 |
|
---|
501 | // For a connection to be made, the user must either have a node selected (connection
|
---|
502 | // is made to that node), or he must have a way selected *and* one of the endpoints
|
---|
503 | // of that way must be the last used node (connection is made to last used node), or
|
---|
504 | // he must have a way and a node selected (connection is made to the selected node).
|
---|
505 |
|
---|
506 | // If the above does not apply, the selection is cleared and a new try is started
|
---|
507 |
|
---|
508 | boolean extendedWay = false;
|
---|
509 | boolean wayIsFinishedTemp = wayIsFinished;
|
---|
510 | wayIsFinished = false;
|
---|
511 |
|
---|
512 | // don't draw lines if shift is held
|
---|
513 | if (!selection.isEmpty() && !shift) {
|
---|
514 | Node selectedNode = null;
|
---|
515 | Way selectedWay = null;
|
---|
516 |
|
---|
517 | for (OsmPrimitive p : selection) {
|
---|
518 | if (p instanceof Node) {
|
---|
519 | if (selectedNode != null) {
|
---|
520 | // Too many nodes selected to do something useful
|
---|
521 | tryAgain(e);
|
---|
522 | return;
|
---|
523 | }
|
---|
524 | selectedNode = (Node) p;
|
---|
525 | } else if (p instanceof Way) {
|
---|
526 | if (selectedWay != null) {
|
---|
527 | // Too many ways selected to do something useful
|
---|
528 | tryAgain(e);
|
---|
529 | return;
|
---|
530 | }
|
---|
531 | selectedWay = (Way) p;
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | // the node from which we make a connection
|
---|
536 | Node n0 = findNodeToContinueFrom(selectedNode, selectedWay);
|
---|
537 | // We have a selection but it isn't suitable. Try again.
|
---|
538 | if (n0 == null) {
|
---|
539 | tryAgain(e);
|
---|
540 | return;
|
---|
541 | }
|
---|
542 | if (!wayIsFinishedTemp) {
|
---|
543 | if (isSelfContainedWay(selectedWay, n0, n))
|
---|
544 | return;
|
---|
545 |
|
---|
546 | // User clicked last node again, finish way
|
---|
547 | if (n0 == n) {
|
---|
548 | finishDrawing();
|
---|
549 | return;
|
---|
550 | }
|
---|
551 |
|
---|
552 | // Ok we know now that we'll insert a line segment, but will it connect to an
|
---|
553 | // existing way or make a new way of its own? The "alt" modifier means that the
|
---|
554 | // user wants a new way.
|
---|
555 | Way way = alt ? null : (selectedWay != null ? selectedWay : getWayForNode(n0));
|
---|
556 | Way wayToSelect;
|
---|
557 |
|
---|
558 | // Don't allow creation of self-overlapping ways
|
---|
559 | if (way != null) {
|
---|
560 | int nodeCount = 0;
|
---|
561 | for (Node p : way.getNodes()) {
|
---|
562 | if (p.equals(n0)) {
|
---|
563 | nodeCount++;
|
---|
564 | }
|
---|
565 | }
|
---|
566 | if (nodeCount > 1) {
|
---|
567 | way = null;
|
---|
568 | }
|
---|
569 | }
|
---|
570 |
|
---|
571 | if (way == null) {
|
---|
572 | way = new Way();
|
---|
573 | way.addNode(n0);
|
---|
574 | cmds.add(new AddCommand(way));
|
---|
575 | wayToSelect = way;
|
---|
576 | } else {
|
---|
577 | int i;
|
---|
578 | if ((i = replacedWays.indexOf(way)) != -1) {
|
---|
579 | way = reuseWays.get(i);
|
---|
580 | wayToSelect = way;
|
---|
581 | } else {
|
---|
582 | wayToSelect = way;
|
---|
583 | Way wnew = new Way(way);
|
---|
584 | cmds.add(new ChangeCommand(way, wnew));
|
---|
585 | way = wnew;
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | // Connected to a node that's already in the way
|
---|
590 | if (way.containsNode(n)) {
|
---|
591 | wayIsFinished = true;
|
---|
592 | selection.clear();
|
---|
593 | }
|
---|
594 |
|
---|
595 | // Add new node to way
|
---|
596 | if (way.getNode(way.getNodesCount() - 1) == n0) {
|
---|
597 | way.addNode(n);
|
---|
598 | } else {
|
---|
599 | way.addNode(0, n);
|
---|
600 | }
|
---|
601 |
|
---|
602 | extendedWay = true;
|
---|
603 | newSelection.clear();
|
---|
604 | newSelection.add(wayToSelect);
|
---|
605 | }
|
---|
606 | }
|
---|
607 | if (!extendedWay && !newNode) {
|
---|
608 | return; // We didn't do anything.
|
---|
609 | }
|
---|
610 |
|
---|
611 | String title = getTitle(newNode, n, newSelection, reuseWays, extendedWay);
|
---|
612 |
|
---|
613 | Command c = new SequenceCommand(title, cmds);
|
---|
614 |
|
---|
615 | Main.main.undoRedo.add(c);
|
---|
616 | if (!wayIsFinished) {
|
---|
617 | lastUsedNode = n;
|
---|
618 | }
|
---|
619 |
|
---|
620 | ds.setSelected(newSelection);
|
---|
621 |
|
---|
622 | // "viewport following" mode for tracing long features
|
---|
623 | // from aerial imagery or GPS tracks.
|
---|
624 | if (Main.map.mapView.viewportFollowing) {
|
---|
625 | Main.map.mapView.smoothScrollTo(n.getEastNorth());
|
---|
626 | }
|
---|
627 | computeHelperLine();
|
---|
628 | removeHighlighting();
|
---|
629 | }
|
---|
630 |
|
---|
631 | private static String getTitle(boolean newNode, Node n, Collection<OsmPrimitive> newSelection, List<Way> reuseWays,
|
---|
632 | boolean extendedWay) {
|
---|
633 | String title;
|
---|
634 | if (!extendedWay) {
|
---|
635 | if (reuseWays.isEmpty()) {
|
---|
636 | title = tr("Add node");
|
---|
637 | } else {
|
---|
638 | title = tr("Add node into way");
|
---|
639 | for (Way w : reuseWays) {
|
---|
640 | newSelection.remove(w);
|
---|
641 | }
|
---|
642 | }
|
---|
643 | newSelection.clear();
|
---|
644 | newSelection.add(n);
|
---|
645 | } else if (!newNode) {
|
---|
646 | title = tr("Connect existing way to node");
|
---|
647 | } else if (reuseWays.isEmpty()) {
|
---|
648 | title = tr("Add a new node to an existing way");
|
---|
649 | } else {
|
---|
650 | title = tr("Add node into way and connect");
|
---|
651 | }
|
---|
652 | return title;
|
---|
653 | }
|
---|
654 |
|
---|
655 | private void insertNodeIntoAllNearbySegments(List<WaySegment> wss, Node n, Collection<OsmPrimitive> newSelection,
|
---|
656 | Collection<Command> cmds, List<Way> replacedWays, List<Way> reuseWays) {
|
---|
657 | Map<Way, List<Integer>> insertPoints = new HashMap<>();
|
---|
658 | for (WaySegment ws : wss) {
|
---|
659 | List<Integer> is;
|
---|
660 | if (insertPoints.containsKey(ws.way)) {
|
---|
661 | is = insertPoints.get(ws.way);
|
---|
662 | } else {
|
---|
663 | is = new ArrayList<>();
|
---|
664 | insertPoints.put(ws.way, is);
|
---|
665 | }
|
---|
666 |
|
---|
667 | is.add(ws.lowerIndex);
|
---|
668 | }
|
---|
669 |
|
---|
670 | Set<Pair<Node, Node>> segSet = new HashSet<>();
|
---|
671 |
|
---|
672 | for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
|
---|
673 | Way w = insertPoint.getKey();
|
---|
674 | List<Integer> is = insertPoint.getValue();
|
---|
675 |
|
---|
676 | Way wnew = new Way(w);
|
---|
677 |
|
---|
678 | pruneSuccsAndReverse(is);
|
---|
679 | for (int i : is) {
|
---|
680 | segSet.add(Pair.sort(new Pair<>(w.getNode(i), w.getNode(i+1))));
|
---|
681 | wnew.addNode(i + 1, n);
|
---|
682 | }
|
---|
683 |
|
---|
684 | // If ALT is pressed, a new way should be created and that new way should get
|
---|
685 | // selected. This works everytime unless the ways the nodes get inserted into
|
---|
686 | // are already selected. This is the case when creating a self-overlapping way
|
---|
687 | // but pressing ALT prevents this. Therefore we must de-select the way manually
|
---|
688 | // here so /only/ the new way will be selected after this method finishes.
|
---|
689 | if (alt) {
|
---|
690 | newSelection.add(insertPoint.getKey());
|
---|
691 | }
|
---|
692 |
|
---|
693 | cmds.add(new ChangeCommand(insertPoint.getKey(), wnew));
|
---|
694 | replacedWays.add(insertPoint.getKey());
|
---|
695 | reuseWays.add(wnew);
|
---|
696 | }
|
---|
697 |
|
---|
698 | adjustNode(segSet, n);
|
---|
699 | }
|
---|
700 |
|
---|
701 | /**
|
---|
702 | * Prevent creation of ways that look like this: <---->
|
---|
703 | * This happens if users want to draw a no-exit-sideway from the main way like this:
|
---|
704 | * ^
|
---|
705 | * |<---->
|
---|
706 | * |
|
---|
707 | * The solution isn't ideal because the main way will end in the side way, which is bad for
|
---|
708 | * navigation software ("drive straight on") but at least easier to fix. Maybe users will fix
|
---|
709 | * it on their own, too. At least it's better than producing an error.
|
---|
710 | *
|
---|
711 | * @param selectedWay the way to check
|
---|
712 | * @param currentNode the current node (i.e. the one the connection will be made from)
|
---|
713 | * @param targetNode the target node (i.e. the one the connection will be made to)
|
---|
714 | * @return {@code true} if this would create a selfcontaining way, {@code false} otherwise.
|
---|
715 | */
|
---|
716 | private boolean isSelfContainedWay(Way selectedWay, Node currentNode, Node targetNode) {
|
---|
717 | if (selectedWay != null) {
|
---|
718 | int posn0 = selectedWay.getNodes().indexOf(currentNode);
|
---|
719 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
720 | if ((posn0 != -1 && // n0 is part of way
|
---|
721 | (posn0 >= 1 && targetNode.equals(selectedWay.getNode(posn0-1)))) || // previous node
|
---|
722 | (posn0 < selectedWay.getNodesCount()-1 && targetNode.equals(selectedWay.getNode(posn0+1)))) { // next node
|
---|
723 | getLayerManager().getEditDataSet().setSelected(targetNode);
|
---|
724 | lastUsedNode = targetNode;
|
---|
725 | return true;
|
---|
726 | }
|
---|
727 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
728 | }
|
---|
729 |
|
---|
730 | return false;
|
---|
731 | }
|
---|
732 |
|
---|
733 | /**
|
---|
734 | * Finds a node to continue drawing from. Decision is based upon given node and way.
|
---|
735 | * @param selectedNode Currently selected node, may be null
|
---|
736 | * @param selectedWay Currently selected way, may be null
|
---|
737 | * @return Node if a suitable node is found, null otherwise
|
---|
738 | */
|
---|
739 | private Node findNodeToContinueFrom(Node selectedNode, Way selectedWay) {
|
---|
740 | // No nodes or ways have been selected, this occurs when a relation
|
---|
741 | // has been selected or the selection is empty
|
---|
742 | if (selectedNode == null && selectedWay == null)
|
---|
743 | return null;
|
---|
744 |
|
---|
745 | if (selectedNode == null) {
|
---|
746 | if (selectedWay.isFirstLastNode(lastUsedNode))
|
---|
747 | return lastUsedNode;
|
---|
748 |
|
---|
749 | // We have a way selected, but no suitable node to continue from. Start anew.
|
---|
750 | return null;
|
---|
751 | }
|
---|
752 |
|
---|
753 | if (selectedWay == null)
|
---|
754 | return selectedNode;
|
---|
755 |
|
---|
756 | if (selectedWay.isFirstLastNode(selectedNode))
|
---|
757 | return selectedNode;
|
---|
758 |
|
---|
759 | // We have a way and node selected, but it's not at the start/end of the way. Start anew.
|
---|
760 | return null;
|
---|
761 | }
|
---|
762 |
|
---|
763 | @Override
|
---|
764 | public void mouseDragged(MouseEvent e) {
|
---|
765 | mouseMoved(e);
|
---|
766 | }
|
---|
767 |
|
---|
768 | @Override
|
---|
769 | public void mouseMoved(MouseEvent e) {
|
---|
770 | if (!Main.map.mapView.isActiveLayerDrawable())
|
---|
771 | return;
|
---|
772 |
|
---|
773 | // we copy ctrl/alt/shift from the event just in case our global
|
---|
774 | // keyDetector didn't make it through the security manager. Unclear
|
---|
775 | // if that can ever happen but better be safe.
|
---|
776 | updateKeyModifiers(e);
|
---|
777 | mousePos = e.getPoint();
|
---|
778 | if (snapHelper.isSnapOn() && ctrl)
|
---|
779 | tryToSetBaseSegmentForAngleSnap();
|
---|
780 |
|
---|
781 | computeHelperLine();
|
---|
782 | addHighlighting();
|
---|
783 | }
|
---|
784 |
|
---|
785 | /**
|
---|
786 | * This method is used to detect segment under mouse and use it as reference for angle snapping
|
---|
787 | */
|
---|
788 | private void tryToSetBaseSegmentForAngleSnap() {
|
---|
789 | WaySegment seg = Main.map.mapView.getNearestWaySegment(mousePos, OsmPrimitive::isSelectable);
|
---|
790 | if (seg != null) {
|
---|
791 | snapHelper.setBaseSegment(seg);
|
---|
792 | }
|
---|
793 | }
|
---|
794 |
|
---|
795 | /**
|
---|
796 | * This method prepares data required for painting the "helper line" from
|
---|
797 | * the last used position to the mouse cursor. It duplicates some code from
|
---|
798 | * mouseReleased() (FIXME).
|
---|
799 | */
|
---|
800 | private void computeHelperLine() {
|
---|
801 | if (mousePos == null) {
|
---|
802 | // Don't draw the line.
|
---|
803 | currentMouseEastNorth = null;
|
---|
804 | currentBaseNode = null;
|
---|
805 | return;
|
---|
806 | }
|
---|
807 |
|
---|
808 | Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
|
---|
809 |
|
---|
810 | MapView mv = Main.map.mapView;
|
---|
811 | Node currentMouseNode = null;
|
---|
812 | mouseOnExistingNode = null;
|
---|
813 | mouseOnExistingWays = new HashSet<>();
|
---|
814 |
|
---|
815 | showStatusInfo(-1, -1, -1, snapHelper.isSnapOn());
|
---|
816 |
|
---|
817 | if (!ctrl && mousePos != null) {
|
---|
818 | currentMouseNode = mv.getNearestNode(mousePos, OsmPrimitive::isSelectable);
|
---|
819 | }
|
---|
820 |
|
---|
821 | // We need this for highlighting and we'll only do so if we actually want to re-use
|
---|
822 | // *and* there is no node nearby (because nodes beat ways when re-using)
|
---|
823 | if (!ctrl && currentMouseNode == null) {
|
---|
824 | List<WaySegment> wss = mv.getNearestWaySegments(mousePos, OsmPrimitive::isSelectable);
|
---|
825 | for (WaySegment ws : wss) {
|
---|
826 | mouseOnExistingWays.add(ws.way);
|
---|
827 | }
|
---|
828 | }
|
---|
829 |
|
---|
830 | if (currentMouseNode != null) {
|
---|
831 | // user clicked on node
|
---|
832 | if (selection.isEmpty()) return;
|
---|
833 | currentMouseEastNorth = currentMouseNode.getEastNorth();
|
---|
834 | mouseOnExistingNode = currentMouseNode;
|
---|
835 | } else {
|
---|
836 | // no node found in clicked area
|
---|
837 | currentMouseEastNorth = mv.getEastNorth(mousePos.x, mousePos.y);
|
---|
838 | }
|
---|
839 |
|
---|
840 | determineCurrentBaseNodeAndPreviousNode(selection);
|
---|
841 | if (previousNode == null) {
|
---|
842 | snapHelper.noSnapNow();
|
---|
843 | }
|
---|
844 |
|
---|
845 | if (getCurrentBaseNode() == null || getCurrentBaseNode() == currentMouseNode)
|
---|
846 | return; // Don't create zero length way segments.
|
---|
847 |
|
---|
848 |
|
---|
849 | double curHdg = Math.toDegrees(getCurrentBaseNode().getEastNorth()
|
---|
850 | .heading(currentMouseEastNorth));
|
---|
851 | double baseHdg = -1;
|
---|
852 | if (previousNode != null) {
|
---|
853 | EastNorth en = previousNode.getEastNorth();
|
---|
854 | if (en != null) {
|
---|
855 | baseHdg = Math.toDegrees(en.heading(getCurrentBaseNode().getEastNorth()));
|
---|
856 | }
|
---|
857 | }
|
---|
858 |
|
---|
859 | snapHelper.checkAngleSnapping(currentMouseEastNorth, baseHdg, curHdg);
|
---|
860 |
|
---|
861 | // status bar was filled by snapHelper
|
---|
862 | }
|
---|
863 |
|
---|
864 | static void showStatusInfo(double angle, double hdg, double distance, boolean activeFlag) {
|
---|
865 | Main.map.statusLine.setAngle(angle);
|
---|
866 | Main.map.statusLine.activateAnglePanel(activeFlag);
|
---|
867 | Main.map.statusLine.setHeading(hdg);
|
---|
868 | Main.map.statusLine.setDist(distance);
|
---|
869 | }
|
---|
870 |
|
---|
871 | /**
|
---|
872 | * Helper function that sets fields currentBaseNode and previousNode
|
---|
873 | * @param selection
|
---|
874 | * uses also lastUsedNode field
|
---|
875 | */
|
---|
876 | private void determineCurrentBaseNodeAndPreviousNode(Collection<OsmPrimitive> selection) {
|
---|
877 | Node selectedNode = null;
|
---|
878 | Way selectedWay = null;
|
---|
879 | for (OsmPrimitive p : selection) {
|
---|
880 | if (p instanceof Node) {
|
---|
881 | if (selectedNode != null)
|
---|
882 | return;
|
---|
883 | selectedNode = (Node) p;
|
---|
884 | } else if (p instanceof Way) {
|
---|
885 | if (selectedWay != null)
|
---|
886 | return;
|
---|
887 | selectedWay = (Way) p;
|
---|
888 | }
|
---|
889 | }
|
---|
890 | // we are here, if not more than 1 way or node is selected,
|
---|
891 |
|
---|
892 | // the node from which we make a connection
|
---|
893 | currentBaseNode = null;
|
---|
894 | previousNode = null;
|
---|
895 |
|
---|
896 | // Try to find an open way to measure angle from it. The way is not to be continued!
|
---|
897 | // warning: may result in changes of currentBaseNode and previousNode
|
---|
898 | // please remove if bugs arise
|
---|
899 | if (selectedWay == null && selectedNode != null) {
|
---|
900 | for (OsmPrimitive p: selectedNode.getReferrers()) {
|
---|
901 | if (p.isUsable() && p instanceof Way && ((Way) p).isFirstLastNode(selectedNode)) {
|
---|
902 | if (selectedWay != null) { // two uncontinued ways, nothing to take as reference
|
---|
903 | selectedWay = null;
|
---|
904 | break;
|
---|
905 | } else {
|
---|
906 | // set us ~continue this way (measure angle from it)
|
---|
907 | selectedWay = (Way) p;
|
---|
908 | }
|
---|
909 | }
|
---|
910 | }
|
---|
911 | }
|
---|
912 |
|
---|
913 | if (selectedNode == null) {
|
---|
914 | if (selectedWay == null)
|
---|
915 | return;
|
---|
916 | continueWayFromNode(selectedWay, lastUsedNode);
|
---|
917 | } else if (selectedWay == null) {
|
---|
918 | currentBaseNode = selectedNode;
|
---|
919 | } else if (!selectedWay.isDeleted()) { // fix #7118
|
---|
920 | continueWayFromNode(selectedWay, selectedNode);
|
---|
921 | }
|
---|
922 | }
|
---|
923 |
|
---|
924 | /**
|
---|
925 | * if one of the ends of {@code way} is given {@code node},
|
---|
926 | * then set currentBaseNode = node and previousNode = adjacent node of way
|
---|
927 | * @param way way to continue
|
---|
928 | * @param node starting node
|
---|
929 | */
|
---|
930 | private void continueWayFromNode(Way way, Node node) {
|
---|
931 | int n = way.getNodesCount();
|
---|
932 | if (node == way.firstNode()) {
|
---|
933 | currentBaseNode = node;
|
---|
934 | if (n > 1) previousNode = way.getNode(1);
|
---|
935 | } else if (node == way.lastNode()) {
|
---|
936 | currentBaseNode = node;
|
---|
937 | if (n > 1) previousNode = way.getNode(n-2);
|
---|
938 | }
|
---|
939 | }
|
---|
940 |
|
---|
941 | /**
|
---|
942 | * Repaint on mouse exit so that the helper line goes away.
|
---|
943 | */
|
---|
944 | @Override
|
---|
945 | public void mouseExited(MouseEvent e) {
|
---|
946 | OsmDataLayer editLayer = Main.getLayerManager().getEditLayer();
|
---|
947 | if (editLayer == null)
|
---|
948 | return;
|
---|
949 | mousePos = e.getPoint();
|
---|
950 | snapHelper.noSnapNow();
|
---|
951 | boolean repaintIssued = removeHighlighting();
|
---|
952 | // force repaint in case snapHelper needs one. If removeHighlighting
|
---|
953 | // caused one already, don't do it again.
|
---|
954 | if (!repaintIssued) {
|
---|
955 | editLayer.invalidate();
|
---|
956 | }
|
---|
957 | }
|
---|
958 |
|
---|
959 | /**
|
---|
960 | * @param n node
|
---|
961 | * @return If the node is the end of exactly one way, return this.
|
---|
962 | * <code>null</code> otherwise.
|
---|
963 | */
|
---|
964 | public static Way getWayForNode(Node n) {
|
---|
965 | Way way = null;
|
---|
966 | for (Way w : Utils.filteredCollection(n.getReferrers(), Way.class)) {
|
---|
967 | if (!w.isUsable() || w.getNodesCount() < 1) {
|
---|
968 | continue;
|
---|
969 | }
|
---|
970 | Node firstNode = w.getNode(0);
|
---|
971 | Node lastNode = w.getNode(w.getNodesCount() - 1);
|
---|
972 | if ((firstNode == n || lastNode == n) && (firstNode != lastNode)) {
|
---|
973 | if (way != null)
|
---|
974 | return null;
|
---|
975 | way = w;
|
---|
976 | }
|
---|
977 | }
|
---|
978 | return way;
|
---|
979 | }
|
---|
980 |
|
---|
981 | /**
|
---|
982 | * Replies the current base node, after having checked it is still usable (see #11105).
|
---|
983 | * @return the current base node (can be null). If not-null, it's guaranteed the node is usable
|
---|
984 | */
|
---|
985 | public Node getCurrentBaseNode() {
|
---|
986 | if (currentBaseNode != null && (currentBaseNode.getDataSet() == null || !currentBaseNode.isUsable())) {
|
---|
987 | currentBaseNode = null;
|
---|
988 | }
|
---|
989 | return currentBaseNode;
|
---|
990 | }
|
---|
991 |
|
---|
992 | private static void pruneSuccsAndReverse(List<Integer> is) {
|
---|
993 | Set<Integer> is2 = new HashSet<>();
|
---|
994 | for (int i : is) {
|
---|
995 | if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
|
---|
996 | is2.add(i);
|
---|
997 | }
|
---|
998 | }
|
---|
999 | is.clear();
|
---|
1000 | is.addAll(is2);
|
---|
1001 | Collections.sort(is);
|
---|
1002 | Collections.reverse(is);
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | /**
|
---|
1006 | * Adjusts the position of a node to lie on a segment (or a segment intersection).
|
---|
1007 | *
|
---|
1008 | * If one or more than two segments are passed, the node is adjusted
|
---|
1009 | * to lie on the first segment that is passed.
|
---|
1010 | *
|
---|
1011 | * If two segments are passed, the node is adjusted to be at their intersection.
|
---|
1012 | *
|
---|
1013 | * No action is taken if no segments are passed.
|
---|
1014 | *
|
---|
1015 | * @param segs the segments to use as a reference when adjusting
|
---|
1016 | * @param n the node to adjust
|
---|
1017 | */
|
---|
1018 | private static void adjustNode(Collection<Pair<Node, Node>> segs, Node n) {
|
---|
1019 | switch (segs.size()) {
|
---|
1020 | case 0:
|
---|
1021 | return;
|
---|
1022 | case 2:
|
---|
1023 | adjustNodeTwoSegments(segs, n);
|
---|
1024 | break;
|
---|
1025 | default:
|
---|
1026 | adjustNodeDefault(segs, n);
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | private static void adjustNodeTwoSegments(Collection<Pair<Node, Node>> segs, Node n) {
|
---|
1031 | // This computes the intersection between the two segments and adjusts the node position.
|
---|
1032 | Iterator<Pair<Node, Node>> i = segs.iterator();
|
---|
1033 | Pair<Node, Node> seg = i.next();
|
---|
1034 | EastNorth pA = seg.a.getEastNorth();
|
---|
1035 | EastNorth pB = seg.b.getEastNorth();
|
---|
1036 | seg = i.next();
|
---|
1037 | EastNorth pC = seg.a.getEastNorth();
|
---|
1038 | EastNorth pD = seg.b.getEastNorth();
|
---|
1039 |
|
---|
1040 | double u = det(pB.east() - pA.east(), pB.north() - pA.north(), pC.east() - pD.east(), pC.north() - pD.north());
|
---|
1041 |
|
---|
1042 | // Check for parallel segments and do nothing if they are
|
---|
1043 | // In practice this will probably only happen when a way has been duplicated
|
---|
1044 |
|
---|
1045 | if (u == 0)
|
---|
1046 | return;
|
---|
1047 |
|
---|
1048 | // q is a number between 0 and 1
|
---|
1049 | // It is the point in the segment where the intersection occurs
|
---|
1050 | // if the segment is scaled to length 1
|
---|
1051 |
|
---|
1052 | double q = det(pB.north() - pC.north(), pB.east() - pC.east(), pD.north() - pC.north(), pD.east() - pC.east()) / u;
|
---|
1053 | EastNorth intersection = new EastNorth(
|
---|
1054 | pB.east() + q * (pA.east() - pB.east()),
|
---|
1055 | pB.north() + q * (pA.north() - pB.north()));
|
---|
1056 |
|
---|
1057 |
|
---|
1058 | // only adjust to intersection if within snapToIntersectionThreshold pixel of mouse click; otherwise
|
---|
1059 | // fall through to default action.
|
---|
1060 | // (for semi-parallel lines, intersection might be miles away!)
|
---|
1061 | if (Main.map.mapView.getPoint2D(n).distance(Main.map.mapView.getPoint2D(intersection)) < SNAP_TO_INTERSECTION_THRESHOLD.get()) {
|
---|
1062 | n.setEastNorth(intersection);
|
---|
1063 | return;
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | adjustNodeDefault(segs, n);
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | private static void adjustNodeDefault(Collection<Pair<Node, Node>> segs, Node n) {
|
---|
1070 | EastNorth p = n.getEastNorth();
|
---|
1071 | Pair<Node, Node> seg = segs.iterator().next();
|
---|
1072 | EastNorth pA = seg.a.getEastNorth();
|
---|
1073 | EastNorth pB = seg.b.getEastNorth();
|
---|
1074 | double a = p.distanceSq(pB);
|
---|
1075 | double b = p.distanceSq(pA);
|
---|
1076 | double c = pA.distanceSq(pB);
|
---|
1077 | double q = (a - b + c) / (2*c);
|
---|
1078 | n.setEastNorth(new EastNorth(pB.east() + q * (pA.east() - pB.east()), pB.north() + q * (pA.north() - pB.north())));
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | // helper for adjustNode
|
---|
1082 | static double det(double a, double b, double c, double d) {
|
---|
1083 | return a * d - b * c;
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | private void tryToMoveNodeOnIntersection(List<WaySegment> wss, Node n) {
|
---|
1087 | if (wss.isEmpty())
|
---|
1088 | return;
|
---|
1089 | WaySegment ws = wss.get(0);
|
---|
1090 | EastNorth p1 = ws.getFirstNode().getEastNorth();
|
---|
1091 | EastNorth p2 = ws.getSecondNode().getEastNorth();
|
---|
1092 | if (snapHelper.dir2 != null && getCurrentBaseNode() != null) {
|
---|
1093 | EastNorth xPoint = Geometry.getSegmentSegmentIntersection(p1, p2, snapHelper.dir2,
|
---|
1094 | getCurrentBaseNode().getEastNorth());
|
---|
1095 | if (xPoint != null) {
|
---|
1096 | n.setEastNorth(xPoint);
|
---|
1097 | }
|
---|
1098 | }
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Takes the data from computeHelperLine to determine which ways/nodes should be highlighted
|
---|
1103 | * (if feature enabled). Also sets the target cursor if appropriate. It adds the to-be-
|
---|
1104 | * highlighted primitives to newHighlights but does not actually highlight them. This work is
|
---|
1105 | * done in redrawIfRequired. This means, calling addHighlighting() without redrawIfRequired()
|
---|
1106 | * will leave the data in an inconsistent state.
|
---|
1107 | *
|
---|
1108 | * The status bar derives its information from oldHighlights, so in order to update the status
|
---|
1109 | * bar both addHighlighting() and repaintIfRequired() are needed, since former fills newHighlights
|
---|
1110 | * and latter processes them into oldHighlights.
|
---|
1111 | */
|
---|
1112 | private void addHighlighting() {
|
---|
1113 | newHighlights = new HashSet<>();
|
---|
1114 |
|
---|
1115 | // if ctrl key is held ("no join"), don't highlight anything
|
---|
1116 | if (ctrl) {
|
---|
1117 | Main.map.mapView.setNewCursor(cursor, this);
|
---|
1118 | redrawIfRequired();
|
---|
1119 | return;
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 | // This happens when nothing is selected, but we still want to highlight the "target node"
|
---|
1123 | if (mouseOnExistingNode == null && mousePos != null && getLayerManager().getEditDataSet().selectionEmpty()) {
|
---|
1124 | mouseOnExistingNode = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive::isSelectable);
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | if (mouseOnExistingNode != null) {
|
---|
1128 | Main.map.mapView.setNewCursor(cursorJoinNode, this);
|
---|
1129 | newHighlights.add(mouseOnExistingNode);
|
---|
1130 | redrawIfRequired();
|
---|
1131 | return;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | // Insert the node into all the nearby way segments
|
---|
1135 | if (mouseOnExistingWays.isEmpty()) {
|
---|
1136 | Main.map.mapView.setNewCursor(cursor, this);
|
---|
1137 | redrawIfRequired();
|
---|
1138 | return;
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | Main.map.mapView.setNewCursor(cursorJoinWay, this);
|
---|
1142 | newHighlights.addAll(mouseOnExistingWays);
|
---|
1143 | redrawIfRequired();
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | /**
|
---|
1147 | * Removes target highlighting from primitives. Issues repaint if required.
|
---|
1148 | * @return true if a repaint has been issued.
|
---|
1149 | */
|
---|
1150 | private boolean removeHighlighting() {
|
---|
1151 | newHighlights = new HashSet<>();
|
---|
1152 | return redrawIfRequired();
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | @Override
|
---|
1156 | public void paint(Graphics2D g, MapView mv, Bounds box) {
|
---|
1157 | // sanity checks
|
---|
1158 | if (Main.map.mapView == null || mousePos == null
|
---|
1159 | // don't draw line if we don't know where from or where to
|
---|
1160 | || currentMouseEastNorth == null || getCurrentBaseNode() == null
|
---|
1161 | // don't draw line if mouse is outside window
|
---|
1162 | || !Main.map.mapView.getBounds().contains(mousePos))
|
---|
1163 | return;
|
---|
1164 |
|
---|
1165 | Graphics2D g2 = g;
|
---|
1166 | snapHelper.drawIfNeeded(g2, mv.getState());
|
---|
1167 | if (!DRAW_HELPER_LINE.get() || wayIsFinished || shift)
|
---|
1168 | return;
|
---|
1169 |
|
---|
1170 | if (!snapHelper.isActive()) {
|
---|
1171 | g2.setColor(RUBBER_LINE_COLOR.get());
|
---|
1172 | g2.setStroke(RUBBER_LINE_STROKE.get());
|
---|
1173 | paintConstructionGeometry(mv, g2);
|
---|
1174 | } else if (DRAW_CONSTRUCTION_GEOMETRY.get()) {
|
---|
1175 | // else use color and stoke from snapHelper.draw
|
---|
1176 | paintConstructionGeometry(mv, g2);
|
---|
1177 | }
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | private void paintConstructionGeometry(MapView mv, Graphics2D g2) {
|
---|
1181 | MapPath2D b = new MapPath2D();
|
---|
1182 | MapViewPoint p1 = mv.getState().getPointFor(getCurrentBaseNode());
|
---|
1183 | MapViewPoint p2 = mv.getState().getPointFor(currentMouseEastNorth);
|
---|
1184 |
|
---|
1185 | b.moveTo(p1);
|
---|
1186 | b.lineTo(p2);
|
---|
1187 |
|
---|
1188 | // if alt key is held ("start new way"), draw a little perpendicular line
|
---|
1189 | if (alt) {
|
---|
1190 | START_WAY_INDICATOR.paintArrowAt(b, p1, p2);
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | g2.draw(b);
|
---|
1194 | g2.setStroke(BASIC_STROKE);
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | @Override
|
---|
1198 | public String getModeHelpText() {
|
---|
1199 | StringBuilder rv;
|
---|
1200 | /*
|
---|
1201 | * No modifiers: all (Connect, Node Re-Use, Auto-Weld)
|
---|
1202 | * CTRL: disables node re-use, auto-weld
|
---|
1203 | * Shift: do not make connection
|
---|
1204 | * ALT: make connection but start new way in doing so
|
---|
1205 | */
|
---|
1206 |
|
---|
1207 | /*
|
---|
1208 | * Status line text generation is split into two parts to keep it maintainable.
|
---|
1209 | * First part looks at what will happen to the new node inserted on click and
|
---|
1210 | * the second part will look if a connection is made or not.
|
---|
1211 | *
|
---|
1212 | * Note that this help text is not absolutely accurate as it doesn't catch any special
|
---|
1213 | * cases (e.g. when preventing <---> ways). The only special that it catches is when
|
---|
1214 | * a way is about to be finished.
|
---|
1215 | *
|
---|
1216 | * First check what happens to the new node.
|
---|
1217 | */
|
---|
1218 |
|
---|
1219 | // oldHighlights stores the current highlights. If this
|
---|
1220 | // list is empty we can assume that we won't do any joins
|
---|
1221 | if (ctrl || oldHighlights.isEmpty()) {
|
---|
1222 | rv = new StringBuilder(tr("Create new node."));
|
---|
1223 | } else {
|
---|
1224 | // oldHighlights may store a node or way, check if it's a node
|
---|
1225 | OsmPrimitive x = oldHighlights.iterator().next();
|
---|
1226 | if (x instanceof Node) {
|
---|
1227 | rv = new StringBuilder(tr("Select node under cursor."));
|
---|
1228 | } else {
|
---|
1229 | rv = new StringBuilder(trn("Insert new node into way.", "Insert new node into {0} ways.",
|
---|
1230 | oldHighlights.size(), oldHighlights.size()));
|
---|
1231 | }
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /*
|
---|
1235 | * Check whether a connection will be made
|
---|
1236 | */
|
---|
1237 | if (!wayIsFinished && getCurrentBaseNode() != null) {
|
---|
1238 | if (alt) {
|
---|
1239 | rv.append(' ').append(tr("Start new way from last node."));
|
---|
1240 | } else {
|
---|
1241 | rv.append(' ').append(tr("Continue way from last node."));
|
---|
1242 | }
|
---|
1243 | if (snapHelper.isSnapOn()) {
|
---|
1244 | rv.append(' ').append(tr("Angle snapping active."));
|
---|
1245 | }
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | Node n = mouseOnExistingNode;
|
---|
1249 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
1250 | /*
|
---|
1251 | * Handle special case: Highlighted node == selected node => finish drawing
|
---|
1252 | */
|
---|
1253 | if (n != null && ds != null && ds.getSelectedNodes().contains(n)) {
|
---|
1254 | if (wayIsFinished) {
|
---|
1255 | rv = new StringBuilder(tr("Select node under cursor."));
|
---|
1256 | } else {
|
---|
1257 | rv = new StringBuilder(tr("Finish drawing."));
|
---|
1258 | }
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | /*
|
---|
1262 | * Handle special case: Self-Overlapping or closing way
|
---|
1263 | */
|
---|
1264 | if (ds != null && !ds.getSelectedWays().isEmpty() && !wayIsFinished && !alt) {
|
---|
1265 | Way w = ds.getSelectedWays().iterator().next();
|
---|
1266 | for (Node m : w.getNodes()) {
|
---|
1267 | if (m.equals(mouseOnExistingNode) || mouseOnExistingWays.contains(w)) {
|
---|
1268 | rv.append(' ').append(tr("Finish drawing."));
|
---|
1269 | break;
|
---|
1270 | }
|
---|
1271 | }
|
---|
1272 | }
|
---|
1273 | return rv.toString();
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | /**
|
---|
1277 | * Get selected primitives, while draw action is in progress.
|
---|
1278 | *
|
---|
1279 | * While drawing a way, technically the last node is selected.
|
---|
1280 | * This is inconvenient when the user tries to add/edit tags to the way.
|
---|
1281 | * For this case, this method returns the current way as selection,
|
---|
1282 | * to work around this issue.
|
---|
1283 | * Otherwise the normal selection of the current data layer is returned.
|
---|
1284 | * @return selected primitives, while draw action is in progress
|
---|
1285 | */
|
---|
1286 | public Collection<OsmPrimitive> getInProgressSelection() {
|
---|
1287 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
1288 | if (ds == null) return null;
|
---|
1289 | if (getCurrentBaseNode() != null && !ds.selectionEmpty()) {
|
---|
1290 | Way continueFrom = getWayForNode(getCurrentBaseNode());
|
---|
1291 | if (continueFrom != null)
|
---|
1292 | return Collections.<OsmPrimitive>singleton(continueFrom);
|
---|
1293 | }
|
---|
1294 | return ds.getSelected();
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | @Override
|
---|
1298 | public boolean layerIsSupported(Layer l) {
|
---|
1299 | return l instanceof OsmDataLayer;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | @Override
|
---|
1303 | protected void updateEnabledState() {
|
---|
1304 | setEnabled(getLayerManager().getEditLayer() != null);
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | @Override
|
---|
1308 | public void destroy() {
|
---|
1309 | super.destroy();
|
---|
1310 | snapChangeAction.destroy();
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | public class BackSpaceAction extends AbstractAction {
|
---|
1314 |
|
---|
1315 | @Override
|
---|
1316 | public void actionPerformed(ActionEvent e) {
|
---|
1317 | Main.main.undoRedo.undo();
|
---|
1318 | Command lastCmd = Main.main.undoRedo.commands.peekLast();
|
---|
1319 | if (lastCmd == null) return;
|
---|
1320 | Node n = null;
|
---|
1321 | for (OsmPrimitive p: lastCmd.getParticipatingPrimitives()) {
|
---|
1322 | if (p instanceof Node) {
|
---|
1323 | if (n == null) {
|
---|
1324 | n = (Node) p; // found one node
|
---|
1325 | wayIsFinished = false;
|
---|
1326 | } else {
|
---|
1327 | // if more than 1 node were affected by previous command,
|
---|
1328 | // we have no way to continue, so we forget about found node
|
---|
1329 | n = null;
|
---|
1330 | break;
|
---|
1331 | }
|
---|
1332 | }
|
---|
1333 | }
|
---|
1334 | // select last added node - maybe we will continue drawing from it
|
---|
1335 | if (n != null) {
|
---|
1336 | getLayerManager().getEditDataSet().addSelected(n);
|
---|
1337 | }
|
---|
1338 | }
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | private class SnapChangeAction extends JosmAction {
|
---|
1342 | /**
|
---|
1343 | * Constructs a new {@code SnapChangeAction}.
|
---|
1344 | */
|
---|
1345 | SnapChangeAction() {
|
---|
1346 | super(tr("Angle snapping"), /* ICON() */ "anglesnap",
|
---|
1347 | tr("Switch angle snapping mode while drawing"), null, false);
|
---|
1348 | putValue("help", ht("/Action/Draw/AngleSnap"));
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | @Override
|
---|
1352 | public void actionPerformed(ActionEvent e) {
|
---|
1353 | if (snapHelper != null) {
|
---|
1354 | snapHelper.toggleSnapping();
|
---|
1355 | }
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | @Override
|
---|
1359 | protected void updateEnabledState() {
|
---|
1360 | setEnabled(Main.map != null && Main.map.mapMode instanceof DrawAction);
|
---|
1361 | }
|
---|
1362 | }
|
---|
1363 | }
|
---|