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

Last change on this file since 6289 was 6289, checked in by Don-vip, 11 years ago

Sonar/Findbugs - fix various problems

  • Property svn:eol-style set to native
File size: 67.4 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.actions.mapmode;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7import static org.openstreetmap.josm.tools.I18n.trn;
8
9import java.awt.AWTEvent;
10import java.awt.BasicStroke;
11import java.awt.Color;
12import java.awt.Component;
13import java.awt.Cursor;
14import java.awt.Graphics2D;
15import java.awt.KeyboardFocusManager;
16import java.awt.Point;
17import java.awt.Stroke;
18import java.awt.Toolkit;
19import java.awt.event.AWTEventListener;
20import java.awt.event.ActionEvent;
21import java.awt.event.ActionListener;
22import java.awt.event.InputEvent;
23import java.awt.event.KeyEvent;
24import java.awt.event.MouseEvent;
25import java.awt.event.MouseListener;
26import java.awt.geom.GeneralPath;
27import java.util.ArrayList;
28import java.util.Arrays;
29import java.util.Collection;
30import java.util.Collections;
31import java.util.HashMap;
32import java.util.HashSet;
33import java.util.Iterator;
34import java.util.LinkedList;
35import java.util.List;
36import java.util.Map;
37import java.util.Set;
38import java.util.TreeSet;
39
40import javax.swing.AbstractAction;
41import javax.swing.JCheckBoxMenuItem;
42import javax.swing.JFrame;
43import javax.swing.JMenuItem;
44import javax.swing.JOptionPane;
45import javax.swing.JPopupMenu;
46import javax.swing.SwingUtilities;
47import javax.swing.Timer;
48
49import org.openstreetmap.josm.Main;
50import org.openstreetmap.josm.actions.JosmAction;
51import org.openstreetmap.josm.command.AddCommand;
52import org.openstreetmap.josm.command.ChangeCommand;
53import org.openstreetmap.josm.command.Command;
54import org.openstreetmap.josm.command.SequenceCommand;
55import org.openstreetmap.josm.data.Bounds;
56import org.openstreetmap.josm.data.SelectionChangedListener;
57import org.openstreetmap.josm.data.coor.EastNorth;
58import org.openstreetmap.josm.data.coor.LatLon;
59import org.openstreetmap.josm.data.osm.DataSet;
60import org.openstreetmap.josm.data.osm.Node;
61import org.openstreetmap.josm.data.osm.OsmPrimitive;
62import org.openstreetmap.josm.data.osm.Way;
63import org.openstreetmap.josm.data.osm.WaySegment;
64import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
65import org.openstreetmap.josm.gui.MainMenu;
66import org.openstreetmap.josm.gui.MapFrame;
67import org.openstreetmap.josm.gui.MapView;
68import org.openstreetmap.josm.gui.NavigatableComponent;
69import org.openstreetmap.josm.gui.layer.Layer;
70import org.openstreetmap.josm.gui.layer.MapViewPaintable;
71import org.openstreetmap.josm.gui.layer.OsmDataLayer;
72import org.openstreetmap.josm.gui.util.GuiHelper;
73import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
74import org.openstreetmap.josm.tools.Geometry;
75import org.openstreetmap.josm.tools.ImageProvider;
76import org.openstreetmap.josm.tools.Pair;
77import org.openstreetmap.josm.tools.Shortcut;
78import org.openstreetmap.josm.tools.Utils;
79
80/**
81 * Mapmode to add nodes, create and extend ways.
82 */
83public class DrawAction extends MapMode implements MapViewPaintable, SelectionChangedListener, AWTEventListener {
84 final private Cursor cursorJoinNode;
85 final private Cursor cursorJoinWay;
86
87 private Node lastUsedNode = null;
88 private double PHI=Math.toRadians(90);
89 private double toleranceMultiplier;
90
91 private Node mouseOnExistingNode;
92 private Set<Way> mouseOnExistingWays = new HashSet<Way>();
93 // old highlights store which primitives are currently highlighted. This
94 // is true, even if target highlighting is disabled since the status bar
95 // derives its information from this list as well.
96 private Set<OsmPrimitive> oldHighlights = new HashSet<OsmPrimitive>();
97 // new highlights contains a list of primitives that should be highlighted
98 // but haven’t been so far. The idea is to compare old and new and only
99 // repaint if there are changes.
100 private Set<OsmPrimitive> newHighlights = new HashSet<OsmPrimitive>();
101 private boolean drawHelperLine;
102 private boolean wayIsFinished = false;
103 private boolean drawTargetHighlight;
104 private Point mousePos;
105 private Point oldMousePos;
106 private Color rubberLineColor;
107
108 private Node currentBaseNode;
109 private Node previousNode;
110 private EastNorth currentMouseEastNorth;
111
112 private final SnapHelper snapHelper = new SnapHelper();
113
114 private Shortcut backspaceShortcut;
115 private BackSpaceAction backspaceAction;
116 private final Shortcut snappingShortcut;
117
118 private final SnapChangeAction snapChangeAction;
119 private final JCheckBoxMenuItem snapCheckboxMenuItem;
120 private boolean useRepeatedShortcut;
121 private Stroke rubberLineStroke;
122 private static final BasicStroke BASIC_STROKE = new BasicStroke(1);
123
124 public DrawAction(MapFrame mapFrame) {
125 super(tr("Draw"), "node/autonode", tr("Draw nodes"),
126 Shortcut.registerShortcut("mapmode:draw", tr("Mode: {0}", tr("Draw")), KeyEvent.VK_A, Shortcut.DIRECT),
127 mapFrame, ImageProvider.getCursor("crosshair", null));
128
129 snappingShortcut = Shortcut.registerShortcut("mapmode:drawanglesnapping",
130 tr("Mode: Draw Angle snapping"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
131 snapChangeAction = new SnapChangeAction();
132 snapCheckboxMenuItem = addMenuItem();
133 snapHelper.setMenuCheckBox(snapCheckboxMenuItem);
134 backspaceShortcut = Shortcut.registerShortcut("mapmode:backspace",
135 tr("Backspace in Add mode"), KeyEvent.VK_BACK_SPACE, Shortcut.DIRECT);
136 backspaceAction = new BackSpaceAction();
137 cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode");
138 cursorJoinWay = ImageProvider.getCursor("crosshair", "joinway");
139 }
140
141 private JCheckBoxMenuItem addMenuItem() {
142 int n=Main.main.menu.editMenu.getItemCount();
143 for (int i=n-1;i>0;i--) {
144 JMenuItem item = Main.main.menu.editMenu.getItem(i);
145 if (item!=null && item.getAction() !=null && item.getAction() instanceof SnapChangeAction) {
146 Main.main.menu.editMenu.remove(i);
147 }
148 }
149 return MainMenu.addWithCheckbox(Main.main.menu.editMenu, snapChangeAction, MainMenu.WINDOW_MENU_GROUP.VOLATILE);
150 }
151
152 /**
153 * Checks if a map redraw is required and does so if needed. Also updates the status bar
154 */
155 private boolean redrawIfRequired() {
156 updateStatusLine();
157 // repaint required if the helper line is active.
158 boolean needsRepaint = drawHelperLine && !wayIsFinished;
159 if(drawTargetHighlight) {
160 // move newHighlights to oldHighlights; only update changed primitives
161 for(OsmPrimitive x : newHighlights) {
162 if(oldHighlights.contains(x)) {
163 continue;
164 }
165 x.setHighlighted(true);
166 needsRepaint = true;
167 }
168 oldHighlights.removeAll(newHighlights);
169 for(OsmPrimitive x : oldHighlights) {
170 x.setHighlighted(false);
171 needsRepaint = true;
172 }
173 }
174 // required in order to print correct help text
175 oldHighlights = newHighlights;
176
177 if (!needsRepaint && !drawTargetHighlight)
178 return false;
179
180 // update selection to reflect which way being modified
181 if (currentBaseNode != null && getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty()) {
182 Way continueFrom = getWayForNode(currentBaseNode);
183 if (alt && continueFrom != null && (!currentBaseNode.isSelected() || continueFrom.isSelected())) {
184 getCurrentDataSet().beginUpdate(); // to prevent the selection listener to screw around with the state
185 getCurrentDataSet().addSelected(currentBaseNode);
186 getCurrentDataSet().clearSelection(continueFrom);
187 getCurrentDataSet().endUpdate();
188 needsRepaint = true;
189 } else if (!alt && continueFrom != null && !continueFrom.isSelected()) {
190 getCurrentDataSet().addSelected(continueFrom);
191 needsRepaint = true;
192 }
193 }
194
195 if(needsRepaint) {
196 Main.map.mapView.repaint();
197 }
198 return needsRepaint;
199 }
200
201 @Override
202 public void enterMode() {
203 if (!isEnabled())
204 return;
205 super.enterMode();
206
207 rubberLineColor = Main.pref.getColor(marktr("helper line"), null);
208 if (rubberLineColor == null) rubberLineColor = PaintColors.SELECTED.get();
209
210 rubberLineStroke = GuiHelper.getCustomizedStroke(Main.pref.get("draw.stroke.helper-line","3"));
211 drawHelperLine = Main.pref.getBoolean("draw.helper-line", true);
212 drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true);
213
214 // determine if selection is suitable to continue drawing. If it
215 // isn't, set wayIsFinished to true to avoid superfluous repaints.
216 determineCurrentBaseNodeAndPreviousNode(getCurrentDataSet().getSelected());
217 wayIsFinished = currentBaseNode == null;
218
219 toleranceMultiplier = 0.01 * NavigatableComponent.PROP_SNAP_DISTANCE.get();
220
221 snapHelper.init();
222 snapCheckboxMenuItem.getAction().setEnabled(true);
223
224 timer = new Timer(0, new ActionListener() {
225 @Override
226 public void actionPerformed(ActionEvent ae) {
227 timer.stop();
228 if (set.remove(releaseEvent.getKeyCode())) {
229 doKeyReleaseEvent(releaseEvent);
230 }
231 }
232
233 });
234 Main.map.statusLine.getAnglePanel().addMouseListener(snapHelper.anglePopupListener);
235 Main.registerActionShortcut(backspaceAction, backspaceShortcut);
236
237 Main.map.mapView.addMouseListener(this);
238 Main.map.mapView.addMouseMotionListener(this);
239 Main.map.mapView.addTemporaryLayer(this);
240 DataSet.addSelectionListener(this);
241
242 try {
243 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
244 } catch (SecurityException ex) {
245 }
246 // would like to but haven't got mouse position yet:
247 // computeHelperLine(false, false, false);
248 }
249
250 @Override
251 public void exitMode() {
252 super.exitMode();
253 Main.map.mapView.removeMouseListener(this);
254 Main.map.mapView.removeMouseMotionListener(this);
255 Main.map.mapView.removeTemporaryLayer(this);
256 DataSet.removeSelectionListener(this);
257 Main.unregisterActionShortcut(backspaceAction, backspaceShortcut);
258 snapHelper.unsetFixedMode();
259 snapCheckboxMenuItem.getAction().setEnabled(false);
260
261 Main.map.statusLine.getAnglePanel().removeMouseListener(snapHelper.anglePopupListener);
262 Main.map.statusLine.activateAnglePanel(false);
263
264 removeHighlighting();
265 try {
266 Toolkit.getDefaultToolkit().removeAWTEventListener(this);
267 } catch (SecurityException ex) {
268 }
269
270 // when exiting we let everybody know about the currently selected
271 // primitives
272 //
273 DataSet ds = getCurrentDataSet();
274 if(ds != null) {
275 ds.fireSelectionChanged();
276 }
277 }
278
279 /**
280 * redraw to (possibly) get rid of helper line if selection changes.
281 */
282 @Override
283 public void eventDispatched(AWTEvent event) {
284 if(Main.map == null || Main.map.mapView == null || !Main.map.mapView.isActiveLayerDrawable())
285 return;
286 if (event instanceof KeyEvent) {
287 KeyEvent e = (KeyEvent) event;
288 if (snappingShortcut.isEvent(e) || (useRepeatedShortcut && getShortcut().isEvent(e))) {
289 Component focused = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
290 if (SwingUtilities.getWindowAncestor(focused) instanceof JFrame) {
291 processKeyEvent(e);
292 }
293 }
294 } // toggle angle snapping
295 updateKeyModifiers((InputEvent) event);
296 computeHelperLine();
297 addHighlighting();
298 }
299
300 // events for crossplatform key holding processing
301 // thanks to http://www.arco.in-berlin.de/keyevent.html
302 private final TreeSet<Integer> set = new TreeSet<Integer>();
303 private KeyEvent releaseEvent;
304 private Timer timer;
305 void processKeyEvent(KeyEvent e) {
306 if (!snappingShortcut.isEvent(e) && !(useRepeatedShortcut && getShortcut().isEvent(e)))
307 return;
308
309 if (e.getID() == KeyEvent.KEY_PRESSED) {
310 if (timer.isRunning()) {
311 timer.stop();
312 } else if (set.add((e.getKeyCode()))) {
313 doKeyPressEvent(e);
314 }
315 } else if (e.getID() == KeyEvent.KEY_RELEASED) {
316 if (timer.isRunning()) {
317 timer.stop();
318 if (set.remove(e.getKeyCode())) {
319 doKeyReleaseEvent(e);
320 }
321 } else {
322 releaseEvent = e;
323 timer.restart();
324 }
325 }
326 }
327
328 private void doKeyPressEvent(KeyEvent e) {
329 snapHelper.setFixedMode();
330 computeHelperLine();
331 redrawIfRequired();
332 }
333 private void doKeyReleaseEvent(KeyEvent e) {
334 snapHelper.unFixOrTurnOff();
335 computeHelperLine();
336 redrawIfRequired();
337 }
338
339 /**
340 * redraw to (possibly) get rid of helper line if selection changes.
341 */
342 @Override
343 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
344 if(!Main.map.mapView.isActiveLayerDrawable())
345 return;
346 computeHelperLine();
347 addHighlighting();
348 }
349
350 private void tryAgain(MouseEvent e) {
351 getCurrentDataSet().setSelected();
352 mouseReleased(e);
353 }
354
355 /**
356 * This function should be called when the user wishes to finish his current draw action.
357 * If Potlatch Style is enabled, it will switch to select tool, otherwise simply disable
358 * the helper line until the user chooses to draw something else.
359 */
360 private void finishDrawing() {
361 // let everybody else know about the current selection
362 //
363 Main.main.getCurrentDataSet().fireSelectionChanged();
364 lastUsedNode = null;
365 wayIsFinished = true;
366 Main.map.selectSelectTool(true);
367 snapHelper.noSnapNow();
368
369 // Redraw to remove the helper line stub
370 computeHelperLine();
371 removeHighlighting();
372 }
373
374 private Point rightClickPressPos;
375
376 @Override
377 public void mousePressed(MouseEvent e) {
378 if (e.getButton() == MouseEvent.BUTTON3) {
379 rightClickPressPos = e.getPoint();
380 }
381 }
382
383 /**
384 * If user clicked with the left button, add a node at the current mouse
385 * position.
386 *
387 * If in nodeway mode, insert the node into the way.
388 */
389 @Override public void mouseReleased(MouseEvent e) {
390 if (e.getButton() == MouseEvent.BUTTON3) {
391 Point curMousePos = e.getPoint();
392 if (curMousePos.equals(rightClickPressPos)) {
393 tryToSetBaseSegmentForAngleSnap();
394 }
395 return;
396 }
397 if (e.getButton() != MouseEvent.BUTTON1)
398 return;
399 if(!Main.map.mapView.isActiveLayerDrawable())
400 return;
401 // request focus in order to enable the expected keyboard shortcuts
402 //
403 Main.map.mapView.requestFocus();
404
405 if(e.getClickCount() > 1 && mousePos != null && mousePos.equals(oldMousePos)) {
406 // A double click equals "user clicked last node again, finish way"
407 // Change draw tool only if mouse position is nearly the same, as
408 // otherwise fast clicks will count as a double click
409 finishDrawing();
410 return;
411 }
412 oldMousePos = mousePos;
413
414 // we copy ctrl/alt/shift from the event just in case our global
415 // AWTEvent didn't make it through the security manager. Unclear
416 // if that can ever happen but better be safe.
417 updateKeyModifiers(e);
418 mousePos = e.getPoint();
419
420 DataSet ds = getCurrentDataSet();
421 Collection<OsmPrimitive> selection = new ArrayList<OsmPrimitive>(ds.getSelected());
422 Collection<Command> cmds = new LinkedList<Command>();
423 Collection<OsmPrimitive> newSelection = new LinkedList<OsmPrimitive>(ds.getSelected());
424
425 ArrayList<Way> reuseWays = new ArrayList<Way>(),
426 replacedWays = new ArrayList<Way>();
427 boolean newNode = false;
428 Node n = null;
429
430 n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
431 if (ctrl) {
432 Iterator<Way> it = getCurrentDataSet().getSelectedWays().iterator();
433 if (it.hasNext()) {
434 // ctrl-click on node of selected way = reuse node despite of ctrl
435 if (!it.next().containsNode(n)) n = null;
436 } else {
437 n=null; // ctrl-click + no selected way = new node
438 }
439 }
440
441 if (n != null && !snapHelper.isActive()) {
442 // user clicked on node
443 if (selection.isEmpty() || wayIsFinished) {
444 // select the clicked node and do nothing else
445 // (this is just a convenience option so that people don't
446 // have to switch modes)
447
448 getCurrentDataSet().setSelected(n);
449 // If we extend/continue an existing way, select it already now to make it obvious
450 Way continueFrom = getWayForNode(n);
451 if (continueFrom != null) {
452 getCurrentDataSet().addSelected(continueFrom);
453 }
454
455 // The user explicitly selected a node, so let him continue drawing
456 wayIsFinished = false;
457 return;
458 }
459 } else {
460 EastNorth newEN;
461 if (n!=null) {
462 EastNorth foundPoint = n.getEastNorth();
463 // project found node to snapping line
464 newEN = snapHelper.getSnapPoint(foundPoint);
465 // do not add new node if there is some node within snapping distance
466 double tolerance = Main.map.mapView.getDist100Pixel() * toleranceMultiplier;
467 if (foundPoint.distance(newEN) > tolerance) {
468 n = new Node(newEN); // point != projected, so we create new node
469 newNode = true;
470 }
471 } else { // n==null, no node found in clicked area
472 EastNorth mouseEN = Main.map.mapView.getEastNorth(e.getX(), e.getY());
473 newEN = snapHelper.isSnapOn() ? snapHelper.getSnapPoint(mouseEN) : mouseEN;
474 n = new Node(newEN); //create node at clicked point
475 newNode = true;
476 }
477 snapHelper.unsetFixedMode();
478 }
479
480 if (newNode) {
481 if (n.getCoor().isOutSideWorld()) {
482 JOptionPane.showMessageDialog(
483 Main.parent,
484 tr("Cannot add a node outside of the world."),
485 tr("Warning"),
486 JOptionPane.WARNING_MESSAGE
487 );
488 return;
489 }
490 cmds.add(new AddCommand(n));
491
492 if (!ctrl) {
493 // Insert the node into all the nearby way segments
494 List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(
495 Main.map.mapView.getPoint(n), OsmPrimitive.isSelectablePredicate);
496 if (snapHelper.isActive()) {
497 tryToMoveNodeOnIntersection(wss,n);
498 }
499 insertNodeIntoAllNearbySegments(wss, n, newSelection, cmds, replacedWays, reuseWays);
500 }
501 }
502 // now "n" is newly created or reused node that shoud be added to some way
503
504 // This part decides whether or not a "segment" (i.e. a connection) is made to an
505 // existing node.
506
507 // For a connection to be made, the user must either have a node selected (connection
508 // is made to that node), or he must have a way selected *and* one of the endpoints
509 // of that way must be the last used node (connection is made to last used node), or
510 // he must have a way and a node selected (connection is made to the selected node).
511
512 // If the above does not apply, the selection is cleared and a new try is started
513
514 boolean extendedWay = false;
515 boolean wayIsFinishedTemp = wayIsFinished;
516 wayIsFinished = false;
517
518 // don't draw lines if shift is held
519 if (!selection.isEmpty() && !shift) {
520 Node selectedNode = null;
521 Way selectedWay = null;
522
523 for (OsmPrimitive p : selection) {
524 if (p instanceof Node) {
525 if (selectedNode != null) {
526 // Too many nodes selected to do something useful
527 tryAgain(e);
528 return;
529 }
530 selectedNode = (Node) p;
531 } else if (p instanceof Way) {
532 if (selectedWay != null) {
533 // Too many ways selected to do something useful
534 tryAgain(e);
535 return;
536 }
537 selectedWay = (Way) p;
538 }
539 }
540
541 // the node from which we make a connection
542 Node n0 = findNodeToContinueFrom(selectedNode, selectedWay);
543 // We have a selection but it isn't suitable. Try again.
544 if(n0 == null) {
545 tryAgain(e);
546 return;
547 }
548 if(!wayIsFinishedTemp){
549 if(isSelfContainedWay(selectedWay, n0, n))
550 return;
551
552 // User clicked last node again, finish way
553 if(n0 == n) {
554 finishDrawing();
555 return;
556 }
557
558 // Ok we know now that we'll insert a line segment, but will it connect to an
559 // existing way or make a new way of its own? The "alt" modifier means that the
560 // user wants a new way.
561 Way way = alt ? null : (selectedWay != null) ? selectedWay : getWayForNode(n0);
562 Way wayToSelect;
563
564 // Don't allow creation of self-overlapping ways
565 if(way != null) {
566 int nodeCount=0;
567 for (Node p : way.getNodes())
568 if(p.equals(n0)) {
569 nodeCount++;
570 }
571 if(nodeCount > 1) {
572 way = null;
573 }
574 }
575
576 if (way == null) {
577 way = new Way();
578 way.addNode(n0);
579 cmds.add(new AddCommand(way));
580 wayToSelect = way;
581 } else {
582 int i;
583 if ((i = replacedWays.indexOf(way)) != -1) {
584 way = reuseWays.get(i);
585 wayToSelect = way;
586 } else {
587 wayToSelect = way;
588 Way wnew = new Way(way);
589 cmds.add(new ChangeCommand(way, wnew));
590 way = wnew;
591 }
592 }
593
594 // Connected to a node that's already in the way
595 if(way.containsNode(n)) {
596 wayIsFinished = true;
597 selection.clear();
598 }
599
600 // Add new node to way
601 if (way.getNode(way.getNodesCount() - 1) == n0) {
602 way.addNode(n);
603 } else {
604 way.addNode(0, n);
605 }
606
607 extendedWay = true;
608 newSelection.clear();
609 newSelection.add(wayToSelect);
610 }
611 }
612
613 String title;
614 if (!extendedWay) {
615 if (!newNode)
616 return; // We didn't do anything.
617 else if (reuseWays.isEmpty()) {
618 title = tr("Add node");
619 } else {
620 title = tr("Add node into way");
621 for (Way w : reuseWays) {
622 newSelection.remove(w);
623 }
624 }
625 newSelection.clear();
626 newSelection.add(n);
627 } else if (!newNode) {
628 title = tr("Connect existing way to node");
629 } else if (reuseWays.isEmpty()) {
630 title = tr("Add a new node to an existing way");
631 } else {
632 title = tr("Add node into way and connect");
633 }
634
635 Command c = new SequenceCommand(title, cmds);
636
637 Main.main.undoRedo.add(c);
638 if(!wayIsFinished) {
639 lastUsedNode = n;
640 }
641
642 getCurrentDataSet().setSelected(newSelection);
643
644 // "viewport following" mode for tracing long features
645 // from aerial imagery or GPS tracks.
646 if (n != null && Main.map.mapView.viewportFollowing) {
647 Main.map.mapView.smoothScrollTo(n.getEastNorth());
648 }
649 computeHelperLine();
650 removeHighlighting();
651 }
652
653 private void insertNodeIntoAllNearbySegments(List<WaySegment> wss, Node n, Collection<OsmPrimitive> newSelection, Collection<Command> cmds, ArrayList<Way> replacedWays, ArrayList<Way> reuseWays) {
654 Map<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>();
655 for (WaySegment ws : wss) {
656 List<Integer> is;
657 if (insertPoints.containsKey(ws.way)) {
658 is = insertPoints.get(ws.way);
659 } else {
660 is = new ArrayList<Integer>();
661 insertPoints.put(ws.way, is);
662 }
663
664 is.add(ws.lowerIndex);
665 }
666
667 Set<Pair<Node,Node>> segSet = new HashSet<Pair<Node,Node>>();
668
669 for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
670 Way w = insertPoint.getKey();
671 List<Integer> is = insertPoint.getValue();
672
673 Way wnew = new Way(w);
674
675 pruneSuccsAndReverse(is);
676 for (int i : is) {
677 segSet.add(Pair.sort(new Pair<Node,Node>(w.getNode(i), w.getNode(i+1))));
678 }
679 for (int i : is) {
680 wnew.addNode(i + 1, n);
681 }
682
683 // If ALT is pressed, a new way should be created and that new way should get
684 // selected. This works everytime unless the ways the nodes get inserted into
685 // are already selected. This is the case when creating a self-overlapping way
686 // but pressing ALT prevents this. Therefore we must de-select the way manually
687 // here so /only/ the new way will be selected after this method finishes.
688 if(alt) {
689 newSelection.add(insertPoint.getKey());
690 }
691
692 cmds.add(new ChangeCommand(insertPoint.getKey(), wnew));
693 replacedWays.add(insertPoint.getKey());
694 reuseWays.add(wnew);
695 }
696
697 adjustNode(segSet, n);
698 }
699
700 /**
701 * Prevent creation of ways that look like this: &lt;----&gt;
702 * This happens if users want to draw a no-exit-sideway from the main way like this:
703 * ^
704 * |&lt;----&gt;
705 * |
706 * The solution isn't ideal because the main way will end in the side way, which is bad for
707 * navigation software ("drive straight on") but at least easier to fix. Maybe users will fix
708 * it on their own, too. At least it's better than producing an error.
709 *
710 * @param selectedWay the way to check
711 * @param currentNode the current node (i.e. the one the connection will be made from)
712 * @param targetNode the target node (i.e. the one the connection will be made to)
713 * @return {@code true} if this would create a selfcontaining way, {@code false} otherwise.
714 */
715 private boolean isSelfContainedWay(Way selectedWay, Node currentNode, Node targetNode) {
716 if(selectedWay != null) {
717 int posn0 = selectedWay.getNodes().indexOf(currentNode);
718 if( posn0 != -1 && // n0 is part of way
719 (posn0 >= 1 && targetNode.equals(selectedWay.getNode(posn0-1))) || // previous node
720 (posn0 < selectedWay.getNodesCount()-1) && targetNode.equals(selectedWay.getNode(posn0+1))) { // next node
721 getCurrentDataSet().setSelected(targetNode);
722 lastUsedNode = targetNode;
723 return true;
724 }
725 }
726
727 return false;
728 }
729
730 /**
731 * Finds a node to continue drawing from. Decision is based upon given node and way.
732 * @param selectedNode Currently selected node, may be null
733 * @param selectedWay Currently selected way, may be null
734 * @return Node if a suitable node is found, null otherwise
735 */
736 private Node findNodeToContinueFrom(Node selectedNode, Way selectedWay) {
737 // No nodes or ways have been selected, this occurs when a relation
738 // has been selected or the selection is empty
739 if(selectedNode == null && selectedWay == null)
740 return null;
741
742 if (selectedNode == null) {
743 if (selectedWay.isFirstLastNode(lastUsedNode))
744 return lastUsedNode;
745
746 // We have a way selected, but no suitable node to continue from. Start anew.
747 return null;
748 }
749
750 if (selectedWay == null)
751 return selectedNode;
752
753 if (selectedWay.isFirstLastNode(selectedNode))
754 return selectedNode;
755
756 // We have a way and node selected, but it's not at the start/end of the way. Start anew.
757 return null;
758 }
759
760 @Override
761 public void mouseDragged(MouseEvent e) {
762 mouseMoved(e);
763 }
764
765 @Override
766 public void mouseMoved(MouseEvent e) {
767 if(!Main.map.mapView.isActiveLayerDrawable())
768 return;
769
770 // we copy ctrl/alt/shift from the event just in case our global
771 // AWTEvent didn't make it through the security manager. Unclear
772 // if that can ever happen but better be safe.
773 updateKeyModifiers(e);
774 mousePos = e.getPoint();
775 if (snapHelper.isSnapOn() && ctrl)
776 tryToSetBaseSegmentForAngleSnap();
777
778 computeHelperLine();
779 addHighlighting();
780 }
781
782 /**
783 * This method is used to detect segment under mouse and use it as reference for angle snapping
784 */
785 private void tryToSetBaseSegmentForAngleSnap() {
786 WaySegment seg = Main.map.mapView.getNearestWaySegment(mousePos, OsmPrimitive.isSelectablePredicate);
787 if (seg!=null) {
788 snapHelper.setBaseSegment(seg);
789 }
790 }
791
792 /**
793 * This method prepares data required for painting the "helper line" from
794 * the last used position to the mouse cursor. It duplicates some code from
795 * mouseReleased() (FIXME).
796 */
797 private void computeHelperLine() {
798 MapView mv = Main.map.mapView;
799 if (mousePos == null) {
800 // Don't draw the line.
801 currentMouseEastNorth = null;
802 currentBaseNode = null;
803 return;
804 }
805
806 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
807
808 Node currentMouseNode = null;
809 mouseOnExistingNode = null;
810 mouseOnExistingWays = new HashSet<Way>();
811
812 showStatusInfo(-1, -1, -1, snapHelper.isSnapOn());
813
814 if (!ctrl && mousePos != null) {
815 currentMouseNode = mv.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
816 }
817
818 // We need this for highlighting and we'll only do so if we actually want to re-use
819 // *and* there is no node nearby (because nodes beat ways when re-using)
820 if(!ctrl && currentMouseNode == null) {
821 List<WaySegment> wss = mv.getNearestWaySegments(mousePos, OsmPrimitive.isSelectablePredicate);
822 for(WaySegment ws : wss) {
823 mouseOnExistingWays.add(ws.way);
824 }
825 }
826
827 if (currentMouseNode != null) {
828 // user clicked on node
829 if (selection.isEmpty()) return;
830 currentMouseEastNorth = currentMouseNode.getEastNorth();
831 mouseOnExistingNode = currentMouseNode;
832 } else {
833 // no node found in clicked area
834 currentMouseEastNorth = mv.getEastNorth(mousePos.x, mousePos.y);
835 }
836
837 determineCurrentBaseNodeAndPreviousNode(selection);
838 if (previousNode == null) {
839 snapHelper.noSnapNow();
840 }
841
842 if (currentBaseNode == null || currentBaseNode == currentMouseNode)
843 return; // Don't create zero length way segments.
844
845
846 double curHdg = Math.toDegrees(currentBaseNode.getEastNorth()
847 .heading(currentMouseEastNorth));
848 double baseHdg=-1;
849 if (previousNode != null) {
850 baseHdg = Math.toDegrees(previousNode.getEastNorth()
851 .heading(currentBaseNode.getEastNorth()));
852 }
853
854 snapHelper.checkAngleSnapping(currentMouseEastNorth,baseHdg, curHdg);
855
856 // status bar was filled by snapHelper
857 }
858
859 private void showStatusInfo(double angle, double hdg, double distance, boolean activeFlag) {
860 Main.map.statusLine.setAngle(angle);
861 Main.map.statusLine.activateAnglePanel(activeFlag);
862 Main.map.statusLine.setHeading(hdg);
863 Main.map.statusLine.setDist(distance);
864 }
865
866 /**
867 * Helper function that sets fields currentBaseNode and previousNode
868 * @param selection
869 * uses also lastUsedNode field
870 */
871 private void determineCurrentBaseNodeAndPreviousNode(Collection<OsmPrimitive> selection) {
872 Node selectedNode = null;
873 Way selectedWay = null;
874 for (OsmPrimitive p : selection) {
875 if (p instanceof Node) {
876 if (selectedNode != null)
877 return;
878 selectedNode = (Node) p;
879 } else if (p instanceof Way) {
880 if (selectedWay != null)
881 return;
882 selectedWay = (Way) p;
883 }
884 }
885 // we are here, if not more than 1 way or node is selected,
886
887 // the node from which we make a connection
888 currentBaseNode = null;
889 previousNode = null;
890
891 // Try to find an open way to measure angle from it. The way is not to be continued!
892 // warning: may result in changes of currentBaseNode and previousNode
893 // please remove if bugs arise
894 if (selectedWay == null && selectedNode != null) {
895 for (OsmPrimitive p: selectedNode.getReferrers()) {
896 if (p.isUsable() && p instanceof Way && ((Way) p).isFirstLastNode(selectedNode)) {
897 if (selectedWay!=null) { // two uncontinued ways, nothing to take as reference
898 selectedWay=null;
899 break;
900 } else {
901 // set us ~continue this way (measure angle from it)
902 selectedWay = (Way) p;
903 }
904 }
905 }
906 }
907
908 if (selectedNode == null) {
909 if (selectedWay == null)
910 return;
911 continueWayFromNode(selectedWay, lastUsedNode);
912 } else if (selectedWay == null) {
913 currentBaseNode = selectedNode;
914 } else if (!selectedWay.isDeleted()) { // fix #7118
915 continueWayFromNode(selectedWay, selectedNode);
916 }
917 }
918
919 /**
920 * if one of the ends of @param way is given @param node ,
921 * then set currentBaseNode = node and previousNode = adjacent node of way
922 */
923 private void continueWayFromNode(Way way, Node node) {
924 int n = way.getNodesCount();
925 if (node == way.firstNode()){
926 currentBaseNode = node;
927 if (n>1) previousNode = way.getNode(1);
928 } else if (node == way.lastNode()) {
929 currentBaseNode = node;
930 if (n>1) previousNode = way.getNode(n-2);
931 }
932 }
933
934 /**
935 * Repaint on mouse exit so that the helper line goes away.
936 */
937 @Override public void mouseExited(MouseEvent e) {
938 if(!Main.map.mapView.isActiveLayerDrawable())
939 return;
940 mousePos = e.getPoint();
941 snapHelper.noSnapNow();
942 boolean repaintIssued = removeHighlighting();
943 // force repaint in case snapHelper needs one. If removeHighlighting
944 // caused one already, don’t do it again.
945 if(!repaintIssued) {
946 Main.map.mapView.repaint();
947 }
948 }
949
950 /**
951 * @return If the node is the end of exactly one way, return this.
952 * <code>null</code> otherwise.
953 */
954 public static Way getWayForNode(Node n) {
955 Way way = null;
956 for (Way w : Utils.filteredCollection(n.getReferrers(), Way.class)) {
957 if (!w.isUsable() || w.getNodesCount() < 1) {
958 continue;
959 }
960 Node firstNode = w.getNode(0);
961 Node lastNode = w.getNode(w.getNodesCount() - 1);
962 if ((firstNode == n || lastNode == n) && (firstNode != lastNode)) {
963 if (way != null)
964 return null;
965 way = w;
966 }
967 }
968 return way;
969 }
970
971 public Node getCurrentBaseNode() {
972 return currentBaseNode;
973 }
974
975 private static void pruneSuccsAndReverse(List<Integer> is) {
976 HashSet<Integer> is2 = new HashSet<Integer>();
977 for (int i : is) {
978 if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
979 is2.add(i);
980 }
981 }
982 is.clear();
983 is.addAll(is2);
984 Collections.sort(is);
985 Collections.reverse(is);
986 }
987
988 /**
989 * Adjusts the position of a node to lie on a segment (or a segment
990 * intersection).
991 *
992 * If one or more than two segments are passed, the node is adjusted
993 * to lie on the first segment that is passed.
994 *
995 * If two segments are passed, the node is adjusted to be at their
996 * intersection.
997 *
998 * No action is taken if no segments are passed.
999 *
1000 * @param segs the segments to use as a reference when adjusting
1001 * @param n the node to adjust
1002 */
1003 private static void adjustNode(Collection<Pair<Node,Node>> segs, Node n) {
1004
1005 switch (segs.size()) {
1006 case 0:
1007 return;
1008 case 2:
1009 // This computes the intersection between
1010 // the two segments and adjusts the node position.
1011 Iterator<Pair<Node,Node>> i = segs.iterator();
1012 Pair<Node,Node> seg = i.next();
1013 EastNorth A = seg.a.getEastNorth();
1014 EastNorth B = seg.b.getEastNorth();
1015 seg = i.next();
1016 EastNorth C = seg.a.getEastNorth();
1017 EastNorth D = seg.b.getEastNorth();
1018
1019 double u=det(B.east() - A.east(), B.north() - A.north(), C.east() - D.east(), C.north() - D.north());
1020
1021 // Check for parallel segments and do nothing if they are
1022 // In practice this will probably only happen when a way has been duplicated
1023
1024 if (u == 0)
1025 return;
1026
1027 // q is a number between 0 and 1
1028 // It is the point in the segment where the intersection occurs
1029 // if the segment is scaled to lenght 1
1030
1031 double q = det(B.north() - C.north(), B.east() - C.east(), D.north() - C.north(), D.east() - C.east()) / u;
1032 EastNorth intersection = new EastNorth(
1033 B.east() + q * (A.east() - B.east()),
1034 B.north() + q * (A.north() - B.north()));
1035
1036 int snapToIntersectionThreshold
1037 = Main.pref.getInteger("edit.snap-intersection-threshold",10);
1038
1039 // only adjust to intersection if within snapToIntersectionThreshold pixel of mouse click; otherwise
1040 // fall through to default action.
1041 // (for semi-parallel lines, intersection might be miles away!)
1042 if (Main.map.mapView.getPoint2D(n).distance(Main.map.mapView.getPoint2D(intersection)) < snapToIntersectionThreshold) {
1043 n.setEastNorth(intersection);
1044 return;
1045 }
1046 default:
1047 EastNorth P = n.getEastNorth();
1048 seg = segs.iterator().next();
1049 A = seg.a.getEastNorth();
1050 B = seg.b.getEastNorth();
1051 double a = P.distanceSq(B);
1052 double b = P.distanceSq(A);
1053 double c = A.distanceSq(B);
1054 q = (a - b + c) / (2*c);
1055 n.setEastNorth(new EastNorth(B.east() + q * (A.east() - B.east()), B.north() + q * (A.north() - B.north())));
1056 }
1057 }
1058
1059 // helper for adjustNode
1060 static double det(double a, double b, double c, double d) {
1061 return a * d - b * c;
1062 }
1063
1064 private void tryToMoveNodeOnIntersection(List<WaySegment> wss, Node n) {
1065 if (wss.isEmpty())
1066 return;
1067 WaySegment ws = wss.get(0);
1068 EastNorth p1=ws.getFirstNode().getEastNorth();
1069 EastNorth p2=ws.getSecondNode().getEastNorth();
1070 if (snapHelper.dir2!=null && currentBaseNode!=null) {
1071 EastNorth xPoint = Geometry.getSegmentSegmentIntersection(p1, p2, snapHelper.dir2, currentBaseNode.getEastNorth());
1072 if (xPoint!=null) {
1073 n.setEastNorth(xPoint);
1074 }
1075 }
1076 }
1077 /**
1078 * Takes the data from computeHelperLine to determine which ways/nodes should be highlighted
1079 * (if feature enabled). Also sets the target cursor if appropriate. It adds the to-be-
1080 * highlighted primitives to newHighlights but does not actually highlight them. This work is
1081 * done in redrawIfRequired. This means, calling addHighlighting() without redrawIfRequired()
1082 * will leave the data in an inconsistent state.
1083 *
1084 * The status bar derives its information from oldHighlights, so in order to update the status
1085 * bar both addHighlighting() and repaintIfRequired() are needed, since former fills newHighlights
1086 * and latter processes them into oldHighlights.
1087 */
1088 private void addHighlighting() {
1089 newHighlights = new HashSet<OsmPrimitive>();
1090
1091 // if ctrl key is held ("no join"), don't highlight anything
1092 if (ctrl) {
1093 Main.map.mapView.setNewCursor(cursor, this);
1094 redrawIfRequired();
1095 return;
1096 }
1097
1098 // This happens when nothing is selected, but we still want to highlight the "target node"
1099 if (mouseOnExistingNode == null && getCurrentDataSet().getSelected().isEmpty()
1100 && mousePos != null) {
1101 mouseOnExistingNode = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
1102 }
1103
1104 if (mouseOnExistingNode != null) {
1105 Main.map.mapView.setNewCursor(cursorJoinNode, this);
1106 newHighlights.add(mouseOnExistingNode);
1107 redrawIfRequired();
1108 return;
1109 }
1110
1111 // Insert the node into all the nearby way segments
1112 if (mouseOnExistingWays.isEmpty()) {
1113 Main.map.mapView.setNewCursor(cursor, this);
1114 redrawIfRequired();
1115 return;
1116 }
1117
1118 Main.map.mapView.setNewCursor(cursorJoinWay, this);
1119 newHighlights.addAll(mouseOnExistingWays);
1120 redrawIfRequired();
1121 }
1122
1123 /**
1124 * Removes target highlighting from primitives. Issues repaint if required.
1125 * Returns true if a repaint has been issued.
1126 */
1127 private boolean removeHighlighting() {
1128 newHighlights = new HashSet<OsmPrimitive>();
1129 return redrawIfRequired();
1130 }
1131
1132 @Override
1133 public void paint(Graphics2D g, MapView mv, Bounds box) {
1134 // sanity checks
1135 if (Main.map.mapView == null || mousePos == null
1136 // don't draw line if we don't know where from or where to
1137 || currentBaseNode == null || currentMouseEastNorth == null
1138 // don't draw line if mouse is outside window
1139 || !Main.map.mapView.getBounds().contains(mousePos))
1140 return;
1141
1142 Graphics2D g2 = g;
1143 snapHelper.drawIfNeeded(g2,mv);
1144 if (!drawHelperLine || wayIsFinished || shift)
1145 return;
1146
1147 if (!snapHelper.isActive()) { // else use color and stoke from snapHelper.draw
1148 g2.setColor(rubberLineColor);
1149 g2.setStroke(rubberLineStroke);
1150 } else if (!snapHelper.drawConstructionGeometry)
1151 return;
1152 GeneralPath b = new GeneralPath();
1153 Point p1=mv.getPoint(currentBaseNode);
1154 Point p2=mv.getPoint(currentMouseEastNorth);
1155
1156 double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
1157
1158 b.moveTo(p1.x,p1.y); b.lineTo(p2.x, p2.y);
1159
1160 // if alt key is held ("start new way"), draw a little perpendicular line
1161 if (alt) {
1162 b.moveTo((int)(p1.x + 8*Math.cos(t+PHI)), (int)(p1.y + 8*Math.sin(t+PHI)));
1163 b.lineTo((int)(p1.x + 8*Math.cos(t-PHI)), (int)(p1.y + 8*Math.sin(t-PHI)));
1164 }
1165
1166 g2.draw(b);
1167 g2.setStroke(BASIC_STROKE);
1168 }
1169
1170 @Override
1171 public String getModeHelpText() {
1172 StringBuilder rv;
1173 /*
1174 * No modifiers: all (Connect, Node Re-Use, Auto-Weld)
1175 * CTRL: disables node re-use, auto-weld
1176 * Shift: do not make connection
1177 * ALT: make connection but start new way in doing so
1178 */
1179
1180 /*
1181 * Status line text generation is split into two parts to keep it maintainable.
1182 * First part looks at what will happen to the new node inserted on click and
1183 * the second part will look if a connection is made or not.
1184 *
1185 * Note that this help text is not absolutely accurate as it doesn't catch any special
1186 * cases (e.g. when preventing <---> ways). The only special that it catches is when
1187 * a way is about to be finished.
1188 *
1189 * First check what happens to the new node.
1190 */
1191
1192 // oldHighlights stores the current highlights. If this
1193 // list is empty we can assume that we won't do any joins
1194 if (ctrl || oldHighlights.isEmpty()) {
1195 rv = new StringBuilder(tr("Create new node."));
1196 } else {
1197 // oldHighlights may store a node or way, check if it's a node
1198 OsmPrimitive x = oldHighlights.iterator().next();
1199 if (x instanceof Node) {
1200 rv = new StringBuilder(tr("Select node under cursor."));
1201 } else {
1202 rv = new StringBuilder(trn("Insert new node into way.", "Insert new node into {0} ways.",
1203 oldHighlights.size(), oldHighlights.size()));
1204 }
1205 }
1206
1207 /*
1208 * Check whether a connection will be made
1209 */
1210 if (currentBaseNode != null && !wayIsFinished) {
1211 if (alt) {
1212 rv.append(" ").append(tr("Start new way from last node."));
1213 } else {
1214 rv.append(" ").append(tr("Continue way from last node."));
1215 }
1216 if (snapHelper.isSnapOn()) {
1217 rv.append(" ").append(tr("Angle snapping active."));
1218 }
1219 }
1220
1221 Node n = mouseOnExistingNode;
1222 /*
1223 * Handle special case: Highlighted node == selected node => finish drawing
1224 */
1225 if (n != null && getCurrentDataSet() != null && getCurrentDataSet().getSelectedNodes().contains(n)) {
1226 if (wayIsFinished) {
1227 rv = new StringBuilder(tr("Select node under cursor."));
1228 } else {
1229 rv = new StringBuilder(tr("Finish drawing."));
1230 }
1231 }
1232
1233 /*
1234 * Handle special case: Self-Overlapping or closing way
1235 */
1236 if (getCurrentDataSet() != null && !getCurrentDataSet().getSelectedWays().isEmpty() && !wayIsFinished && !alt) {
1237 Way w = getCurrentDataSet().getSelectedWays().iterator().next();
1238 for (Node m : w.getNodes()) {
1239 if (m.equals(mouseOnExistingNode) || mouseOnExistingWays.contains(w)) {
1240 rv.append(" ").append(tr("Finish drawing."));
1241 break;
1242 }
1243 }
1244 }
1245 return rv.toString();
1246 }
1247
1248 /**
1249 * Get selected primitives, while draw action is in progress.
1250 *
1251 * While drawing a way, technically the last node is selected.
1252 * This is inconvenient when the user tries to add tags to the
1253 * way using a keyboard shortcut. In that case, this method returns
1254 * the current way as selection, to work around this issue.
1255 * Otherwise the normal selection of the current data layer is returned.
1256 */
1257 public Collection<OsmPrimitive> getInProgressSelection() {
1258 DataSet ds = getCurrentDataSet();
1259 if (ds == null) return null;
1260 if (currentBaseNode != null && !ds.getSelected().isEmpty()) {
1261 Way continueFrom = getWayForNode(currentBaseNode);
1262 if (alt && continueFrom != null)
1263 return Collections.<OsmPrimitive>singleton(continueFrom);
1264 }
1265 return ds.getSelected();
1266 }
1267
1268 @Override
1269 public boolean layerIsSupported(Layer l) {
1270 return l instanceof OsmDataLayer;
1271 }
1272
1273 @Override
1274 protected void updateEnabledState() {
1275 setEnabled(getEditLayer() != null);
1276 }
1277
1278 @Override
1279 public void destroy() {
1280 super.destroy();
1281 snapChangeAction.destroy();
1282 }
1283
1284 public class BackSpaceAction extends AbstractAction {
1285
1286 @Override
1287 public void actionPerformed(ActionEvent e) {
1288 Main.main.undoRedo.undo();
1289 Node n=null;
1290 Command lastCmd=Main.main.undoRedo.commands.peekLast();
1291 if (lastCmd==null) return;
1292 for (OsmPrimitive p: lastCmd.getParticipatingPrimitives()) {
1293 if (p instanceof Node) {
1294 if (n==null) {
1295 n=(Node) p; // found one node
1296 wayIsFinished=false;
1297 } else {
1298 // if more than 1 node were affected by previous command,
1299 // we have no way to continue, so we forget about found node
1300 n=null;
1301 break;
1302 }
1303 }
1304 }
1305 // select last added node - maybe we will continue drawing from it
1306 if (n!=null) {
1307 getCurrentDataSet().addSelected(n);
1308 }
1309 }
1310 }
1311
1312 private class SnapHelper {
1313 boolean snapOn; // snapping is turned on
1314
1315 private boolean active; // snapping is active for current mouse position
1316 private boolean fixed; // snap angle is fixed
1317 private boolean absoluteFix; // snap angle is absolute
1318
1319 private boolean drawConstructionGeometry;
1320 private boolean showProjectedPoint;
1321 private boolean showAngle;
1322
1323 private boolean snapToProjections;
1324
1325 EastNorth dir2;
1326 EastNorth projected;
1327 String labelText;
1328 double lastAngle;
1329
1330 double customBaseHeading=-1; // angle of base line, if not last segment)
1331 private EastNorth segmentPoint1; // remembered first point of base segment
1332 private EastNorth segmentPoint2; // remembered second point of base segment
1333 private EastNorth projectionSource; // point that we are projecting to the line
1334
1335 double[] snapAngles;
1336 double snapAngleTolerance;
1337
1338 double pe,pn; // (pe,pn) - direction of snapping line
1339 double e0,n0; // (e0,n0) - origin of snapping line
1340
1341 final String fixFmt="%d "+tr("FIX");
1342 Color snapHelperColor;
1343 private Color highlightColor;
1344
1345 private Stroke normalStroke;
1346 private Stroke helperStroke;
1347 private Stroke highlightStroke;
1348
1349 JCheckBoxMenuItem checkBox;
1350 public final Color ORANGE_TRANSPARENT = new Color(Color.ORANGE.getRed(),Color.ORANGE.getGreen(),Color.ORANGE.getBlue(),128);
1351
1352 public void init() {
1353 snapOn=false;
1354 checkBox.setState(snapOn);
1355 fixed=false; absoluteFix=false;
1356
1357 Collection<String> angles = Main.pref.getCollection("draw.anglesnap.angles",
1358 Arrays.asList("0","30","45","60","90","120","135","150","180"));
1359
1360 snapAngles = new double[2*angles.size()];
1361 int i=0;
1362 for (String s: angles) {
1363 try {
1364 snapAngles[i] = Double.parseDouble(s); i++;
1365 snapAngles[i] = 360-Double.parseDouble(s); i++;
1366 } catch (NumberFormatException e) {
1367 Main.warn("Incorrect number in draw.anglesnap.angles preferences: "+s);
1368 snapAngles[i]=0;i++;
1369 snapAngles[i]=0;i++;
1370 }
1371 }
1372 snapAngleTolerance = Main.pref.getDouble("draw.anglesnap.tolerance", 5.0);
1373 drawConstructionGeometry = Main.pref.getBoolean("draw.anglesnap.drawConstructionGeometry", true);
1374 showProjectedPoint = Main.pref.getBoolean("draw.anglesnap.drawProjectedPoint", true);
1375 snapToProjections = Main.pref.getBoolean("draw.anglesnap.projectionsnap", true);
1376
1377 showAngle = Main.pref.getBoolean("draw.anglesnap.showAngle", true);
1378 useRepeatedShortcut = Main.pref.getBoolean("draw.anglesnap.toggleOnRepeatedA", true);
1379
1380 normalStroke = rubberLineStroke;
1381 snapHelperColor = Main.pref.getColor(marktr("draw angle snap"), Color.ORANGE);
1382
1383 highlightColor = Main.pref.getColor(marktr("draw angle snap highlight"), ORANGE_TRANSPARENT);
1384 highlightStroke = GuiHelper.getCustomizedStroke(Main.pref.get("draw.anglesnap.stroke.highlight","10"));
1385 helperStroke = GuiHelper.getCustomizedStroke(Main.pref.get("draw.anglesnap.stroke.helper","1 4"));
1386 }
1387
1388 public void saveAngles(String ... angles) {
1389 Main.pref.putCollection("draw.anglesnap.angles", Arrays.asList(angles));
1390 }
1391
1392 public void setMenuCheckBox(JCheckBoxMenuItem checkBox) {
1393 this.checkBox = checkBox;
1394 }
1395
1396 public void drawIfNeeded(Graphics2D g2, MapView mv) {
1397 if (!snapOn || !active)
1398 return;
1399 Point p1=mv.getPoint(currentBaseNode);
1400 Point p2=mv.getPoint(dir2);
1401 Point p3=mv.getPoint(projected);
1402 GeneralPath b;
1403 if (drawConstructionGeometry) {
1404 g2.setColor(snapHelperColor);
1405 g2.setStroke(helperStroke);
1406
1407 b = new GeneralPath();
1408 if (absoluteFix) {
1409 b.moveTo(p2.x,p2.y);
1410 b.lineTo(2*p1.x-p2.x,2*p1.y-p2.y); // bi-directional line
1411 } else {
1412 b.moveTo(p2.x,p2.y);
1413 b.lineTo(p3.x,p3.y);
1414 }
1415 g2.draw(b);
1416 }
1417 if (projectionSource != null) {
1418 g2.setColor(snapHelperColor);
1419 g2.setStroke(helperStroke);
1420 b = new GeneralPath();
1421 b.moveTo(p3.x,p3.y);
1422 Point pp=mv.getPoint(projectionSource);
1423 b.lineTo(pp.x,pp.y);
1424 g2.draw(b);
1425 }
1426
1427 if (customBaseHeading >= 0) {
1428 g2.setColor(highlightColor);
1429 g2.setStroke(highlightStroke);
1430 b = new GeneralPath();
1431 Point pp1=mv.getPoint(segmentPoint1);
1432 Point pp2=mv.getPoint(segmentPoint2);
1433 b.moveTo(pp1.x,pp1.y);
1434 b.lineTo(pp2.x,pp2.y);
1435 g2.draw(b);
1436 }
1437
1438 g2.setColor(rubberLineColor);
1439 g2.setStroke(normalStroke);
1440 b = new GeneralPath();
1441 b.moveTo(p1.x,p1.y);
1442 b.lineTo(p3.x,p3.y);
1443 g2.draw(b);
1444
1445 g2.drawString(labelText, p3.x-5, p3.y+20);
1446 if (showProjectedPoint) {
1447 g2.setStroke(normalStroke);
1448 g2.drawOval(p3.x-5, p3.y-5, 10, 10); // projected point
1449 }
1450
1451 g2.setColor(snapHelperColor);
1452 g2.setStroke(helperStroke);
1453 }
1454
1455 /* If mouse position is close to line at 15-30-45-... angle, remembers this direction
1456 */
1457 public void checkAngleSnapping(EastNorth currentEN, double baseHeading, double curHeading) {
1458 EastNorth p0 = currentBaseNode.getEastNorth();
1459 EastNorth snapPoint = currentEN;
1460 double angle = -1;
1461
1462 double activeBaseHeading = (customBaseHeading>=0)? customBaseHeading : baseHeading;
1463
1464 if (snapOn && (activeBaseHeading>=0)) {
1465 angle = curHeading - activeBaseHeading;
1466 if (angle < 0) {
1467 angle+=360;
1468 }
1469 if (angle > 360) {
1470 angle=0;
1471 }
1472
1473 double nearestAngle;
1474 if (fixed) {
1475 nearestAngle = lastAngle; // if direction is fixed use previous angle
1476 active = true;
1477 } else {
1478 nearestAngle = getNearestAngle(angle);
1479 if (getAngleDelta(nearestAngle, angle) < snapAngleTolerance) {
1480 active = (customBaseHeading>=0)? true : Math.abs(nearestAngle - 180) > 1e-3;
1481 // if angle is to previous segment, exclude 180 degrees
1482 lastAngle = nearestAngle;
1483 } else {
1484 active=false;
1485 }
1486 }
1487
1488 if (active) {
1489 double phi;
1490 e0 = p0.east();
1491 n0 = p0.north();
1492 buildLabelText((nearestAngle<=180) ? nearestAngle : nearestAngle-360);
1493
1494 phi = (nearestAngle + activeBaseHeading) * Math.PI / 180;
1495 // (pe,pn) - direction of snapping line
1496 pe = Math.sin(phi);
1497 pn = Math.cos(phi);
1498 double scale = 20 * Main.map.mapView.getDist100Pixel();
1499 dir2 = new EastNorth(e0 + scale * pe, n0 + scale * pn);
1500 snapPoint = getSnapPoint(currentEN);
1501 } else {
1502 noSnapNow();
1503 }
1504 }
1505
1506 // find out the distance, in metres, between the base point and projected point
1507 LatLon mouseLatLon = Main.map.mapView.getProjection().eastNorth2latlon(snapPoint);
1508 double distance = currentBaseNode.getCoor().greatCircleDistance(mouseLatLon);
1509 double hdg = Math.toDegrees(p0.heading(snapPoint));
1510 // heading of segment from current to calculated point, not to mouse position
1511
1512 if (baseHeading >=0 ) { // there is previous line segment with some heading
1513 angle = hdg - baseHeading;
1514 if (angle < 0) {
1515 angle+=360;
1516 }
1517 if (angle > 360) {
1518 angle=0;
1519 }
1520 }
1521 showStatusInfo(angle, hdg, distance, isSnapOn());
1522 }
1523
1524 private void buildLabelText(double nearestAngle) {
1525 if (showAngle) {
1526 if (fixed) {
1527 if (absoluteFix) {
1528 labelText = "=";
1529 } else {
1530 labelText = String.format(fixFmt, (int) nearestAngle);
1531 }
1532 } else {
1533 labelText = String.format("%d", (int) nearestAngle);
1534 }
1535 } else {
1536 if (fixed) {
1537 if (absoluteFix) {
1538 labelText = "=";
1539 } else {
1540 labelText = String.format(tr("FIX"), 0);
1541 }
1542 } else {
1543 labelText = "";
1544 }
1545 }
1546 }
1547
1548 public EastNorth getSnapPoint(EastNorth p) {
1549 if (!active)
1550 return p;
1551 double de=p.east()-e0;
1552 double dn=p.north()-n0;
1553 double l = de*pe+dn*pn;
1554 double delta = Main.map.mapView.getDist100Pixel()/20;
1555 if (!absoluteFix && l<delta) {
1556 active=false;
1557 return p;
1558 } // do not go backward!
1559
1560 projectionSource=null;
1561 if (snapToProjections) {
1562 DataSet ds = getCurrentDataSet();
1563 Collection<Way> selectedWays = ds.getSelectedWays();
1564 if (selectedWays.size()==1) {
1565 Way w = selectedWays.iterator().next();
1566 Collection <EastNorth> pointsToProject = new ArrayList<EastNorth>();
1567 if (w.getNodesCount()<1000) {
1568 for (Node n: w.getNodes()) {
1569 pointsToProject.add(n.getEastNorth());
1570 }
1571 }
1572 if (customBaseHeading >=0 ) {
1573 pointsToProject.add(segmentPoint1);
1574 pointsToProject.add(segmentPoint2);
1575 }
1576 EastNorth enOpt=null;
1577 double dOpt=1e5;
1578 for (EastNorth en: pointsToProject) { // searching for besht projection
1579 double l1 = (en.east()-e0)*pe+(en.north()-n0)*pn;
1580 double d1 = Math.abs(l1-l);
1581 if (d1 < delta && d1 < dOpt) {
1582 l=l1;
1583 enOpt = en;
1584 dOpt = d1;
1585 }
1586 }
1587 if (enOpt!=null) {
1588 projectionSource = enOpt;
1589 }
1590 }
1591 }
1592 return projected = new EastNorth(e0+l*pe, n0+l*pn);
1593 }
1594
1595
1596 public void noSnapNow() {
1597 active=false;
1598 dir2=null; projected=null;
1599 labelText=null;
1600 }
1601
1602 public void setBaseSegment(WaySegment seg) {
1603 if (seg==null) return;
1604 segmentPoint1=seg.getFirstNode().getEastNorth();
1605 segmentPoint2=seg.getSecondNode().getEastNorth();
1606
1607 double hdg = segmentPoint1.heading(segmentPoint2);
1608 hdg=Math.toDegrees(hdg);
1609 if (hdg<0) {
1610 hdg+=360;
1611 }
1612 if (hdg>360) {
1613 hdg-=360;
1614 }
1615 //fixed=true;
1616 //absoluteFix=true;
1617 customBaseHeading=hdg;
1618 }
1619
1620 private void nextSnapMode() {
1621 if (snapOn) {
1622 // turn off snapping if we are in fixed mode or no actile snapping line exist
1623 if (fixed || !active) { snapOn=false; unsetFixedMode(); } else {
1624 setFixedMode();
1625 }
1626 } else {
1627 snapOn=true;
1628 unsetFixedMode();
1629 }
1630 checkBox.setState(snapOn);
1631 customBaseHeading=-1;
1632 }
1633
1634 private void enableSnapping() {
1635 snapOn = true;
1636 checkBox.setState(snapOn);
1637 customBaseHeading=-1;
1638 unsetFixedMode();
1639 }
1640
1641 private void toggleSnapping() {
1642 snapOn = !snapOn;
1643 checkBox.setState(snapOn);
1644 customBaseHeading=-1;
1645 unsetFixedMode();
1646 }
1647
1648 public void setFixedMode() {
1649 if (active) {
1650 fixed=true;
1651 }
1652 }
1653
1654
1655 public void unsetFixedMode() {
1656 fixed=false;
1657 absoluteFix=false;
1658 lastAngle=0;
1659 active=false;
1660 }
1661
1662 public boolean isActive() {
1663 return active;
1664 }
1665
1666 public boolean isSnapOn() {
1667 return snapOn;
1668 }
1669
1670 private double getNearestAngle(double angle) {
1671 double delta,minDelta=1e5, bestAngle=0.0;
1672 for (double snapAngle : snapAngles) {
1673 delta = getAngleDelta(angle, snapAngle);
1674 if (delta < minDelta) {
1675 minDelta = delta;
1676 bestAngle = snapAngle;
1677 }
1678 }
1679 if (Math.abs(bestAngle-360) < 1e-3) {
1680 bestAngle=0;
1681 }
1682 return bestAngle;
1683 }
1684
1685 private double getAngleDelta(double a, double b) {
1686 double delta = Math.abs(a-b);
1687 if (delta>180)
1688 return 360-delta;
1689 else
1690 return delta;
1691 }
1692
1693 private void unFixOrTurnOff() {
1694 if (absoluteFix) {
1695 unsetFixedMode();
1696 } else {
1697 toggleSnapping();
1698 }
1699 }
1700
1701 MouseListener anglePopupListener = new PopupMenuLauncher( new JPopupMenu() {
1702 JCheckBoxMenuItem repeatedCb = new JCheckBoxMenuItem(new AbstractAction(tr("Toggle snapping by {0}", getShortcut().getKeyText())){
1703 @Override public void actionPerformed(ActionEvent e) {
1704 boolean sel=((JCheckBoxMenuItem) e.getSource()).getState();
1705 Main.pref.put("draw.anglesnap.toggleOnRepeatedA", sel);
1706 init();
1707 }
1708 });
1709 JCheckBoxMenuItem helperCb = new JCheckBoxMenuItem(new AbstractAction(tr("Show helper geometry")){
1710 @Override public void actionPerformed(ActionEvent e) {
1711 boolean sel=((JCheckBoxMenuItem) e.getSource()).getState();
1712 Main.pref.put("draw.anglesnap.drawConstructionGeometry", sel);
1713 Main.pref.put("draw.anglesnap.drawProjectedPoint", sel);
1714 Main.pref.put("draw.anglesnap.showAngle", sel);
1715 init();
1716 enableSnapping();
1717 }
1718 });
1719 JCheckBoxMenuItem projectionCb = new JCheckBoxMenuItem(new AbstractAction(tr("Snap to node projections")){
1720 @Override public void actionPerformed(ActionEvent e) {
1721 boolean sel=((JCheckBoxMenuItem) e.getSource()).getState();
1722 Main.pref.put("draw.anglesnap.projectionsnap", sel);
1723 init();
1724 enableSnapping();
1725 }
1726 });
1727 {
1728 helperCb.setState(Main.pref.getBoolean("draw.anglesnap.drawConstructionGeometry",true));
1729 projectionCb.setState(Main.pref.getBoolean("draw.anglesnap.projectionsnapgvff",true));
1730 repeatedCb.setState(Main.pref.getBoolean("draw.anglesnap.toggleOnRepeatedA",true));
1731 add(repeatedCb);
1732 add(helperCb);
1733 add(projectionCb);
1734 add(new AbstractAction(tr("Disable")) {
1735 @Override public void actionPerformed(ActionEvent e) {
1736 saveAngles("180");
1737 init();
1738 enableSnapping();
1739 }
1740 });
1741 add(new AbstractAction(tr("0,90,...")) {
1742 @Override public void actionPerformed(ActionEvent e) {
1743 saveAngles("0","90","180");
1744 init();
1745 enableSnapping();
1746 }
1747 });
1748 add(new AbstractAction(tr("0,45,90,...")) {
1749 @Override public void actionPerformed(ActionEvent e) {
1750 saveAngles("0","45","90","135","180");
1751 init();
1752 enableSnapping();
1753 }
1754 });
1755 add(new AbstractAction(tr("0,30,45,60,90,...")) {
1756 @Override public void actionPerformed(ActionEvent e) {
1757 saveAngles("0","30","45","60","90","120","135","150","180");
1758 init();
1759 enableSnapping();
1760 }
1761 });
1762 }
1763 }) {
1764 @Override
1765 public void mouseClicked(MouseEvent e) {
1766 super.mouseClicked(e);
1767 if (e.getButton() == MouseEvent.BUTTON1) {
1768 toggleSnapping();
1769 updateStatusLine();
1770 }
1771 }
1772 };
1773 }
1774
1775 private class SnapChangeAction extends JosmAction {
1776 public SnapChangeAction() {
1777 super(tr("Angle snapping"), "anglesnap",
1778 tr("Switch angle snapping mode while drawing"), null, false);
1779 putValue("help", ht("/Action/Draw/AngleSnap"));
1780 }
1781
1782 @Override
1783 public void actionPerformed(ActionEvent e) {
1784 if (snapHelper!=null) {
1785 snapHelper.toggleSnapping();
1786 }
1787 }
1788 }
1789}
Note: See TracBrowser for help on using the repository browser.