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