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