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