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

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

fixed SHIFT handling in add mode

  • Property svn:eol-style set to native
File size: 34.1 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 /**
142 * Checks if a map redraw is required and does so if needed. Also updates the status bar
143 */
144 private void redrawIfRequired() {
145 updateStatusLine();
146 if ((!drawHelperLine || wayIsFinished) && !drawTargetHighlight) return;
147 Main.map.mapView.repaint();
148 }
149
150 /**
151 * Takes the data from computeHelperLine to determine which ways/nodes should be highlighted
152 * (if feature enabled). Also sets the target cursor if appropriate.
153 */
154 private void addHighlighting() {
155 removeHighlighting();
156 // if ctrl key is held ("no join"), don't highlight anything
157 if (ctrl) {
158 setCursor(Cursors.crosshair);
159 return;
160 }
161
162 // This happens when nothing is selected, but we still want to highlight the "target node"
163 if(mouseOnExistingNode == null && Main.ds.getSelected().size() == 0
164 && mousePos != null && !shift)
165 mouseOnExistingNode = Main.map.mapView.getNearestNode(mousePos);
166
167 if (mouseOnExistingNode != null) {
168 setCursor(Cursors.node);
169 // We also need this list for the statusbar help text
170 oldHighlights.add(mouseOnExistingNode);
171 if(drawTargetHighlight)
172 mouseOnExistingNode.highlighted = true;
173 return;
174 }
175
176 // Insert the node into all the nearby way segments
177 if(mouseOnExistingWays.size() == 0) {
178 setCursor(Cursors.crosshair);
179 return;
180 }
181
182 setCursor(Cursors.way);
183
184 // We also need this list for the statusbar help text
185 oldHighlights.addAll(mouseOnExistingWays);
186 if(!drawTargetHighlight) return;
187 for(Way w : mouseOnExistingWays) {
188 w.highlighted = true;
189 }
190 }
191
192 /**
193 * Removes target highlighting from primitives
194 */
195 private void removeHighlighting() {
196 for(OsmPrimitive prim : oldHighlights) {
197 prim.highlighted = false;
198 }
199 oldHighlights = new HashSet<OsmPrimitive>();
200 }
201
202 @Override public void enterMode() {
203 super.enterMode();
204 currCursor = Cursors.crosshair;
205 selectedColor = Main.pref.getColor(marktr("selected"), Color.red);
206 drawHelperLine = Main.pref.getBoolean("draw.helper-line", true);
207 drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true);
208 drawTargetCursor = Main.pref.getBoolean("draw.target-cursor", true);
209 wayIsFinished = false;
210
211 Main.map.mapView.addMouseListener(this);
212 Main.map.mapView.addMouseMotionListener(this);
213 Main.map.mapView.addTemporaryLayer(this);
214 DataSet.selListeners.add(this);
215
216 try {
217 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
218 } catch (SecurityException ex) {
219 }
220 // would like to but haven't got mouse position yet:
221 // computeHelperLine(false, false, false);
222 }
223
224 @Override public void exitMode() {
225 super.exitMode();
226 Main.map.mapView.removeMouseListener(this);
227 Main.map.mapView.removeMouseMotionListener(this);
228 Main.map.mapView.removeTemporaryLayer(this);
229 DataSet.selListeners.remove(this);
230 removeHighlighting();
231 try {
232 Toolkit.getDefaultToolkit().removeAWTEventListener(this);
233 } catch (SecurityException ex) {
234 }
235 }
236
237 /**
238 * redraw to (possibly) get rid of helper line if selection changes.
239 */
240 public void eventDispatched(AWTEvent event) {
241 if(Main.map == null || Main.map.mapView == null || !Main.map.mapView.isDrawableLayer())
242 return;
243 updateKeyModifiers((InputEvent) event);
244 computeHelperLine();
245 addHighlighting();
246 redrawIfRequired();
247 }
248 /**
249 * redraw to (possibly) get rid of helper line if selection changes.
250 */
251 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
252 if(!Main.map.mapView.isDrawableLayer())
253 return;
254 wayIsFinished = false;
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 && !shift)
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 return;
327 }
328 } else {
329 // no node found in clicked area
330 n = new Node(Main.map.mapView.getLatLon(e.getX(), e.getY()));
331 if (n.coor.isOutSideWorld()) {
332 JOptionPane.showMessageDialog(Main.parent,
333 tr("Cannot add a node outside of the world."));
334 return;
335 }
336 newNode = true;
337
338 cmds.add(new AddCommand(n));
339
340 if (!ctrl) {
341 // Insert the node into all the nearby way segments
342 List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(e.getPoint());
343 Map<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>();
344 for (WaySegment ws : wss) {
345 List<Integer> is;
346 if (insertPoints.containsKey(ws.way)) {
347 is = insertPoints.get(ws.way);
348 } else {
349 is = new ArrayList<Integer>();
350 insertPoints.put(ws.way, is);
351 }
352
353 is.add(ws.lowerIndex);
354 }
355
356 Set<Pair<Node,Node>> segSet = new HashSet<Pair<Node,Node>>();
357
358 for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
359 Way w = insertPoint.getKey();
360 List<Integer> is = insertPoint.getValue();
361
362 Way wnew = new Way(w);
363
364 pruneSuccsAndReverse(is);
365 for (int i : is) segSet.add(
366 Pair.sort(new Pair<Node,Node>(w.nodes.get(i), w.nodes.get(i+1))));
367 for (int i : is) wnew.addNode(i + 1, n);
368
369 // If ALT is pressed, a new way should be created and that new way should get
370 // selected. This works everytime unless the ways the nodes get inserted into
371 // are already selected. This is the case when creating a self-overlapping way
372 // but pressing ALT prevents this. Therefore we must de-select the way manually
373 // here so /only/ the new way will be selected after this method finishes.
374 if(alt) wnew.selected = false;
375
376 cmds.add(new ChangeCommand(insertPoint.getKey(), wnew));
377 replacedWays.add(insertPoint.getKey());
378 reuseWays.add(wnew);
379 }
380
381 adjustNode(segSet, n);
382 }
383 }
384
385 // This part decides whether or not a "segment" (i.e. a connection) is made to an
386 // existing node.
387
388 // For a connection to be made, the user must either have a node selected (connection
389 // is made to that node), or he must have a way selected *and* one of the endpoints
390 // of that way must be the last used node (connection is made to last used node), or
391 // he must have a way and a node selected (connection is made to the selected node).
392
393 // If the above does not apply, the selection is cleared and a new try is started
394 boolean extendedWay = false;
395 boolean wayIsFinishedTemp = wayIsFinished;
396 wayIsFinished = false;
397 if (selection.size() > 0 && !wayIsFinishedTemp) {
398 Node selectedNode = null;
399 Way selectedWay = null;
400
401 for (OsmPrimitive p : selection) {
402 if (p instanceof Node) {
403 if (selectedNode != null) {
404 // Too many nodes selected to do something useful
405 tryAgain(e);
406 return;
407 }
408 selectedNode = (Node) p;
409 } else if (p instanceof Way) {
410 if (selectedWay != null) {
411 // Too many ways selected to do something useful
412 tryAgain(e);
413 return;
414 }
415 selectedWay = (Way) p;
416 }
417 }
418
419 // the node from which we make a connection
420 Node n0 = findNodeToContinueFrom(selectedNode, selectedWay);
421 // We have a selection but it isn't suitable. Try again.
422 if(n0 == null) {
423 tryAgain(e);
424 return;
425 }
426
427 if(isSelfContainedWay(selectedWay, n0, n))
428 return;
429
430 // User clicked last node again, finish way
431 if(n0 == n) {
432 finishDrawing();
433 return;
434 }
435
436 // Ok we know now that we'll insert a line segment, but will it connect to an
437 // existing way or make a new way of its own? The "alt" modifier means that the
438 // user wants a new way.
439 Way way = alt ? null : (selectedWay != null) ? selectedWay : getWayForNode(n0);
440
441 // Don't allow creation of self-overlapping ways
442 if(way != null) {
443 int nodeCount=0;
444 for (Node p : way.nodes)
445 if(p.equals(n0)) nodeCount++;
446 if(nodeCount > 1) way = null;
447 }
448
449 if (way == null) {
450 way = new Way();
451 way.addNode(n0);
452 cmds.add(new AddCommand(way));
453 } else {
454 int i;
455 if ((i = replacedWays.indexOf(way)) != -1) {
456 way = reuseWays.get(i);
457 } else {
458 Way wnew = new Way(way);
459 cmds.add(new ChangeCommand(way, wnew));
460 way = wnew;
461 }
462 }
463
464 // Connected to a node that's already in the way
465 if(way.nodes.contains(n)) {
466 wayIsFinished = true;
467 selection.clear();
468 }
469
470 // Add new node to way
471 if (way.nodes.get(way.nodes.size() - 1) == n0)
472 way.addNode(n);
473 else
474 way.addNode(0, n);
475
476 extendedWay = true;
477 Main.ds.setSelected(way);
478 }
479
480 String title;
481 if (!extendedWay) {
482 if (!newNode) {
483 return; // We didn't do anything.
484 } else if (reuseWays.isEmpty()) {
485 title = tr("Add node");
486 } else {
487 title = tr("Add node into way");
488 for (Way w : reuseWays) w.selected = false;
489 }
490 Main.ds.setSelected(n);
491 } else if (!newNode) {
492 title = tr("Connect existing way to node");
493 } else if (reuseWays.isEmpty()) {
494 title = tr("Add a new node to an existing way");
495 } else {
496 title = tr("Add node into way and connect");
497 }
498
499 Command c = new SequenceCommand(title, cmds);
500
501 Main.main.undoRedo.add(c);
502 if(!wayIsFinished) lastUsedNode = n;
503
504 computeHelperLine();
505 removeHighlighting();
506 redrawIfRequired();
507 }
508
509 /**
510 * Prevent creation of ways that look like this: <---->
511 * This happens if users want to draw a no-exit-sideway from the main way like this:
512 * ^
513 * |<---->
514 * |
515 * The solution isn't ideal because the main way will end in the side way, which is bad for
516 * navigation software ("drive straight on") but at least easier to fix. Maybe users will fix
517 * it on their own, too. At least it's better than producing an error.
518 *
519 * @param Way the way to check
520 * @param Node the current node (i.e. the one the connection will be made from)
521 * @param Node the target node (i.e. the one the connection will be made to)
522 * @return Boolean True if this would create a selfcontaining way, false otherwise.
523 */
524 private boolean isSelfContainedWay(Way selectedWay, Node currentNode, Node targetNode) {
525 if(selectedWay != null && selectedWay.nodes != null) {
526 int posn0 = selectedWay.nodes.indexOf(currentNode);
527 if( posn0 != -1 && // n0 is part of way
528 (posn0 >= 1 && targetNode.equals(selectedWay.nodes.get(posn0-1))) || // previous node
529 (posn0 < selectedWay.nodes.size()-1) && targetNode.equals(selectedWay.nodes.get(posn0+1))) { // next node
530 Main.ds.setSelected(targetNode);
531 lastUsedNode = targetNode;
532 return true;
533 }
534 }
535
536 return false;
537 }
538
539 /**
540 * Finds a node to continue drawing from. Decision is based upon given node and way.
541 * @param selectedNode Currently selected node, may be null
542 * @param selectedWay Currently selected way, may be null
543 * @return Node if a suitable node is found, null otherwise
544 */
545 private Node findNodeToContinueFrom(Node selectedNode, Way selectedWay) {
546 // No nodes or ways have been selected, this occurs when a relation
547 // has been selected or the selection is empty
548 if(selectedNode == null && selectedWay == null)
549 return null;
550
551 if (selectedNode == null) {
552 if (selectedWay.isFirstLastNode(lastUsedNode))
553 return lastUsedNode;
554
555 // We have a way selected, but no suitable node to continue from. Start anew.
556 return null;
557 }
558
559 if (selectedWay == null)
560 return selectedNode;
561
562 if (selectedWay.isFirstLastNode(selectedNode))
563 return selectedNode;
564
565 // We have a way and node selected, but it's not at the start/end of the way. Start anew.
566 return null;
567 }
568
569 @Override public void mouseMoved(MouseEvent e) {
570 if(!Main.map.mapView.isDrawableLayer())
571 return;
572
573 // we copy ctrl/alt/shift from the event just in case our global
574 // AWTEvent didn't make it through the security manager. Unclear
575 // if that can ever happen but better be safe.
576 updateKeyModifiers(e);
577 mousePos = e.getPoint();
578
579 computeHelperLine();
580 addHighlighting();
581 redrawIfRequired();
582 }
583
584 private void updateKeyModifiers(InputEvent e) {
585 ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
586 alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
587 shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
588 }
589
590 private void updateKeyModifiers(MouseEvent e) {
591 ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
592 alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
593 shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
594 }
595
596 /**
597 * This method prepares data required for painting the "helper line" from
598 * the last used position to the mouse cursor. It duplicates some code from
599 * mouseClicked() (FIXME).
600 */
601 private void computeHelperLine() {
602 if (mousePos == null) {
603 // Don't draw the line.
604 currentMouseEastNorth = null;
605 currentBaseNode = null;
606 return;
607 }
608
609 double distance = -1;
610 double angle = -1;
611
612 Collection<OsmPrimitive> selection = Main.ds.getSelected();
613
614 Node selectedNode = null;
615 Way selectedWay = null;
616 Node currentMouseNode = null;
617 mouseOnExistingNode = null;
618 mouseOnExistingWays = new HashSet<Way>();
619
620 Main.map.statusLine.setAngle(-1);
621 Main.map.statusLine.setHeading(-1);
622 Main.map.statusLine.setDist(-1);
623
624 if (!ctrl && !shift && mousePos != null) {
625 currentMouseNode = Main.map.mapView.getNearestNode(mousePos);
626 }
627
628 // We need this for highlighting and we'll only do so if we actually want to re-use
629 // *and* there is no node nearby (because nodes beat ways when re-using)
630 if(!ctrl && currentMouseNode == null) {
631 List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(mousePos);
632 for(WaySegment ws : wss)
633 mouseOnExistingWays.add(ws.way);
634 }
635
636 if (currentMouseNode != null) {
637 // user clicked on node
638 if (selection.isEmpty()) return;
639 currentMouseEastNorth = currentMouseNode.eastNorth;
640 mouseOnExistingNode = currentMouseNode;
641 } else {
642 // no node found in clicked area
643 currentMouseEastNorth = Main.map.mapView.getEastNorth(mousePos.x, mousePos.y);
644 }
645
646 for (OsmPrimitive p : selection) {
647 if (p instanceof Node) {
648 if (selectedNode != null) return;
649 selectedNode = (Node) p;
650 } else if (p instanceof Way) {
651 if (selectedWay != null) return;
652 selectedWay = (Way) p;
653 }
654 }
655
656 // the node from which we make a connection
657 currentBaseNode = null;
658 Node previousNode = null;
659
660 if (selectedNode == null) {
661 if (selectedWay == null)
662 return;
663 if (selectedWay.isFirstLastNode(lastUsedNode)) {
664 currentBaseNode = lastUsedNode;
665 if (lastUsedNode == selectedWay.nodes.get(selectedWay.nodes.size()-1) && selectedWay.nodes.size() > 1) {
666 previousNode = selectedWay.nodes.get(selectedWay.nodes.size()-2);
667 }
668 }
669 } else if (selectedWay == null) {
670 currentBaseNode = selectedNode;
671 } else {
672 if (selectedNode == selectedWay.nodes.get(0) || selectedNode == selectedWay.nodes.get(selectedWay.nodes.size()-1)) {
673 currentBaseNode = selectedNode;
674 }
675 }
676
677 if (currentBaseNode == null || currentBaseNode == currentMouseNode)
678 return; // Don't create zero length way segments.
679
680 // find out the distance, in metres, between the base point and the mouse cursor
681 LatLon mouseLatLon = Main.proj.eastNorth2latlon(currentMouseEastNorth);
682 distance = currentBaseNode.coor.greatCircleDistance(mouseLatLon);
683 double hdg = Math.toDegrees(currentBaseNode.coor.heading(mouseLatLon));
684 if (previousNode != null) {
685 angle = hdg - Math.toDegrees(previousNode.coor.heading(currentBaseNode.coor));
686 if (angle < 0) angle += 360;
687 }
688 Main.map.statusLine.setAngle(angle);
689 Main.map.statusLine.setHeading(hdg);
690 Main.map.statusLine.setDist(distance);
691 // Now done in redrawIfRequired()
692 //updateStatusLine();
693 }
694
695 /**
696 * Repaint on mouse exit so that the helper line goes away.
697 */
698 @Override public void mouseExited(MouseEvent e) {
699 if(!Main.map.mapView.isDrawableLayer())
700 return;
701 mousePos = e.getPoint();
702 Main.map.mapView.repaint();
703 }
704
705 /**
706 * @return If the node is the end of exactly one way, return this.
707 * <code>null</code> otherwise.
708 */
709 public static Way getWayForNode(Node n) {
710 Way way = null;
711 for (Way w : Main.ds.ways) {
712 if (w.deleted || w.incomplete || w.nodes.size() < 1) continue;
713 Node firstNode = w.nodes.get(0);
714 Node lastNode = w.nodes.get(w.nodes.size() - 1);
715 if ((firstNode == n || lastNode == n) && (firstNode != lastNode)) {
716 if (way != null)
717 return null;
718 way = w;
719 }
720 }
721 return way;
722 }
723
724 private static void pruneSuccsAndReverse(List<Integer> is) {
725 //if (is.size() < 2) return;
726
727 HashSet<Integer> is2 = new HashSet<Integer>();
728 for (int i : is) {
729 if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
730 is2.add(i);
731 }
732 }
733 is.clear();
734 is.addAll(is2);
735 Collections.sort(is);
736 Collections.reverse(is);
737 }
738
739 /**
740 * Adjusts the position of a node to lie on a segment (or a segment
741 * intersection).
742 *
743 * If one or more than two segments are passed, the node is adjusted
744 * to lie on the first segment that is passed.
745 *
746 * If two segments are passed, the node is adjusted to be at their
747 * intersection.
748 *
749 * No action is taken if no segments are passed.
750 *
751 * @param segs the segments to use as a reference when adjusting
752 * @param n the node to adjust
753 */
754 private static void adjustNode(Collection<Pair<Node,Node>> segs, Node n) {
755
756 switch (segs.size()) {
757 case 0:
758 return;
759 case 2:
760 // This computes the intersection between
761 // the two segments and adjusts the node position.
762 Iterator<Pair<Node,Node>> i = segs.iterator();
763 Pair<Node,Node> seg = i.next();
764 EastNorth A = seg.a.eastNorth;
765 EastNorth B = seg.b.eastNorth;
766 seg = i.next();
767 EastNorth C = seg.a.eastNorth;
768 EastNorth D = seg.b.eastNorth;
769
770 double u=det(B.east() - A.east(), B.north() - A.north(), C.east() - D.east(), C.north() - D.north());
771
772 // Check for parallel segments and do nothing if they are
773 // In practice this will probably only happen when a way has been duplicated
774
775 if (u == 0) return;
776
777 // q is a number between 0 and 1
778 // It is the point in the segment where the intersection occurs
779 // if the segment is scaled to lenght 1
780
781 double q = det(B.north() - C.north(), B.east() - C.east(), D.north() - C.north(), D.east() - C.east()) / u;
782 EastNorth intersection = new EastNorth(
783 B.east() + q * (A.east() - B.east()),
784 B.north() + q * (A.north() - B.north()));
785
786 int snapToIntersectionThreshold
787 = Main.pref.getInteger("edit.snap-intersection-threshold",10);
788
789 // only adjust to intersection if within snapToIntersectionThreshold pixel of mouse click; otherwise
790 // fall through to default action.
791 // (for semi-parallel lines, intersection might be miles away!)
792 if (Main.map.mapView.getPoint(n.eastNorth).distance(Main.map.mapView.getPoint(intersection)) < snapToIntersectionThreshold) {
793 n.eastNorth = intersection;
794 return;
795 }
796
797 default:
798 EastNorth P = n.eastNorth;
799 seg = segs.iterator().next();
800 A = seg.a.eastNorth;
801 B = seg.b.eastNorth;
802 double a = P.distanceSq(B);
803 double b = P.distanceSq(A);
804 double c = A.distanceSq(B);
805 q = (a - b + c) / (2*c);
806 n.eastNorth = new EastNorth(
807 B.east() + q * (A.east() - B.east()),
808 B.north() + q * (A.north() - B.north()));
809 }
810 }
811
812 // helper for adjustNode
813 static double det(double a, double b, double c, double d) {
814 return a * d - b * c;
815 }
816
817 public void paint(Graphics g, MapView mv) {
818 if (!drawHelperLine || wayIsFinished) return;
819
820 // sanity checks
821 if (Main.map.mapView == null) return;
822 if (mousePos == null) return;
823
824 // don't draw line if we don't know where from or where to
825 if (currentBaseNode == null || currentMouseEastNorth == null) return;
826
827 // don't draw line if mouse is outside window
828 if (!Main.map.mapView.getBounds().contains(mousePos)) return;
829
830 Graphics2D g2 = (Graphics2D) g;
831 g2.setColor(selectedColor);
832 g2.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
833 GeneralPath b = new GeneralPath();
834 Point p1=mv.getPoint(currentBaseNode.eastNorth);
835 Point p2=mv.getPoint(currentMouseEastNorth);
836
837 double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
838
839 b.moveTo(p1.x,p1.y); b.lineTo(p2.x, p2.y);
840
841 // if alt key is held ("start new way"), draw a little perpendicular line
842 if (alt) {
843 b.moveTo((int)(p1.x + 8*Math.cos(t+PHI)), (int)(p1.y + 8*Math.sin(t+PHI)));
844 b.lineTo((int)(p1.x + 8*Math.cos(t-PHI)), (int)(p1.y + 8*Math.sin(t-PHI)));
845 }
846
847 g2.draw(b);
848 g2.setStroke(new BasicStroke(1));
849 }
850
851 @Override public String getModeHelpText() {
852 String rv = "";
853 /*
854 * No modifiers: all (Connect, Node Re-Use, Auto-Weld)
855 * CTRL: disables node re-use, auto-weld
856 * Shift: disables node re-use
857 * ALT: disables connect
858 */
859
860 /*
861 * Status line text generation is split into two parts to keep it maintainable.
862 * First part looks at what will happen to the new node inserted on click and
863 * the second part will look if a connection is made or not.
864 *
865 * Note that this help text is not absolutely accurate as it doesn't catch any special
866 * cases (e.g. when preventing <---> ways). The only special that it catches is when
867 * a way is about to be finished.
868 *
869 * First check what happens to the new node.
870 */
871
872 // oldHighlights stores the current highlights. If this
873 // list is empty we can assume that we won't do any joins
874 if(ctrl || oldHighlights.isEmpty())
875 rv = tr("Create new node.");
876 else if(shift) {
877 // We already know oldHighlights is not empty, but shift is pressed.
878 // We can assume the new node will be joined into an existing way
879 rv = tr("Insert new node into {0} way(s).", oldHighlights.size());
880 } else {
881 // oldHighlights may store a node or way, check if it's a node
882 OsmPrimitive x = oldHighlights.iterator().next();
883 if(x instanceof Node)
884 rv = tr("Select node under cursor.");
885 else
886 rv = tr("Insert new node into {0} way(s).", oldHighlights.size());
887 }
888
889 /*
890 * Check whether a connection will be made
891 */
892 if (currentBaseNode != null && !wayIsFinished) {
893 if(alt)
894 rv += " " + tr("Start new way from last node.");
895 else
896 rv += " " + tr("Continue way from last node.");
897 }
898
899 Node n = mouseOnExistingNode;
900 /*
901 * Handle special case: Highlighted node == selected node => finish drawing
902 */
903
904 if(n != null && Main.ds.getSelectedNodes().contains(n)) {
905 if(wayIsFinished)
906 rv = tr("Select node under cursor.");
907 else
908 rv = tr("Finish drawing.");
909 }
910
911 /*
912 * Handle special case: Self-Overlapping or closing way
913 */
914 if(Main.ds.getSelectedWays().size() > 0 && !wayIsFinished && !alt) {
915 Way w = (Way) Main.ds.getSelectedWays().iterator().next();
916 for(Node m : w.nodes) {
917 if(m.equals(mouseOnExistingNode) || mouseOnExistingWays.contains(w)) {
918 rv += " " + tr("Finish drawing.");
919 break;
920 }
921 }
922 }
923
924 return rv;
925 }
926
927 @Override public boolean layerIsSupported(Layer l) {
928 return l instanceof OsmDataLayer;
929 }
930}
Note: See TracBrowser for help on using the repository browser.