1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.AWTEvent;
|
---|
8 | import java.awt.BasicStroke;
|
---|
9 | import java.awt.Color;
|
---|
10 | import java.awt.Cursor;
|
---|
11 | import java.awt.EventQueue;
|
---|
12 | import java.awt.Graphics2D;
|
---|
13 | import java.awt.Point;
|
---|
14 | import java.awt.Toolkit;
|
---|
15 | import java.awt.event.AWTEventListener;
|
---|
16 | import java.awt.event.ActionEvent;
|
---|
17 | import java.awt.event.InputEvent;
|
---|
18 | import java.awt.event.KeyEvent;
|
---|
19 | import java.awt.event.MouseEvent;
|
---|
20 | import java.awt.geom.GeneralPath;
|
---|
21 | import java.util.ArrayList;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.Collections;
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.HashSet;
|
---|
26 | import java.util.Iterator;
|
---|
27 | import java.util.LinkedList;
|
---|
28 | import java.util.List;
|
---|
29 | import java.util.Map;
|
---|
30 | import java.util.Set;
|
---|
31 |
|
---|
32 | import javax.swing.JOptionPane;
|
---|
33 |
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
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.coor.LatLon;
|
---|
43 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
44 | import org.openstreetmap.josm.data.osm.Node;
|
---|
45 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
46 | import org.openstreetmap.josm.data.osm.Way;
|
---|
47 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
48 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
49 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
50 | import org.openstreetmap.josm.gui.MapView;
|
---|
51 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
52 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
---|
53 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
54 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
55 | import org.openstreetmap.josm.tools.Pair;
|
---|
56 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | *
|
---|
60 | */
|
---|
61 | public class DrawAction extends MapMode implements MapViewPaintable, SelectionChangedListener, AWTEventListener {
|
---|
62 | //static private final Logger logger = Logger.getLogger(DrawAction.class.getName());
|
---|
63 |
|
---|
64 | final private Cursor cursorJoinNode;
|
---|
65 | final private Cursor cursorJoinWay;
|
---|
66 |
|
---|
67 | private Node lastUsedNode = null;
|
---|
68 | private double PHI=Math.toRadians(90);
|
---|
69 |
|
---|
70 | private boolean ctrl;
|
---|
71 | private boolean alt;
|
---|
72 | private boolean shift;
|
---|
73 | private Node mouseOnExistingNode;
|
---|
74 | private Set<Way> mouseOnExistingWays = new HashSet<Way>();
|
---|
75 | private Set<OsmPrimitive> oldHighlights = new HashSet<OsmPrimitive>();
|
---|
76 | private boolean drawHelperLine;
|
---|
77 | private boolean wayIsFinished = false;
|
---|
78 | private boolean drawTargetHighlight;
|
---|
79 | private Point mousePos;
|
---|
80 | private Point oldMousePos;
|
---|
81 | private Color selectedColor;
|
---|
82 |
|
---|
83 | private Node currentBaseNode;
|
---|
84 | private EastNorth currentMouseEastNorth;
|
---|
85 |
|
---|
86 | private Shortcut extraShortcut;
|
---|
87 |
|
---|
88 | public DrawAction(MapFrame mapFrame) {
|
---|
89 | super(tr("Draw"), "node/autonode", tr("Draw nodes"),
|
---|
90 | Shortcut.registerShortcut("mapmode:draw", tr("Mode: {0}", tr("Draw")), KeyEvent.VK_A, Shortcut.GROUP_EDIT),
|
---|
91 | mapFrame, ImageProvider.getCursor("crosshair", null));
|
---|
92 |
|
---|
93 | // Add extra shortcut N
|
---|
94 | extraShortcut = Shortcut.registerShortcut("mapmode:drawfocus", tr("Mode: Draw Focus"), KeyEvent.VK_N, Shortcut.GROUP_EDIT);
|
---|
95 | Main.registerActionShortcut(this, extraShortcut);
|
---|
96 |
|
---|
97 | cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode");
|
---|
98 | cursorJoinWay = ImageProvider.getCursor("crosshair", "joinway");
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Checks if a map redraw is required and does so if needed. Also updates the status bar
|
---|
103 | */
|
---|
104 | private void redrawIfRequired() {
|
---|
105 | updateStatusLine();
|
---|
106 | if ((!drawHelperLine || wayIsFinished) && !drawTargetHighlight) return;
|
---|
107 | Main.map.mapView.repaint();
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Takes the data from computeHelperLine to determine which ways/nodes should be highlighted
|
---|
112 | * (if feature enabled). Also sets the target cursor if appropriate.
|
---|
113 | */
|
---|
114 | private void addHighlighting() {
|
---|
115 | removeHighlighting();
|
---|
116 | // if ctrl key is held ("no join"), don't highlight anything
|
---|
117 | if (ctrl) {
|
---|
118 | Main.map.mapView.setNewCursor(cursor, this);
|
---|
119 | return;
|
---|
120 | }
|
---|
121 |
|
---|
122 | // This happens when nothing is selected, but we still want to highlight the "target node"
|
---|
123 | if (mouseOnExistingNode == null && getCurrentDataSet().getSelected().size() == 0
|
---|
124 | && mousePos != null) {
|
---|
125 | mouseOnExistingNode = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (mouseOnExistingNode != null) {
|
---|
129 | Main.map.mapView.setNewCursor(cursorJoinNode, this);
|
---|
130 | // We also need this list for the statusbar help text
|
---|
131 | oldHighlights.add(mouseOnExistingNode);
|
---|
132 | if(drawTargetHighlight) {
|
---|
133 | mouseOnExistingNode.setHighlighted(true);
|
---|
134 | }
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | // Insert the node into all the nearby way segments
|
---|
139 | if (mouseOnExistingWays.size() == 0) {
|
---|
140 | Main.map.mapView.setNewCursor(cursor, this);
|
---|
141 | return;
|
---|
142 | }
|
---|
143 |
|
---|
144 | Main.map.mapView.setNewCursor(cursorJoinWay, this);
|
---|
145 |
|
---|
146 | // We also need this list for the statusbar help text
|
---|
147 | oldHighlights.addAll(mouseOnExistingWays);
|
---|
148 | if (!drawTargetHighlight) return;
|
---|
149 | for (Way w : mouseOnExistingWays) {
|
---|
150 | w.setHighlighted(true);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Removes target highlighting from primitives
|
---|
156 | */
|
---|
157 | private void removeHighlighting() {
|
---|
158 | for(OsmPrimitive prim : oldHighlights) {
|
---|
159 | prim.setHighlighted(false);
|
---|
160 | }
|
---|
161 | oldHighlights = new HashSet<OsmPrimitive>();
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override public void enterMode() {
|
---|
165 | if (!isEnabled())
|
---|
166 | return;
|
---|
167 | super.enterMode();
|
---|
168 | selectedColor =PaintColors.SELECTED.get();
|
---|
169 | drawHelperLine = Main.pref.getBoolean("draw.helper-line", true);
|
---|
170 | drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true);
|
---|
171 | wayIsFinished = false;
|
---|
172 |
|
---|
173 | Main.map.mapView.addMouseListener(this);
|
---|
174 | Main.map.mapView.addMouseMotionListener(this);
|
---|
175 | Main.map.mapView.addTemporaryLayer(this);
|
---|
176 | DataSet.addSelectionListener(this);
|
---|
177 |
|
---|
178 | try {
|
---|
179 | Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
|
---|
180 | } catch (SecurityException ex) {
|
---|
181 | }
|
---|
182 | // would like to but haven't got mouse position yet:
|
---|
183 | // computeHelperLine(false, false, false);
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override public void exitMode() {
|
---|
187 | super.exitMode();
|
---|
188 | Main.map.mapView.removeMouseListener(this);
|
---|
189 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
190 | Main.map.mapView.removeTemporaryLayer(this);
|
---|
191 | DataSet.removeSelectionListener(this);
|
---|
192 | removeHighlighting();
|
---|
193 | try {
|
---|
194 | Toolkit.getDefaultToolkit().removeAWTEventListener(this);
|
---|
195 | } catch (SecurityException ex) {
|
---|
196 | }
|
---|
197 |
|
---|
198 | // when exiting we let everybody know about the currently selected
|
---|
199 | // primitives
|
---|
200 | //
|
---|
201 | DataSet ds = getCurrentDataSet();
|
---|
202 | if(ds != null) {
|
---|
203 | ds.fireSelectionChanged();
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * redraw to (possibly) get rid of helper line if selection changes.
|
---|
209 | */
|
---|
210 | public void eventDispatched(AWTEvent event) {
|
---|
211 | if(Main.map == null || Main.map.mapView == null || !Main.map.mapView.isActiveLayerDrawable())
|
---|
212 | return;
|
---|
213 | updateKeyModifiers((InputEvent) event);
|
---|
214 | computeHelperLine();
|
---|
215 | addHighlighting();
|
---|
216 | redrawIfRequired();
|
---|
217 | }
|
---|
218 | /**
|
---|
219 | * redraw to (possibly) get rid of helper line if selection changes.
|
---|
220 | */
|
---|
221 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
222 | if(!Main.map.mapView.isActiveLayerDrawable())
|
---|
223 | return;
|
---|
224 | computeHelperLine();
|
---|
225 | addHighlighting();
|
---|
226 | redrawIfRequired();
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void tryAgain(MouseEvent e) {
|
---|
230 | getCurrentDataSet().setSelected();
|
---|
231 | mouseReleased(e);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * This function should be called when the user wishes to finish his current draw action.
|
---|
236 | * If Potlatch Style is enabled, it will switch to select tool, otherwise simply disable
|
---|
237 | * the helper line until the user chooses to draw something else.
|
---|
238 | */
|
---|
239 | private void finishDrawing() {
|
---|
240 | // let everybody else know about the current selection
|
---|
241 | //
|
---|
242 | Main.main.getCurrentDataSet().fireSelectionChanged();
|
---|
243 | lastUsedNode = null;
|
---|
244 | wayIsFinished = true;
|
---|
245 | Main.map.selectSelectTool(true);
|
---|
246 |
|
---|
247 | // Redraw to remove the helper line stub
|
---|
248 | computeHelperLine();
|
---|
249 | removeHighlighting();
|
---|
250 | redrawIfRequired();
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * If user clicked with the left button, add a node at the current mouse
|
---|
255 | * position.
|
---|
256 | *
|
---|
257 | * If in nodeway mode, insert the node into the way.
|
---|
258 | */
|
---|
259 | @Override public void mouseReleased(MouseEvent e) {
|
---|
260 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
261 | return;
|
---|
262 | if(!Main.map.mapView.isActiveLayerDrawable())
|
---|
263 | return;
|
---|
264 | // request focus in order to enable the expected keyboard shortcuts
|
---|
265 | //
|
---|
266 | Main.map.mapView.requestFocus();
|
---|
267 |
|
---|
268 | if(e.getClickCount() > 1 && mousePos != null && mousePos.equals(oldMousePos)) {
|
---|
269 | // A double click equals "user clicked last node again, finish way"
|
---|
270 | // Change draw tool only if mouse position is nearly the same, as
|
---|
271 | // otherwise fast clicks will count as a double click
|
---|
272 | finishDrawing();
|
---|
273 | return;
|
---|
274 | }
|
---|
275 | oldMousePos = mousePos;
|
---|
276 |
|
---|
277 | // we copy ctrl/alt/shift from the event just in case our global
|
---|
278 | // AWTEvent didn't make it through the security manager. Unclear
|
---|
279 | // if that can ever happen but better be safe.
|
---|
280 | updateKeyModifiers(e);
|
---|
281 | mousePos = e.getPoint();
|
---|
282 |
|
---|
283 | DataSet ds = getCurrentDataSet();
|
---|
284 | Collection<OsmPrimitive> selection = new ArrayList<OsmPrimitive>(ds.getSelected());
|
---|
285 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
286 | Collection<OsmPrimitive> newSelection = new LinkedList<OsmPrimitive>(ds.getSelected());
|
---|
287 |
|
---|
288 | ArrayList<Way> reuseWays = new ArrayList<Way>(),
|
---|
289 | replacedWays = new ArrayList<Way>();
|
---|
290 | boolean newNode = false;
|
---|
291 | Node n = null;
|
---|
292 |
|
---|
293 | if (!ctrl) {
|
---|
294 | n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (n != null) {
|
---|
298 | // user clicked on node
|
---|
299 | if (selection.isEmpty() || wayIsFinished) {
|
---|
300 | // select the clicked node and do nothing else
|
---|
301 | // (this is just a convenience option so that people don't
|
---|
302 | // have to switch modes)
|
---|
303 | newSelection.clear();
|
---|
304 | newSelection.add(n);
|
---|
305 | getCurrentDataSet().setSelected(n);
|
---|
306 | // The user explicitly selected a node, so let him continue drawing
|
---|
307 | wayIsFinished = false;
|
---|
308 | return;
|
---|
309 | }
|
---|
310 | } else {
|
---|
311 | // no node found in clicked area
|
---|
312 | n = new Node(Main.map.mapView.getLatLon(e.getX(), e.getY()));
|
---|
313 | if (n.getCoor().isOutSideWorld()) {
|
---|
314 | JOptionPane.showMessageDialog(
|
---|
315 | Main.parent,
|
---|
316 | tr("Cannot add a node outside of the world."),
|
---|
317 | tr("Warning"),
|
---|
318 | JOptionPane.WARNING_MESSAGE
|
---|
319 | );
|
---|
320 | return;
|
---|
321 | }
|
---|
322 | newNode = true;
|
---|
323 |
|
---|
324 | cmds.add(new AddCommand(n));
|
---|
325 |
|
---|
326 | if (!ctrl) {
|
---|
327 | // Insert the node into all the nearby way segments
|
---|
328 | List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(e.getPoint(), OsmPrimitive.isSelectablePredicate);
|
---|
329 | Map<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>();
|
---|
330 | for (WaySegment ws : wss) {
|
---|
331 | List<Integer> is;
|
---|
332 | if (insertPoints.containsKey(ws.way)) {
|
---|
333 | is = insertPoints.get(ws.way);
|
---|
334 | } else {
|
---|
335 | is = new ArrayList<Integer>();
|
---|
336 | insertPoints.put(ws.way, is);
|
---|
337 | }
|
---|
338 |
|
---|
339 | is.add(ws.lowerIndex);
|
---|
340 | }
|
---|
341 |
|
---|
342 | Set<Pair<Node,Node>> segSet = new HashSet<Pair<Node,Node>>();
|
---|
343 |
|
---|
344 | for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
|
---|
345 | Way w = insertPoint.getKey();
|
---|
346 | List<Integer> is = insertPoint.getValue();
|
---|
347 |
|
---|
348 | Way wnew = new Way(w);
|
---|
349 |
|
---|
350 | pruneSuccsAndReverse(is);
|
---|
351 | for (int i : is) {
|
---|
352 | segSet.add(
|
---|
353 | Pair.sort(new Pair<Node,Node>(w.getNode(i), w.getNode(i+1))));
|
---|
354 | }
|
---|
355 | for (int i : is) {
|
---|
356 | wnew.addNode(i + 1, n);
|
---|
357 | }
|
---|
358 |
|
---|
359 | // If ALT is pressed, a new way should be created and that new way should get
|
---|
360 | // selected. This works everytime unless the ways the nodes get inserted into
|
---|
361 | // are already selected. This is the case when creating a self-overlapping way
|
---|
362 | // but pressing ALT prevents this. Therefore we must de-select the way manually
|
---|
363 | // here so /only/ the new way will be selected after this method finishes.
|
---|
364 | if(alt) {
|
---|
365 | newSelection.add(insertPoint.getKey());
|
---|
366 | }
|
---|
367 |
|
---|
368 | cmds.add(new ChangeCommand(insertPoint.getKey(), wnew));
|
---|
369 | replacedWays.add(insertPoint.getKey());
|
---|
370 | reuseWays.add(wnew);
|
---|
371 | }
|
---|
372 |
|
---|
373 | adjustNode(segSet, n);
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | // This part decides whether or not a "segment" (i.e. a connection) is made to an
|
---|
378 | // existing node.
|
---|
379 |
|
---|
380 | // For a connection to be made, the user must either have a node selected (connection
|
---|
381 | // is made to that node), or he must have a way selected *and* one of the endpoints
|
---|
382 | // of that way must be the last used node (connection is made to last used node), or
|
---|
383 | // he must have a way and a node selected (connection is made to the selected node).
|
---|
384 |
|
---|
385 | // If the above does not apply, the selection is cleared and a new try is started
|
---|
386 |
|
---|
387 | boolean extendedWay = false;
|
---|
388 | boolean wayIsFinishedTemp = wayIsFinished;
|
---|
389 | wayIsFinished = false;
|
---|
390 |
|
---|
391 | // don't draw lines if shift is held
|
---|
392 | if (selection.size() > 0 && !shift) {
|
---|
393 | Node selectedNode = null;
|
---|
394 | Way selectedWay = null;
|
---|
395 |
|
---|
396 | for (OsmPrimitive p : selection) {
|
---|
397 | if (p instanceof Node) {
|
---|
398 | if (selectedNode != null) {
|
---|
399 | // Too many nodes selected to do something useful
|
---|
400 | tryAgain(e);
|
---|
401 | return;
|
---|
402 | }
|
---|
403 | selectedNode = (Node) p;
|
---|
404 | } else if (p instanceof Way) {
|
---|
405 | if (selectedWay != null) {
|
---|
406 | // Too many ways selected to do something useful
|
---|
407 | tryAgain(e);
|
---|
408 | return;
|
---|
409 | }
|
---|
410 | selectedWay = (Way) p;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | // the node from which we make a connection
|
---|
415 | Node n0 = findNodeToContinueFrom(selectedNode, selectedWay);
|
---|
416 | // We have a selection but it isn't suitable. Try again.
|
---|
417 | if(n0 == null) {
|
---|
418 | tryAgain(e);
|
---|
419 | return;
|
---|
420 | }
|
---|
421 | if(!wayIsFinishedTemp){
|
---|
422 | if(isSelfContainedWay(selectedWay, n0, n))
|
---|
423 | return;
|
---|
424 |
|
---|
425 | // User clicked last node again, finish way
|
---|
426 | if(n0 == n) {
|
---|
427 | finishDrawing();
|
---|
428 | return;
|
---|
429 | }
|
---|
430 |
|
---|
431 | // Ok we know now that we'll insert a line segment, but will it connect to an
|
---|
432 | // existing way or make a new way of its own? The "alt" modifier means that the
|
---|
433 | // user wants a new way.
|
---|
434 | Way way = alt ? null : (selectedWay != null) ? selectedWay : getWayForNode(n0);
|
---|
435 | Way wayToSelect;
|
---|
436 |
|
---|
437 | // Don't allow creation of self-overlapping ways
|
---|
438 | if(way != null) {
|
---|
439 | int nodeCount=0;
|
---|
440 | for (Node p : way.getNodes())
|
---|
441 | if(p.equals(n0)) {
|
---|
442 | nodeCount++;
|
---|
443 | }
|
---|
444 | if(nodeCount > 1) {
|
---|
445 | way = null;
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | if (way == null) {
|
---|
450 | way = new Way();
|
---|
451 | way.addNode(n0);
|
---|
452 | cmds.add(new AddCommand(way));
|
---|
453 | wayToSelect = way;
|
---|
454 | } else {
|
---|
455 | int i;
|
---|
456 | if ((i = replacedWays.indexOf(way)) != -1) {
|
---|
457 | way = reuseWays.get(i);
|
---|
458 | wayToSelect = way;
|
---|
459 | } else {
|
---|
460 | wayToSelect = way;
|
---|
461 | Way wnew = new Way(way);
|
---|
462 | cmds.add(new ChangeCommand(way, wnew));
|
---|
463 | way = wnew;
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | // Connected to a node that's already in the way
|
---|
468 | if(way.containsNode(n)) {
|
---|
469 | wayIsFinished = true;
|
---|
470 | selection.clear();
|
---|
471 | }
|
---|
472 |
|
---|
473 | // Add new node to way
|
---|
474 | if (way.getNode(way.getNodesCount() - 1) == n0) {
|
---|
475 | way.addNode(n);
|
---|
476 | } else {
|
---|
477 | way.addNode(0, n);
|
---|
478 | }
|
---|
479 |
|
---|
480 | extendedWay = true;
|
---|
481 | newSelection.clear();
|
---|
482 | newSelection.add(wayToSelect);
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | String title;
|
---|
487 | if (!extendedWay) {
|
---|
488 | if (!newNode)
|
---|
489 | return; // We didn't do anything.
|
---|
490 | else if (reuseWays.isEmpty()) {
|
---|
491 | title = tr("Add node");
|
---|
492 | } else {
|
---|
493 | title = tr("Add node into way");
|
---|
494 | for (Way w : reuseWays) {
|
---|
495 | newSelection.remove(w);
|
---|
496 | }
|
---|
497 | }
|
---|
498 | newSelection.clear();
|
---|
499 | newSelection.add(n);
|
---|
500 | } else if (!newNode) {
|
---|
501 | title = tr("Connect existing way to node");
|
---|
502 | } else if (reuseWays.isEmpty()) {
|
---|
503 | title = tr("Add a new node to an existing way");
|
---|
504 | } else {
|
---|
505 | title = tr("Add node into way and connect");
|
---|
506 | }
|
---|
507 |
|
---|
508 | Command c = new SequenceCommand(title, cmds);
|
---|
509 |
|
---|
510 | Main.main.undoRedo.add(c);
|
---|
511 | if(!wayIsFinished) {
|
---|
512 | lastUsedNode = n;
|
---|
513 | }
|
---|
514 |
|
---|
515 | getCurrentDataSet().setSelected(newSelection);
|
---|
516 |
|
---|
517 | // "viewport following" mode for tracing long features
|
---|
518 | // from aerial imagery or GPS tracks.
|
---|
519 | if (n != null && Main.map.mapView.viewportFollowing) {
|
---|
520 | Main.map.mapView.smoothScrollTo(n.getEastNorth());
|
---|
521 | };
|
---|
522 | computeHelperLine();
|
---|
523 | removeHighlighting();
|
---|
524 | redrawIfRequired();
|
---|
525 | }
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * Prevent creation of ways that look like this: <---->
|
---|
529 | * This happens if users want to draw a no-exit-sideway from the main way like this:
|
---|
530 | * ^
|
---|
531 | * |<---->
|
---|
532 | * |
|
---|
533 | * The solution isn't ideal because the main way will end in the side way, which is bad for
|
---|
534 | * navigation software ("drive straight on") but at least easier to fix. Maybe users will fix
|
---|
535 | * it on their own, too. At least it's better than producing an error.
|
---|
536 | *
|
---|
537 | * @param Way the way to check
|
---|
538 | * @param Node the current node (i.e. the one the connection will be made from)
|
---|
539 | * @param Node the target node (i.e. the one the connection will be made to)
|
---|
540 | * @return Boolean True if this would create a selfcontaining way, false otherwise.
|
---|
541 | */
|
---|
542 | private boolean isSelfContainedWay(Way selectedWay, Node currentNode, Node targetNode) {
|
---|
543 | if(selectedWay != null) {
|
---|
544 | int posn0 = selectedWay.getNodes().indexOf(currentNode);
|
---|
545 | if( posn0 != -1 && // n0 is part of way
|
---|
546 | (posn0 >= 1 && targetNode.equals(selectedWay.getNode(posn0-1))) || // previous node
|
---|
547 | (posn0 < selectedWay.getNodesCount()-1) && targetNode.equals(selectedWay.getNode(posn0+1))) { // next node
|
---|
548 | getCurrentDataSet().setSelected(targetNode);
|
---|
549 | lastUsedNode = targetNode;
|
---|
550 | return true;
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | return false;
|
---|
555 | }
|
---|
556 |
|
---|
557 | /**
|
---|
558 | * Finds a node to continue drawing from. Decision is based upon given node and way.
|
---|
559 | * @param selectedNode Currently selected node, may be null
|
---|
560 | * @param selectedWay Currently selected way, may be null
|
---|
561 | * @return Node if a suitable node is found, null otherwise
|
---|
562 | */
|
---|
563 | private Node findNodeToContinueFrom(Node selectedNode, Way selectedWay) {
|
---|
564 | // No nodes or ways have been selected, this occurs when a relation
|
---|
565 | // has been selected or the selection is empty
|
---|
566 | if(selectedNode == null && selectedWay == null)
|
---|
567 | return null;
|
---|
568 |
|
---|
569 | if (selectedNode == null) {
|
---|
570 | if (selectedWay.isFirstLastNode(lastUsedNode))
|
---|
571 | return lastUsedNode;
|
---|
572 |
|
---|
573 | // We have a way selected, but no suitable node to continue from. Start anew.
|
---|
574 | return null;
|
---|
575 | }
|
---|
576 |
|
---|
577 | if (selectedWay == null)
|
---|
578 | return selectedNode;
|
---|
579 |
|
---|
580 | if (selectedWay.isFirstLastNode(selectedNode))
|
---|
581 | return selectedNode;
|
---|
582 |
|
---|
583 | // We have a way and node selected, but it's not at the start/end of the way. Start anew.
|
---|
584 | return null;
|
---|
585 | }
|
---|
586 |
|
---|
587 | @Override public void mouseDragged(MouseEvent e) {
|
---|
588 | mouseMoved(e);
|
---|
589 | }
|
---|
590 |
|
---|
591 | @Override public void mouseMoved(MouseEvent e) {
|
---|
592 | if(!Main.map.mapView.isActiveLayerDrawable())
|
---|
593 | return;
|
---|
594 |
|
---|
595 | // we copy ctrl/alt/shift from the event just in case our global
|
---|
596 | // AWTEvent didn't make it through the security manager. Unclear
|
---|
597 | // if that can ever happen but better be safe.
|
---|
598 | updateKeyModifiers(e);
|
---|
599 | mousePos = e.getPoint();
|
---|
600 |
|
---|
601 | computeHelperLine();
|
---|
602 | addHighlighting();
|
---|
603 | redrawIfRequired();
|
---|
604 | }
|
---|
605 |
|
---|
606 | private void updateKeyModifiers(InputEvent e) {
|
---|
607 | ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
---|
608 | alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
|
---|
609 | shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
---|
610 | }
|
---|
611 |
|
---|
612 | private void updateKeyModifiers(MouseEvent e) {
|
---|
613 | ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
---|
614 | alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
|
---|
615 | shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
---|
616 | }
|
---|
617 |
|
---|
618 | /**
|
---|
619 | * This method prepares data required for painting the "helper line" from
|
---|
620 | * the last used position to the mouse cursor. It duplicates some code from
|
---|
621 | * mouseReleased() (FIXME).
|
---|
622 | */
|
---|
623 | private void computeHelperLine() {
|
---|
624 | MapView mv = Main.map.mapView;
|
---|
625 | if (mousePos == null) {
|
---|
626 | // Don't draw the line.
|
---|
627 | currentMouseEastNorth = null;
|
---|
628 | currentBaseNode = null;
|
---|
629 | return;
|
---|
630 | }
|
---|
631 |
|
---|
632 | double distance = -1;
|
---|
633 | double angle = -1;
|
---|
634 |
|
---|
635 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
636 |
|
---|
637 | Node selectedNode = null;
|
---|
638 | Way selectedWay = null;
|
---|
639 | Node currentMouseNode = null;
|
---|
640 | mouseOnExistingNode = null;
|
---|
641 | mouseOnExistingWays = new HashSet<Way>();
|
---|
642 |
|
---|
643 | Main.map.statusLine.setAngle(-1);
|
---|
644 | Main.map.statusLine.setHeading(-1);
|
---|
645 | Main.map.statusLine.setDist(-1);
|
---|
646 |
|
---|
647 | if (!ctrl && mousePos != null) {
|
---|
648 | currentMouseNode = mv.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
|
---|
649 | }
|
---|
650 |
|
---|
651 | // We need this for highlighting and we'll only do so if we actually want to re-use
|
---|
652 | // *and* there is no node nearby (because nodes beat ways when re-using)
|
---|
653 | if(!ctrl && currentMouseNode == null) {
|
---|
654 | List<WaySegment> wss = mv.getNearestWaySegments(mousePos, OsmPrimitive.isSelectablePredicate);
|
---|
655 | for(WaySegment ws : wss) {
|
---|
656 | mouseOnExistingWays.add(ws.way);
|
---|
657 | }
|
---|
658 | }
|
---|
659 |
|
---|
660 | if (currentMouseNode != null) {
|
---|
661 | // user clicked on node
|
---|
662 | if (selection.isEmpty()) return;
|
---|
663 | currentMouseEastNorth = currentMouseNode.getEastNorth();
|
---|
664 | mouseOnExistingNode = currentMouseNode;
|
---|
665 | } else {
|
---|
666 | // no node found in clicked area
|
---|
667 | currentMouseEastNorth = mv.getEastNorth(mousePos.x, mousePos.y);
|
---|
668 | }
|
---|
669 |
|
---|
670 | for (OsmPrimitive p : selection) {
|
---|
671 | if (p instanceof Node) {
|
---|
672 | if (selectedNode != null) return;
|
---|
673 | selectedNode = (Node) p;
|
---|
674 | } else if (p instanceof Way) {
|
---|
675 | if (selectedWay != null) return;
|
---|
676 | selectedWay = (Way) p;
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 | // the node from which we make a connection
|
---|
681 | currentBaseNode = null;
|
---|
682 | Node previousNode = null;
|
---|
683 |
|
---|
684 | if (selectedNode == null) {
|
---|
685 | if (selectedWay == null)
|
---|
686 | return;
|
---|
687 | if (selectedWay.isFirstLastNode(lastUsedNode)) {
|
---|
688 | currentBaseNode = lastUsedNode;
|
---|
689 | if (lastUsedNode == selectedWay.getNode(selectedWay.getNodesCount()-1) && selectedWay.getNodesCount() > 1) {
|
---|
690 | previousNode = selectedWay.getNode(selectedWay.getNodesCount()-2);
|
---|
691 | }
|
---|
692 | }
|
---|
693 | } else if (selectedWay == null) {
|
---|
694 | currentBaseNode = selectedNode;
|
---|
695 | } else {
|
---|
696 | if (selectedNode == selectedWay.getNode(0) || selectedNode == selectedWay.getNode(selectedWay.getNodesCount()-1)) {
|
---|
697 | currentBaseNode = selectedNode;
|
---|
698 | }
|
---|
699 | }
|
---|
700 |
|
---|
701 | if (currentBaseNode == null || currentBaseNode == currentMouseNode)
|
---|
702 | return; // Don't create zero length way segments.
|
---|
703 |
|
---|
704 | // find out the distance, in metres, between the base point and the mouse cursor
|
---|
705 | LatLon mouseLatLon = mv.getProjection().eastNorth2latlon(currentMouseEastNorth);
|
---|
706 | distance = currentBaseNode.getCoor().greatCircleDistance(mouseLatLon);
|
---|
707 |
|
---|
708 | double hdg = Math.toDegrees(currentBaseNode.getEastNorth()
|
---|
709 | .heading(currentMouseEastNorth));
|
---|
710 | if (previousNode != null) {
|
---|
711 | angle = hdg - Math.toDegrees(previousNode.getEastNorth()
|
---|
712 | .heading(currentBaseNode.getEastNorth()));
|
---|
713 | angle += angle < 0 ? 360 : 0;
|
---|
714 | }
|
---|
715 |
|
---|
716 | Main.map.statusLine.setAngle(angle);
|
---|
717 | Main.map.statusLine.setHeading(hdg);
|
---|
718 | Main.map.statusLine.setDist(distance);
|
---|
719 | // Now done in redrawIfRequired()
|
---|
720 | //updateStatusLine();
|
---|
721 | }
|
---|
722 |
|
---|
723 | /**
|
---|
724 | * Repaint on mouse exit so that the helper line goes away.
|
---|
725 | */
|
---|
726 | @Override public void mouseExited(MouseEvent e) {
|
---|
727 | if(!Main.map.mapView.isActiveLayerDrawable())
|
---|
728 | return;
|
---|
729 | mousePos = e.getPoint();
|
---|
730 | Main.map.mapView.repaint();
|
---|
731 | }
|
---|
732 |
|
---|
733 | /**
|
---|
734 | * @return If the node is the end of exactly one way, return this.
|
---|
735 | * <code>null</code> otherwise.
|
---|
736 | */
|
---|
737 | public Way getWayForNode(Node n) {
|
---|
738 | Way way = null;
|
---|
739 | for (Way w : OsmPrimitive.getFilteredList(n.getReferrers(), Way.class)) {
|
---|
740 | if (!w.isUsable() || w.getNodesCount() < 1) {
|
---|
741 | continue;
|
---|
742 | }
|
---|
743 | Node firstNode = w.getNode(0);
|
---|
744 | Node lastNode = w.getNode(w.getNodesCount() - 1);
|
---|
745 | if ((firstNode == n || lastNode == n) && (firstNode != lastNode)) {
|
---|
746 | if (way != null)
|
---|
747 | return null;
|
---|
748 | way = w;
|
---|
749 | }
|
---|
750 | }
|
---|
751 | return way;
|
---|
752 | }
|
---|
753 |
|
---|
754 | public Node getCurrentBaseNode() {
|
---|
755 | return currentBaseNode;
|
---|
756 | }
|
---|
757 |
|
---|
758 | private static void pruneSuccsAndReverse(List<Integer> is) {
|
---|
759 | //if (is.size() < 2) return;
|
---|
760 |
|
---|
761 | HashSet<Integer> is2 = new HashSet<Integer>();
|
---|
762 | for (int i : is) {
|
---|
763 | if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
|
---|
764 | is2.add(i);
|
---|
765 | }
|
---|
766 | }
|
---|
767 | is.clear();
|
---|
768 | is.addAll(is2);
|
---|
769 | Collections.sort(is);
|
---|
770 | Collections.reverse(is);
|
---|
771 | }
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * Adjusts the position of a node to lie on a segment (or a segment
|
---|
775 | * intersection).
|
---|
776 | *
|
---|
777 | * If one or more than two segments are passed, the node is adjusted
|
---|
778 | * to lie on the first segment that is passed.
|
---|
779 | *
|
---|
780 | * If two segments are passed, the node is adjusted to be at their
|
---|
781 | * intersection.
|
---|
782 | *
|
---|
783 | * No action is taken if no segments are passed.
|
---|
784 | *
|
---|
785 | * @param segs the segments to use as a reference when adjusting
|
---|
786 | * @param n the node to adjust
|
---|
787 | */
|
---|
788 | private static void adjustNode(Collection<Pair<Node,Node>> segs, Node n) {
|
---|
789 |
|
---|
790 | switch (segs.size()) {
|
---|
791 | case 0:
|
---|
792 | return;
|
---|
793 | case 2:
|
---|
794 | // This computes the intersection between
|
---|
795 | // the two segments and adjusts the node position.
|
---|
796 | Iterator<Pair<Node,Node>> i = segs.iterator();
|
---|
797 | Pair<Node,Node> seg = i.next();
|
---|
798 | EastNorth A = seg.a.getEastNorth();
|
---|
799 | EastNorth B = seg.b.getEastNorth();
|
---|
800 | seg = i.next();
|
---|
801 | EastNorth C = seg.a.getEastNorth();
|
---|
802 | EastNorth D = seg.b.getEastNorth();
|
---|
803 |
|
---|
804 | double u=det(B.east() - A.east(), B.north() - A.north(), C.east() - D.east(), C.north() - D.north());
|
---|
805 |
|
---|
806 | // Check for parallel segments and do nothing if they are
|
---|
807 | // In practice this will probably only happen when a way has been duplicated
|
---|
808 |
|
---|
809 | if (u == 0) return;
|
---|
810 |
|
---|
811 | // q is a number between 0 and 1
|
---|
812 | // It is the point in the segment where the intersection occurs
|
---|
813 | // if the segment is scaled to lenght 1
|
---|
814 |
|
---|
815 | double q = det(B.north() - C.north(), B.east() - C.east(), D.north() - C.north(), D.east() - C.east()) / u;
|
---|
816 | EastNorth intersection = new EastNorth(
|
---|
817 | B.east() + q * (A.east() - B.east()),
|
---|
818 | B.north() + q * (A.north() - B.north()));
|
---|
819 |
|
---|
820 | int snapToIntersectionThreshold
|
---|
821 | = Main.pref.getInteger("edit.snap-intersection-threshold",10);
|
---|
822 |
|
---|
823 | // only adjust to intersection if within snapToIntersectionThreshold pixel of mouse click; otherwise
|
---|
824 | // fall through to default action.
|
---|
825 | // (for semi-parallel lines, intersection might be miles away!)
|
---|
826 | if (Main.map.mapView.getPoint(n).distance(Main.map.mapView.getPoint(intersection)) < snapToIntersectionThreshold) {
|
---|
827 | n.setEastNorth(intersection);
|
---|
828 | return;
|
---|
829 | }
|
---|
830 |
|
---|
831 | default:
|
---|
832 | EastNorth P = n.getEastNorth();
|
---|
833 | seg = segs.iterator().next();
|
---|
834 | A = seg.a.getEastNorth();
|
---|
835 | B = seg.b.getEastNorth();
|
---|
836 | double a = P.distanceSq(B);
|
---|
837 | double b = P.distanceSq(A);
|
---|
838 | double c = A.distanceSq(B);
|
---|
839 | q = (a - b + c) / (2*c);
|
---|
840 | n.setEastNorth(new EastNorth(B.east() + q * (A.east() - B.east()), B.north() + q * (A.north() - B.north())));
|
---|
841 | }
|
---|
842 | }
|
---|
843 |
|
---|
844 | // helper for adjustNode
|
---|
845 | static double det(double a, double b, double c, double d) {
|
---|
846 | return a * d - b * c;
|
---|
847 | }
|
---|
848 |
|
---|
849 | public void paint(Graphics2D g, MapView mv, Bounds box) {
|
---|
850 | if (!drawHelperLine || wayIsFinished || shift) return;
|
---|
851 |
|
---|
852 | // sanity checks
|
---|
853 | if (Main.map.mapView == null) return;
|
---|
854 | if (mousePos == null) return;
|
---|
855 |
|
---|
856 | // don't draw line if we don't know where from or where to
|
---|
857 | if (currentBaseNode == null || currentMouseEastNorth == null) return;
|
---|
858 |
|
---|
859 | // don't draw line if mouse is outside window
|
---|
860 | if (!Main.map.mapView.getBounds().contains(mousePos)) return;
|
---|
861 |
|
---|
862 | Graphics2D g2 = g;
|
---|
863 | g2.setColor(selectedColor);
|
---|
864 | g2.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
---|
865 | GeneralPath b = new GeneralPath();
|
---|
866 | Point p1=mv.getPoint(currentBaseNode);
|
---|
867 | Point p2=mv.getPoint(currentMouseEastNorth);
|
---|
868 |
|
---|
869 | double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
|
---|
870 |
|
---|
871 | b.moveTo(p1.x,p1.y); b.lineTo(p2.x, p2.y);
|
---|
872 |
|
---|
873 | // if alt key is held ("start new way"), draw a little perpendicular line
|
---|
874 | if (alt) {
|
---|
875 | b.moveTo((int)(p1.x + 8*Math.cos(t+PHI)), (int)(p1.y + 8*Math.sin(t+PHI)));
|
---|
876 | b.lineTo((int)(p1.x + 8*Math.cos(t-PHI)), (int)(p1.y + 8*Math.sin(t-PHI)));
|
---|
877 | }
|
---|
878 |
|
---|
879 | g2.draw(b);
|
---|
880 | g2.setStroke(new BasicStroke(1));
|
---|
881 | }
|
---|
882 |
|
---|
883 | @Override public String getModeHelpText() {
|
---|
884 | String rv = "";
|
---|
885 | /*
|
---|
886 | * No modifiers: all (Connect, Node Re-Use, Auto-Weld)
|
---|
887 | * CTRL: disables node re-use, auto-weld
|
---|
888 | * Shift: do not make connection
|
---|
889 | * ALT: make connection but start new way in doing so
|
---|
890 | */
|
---|
891 |
|
---|
892 | /*
|
---|
893 | * Status line text generation is split into two parts to keep it maintainable.
|
---|
894 | * First part looks at what will happen to the new node inserted on click and
|
---|
895 | * the second part will look if a connection is made or not.
|
---|
896 | *
|
---|
897 | * Note that this help text is not absolutely accurate as it doesn't catch any special
|
---|
898 | * cases (e.g. when preventing <---> ways). The only special that it catches is when
|
---|
899 | * a way is about to be finished.
|
---|
900 | *
|
---|
901 | * First check what happens to the new node.
|
---|
902 | */
|
---|
903 |
|
---|
904 | // oldHighlights stores the current highlights. If this
|
---|
905 | // list is empty we can assume that we won't do any joins
|
---|
906 | if (ctrl || oldHighlights.isEmpty()) {
|
---|
907 | rv = tr("Create new node.");
|
---|
908 | } else {
|
---|
909 | // oldHighlights may store a node or way, check if it's a node
|
---|
910 | OsmPrimitive x = oldHighlights.iterator().next();
|
---|
911 | if (x instanceof Node) {
|
---|
912 | rv = tr("Select node under cursor.");
|
---|
913 | } else {
|
---|
914 | rv = trn("Insert new node into way.", "Insert new node into {0} ways.",
|
---|
915 | oldHighlights.size(), oldHighlights.size());
|
---|
916 | }
|
---|
917 | }
|
---|
918 |
|
---|
919 | /*
|
---|
920 | * Check whether a connection will be made
|
---|
921 | */
|
---|
922 | if (currentBaseNode != null && !wayIsFinished) {
|
---|
923 | if (alt) {
|
---|
924 | rv += " " + tr("Start new way from last node.");
|
---|
925 | } else {
|
---|
926 | rv += " " + tr("Continue way from last node.");
|
---|
927 | }
|
---|
928 | }
|
---|
929 |
|
---|
930 | Node n = mouseOnExistingNode;
|
---|
931 | /*
|
---|
932 | * Handle special case: Highlighted node == selected node => finish drawing
|
---|
933 | */
|
---|
934 | if (n != null && getCurrentDataSet() != null && getCurrentDataSet().getSelectedNodes().contains(n)) {
|
---|
935 | if (wayIsFinished) {
|
---|
936 | rv = tr("Select node under cursor.");
|
---|
937 | } else {
|
---|
938 | rv = tr("Finish drawing.");
|
---|
939 | }
|
---|
940 | }
|
---|
941 |
|
---|
942 | /*
|
---|
943 | * Handle special case: Self-Overlapping or closing way
|
---|
944 | */
|
---|
945 | if (getCurrentDataSet() != null && getCurrentDataSet().getSelectedWays().size() > 0 && !wayIsFinished && !alt) {
|
---|
946 | Way w = getCurrentDataSet().getSelectedWays().iterator().next();
|
---|
947 | for (Node m : w.getNodes()) {
|
---|
948 | if (m.equals(mouseOnExistingNode) || mouseOnExistingWays.contains(w)) {
|
---|
949 | rv += " " + tr("Finish drawing.");
|
---|
950 | break;
|
---|
951 | }
|
---|
952 | }
|
---|
953 | }
|
---|
954 | return rv;
|
---|
955 | }
|
---|
956 |
|
---|
957 | @Override public boolean layerIsSupported(Layer l) {
|
---|
958 | return l instanceof OsmDataLayer;
|
---|
959 | }
|
---|
960 |
|
---|
961 | @Override
|
---|
962 | protected void updateEnabledState() {
|
---|
963 | setEnabled(getEditLayer() != null);
|
---|
964 | }
|
---|
965 |
|
---|
966 | @Override
|
---|
967 | public void destroy() {
|
---|
968 | super.destroy();
|
---|
969 | Main.unregisterActionShortcut(extraShortcut);
|
---|
970 | }
|
---|
971 | }
|
---|