source: josm/trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java@ 1448

Last change on this file since 1448 was 1448, checked in by stoecker, 15 years ago

close #2249. patch by xeen

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