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