1 | // License: GPL. For details, see LICENSE file.
|
---|
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 |
|
---|
8 | import java.awt.BasicStroke;
|
---|
9 | import java.awt.Color;
|
---|
10 | import java.awt.Cursor;
|
---|
11 | import java.awt.Graphics2D;
|
---|
12 | import java.awt.Point;
|
---|
13 | import java.awt.Rectangle;
|
---|
14 | import java.awt.Stroke;
|
---|
15 | import java.awt.event.ActionEvent;
|
---|
16 | import java.awt.event.KeyEvent;
|
---|
17 | import java.awt.event.MouseEvent;
|
---|
18 | import java.awt.geom.AffineTransform;
|
---|
19 | import java.awt.geom.GeneralPath;
|
---|
20 | import java.awt.geom.Line2D;
|
---|
21 | import java.awt.geom.NoninvertibleTransformException;
|
---|
22 | import java.awt.geom.Point2D;
|
---|
23 | import java.util.ArrayList;
|
---|
24 | import java.util.Collection;
|
---|
25 | import java.util.LinkedList;
|
---|
26 | import java.util.List;
|
---|
27 |
|
---|
28 | import javax.swing.JCheckBoxMenuItem;
|
---|
29 | import javax.swing.JMenuItem;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
33 | import org.openstreetmap.josm.actions.MergeNodesAction;
|
---|
34 | import org.openstreetmap.josm.command.AddCommand;
|
---|
35 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
36 | import org.openstreetmap.josm.command.Command;
|
---|
37 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
38 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
39 | import org.openstreetmap.josm.data.Bounds;
|
---|
40 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
41 | import org.openstreetmap.josm.data.osm.Node;
|
---|
42 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
43 | import org.openstreetmap.josm.data.osm.Way;
|
---|
44 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
45 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
46 | import org.openstreetmap.josm.data.preferences.ColorProperty;
|
---|
47 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
48 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
49 | import org.openstreetmap.josm.gui.MapView;
|
---|
50 | import org.openstreetmap.josm.gui.draw.MapViewPath;
|
---|
51 | import org.openstreetmap.josm.gui.draw.SymbolShape;
|
---|
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.gui.util.GuiHelper;
|
---|
56 | import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
|
---|
57 | import org.openstreetmap.josm.gui.util.ModifierListener;
|
---|
58 | import org.openstreetmap.josm.tools.Geometry;
|
---|
59 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
60 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Makes a rectangle from a line, or modifies a rectangle.
|
---|
64 | */
|
---|
65 | public class ExtrudeAction extends MapMode implements MapViewPaintable, KeyPressReleaseListener, ModifierListener {
|
---|
66 |
|
---|
67 | enum Mode { extrude, translate, select, create_new, translate_node }
|
---|
68 |
|
---|
69 | private Mode mode = Mode.select;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * If {@code true}, when extruding create new node(s) even if segments are parallel.
|
---|
73 | */
|
---|
74 | private boolean alwaysCreateNodes;
|
---|
75 | private boolean nodeDragWithoutCtrl;
|
---|
76 |
|
---|
77 | private long mouseDownTime;
|
---|
78 | private transient WaySegment selectedSegment;
|
---|
79 | private transient Node selectedNode;
|
---|
80 | private Color mainColor;
|
---|
81 | private transient Stroke mainStroke;
|
---|
82 |
|
---|
83 | /** settings value whether shared nodes should be ignored or not */
|
---|
84 | private boolean ignoreSharedNodes;
|
---|
85 |
|
---|
86 | private boolean keepSegmentDirection;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * drawing settings for helper lines
|
---|
90 | */
|
---|
91 | private Color helperColor;
|
---|
92 | private transient Stroke helperStrokeDash;
|
---|
93 | private transient Stroke helperStrokeRA;
|
---|
94 |
|
---|
95 | private transient Stroke oldLineStroke;
|
---|
96 | private double symbolSize;
|
---|
97 | /**
|
---|
98 | * Possible directions to move to.
|
---|
99 | */
|
---|
100 | private transient List<ReferenceSegment> possibleMoveDirections;
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Collection of nodes that is moved
|
---|
105 | */
|
---|
106 | private transient List<Node> movingNodeList;
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * The direction that is currently active.
|
---|
110 | */
|
---|
111 | private transient ReferenceSegment activeMoveDirection;
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * The position of the mouse cursor when the drag action was initiated.
|
---|
115 | */
|
---|
116 | private Point initialMousePos;
|
---|
117 | /**
|
---|
118 | * The time which needs to pass between click and release before something
|
---|
119 | * counts as a move, in milliseconds
|
---|
120 | */
|
---|
121 | private int initialMoveDelay = 200;
|
---|
122 | /**
|
---|
123 | * The minimal shift of mouse (in pixels) befire something counts as move
|
---|
124 | */
|
---|
125 | private int initialMoveThreshold = 1;
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * The initial EastNorths of node1 and node2
|
---|
129 | */
|
---|
130 | private EastNorth initialN1en;
|
---|
131 | private EastNorth initialN2en;
|
---|
132 | /**
|
---|
133 | * The new EastNorths of node1 and node2
|
---|
134 | */
|
---|
135 | private EastNorth newN1en;
|
---|
136 | private EastNorth newN2en;
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * the command that performed last move.
|
---|
140 | */
|
---|
141 | private transient MoveCommand moveCommand;
|
---|
142 | /**
|
---|
143 | * The command used for dual alignment movement.
|
---|
144 | * Needs to be separate, due to two nodes moving in different directions.
|
---|
145 | */
|
---|
146 | private transient MoveCommand moveCommand2;
|
---|
147 |
|
---|
148 | /** The cursor for the 'create_new' mode. */
|
---|
149 | private final Cursor cursorCreateNew;
|
---|
150 |
|
---|
151 | /** The cursor for the 'translate' mode. */
|
---|
152 | private final Cursor cursorTranslate;
|
---|
153 |
|
---|
154 | /** The cursor for the 'alwaysCreateNodes' submode. */
|
---|
155 | private final Cursor cursorCreateNodes;
|
---|
156 |
|
---|
157 | private static class ReferenceSegment {
|
---|
158 | public final EastNorth en;
|
---|
159 | public final EastNorth p1;
|
---|
160 | public final EastNorth p2;
|
---|
161 | public final boolean perpendicular;
|
---|
162 |
|
---|
163 | ReferenceSegment(EastNorth en, EastNorth p1, EastNorth p2, boolean perpendicular) {
|
---|
164 | this.en = en;
|
---|
165 | this.p1 = p1;
|
---|
166 | this.p2 = p2;
|
---|
167 | this.perpendicular = perpendicular;
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Override
|
---|
171 | public String toString() {
|
---|
172 | return "ReferenceSegment[en=" + en + ", p1=" + p1 + ", p2=" + p2 + ", perp=" + perpendicular + ']';
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | // Dual alignment mode stuff
|
---|
177 | /** {@code true}, if dual alignment mode is enabled. User wants following extrude to be dual aligned. */
|
---|
178 | private boolean dualAlignEnabled;
|
---|
179 | /** {@code true}, if dual alignment is active. User is dragging the mouse, required conditions are met.
|
---|
180 | * Treat {@link #mode} (extrude/translate/create_new) as dual aligned. */
|
---|
181 | private boolean dualAlignActive;
|
---|
182 | /** Dual alignment reference segments */
|
---|
183 | private transient ReferenceSegment dualAlignSegment1, dualAlignSegment2;
|
---|
184 | /** {@code true}, if new segment was collapsed */
|
---|
185 | private boolean dualAlignSegmentCollapsed;
|
---|
186 | // Dual alignment UI stuff
|
---|
187 | private final DualAlignChangeAction dualAlignChangeAction;
|
---|
188 | private final JCheckBoxMenuItem dualAlignCheckboxMenuItem;
|
---|
189 | private final transient Shortcut dualAlignShortcut;
|
---|
190 | private boolean useRepeatedShortcut;
|
---|
191 | private boolean ignoreNextKeyRelease;
|
---|
192 |
|
---|
193 | private class DualAlignChangeAction extends JosmAction {
|
---|
194 | DualAlignChangeAction() {
|
---|
195 | super(tr("Dual alignment"), /* ICON() */ "mapmode/extrude/dualalign",
|
---|
196 | tr("Switch dual alignment mode while extruding"), null, false);
|
---|
197 | putValue("help", ht("/Action/Extrude#DualAlign"));
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public void actionPerformed(ActionEvent e) {
|
---|
202 | toggleDualAlign();
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | protected void updateEnabledState() {
|
---|
207 | setEnabled(Main.map != null && Main.map.mapMode instanceof ExtrudeAction);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Creates a new ExtrudeAction
|
---|
213 | * @param mapFrame The MapFrame this action belongs to.
|
---|
214 | */
|
---|
215 | public ExtrudeAction(MapFrame mapFrame) {
|
---|
216 | super(tr("Extrude"), /* ICON(mapmode/) */ "extrude/extrude", tr("Create areas"),
|
---|
217 | Shortcut.registerShortcut("mapmode:extrude", tr("Mode: {0}", tr("Extrude")), KeyEvent.VK_X, Shortcut.DIRECT),
|
---|
218 | mapFrame,
|
---|
219 | ImageProvider.getCursor("normal", "rectangle"));
|
---|
220 | putValue("help", ht("/Action/Extrude"));
|
---|
221 | cursorCreateNew = ImageProvider.getCursor("normal", "rectangle_plus");
|
---|
222 | cursorTranslate = ImageProvider.getCursor("normal", "rectangle_move");
|
---|
223 | cursorCreateNodes = ImageProvider.getCursor("normal", "rectangle_plussmall");
|
---|
224 |
|
---|
225 | dualAlignEnabled = false;
|
---|
226 | dualAlignChangeAction = new DualAlignChangeAction();
|
---|
227 | dualAlignCheckboxMenuItem = addDualAlignMenuItem();
|
---|
228 | dualAlignCheckboxMenuItem.getAction().setEnabled(false);
|
---|
229 | dualAlignCheckboxMenuItem.setState(dualAlignEnabled);
|
---|
230 | dualAlignShortcut = Shortcut.registerShortcut("mapmode:extrudedualalign",
|
---|
231 | tr("Mode: {0}", tr("Extrude Dual alignment")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
|
---|
232 | readPreferences(); // to show prefernces in table before entering the mode
|
---|
233 | }
|
---|
234 |
|
---|
235 | @Override
|
---|
236 | public void destroy() {
|
---|
237 | super.destroy();
|
---|
238 | dualAlignChangeAction.destroy();
|
---|
239 | }
|
---|
240 |
|
---|
241 | private JCheckBoxMenuItem addDualAlignMenuItem() {
|
---|
242 | int n = Main.main.menu.editMenu.getItemCount();
|
---|
243 | for (int i = n-1; i > 0; i--) {
|
---|
244 | JMenuItem item = Main.main.menu.editMenu.getItem(i);
|
---|
245 | if (item != null && item.getAction() != null && item.getAction() instanceof DualAlignChangeAction) {
|
---|
246 | Main.main.menu.editMenu.remove(i);
|
---|
247 | }
|
---|
248 | }
|
---|
249 | return MainMenu.addWithCheckbox(Main.main.menu.editMenu, dualAlignChangeAction, MainMenu.WINDOW_MENU_GROUP.VOLATILE);
|
---|
250 | }
|
---|
251 |
|
---|
252 | // -------------------------------------------------------------------------
|
---|
253 | // Mode methods
|
---|
254 | // -------------------------------------------------------------------------
|
---|
255 |
|
---|
256 | @Override
|
---|
257 | public String getModeHelpText() {
|
---|
258 | StringBuilder rv;
|
---|
259 | if (mode == Mode.select) {
|
---|
260 | rv = new StringBuilder(tr("Drag a way segment to make a rectangle. Ctrl-drag to move a segment along its normal, " +
|
---|
261 | "Alt-drag to create a new rectangle, double click to add a new node."));
|
---|
262 | if (dualAlignEnabled) {
|
---|
263 | rv.append(' ').append(tr("Dual alignment active."));
|
---|
264 | if (dualAlignSegmentCollapsed)
|
---|
265 | rv.append(' ').append(tr("Segment collapsed due to its direction reversing."));
|
---|
266 | }
|
---|
267 | } else {
|
---|
268 | if (mode == Mode.translate)
|
---|
269 | rv = new StringBuilder(tr("Move a segment along its normal, then release the mouse button."));
|
---|
270 | else if (mode == Mode.translate_node)
|
---|
271 | rv = new StringBuilder(tr("Move the node along one of the segments, then release the mouse button."));
|
---|
272 | else if (mode == Mode.extrude || mode == Mode.create_new)
|
---|
273 | rv = new StringBuilder(tr("Draw a rectangle of the desired size, then release the mouse button."));
|
---|
274 | else {
|
---|
275 | Main.warn("Extrude: unknown mode " + mode);
|
---|
276 | rv = new StringBuilder();
|
---|
277 | }
|
---|
278 | if (dualAlignActive) {
|
---|
279 | rv.append(' ').append(tr("Dual alignment active."));
|
---|
280 | if (dualAlignSegmentCollapsed) {
|
---|
281 | rv.append(' ').append(tr("Segment collapsed due to its direction reversing."));
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 | return rv.toString();
|
---|
286 | }
|
---|
287 |
|
---|
288 | @Override
|
---|
289 | public boolean layerIsSupported(Layer l) {
|
---|
290 | return l instanceof OsmDataLayer;
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Override
|
---|
294 | public void enterMode() {
|
---|
295 | super.enterMode();
|
---|
296 | Main.map.mapView.addMouseListener(this);
|
---|
297 | Main.map.mapView.addMouseMotionListener(this);
|
---|
298 | ignoreNextKeyRelease = true;
|
---|
299 | Main.map.keyDetector.addKeyListener(this);
|
---|
300 | Main.map.keyDetector.addModifierListener(this);
|
---|
301 | }
|
---|
302 |
|
---|
303 | @Override
|
---|
304 | protected void readPreferences() {
|
---|
305 | initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay", 200);
|
---|
306 | initialMoveThreshold = Main.pref.getInteger("extrude.initial-move-threshold", 1);
|
---|
307 | mainColor = new ColorProperty(marktr("Extrude: main line"), (Color) null).get();
|
---|
308 | if (mainColor == null) mainColor = PaintColors.SELECTED.get();
|
---|
309 | helperColor = new ColorProperty(marktr("Extrude: helper line"), Color.ORANGE).get();
|
---|
310 | helperStrokeDash = GuiHelper.getCustomizedStroke(Main.pref.get("extrude.stroke.helper-line", "1 4"));
|
---|
311 | helperStrokeRA = new BasicStroke(1);
|
---|
312 | symbolSize = Main.pref.getDouble("extrude.angle-symbol-radius", 8);
|
---|
313 | nodeDragWithoutCtrl = Main.pref.getBoolean("extrude.drag-nodes-without-ctrl", false);
|
---|
314 | oldLineStroke = GuiHelper.getCustomizedStroke(Main.pref.get("extrude.ctrl.stroke.old-line", "1"));
|
---|
315 | mainStroke = GuiHelper.getCustomizedStroke(Main.pref.get("extrude.stroke.main", "3"));
|
---|
316 |
|
---|
317 | ignoreSharedNodes = Main.pref.getBoolean("extrude.ignore-shared-nodes", true);
|
---|
318 | dualAlignCheckboxMenuItem.getAction().setEnabled(true);
|
---|
319 | useRepeatedShortcut = Main.pref.getBoolean("extrude.dualalign.toggleOnRepeatedX", true);
|
---|
320 | keepSegmentDirection = Main.pref.getBoolean("extrude.dualalign.keep-segment-direction", true);
|
---|
321 | }
|
---|
322 |
|
---|
323 | @Override
|
---|
324 | public void exitMode() {
|
---|
325 | Main.map.mapView.removeMouseListener(this);
|
---|
326 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
327 | Main.map.mapView.removeTemporaryLayer(this);
|
---|
328 | dualAlignCheckboxMenuItem.getAction().setEnabled(false);
|
---|
329 | Main.map.keyDetector.removeKeyListener(this);
|
---|
330 | Main.map.keyDetector.removeModifierListener(this);
|
---|
331 | super.exitMode();
|
---|
332 | }
|
---|
333 |
|
---|
334 | // -------------------------------------------------------------------------
|
---|
335 | // Event handlers
|
---|
336 | // -------------------------------------------------------------------------
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * This method is called to indicate different modes via cursor when the Alt/Ctrl/Shift modifier is pressed,
|
---|
340 | */
|
---|
341 | @Override
|
---|
342 | public void modifiersChanged(int modifiers) {
|
---|
343 | if (!Main.isDisplayingMapView() || !Main.map.mapView.isActiveLayerDrawable())
|
---|
344 | return;
|
---|
345 | updateKeyModifiers(modifiers);
|
---|
346 | if (mode == Mode.select) {
|
---|
347 | Main.map.mapView.setNewCursor(ctrl ? cursorTranslate : alt ? cursorCreateNew : shift ? cursorCreateNodes : cursor, this);
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | @Override
|
---|
352 | public void doKeyPressed(KeyEvent e) {
|
---|
353 | // Do nothing
|
---|
354 | }
|
---|
355 |
|
---|
356 | @Override
|
---|
357 | public void doKeyReleased(KeyEvent e) {
|
---|
358 | if (!dualAlignShortcut.isEvent(e) && !(useRepeatedShortcut && getShortcut().isEvent(e)))
|
---|
359 | return;
|
---|
360 | if (ignoreNextKeyRelease) {
|
---|
361 | ignoreNextKeyRelease = false;
|
---|
362 | } else {
|
---|
363 | toggleDualAlign();
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Toggles dual alignment mode.
|
---|
369 | */
|
---|
370 | private void toggleDualAlign() {
|
---|
371 | dualAlignEnabled = !dualAlignEnabled;
|
---|
372 | dualAlignCheckboxMenuItem.setState(dualAlignEnabled);
|
---|
373 | updateStatusLine();
|
---|
374 | }
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * If the left mouse button is pressed over a segment or a node, switches
|
---|
378 | * to appropriate {@link #mode}, depending on Ctrl/Alt/Shift modifiers and
|
---|
379 | * {@link #dualAlignEnabled}.
|
---|
380 | * @param e current mouse event
|
---|
381 | */
|
---|
382 | @Override
|
---|
383 | public void mousePressed(MouseEvent e) {
|
---|
384 | if (!Main.map.mapView.isActiveLayerVisible())
|
---|
385 | return;
|
---|
386 | if (!(Boolean) this.getValue("active"))
|
---|
387 | return;
|
---|
388 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
389 | return;
|
---|
390 |
|
---|
391 | requestFocusInMapView();
|
---|
392 | updateKeyModifiers(e);
|
---|
393 |
|
---|
394 | selectedNode = Main.map.mapView.getNearestNode(e.getPoint(), OsmPrimitive::isSelectable);
|
---|
395 | selectedSegment = Main.map.mapView.getNearestWaySegment(e.getPoint(), OsmPrimitive::isSelectable);
|
---|
396 |
|
---|
397 | // If nothing gets caught, stay in select mode
|
---|
398 | if (selectedSegment == null && selectedNode == null) return;
|
---|
399 |
|
---|
400 | if (selectedNode != null) {
|
---|
401 | if (ctrl || nodeDragWithoutCtrl) {
|
---|
402 | movingNodeList = new ArrayList<>();
|
---|
403 | movingNodeList.add(selectedNode);
|
---|
404 | calculatePossibleDirectionsByNode();
|
---|
405 | if (possibleMoveDirections.isEmpty()) {
|
---|
406 | // if no directions fould, do not enter dragging mode
|
---|
407 | return;
|
---|
408 | }
|
---|
409 | mode = Mode.translate_node;
|
---|
410 | dualAlignActive = false;
|
---|
411 | }
|
---|
412 | } else {
|
---|
413 | // Otherwise switch to another mode
|
---|
414 | if (dualAlignEnabled && checkDualAlignConditions()) {
|
---|
415 | dualAlignActive = true;
|
---|
416 | calculatePossibleDirectionsForDualAlign();
|
---|
417 | dualAlignSegmentCollapsed = false;
|
---|
418 | } else {
|
---|
419 | dualAlignActive = false;
|
---|
420 | calculatePossibleDirectionsBySegment();
|
---|
421 | }
|
---|
422 | if (ctrl) {
|
---|
423 | mode = Mode.translate;
|
---|
424 | movingNodeList = new ArrayList<>();
|
---|
425 | movingNodeList.add(selectedSegment.getFirstNode());
|
---|
426 | movingNodeList.add(selectedSegment.getSecondNode());
|
---|
427 | } else if (alt) {
|
---|
428 | mode = Mode.create_new;
|
---|
429 | // create a new segment and then select and extrude the new segment
|
---|
430 | getLayerManager().getEditDataSet().setSelected(selectedSegment.way);
|
---|
431 | alwaysCreateNodes = true;
|
---|
432 | } else {
|
---|
433 | mode = Mode.extrude;
|
---|
434 | getLayerManager().getEditDataSet().setSelected(selectedSegment.way);
|
---|
435 | alwaysCreateNodes = shift;
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | // Signifies that nothing has happened yet
|
---|
440 | newN1en = null;
|
---|
441 | newN2en = null;
|
---|
442 | moveCommand = null;
|
---|
443 | moveCommand2 = null;
|
---|
444 |
|
---|
445 | Main.map.mapView.addTemporaryLayer(this);
|
---|
446 |
|
---|
447 | updateStatusLine();
|
---|
448 | Main.map.mapView.repaint();
|
---|
449 |
|
---|
450 | // Make note of time pressed
|
---|
451 | mouseDownTime = System.currentTimeMillis();
|
---|
452 |
|
---|
453 | // Make note of mouse position
|
---|
454 | initialMousePos = e.getPoint();
|
---|
455 | }
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Performs action depending on what {@link #mode} we're in.
|
---|
459 | * @param e current mouse event
|
---|
460 | */
|
---|
461 | @Override
|
---|
462 | public void mouseDragged(MouseEvent e) {
|
---|
463 | if (!Main.map.mapView.isActiveLayerVisible())
|
---|
464 | return;
|
---|
465 |
|
---|
466 | // do not count anything as a drag if it lasts less than 100 milliseconds.
|
---|
467 | if (System.currentTimeMillis() - mouseDownTime < initialMoveDelay)
|
---|
468 | return;
|
---|
469 |
|
---|
470 | if (mode == Mode.select) {
|
---|
471 | // Just sit tight and wait for mouse to be released.
|
---|
472 | } else {
|
---|
473 | //move, create new and extrude mode - move the selected segment
|
---|
474 |
|
---|
475 | EastNorth mouseEn = Main.map.mapView.getEastNorth(e.getPoint().x, e.getPoint().y);
|
---|
476 | EastNorth bestMovement = calculateBestMovementAndNewNodes(mouseEn);
|
---|
477 |
|
---|
478 | Main.map.mapView.setNewCursor(Cursor.MOVE_CURSOR, this);
|
---|
479 |
|
---|
480 | if (dualAlignActive) {
|
---|
481 | if (mode == Mode.extrude || mode == Mode.create_new) {
|
---|
482 | // nothing here
|
---|
483 | } else if (mode == Mode.translate) {
|
---|
484 | EastNorth movement1 = newN1en.subtract(initialN1en);
|
---|
485 | EastNorth movement2 = newN2en.subtract(initialN2en);
|
---|
486 | // move nodes to new position
|
---|
487 | if (moveCommand == null || moveCommand2 == null) {
|
---|
488 | // make a new move commands
|
---|
489 | moveCommand = new MoveCommand(movingNodeList.get(0), movement1.getX(), movement1.getY());
|
---|
490 | moveCommand2 = new MoveCommand(movingNodeList.get(1), movement2.getX(), movement2.getY());
|
---|
491 | Command c = new SequenceCommand(tr("Extrude Way"), moveCommand, moveCommand2);
|
---|
492 | Main.main.undoRedo.add(c);
|
---|
493 | } else {
|
---|
494 | // reuse existing move commands
|
---|
495 | moveCommand.moveAgainTo(movement1.getX(), movement1.getY());
|
---|
496 | moveCommand2.moveAgainTo(movement2.getX(), movement2.getY());
|
---|
497 | }
|
---|
498 | }
|
---|
499 | } else if (bestMovement != null) {
|
---|
500 | if (mode == Mode.extrude || mode == Mode.create_new) {
|
---|
501 | //nothing here
|
---|
502 | } else if (mode == Mode.translate_node || mode == Mode.translate) {
|
---|
503 | //move nodes to new position
|
---|
504 | if (moveCommand == null) {
|
---|
505 | //make a new move command
|
---|
506 | moveCommand = new MoveCommand(new ArrayList<OsmPrimitive>(movingNodeList), bestMovement);
|
---|
507 | Main.main.undoRedo.add(moveCommand);
|
---|
508 | } else {
|
---|
509 | //reuse existing move command
|
---|
510 | moveCommand.moveAgainTo(bestMovement.getX(), bestMovement.getY());
|
---|
511 | }
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | Main.map.mapView.repaint();
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Does anything that needs to be done, then switches back to select mode.
|
---|
521 | * @param e current mouse event
|
---|
522 | */
|
---|
523 | @Override
|
---|
524 | public void mouseReleased(MouseEvent e) {
|
---|
525 |
|
---|
526 | if (!Main.map.mapView.isActiveLayerVisible())
|
---|
527 | return;
|
---|
528 |
|
---|
529 | if (mode == Mode.select) {
|
---|
530 | // Nothing to be done
|
---|
531 | } else {
|
---|
532 | if (mode == Mode.create_new) {
|
---|
533 | if (e.getPoint().distance(initialMousePos) > initialMoveThreshold && newN1en != null) {
|
---|
534 | createNewRectangle();
|
---|
535 | }
|
---|
536 | } else if (mode == Mode.extrude) {
|
---|
537 | if (e.getClickCount() == 2 && e.getPoint().equals(initialMousePos)) {
|
---|
538 | // double click adds a new node
|
---|
539 | addNewNode(e);
|
---|
540 | } else if (e.getPoint().distance(initialMousePos) > initialMoveThreshold && newN1en != null && selectedSegment != null) {
|
---|
541 | // main extrusion commands
|
---|
542 | performExtrusion();
|
---|
543 | }
|
---|
544 | } else if (mode == Mode.translate || mode == Mode.translate_node) {
|
---|
545 | //Commit translate
|
---|
546 | //the move command is already committed in mouseDragged
|
---|
547 | joinNodesIfCollapsed(movingNodeList);
|
---|
548 | }
|
---|
549 |
|
---|
550 | updateKeyModifiers(e);
|
---|
551 | // Switch back into select mode
|
---|
552 | Main.map.mapView.setNewCursor(ctrl ? cursorTranslate : alt ? cursorCreateNew : shift ? cursorCreateNodes : cursor, this);
|
---|
553 | Main.map.mapView.removeTemporaryLayer(this);
|
---|
554 | selectedSegment = null;
|
---|
555 | moveCommand = null;
|
---|
556 | mode = Mode.select;
|
---|
557 | dualAlignSegmentCollapsed = false;
|
---|
558 | updateStatusLine();
|
---|
559 | Main.map.mapView.repaint();
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|
563 | // -------------------------------------------------------------------------
|
---|
564 | // Custom methods
|
---|
565 | // -------------------------------------------------------------------------
|
---|
566 |
|
---|
567 | /**
|
---|
568 | * Inserts node into nearby segment.
|
---|
569 | * @param e current mouse point
|
---|
570 | */
|
---|
571 | private static void addNewNode(MouseEvent e) {
|
---|
572 | // Should maybe do the same as in DrawAction and fetch all nearby segments?
|
---|
573 | WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint(), OsmPrimitive::isSelectable);
|
---|
574 | if (ws != null) {
|
---|
575 | Node n = new Node(Main.map.mapView.getLatLon(e.getX(), e.getY()));
|
---|
576 | EastNorth a = ws.getFirstNode().getEastNorth();
|
---|
577 | EastNorth b = ws.getSecondNode().getEastNorth();
|
---|
578 | n.setEastNorth(Geometry.closestPointToSegment(a, b, n.getEastNorth()));
|
---|
579 | Way wnew = new Way(ws.way);
|
---|
580 | wnew.addNode(ws.lowerIndex+1, n);
|
---|
581 | SequenceCommand cmds = new SequenceCommand(tr("Add a new node to an existing way"),
|
---|
582 | new AddCommand(n), new ChangeCommand(ws.way, wnew));
|
---|
583 | Main.main.undoRedo.add(cmds);
|
---|
584 | }
|
---|
585 | }
|
---|
586 |
|
---|
587 | /**
|
---|
588 | * Creates a new way that shares segment with selected way.
|
---|
589 | */
|
---|
590 | private void createNewRectangle() {
|
---|
591 | if (selectedSegment == null) return;
|
---|
592 | // crete a new rectangle
|
---|
593 | Collection<Command> cmds = new LinkedList<>();
|
---|
594 | Node third = new Node(newN2en);
|
---|
595 | Node fourth = new Node(newN1en);
|
---|
596 | Way wnew = new Way();
|
---|
597 | wnew.addNode(selectedSegment.getFirstNode());
|
---|
598 | wnew.addNode(selectedSegment.getSecondNode());
|
---|
599 | wnew.addNode(third);
|
---|
600 | if (!dualAlignSegmentCollapsed) {
|
---|
601 | // rectangle can degrade to triangle for dual alignment after collapsing
|
---|
602 | wnew.addNode(fourth);
|
---|
603 | }
|
---|
604 | // ... and close the way
|
---|
605 | wnew.addNode(selectedSegment.getFirstNode());
|
---|
606 | // undo support
|
---|
607 | cmds.add(new AddCommand(third));
|
---|
608 | if (!dualAlignSegmentCollapsed) {
|
---|
609 | cmds.add(new AddCommand(fourth));
|
---|
610 | }
|
---|
611 | cmds.add(new AddCommand(wnew));
|
---|
612 | Command c = new SequenceCommand(tr("Extrude Way"), cmds);
|
---|
613 | Main.main.undoRedo.add(c);
|
---|
614 | getLayerManager().getEditDataSet().setSelected(wnew);
|
---|
615 | }
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * Does actual extrusion of {@link #selectedSegment}.
|
---|
619 | * Uses {@link #initialN1en}, {@link #initialN2en} saved in calculatePossibleDirections* call
|
---|
620 | * Uses {@link #newN1en}, {@link #newN2en} calculated by {@link #calculateBestMovementAndNewNodes}
|
---|
621 | */
|
---|
622 | private void performExtrusion() {
|
---|
623 | // create extrusion
|
---|
624 | Collection<Command> cmds = new LinkedList<>();
|
---|
625 | Way wnew = new Way(selectedSegment.way);
|
---|
626 | boolean wayWasModified = false;
|
---|
627 | boolean wayWasSingleSegment = wnew.getNodesCount() == 2;
|
---|
628 | int insertionPoint = selectedSegment.lowerIndex + 1;
|
---|
629 |
|
---|
630 | //find if the new points overlap existing segments (in case of 90 degree angles)
|
---|
631 | Node prevNode = getPreviousNode(selectedSegment.lowerIndex);
|
---|
632 | boolean nodeOverlapsSegment = prevNode != null && Geometry.segmentsParallel(initialN1en, prevNode.getEastNorth(), initialN1en, newN1en);
|
---|
633 | // segmentAngleZero marks subset of nodeOverlapsSegment.
|
---|
634 | // nodeOverlapsSegment is true if angle between segments is 0 or PI, segmentAngleZero only if angle is 0
|
---|
635 | boolean segmentAngleZero = prevNode != null && Math.abs(Geometry.getCornerAngle(prevNode.getEastNorth(), initialN1en, newN1en)) < 1e-5;
|
---|
636 | boolean hasOtherWays = hasNodeOtherWays(selectedSegment.getFirstNode(), selectedSegment.way);
|
---|
637 | List<Node> changedNodes = new ArrayList<>();
|
---|
638 | if (nodeOverlapsSegment && !alwaysCreateNodes && !hasOtherWays) {
|
---|
639 | //move existing node
|
---|
640 | Node n1Old = selectedSegment.getFirstNode();
|
---|
641 | cmds.add(new MoveCommand(n1Old, Main.getProjection().eastNorth2latlon(newN1en)));
|
---|
642 | changedNodes.add(n1Old);
|
---|
643 | } else if (ignoreSharedNodes && segmentAngleZero && !alwaysCreateNodes && hasOtherWays) {
|
---|
644 | // replace shared node with new one
|
---|
645 | Node n1Old = selectedSegment.getFirstNode();
|
---|
646 | Node n1New = new Node(Main.getProjection().eastNorth2latlon(newN1en));
|
---|
647 | wnew.addNode(insertionPoint, n1New);
|
---|
648 | wnew.removeNode(n1Old);
|
---|
649 | wayWasModified = true;
|
---|
650 | cmds.add(new AddCommand(n1New));
|
---|
651 | changedNodes.add(n1New);
|
---|
652 | } else {
|
---|
653 | //introduce new node
|
---|
654 | Node n1New = new Node(Main.getProjection().eastNorth2latlon(newN1en));
|
---|
655 | wnew.addNode(insertionPoint, n1New);
|
---|
656 | wayWasModified = true;
|
---|
657 | insertionPoint++;
|
---|
658 | cmds.add(new AddCommand(n1New));
|
---|
659 | changedNodes.add(n1New);
|
---|
660 | }
|
---|
661 |
|
---|
662 | //find if the new points overlap existing segments (in case of 90 degree angles)
|
---|
663 | Node nextNode = getNextNode(selectedSegment.lowerIndex + 1);
|
---|
664 | nodeOverlapsSegment = nextNode != null && Geometry.segmentsParallel(initialN2en, nextNode.getEastNorth(), initialN2en, newN2en);
|
---|
665 | segmentAngleZero = nextNode != null && Math.abs(Geometry.getCornerAngle(nextNode.getEastNorth(), initialN2en, newN2en)) < 1e-5;
|
---|
666 | hasOtherWays = hasNodeOtherWays(selectedSegment.getSecondNode(), selectedSegment.way);
|
---|
667 |
|
---|
668 | if (nodeOverlapsSegment && !alwaysCreateNodes && !hasOtherWays) {
|
---|
669 | //move existing node
|
---|
670 | Node n2Old = selectedSegment.getSecondNode();
|
---|
671 | cmds.add(new MoveCommand(n2Old, Main.getProjection().eastNorth2latlon(newN2en)));
|
---|
672 | changedNodes.add(n2Old);
|
---|
673 | } else if (ignoreSharedNodes && segmentAngleZero && !alwaysCreateNodes && hasOtherWays) {
|
---|
674 | // replace shared node with new one
|
---|
675 | Node n2Old = selectedSegment.getSecondNode();
|
---|
676 | Node n2New = new Node(Main.getProjection().eastNorth2latlon(newN2en));
|
---|
677 | wnew.addNode(insertionPoint, n2New);
|
---|
678 | wnew.removeNode(n2Old);
|
---|
679 | wayWasModified = true;
|
---|
680 | cmds.add(new AddCommand(n2New));
|
---|
681 | changedNodes.add(n2New);
|
---|
682 | } else {
|
---|
683 | //introduce new node
|
---|
684 | Node n2New = new Node(Main.getProjection().eastNorth2latlon(newN2en));
|
---|
685 | wnew.addNode(insertionPoint, n2New);
|
---|
686 | wayWasModified = true;
|
---|
687 | cmds.add(new AddCommand(n2New));
|
---|
688 | changedNodes.add(n2New);
|
---|
689 | }
|
---|
690 |
|
---|
691 | //the way was a single segment, close the way
|
---|
692 | if (wayWasSingleSegment) {
|
---|
693 | wnew.addNode(selectedSegment.getFirstNode());
|
---|
694 | wayWasModified = true;
|
---|
695 | }
|
---|
696 | if (wayWasModified) {
|
---|
697 | // we only need to change the way if its node list was really modified
|
---|
698 | cmds.add(new ChangeCommand(selectedSegment.way, wnew));
|
---|
699 | }
|
---|
700 | Command c = new SequenceCommand(tr("Extrude Way"), cmds);
|
---|
701 | Main.main.undoRedo.add(c);
|
---|
702 | joinNodesIfCollapsed(changedNodes);
|
---|
703 | }
|
---|
704 |
|
---|
705 | private void joinNodesIfCollapsed(List<Node> changedNodes) {
|
---|
706 | if (!dualAlignActive || newN1en == null || newN2en == null) return;
|
---|
707 | if (newN1en.distance(newN2en) > 1e-6) return;
|
---|
708 | // If the dual alignment moved two nodes to the same point, merge them
|
---|
709 | Node targetNode = MergeNodesAction.selectTargetNode(changedNodes);
|
---|
710 | Node locNode = MergeNodesAction.selectTargetLocationNode(changedNodes);
|
---|
711 | Command mergeCmd = MergeNodesAction.mergeNodes(Main.getLayerManager().getEditLayer(), changedNodes, targetNode, locNode);
|
---|
712 | if (mergeCmd != null) {
|
---|
713 | Main.main.undoRedo.add(mergeCmd);
|
---|
714 | } else {
|
---|
715 | // undo extruding command itself
|
---|
716 | Main.main.undoRedo.undo();
|
---|
717 | }
|
---|
718 | }
|
---|
719 |
|
---|
720 | /**
|
---|
721 | * This method tests if {@code node} has other ways apart from the given one.
|
---|
722 | * @param node node to test
|
---|
723 | * @param myWay way known to contain this node
|
---|
724 | * @return {@code true} if {@code node} belongs only to {@code myWay}, false if there are more ways.
|
---|
725 | */
|
---|
726 | private static boolean hasNodeOtherWays(Node node, Way myWay) {
|
---|
727 | for (OsmPrimitive p : node.getReferrers()) {
|
---|
728 | if (p instanceof Way && p.isUsable() && p != myWay)
|
---|
729 | return true;
|
---|
730 | }
|
---|
731 | return false;
|
---|
732 | }
|
---|
733 |
|
---|
734 | /**
|
---|
735 | * Determines best movement from {@link #initialMousePos} to current mouse position,
|
---|
736 | * choosing one of the directions from {@link #possibleMoveDirections}.
|
---|
737 | * @param mouseEn current mouse position
|
---|
738 | * @return movement vector
|
---|
739 | */
|
---|
740 | private EastNorth calculateBestMovement(EastNorth mouseEn) {
|
---|
741 |
|
---|
742 | EastNorth initialMouseEn = Main.map.mapView.getEastNorth(initialMousePos.x, initialMousePos.y);
|
---|
743 | EastNorth mouseMovement = mouseEn.subtract(initialMouseEn);
|
---|
744 |
|
---|
745 | double bestDistance = Double.POSITIVE_INFINITY;
|
---|
746 | EastNorth bestMovement = null;
|
---|
747 | activeMoveDirection = null;
|
---|
748 |
|
---|
749 | //find the best movement direction and vector
|
---|
750 | for (ReferenceSegment direction : possibleMoveDirections) {
|
---|
751 | EastNorth movement = calculateSegmentOffset(initialN1en, initialN2en, direction.en, mouseEn);
|
---|
752 | if (movement == null) {
|
---|
753 | //if direction parallel to segment.
|
---|
754 | continue;
|
---|
755 | }
|
---|
756 |
|
---|
757 | double distanceFromMouseMovement = movement.distance(mouseMovement);
|
---|
758 | if (bestDistance > distanceFromMouseMovement) {
|
---|
759 | bestDistance = distanceFromMouseMovement;
|
---|
760 | activeMoveDirection = direction;
|
---|
761 | bestMovement = movement;
|
---|
762 | }
|
---|
763 | }
|
---|
764 | return bestMovement;
|
---|
765 | }
|
---|
766 |
|
---|
767 | /***
|
---|
768 | * This method calculates offset amount by which to move the given segment
|
---|
769 | * perpendicularly for it to be in line with mouse position.
|
---|
770 | * @param segmentP1 segment's first point
|
---|
771 | * @param segmentP2 segment's second point
|
---|
772 | * @param moveDirection direction of movement
|
---|
773 | * @param targetPos mouse position
|
---|
774 | * @return offset amount of P1 and P2.
|
---|
775 | */
|
---|
776 | private static EastNorth calculateSegmentOffset(EastNorth segmentP1, EastNorth segmentP2, EastNorth moveDirection,
|
---|
777 | EastNorth targetPos) {
|
---|
778 | EastNorth intersectionPoint;
|
---|
779 | if (segmentP1.distanceSq(segmentP2) > 1e-7) {
|
---|
780 | intersectionPoint = Geometry.getLineLineIntersection(segmentP1, segmentP2, targetPos, targetPos.add(moveDirection));
|
---|
781 | } else {
|
---|
782 | intersectionPoint = Geometry.closestPointToLine(targetPos, targetPos.add(moveDirection), segmentP1);
|
---|
783 | }
|
---|
784 |
|
---|
785 | if (intersectionPoint == null)
|
---|
786 | return null;
|
---|
787 | else
|
---|
788 | //return distance form base to target position
|
---|
789 | return targetPos.subtract(intersectionPoint);
|
---|
790 | }
|
---|
791 |
|
---|
792 | /**
|
---|
793 | * Gathers possible move directions - perpendicular to the selected segment
|
---|
794 | * and parallel to neighboring segments.
|
---|
795 | */
|
---|
796 | private void calculatePossibleDirectionsBySegment() {
|
---|
797 | // remember initial positions for segment nodes.
|
---|
798 | initialN1en = selectedSegment.getFirstNode().getEastNorth();
|
---|
799 | initialN2en = selectedSegment.getSecondNode().getEastNorth();
|
---|
800 |
|
---|
801 | //add direction perpendicular to the selected segment
|
---|
802 | possibleMoveDirections = new ArrayList<>();
|
---|
803 | possibleMoveDirections.add(new ReferenceSegment(new EastNorth(
|
---|
804 | initialN1en.getY() - initialN2en.getY(),
|
---|
805 | initialN2en.getX() - initialN1en.getX()
|
---|
806 | ), initialN1en, initialN2en, true));
|
---|
807 |
|
---|
808 |
|
---|
809 | //add directions parallel to neighbor segments
|
---|
810 | Node prevNode = getPreviousNode(selectedSegment.lowerIndex);
|
---|
811 | if (prevNode != null) {
|
---|
812 | EastNorth en = prevNode.getEastNorth();
|
---|
813 | possibleMoveDirections.add(new ReferenceSegment(new EastNorth(
|
---|
814 | initialN1en.getX() - en.getX(),
|
---|
815 | initialN1en.getY() - en.getY()
|
---|
816 | ), initialN1en, en, false));
|
---|
817 | }
|
---|
818 |
|
---|
819 | Node nextNode = getNextNode(selectedSegment.lowerIndex + 1);
|
---|
820 | if (nextNode != null) {
|
---|
821 | EastNorth en = nextNode.getEastNorth();
|
---|
822 | possibleMoveDirections.add(new ReferenceSegment(new EastNorth(
|
---|
823 | initialN2en.getX() - en.getX(),
|
---|
824 | initialN2en.getY() - en.getY()
|
---|
825 | ), initialN2en, en, false));
|
---|
826 | }
|
---|
827 | }
|
---|
828 |
|
---|
829 | /**
|
---|
830 | * Gathers possible move directions - along all adjacent segments.
|
---|
831 | */
|
---|
832 | private void calculatePossibleDirectionsByNode() {
|
---|
833 | // remember initial positions for segment nodes.
|
---|
834 | initialN1en = selectedNode.getEastNorth();
|
---|
835 | initialN2en = initialN1en;
|
---|
836 | possibleMoveDirections = new ArrayList<>();
|
---|
837 | for (OsmPrimitive p: selectedNode.getReferrers()) {
|
---|
838 | if (p instanceof Way && p.isUsable()) {
|
---|
839 | for (Node neighbor: ((Way) p).getNeighbours(selectedNode)) {
|
---|
840 | EastNorth en = neighbor.getEastNorth();
|
---|
841 | possibleMoveDirections.add(new ReferenceSegment(new EastNorth(
|
---|
842 | initialN1en.getX() - en.getX(),
|
---|
843 | initialN1en.getY() - en.getY()
|
---|
844 | ), initialN1en, en, false));
|
---|
845 | }
|
---|
846 | }
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | /**
|
---|
851 | * Checks dual alignment conditions:
|
---|
852 | * 1. selected segment has both neighboring segments,
|
---|
853 | * 2. selected segment is not parallel with neighboring segments.
|
---|
854 | * @return {@code true} if dual alignment conditions are satisfied
|
---|
855 | */
|
---|
856 | private boolean checkDualAlignConditions() {
|
---|
857 | Node prevNode = getPreviousNode(selectedSegment.lowerIndex);
|
---|
858 | Node nextNode = getNextNode(selectedSegment.lowerIndex + 1);
|
---|
859 | if (prevNode == null || nextNode == null) {
|
---|
860 | return false;
|
---|
861 | }
|
---|
862 |
|
---|
863 | EastNorth n1en = selectedSegment.getFirstNode().getEastNorth();
|
---|
864 | EastNorth n2en = selectedSegment.getSecondNode().getEastNorth();
|
---|
865 | if (n1en.distance(prevNode.getEastNorth()) < 1e-4 ||
|
---|
866 | n2en.distance(nextNode.getEastNorth()) < 1e-4) {
|
---|
867 | return false;
|
---|
868 | }
|
---|
869 |
|
---|
870 | boolean prevSegmentParallel = Geometry.segmentsParallel(n1en, prevNode.getEastNorth(), n1en, n2en);
|
---|
871 | boolean nextSegmentParallel = Geometry.segmentsParallel(n2en, nextNode.getEastNorth(), n1en, n2en);
|
---|
872 | if (prevSegmentParallel || nextSegmentParallel) {
|
---|
873 | return false;
|
---|
874 | }
|
---|
875 |
|
---|
876 | return true;
|
---|
877 | }
|
---|
878 |
|
---|
879 | /**
|
---|
880 | * Gathers possible move directions - perpendicular to the selected segment only.
|
---|
881 | * Neighboring segments go to {@link #dualAlignSegment1} and {@link #dualAlignSegment2}.
|
---|
882 | */
|
---|
883 | private void calculatePossibleDirectionsForDualAlign() {
|
---|
884 | // remember initial positions for segment nodes.
|
---|
885 | initialN1en = selectedSegment.getFirstNode().getEastNorth();
|
---|
886 | initialN2en = selectedSegment.getSecondNode().getEastNorth();
|
---|
887 |
|
---|
888 | // add direction perpendicular to the selected segment
|
---|
889 | possibleMoveDirections = new ArrayList<>();
|
---|
890 | possibleMoveDirections.add(new ReferenceSegment(new EastNorth(
|
---|
891 | initialN1en.getY() - initialN2en.getY(),
|
---|
892 | initialN2en.getX() - initialN1en.getX()
|
---|
893 | ), initialN1en, initialN2en, true));
|
---|
894 |
|
---|
895 | // set neighboring segments
|
---|
896 | Node prevNode = getPreviousNode(selectedSegment.lowerIndex);
|
---|
897 | if (prevNode != null) {
|
---|
898 | EastNorth prevNodeEn = prevNode.getEastNorth();
|
---|
899 | dualAlignSegment1 = new ReferenceSegment(new EastNorth(
|
---|
900 | initialN1en.getX() - prevNodeEn.getX(),
|
---|
901 | initialN1en.getY() - prevNodeEn.getY()
|
---|
902 | ), initialN1en, prevNodeEn, false);
|
---|
903 | }
|
---|
904 |
|
---|
905 | Node nextNode = getNextNode(selectedSegment.lowerIndex + 1);
|
---|
906 | if (nextNode != null) {
|
---|
907 | EastNorth nextNodeEn = nextNode.getEastNorth();
|
---|
908 | dualAlignSegment2 = new ReferenceSegment(new EastNorth(
|
---|
909 | initialN2en.getX() - nextNodeEn.getX(),
|
---|
910 | initialN2en.getY() - nextNodeEn.getY()
|
---|
911 | ), initialN2en, nextNodeEn, false);
|
---|
912 | }
|
---|
913 | }
|
---|
914 |
|
---|
915 | /**
|
---|
916 | * Calculate newN1en, newN2en best suitable for given mouse coordinates
|
---|
917 | * For dual align, calculates positions of new nodes, aligning them to neighboring segments.
|
---|
918 | * Elsewhere, just adds the vetor returned by calculateBestMovement to {@link #initialN1en}, {@link #initialN2en}.
|
---|
919 | * @param mouseEn mouse coordinates
|
---|
920 | * @return best movement vector
|
---|
921 | */
|
---|
922 | private EastNorth calculateBestMovementAndNewNodes(EastNorth mouseEn) {
|
---|
923 | EastNorth bestMovement = calculateBestMovement(mouseEn);
|
---|
924 | EastNorth n1movedEn = initialN1en.add(bestMovement), n2movedEn;
|
---|
925 |
|
---|
926 | // find out the movement distance, in metres
|
---|
927 | double distance = Main.getProjection().eastNorth2latlon(initialN1en).greatCircleDistance(
|
---|
928 | Main.getProjection().eastNorth2latlon(n1movedEn));
|
---|
929 | Main.map.statusLine.setDist(distance);
|
---|
930 | updateStatusLine();
|
---|
931 |
|
---|
932 | if (dualAlignActive) {
|
---|
933 | // new positions of selected segment's nodes, without applying dual alignment
|
---|
934 | n1movedEn = initialN1en.add(bestMovement);
|
---|
935 | n2movedEn = initialN2en.add(bestMovement);
|
---|
936 |
|
---|
937 | // calculate intersections of parallel shifted segment and the adjacent lines
|
---|
938 | newN1en = Geometry.getLineLineIntersection(n1movedEn, n2movedEn, dualAlignSegment1.p1, dualAlignSegment1.p2);
|
---|
939 | newN2en = Geometry.getLineLineIntersection(n1movedEn, n2movedEn, dualAlignSegment2.p1, dualAlignSegment2.p2);
|
---|
940 | if (newN1en == null || newN2en == null) return bestMovement;
|
---|
941 | if (keepSegmentDirection && isOppositeDirection(newN1en, newN2en, initialN1en, initialN2en)) {
|
---|
942 | EastNorth collapsedSegmentPosition = Geometry.getLineLineIntersection(dualAlignSegment1.p1, dualAlignSegment1.p2,
|
---|
943 | dualAlignSegment2.p1, dualAlignSegment2.p2);
|
---|
944 | newN1en = collapsedSegmentPosition;
|
---|
945 | newN2en = collapsedSegmentPosition;
|
---|
946 | dualAlignSegmentCollapsed = true;
|
---|
947 | } else {
|
---|
948 | dualAlignSegmentCollapsed = false;
|
---|
949 | }
|
---|
950 | } else {
|
---|
951 | newN1en = n1movedEn;
|
---|
952 | newN2en = initialN2en.add(bestMovement);
|
---|
953 | }
|
---|
954 | return bestMovement;
|
---|
955 | }
|
---|
956 |
|
---|
957 | /**
|
---|
958 | * Gets a node index from selected way before given index.
|
---|
959 | * @param index index of current node
|
---|
960 | * @return index of previous node or <code>-1</code> if there are no nodes there.
|
---|
961 | */
|
---|
962 | private int getPreviousNodeIndex(int index) {
|
---|
963 | if (index > 0)
|
---|
964 | return index - 1;
|
---|
965 | else if (selectedSegment.way.isClosed())
|
---|
966 | return selectedSegment.way.getNodesCount() - 2;
|
---|
967 | else
|
---|
968 | return -1;
|
---|
969 | }
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * Gets a node from selected way before given index.
|
---|
973 | * @param index index of current node
|
---|
974 | * @return previous node or <code>null</code> if there are no nodes there.
|
---|
975 | */
|
---|
976 | private Node getPreviousNode(int index) {
|
---|
977 | int indexPrev = getPreviousNodeIndex(index);
|
---|
978 | if (indexPrev >= 0)
|
---|
979 | return selectedSegment.way.getNode(indexPrev);
|
---|
980 | else
|
---|
981 | return null;
|
---|
982 | }
|
---|
983 |
|
---|
984 |
|
---|
985 | /**
|
---|
986 | * Gets a node index from selected way after given index.
|
---|
987 | * @param index index of current node
|
---|
988 | * @return index of next node or <code>-1</code> if there are no nodes there.
|
---|
989 | */
|
---|
990 | private int getNextNodeIndex(int index) {
|
---|
991 | int count = selectedSegment.way.getNodesCount();
|
---|
992 | if (index < count - 1)
|
---|
993 | return index + 1;
|
---|
994 | else if (selectedSegment.way.isClosed())
|
---|
995 | return 1;
|
---|
996 | else
|
---|
997 | return -1;
|
---|
998 | }
|
---|
999 |
|
---|
1000 | /**
|
---|
1001 | * Gets a node from selected way after given index.
|
---|
1002 | * @param index index of current node
|
---|
1003 | * @return next node or <code>null</code> if there are no nodes there.
|
---|
1004 | */
|
---|
1005 | private Node getNextNode(int index) {
|
---|
1006 | int indexNext = getNextNodeIndex(index);
|
---|
1007 | if (indexNext >= 0)
|
---|
1008 | return selectedSegment.way.getNode(indexNext);
|
---|
1009 | else
|
---|
1010 | return null;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | // -------------------------------------------------------------------------
|
---|
1014 | // paint methods
|
---|
1015 | // -------------------------------------------------------------------------
|
---|
1016 |
|
---|
1017 | @Override
|
---|
1018 | public void paint(Graphics2D g, MapView mv, Bounds box) {
|
---|
1019 | Graphics2D g2 = g;
|
---|
1020 | if (mode == Mode.select) {
|
---|
1021 | // Nothing to do
|
---|
1022 | } else {
|
---|
1023 | if (newN1en != null) {
|
---|
1024 |
|
---|
1025 | EastNorth p1 = initialN1en;
|
---|
1026 | EastNorth p2 = initialN2en;
|
---|
1027 | EastNorth p3 = newN1en;
|
---|
1028 | EastNorth p4 = newN2en;
|
---|
1029 |
|
---|
1030 | Point2D normalUnitVector = activeMoveDirection != null ? getNormalUniVector() : null;
|
---|
1031 |
|
---|
1032 | if (mode == Mode.extrude || mode == Mode.create_new) {
|
---|
1033 | g2.setColor(mainColor);
|
---|
1034 | g2.setStroke(mainStroke);
|
---|
1035 | // Draw rectangle around new area.
|
---|
1036 | MapViewPath b = new MapViewPath(mv);
|
---|
1037 | b.moveTo(p1);
|
---|
1038 | b.lineTo(p3);
|
---|
1039 | b.lineTo(p4);
|
---|
1040 | b.lineTo(p2);
|
---|
1041 | b.lineTo(p1);
|
---|
1042 | g2.draw(b);
|
---|
1043 |
|
---|
1044 | if (dualAlignActive) {
|
---|
1045 | // Draw reference ways
|
---|
1046 | drawReferenceSegment(g2, mv, dualAlignSegment1);
|
---|
1047 | drawReferenceSegment(g2, mv, dualAlignSegment2);
|
---|
1048 | } else if (activeMoveDirection != null && normalUnitVector != null) {
|
---|
1049 | // Draw reference way
|
---|
1050 | drawReferenceSegment(g2, mv, activeMoveDirection);
|
---|
1051 |
|
---|
1052 | // Draw right angle marker on first node position, only when moving at right angle
|
---|
1053 | if (activeMoveDirection.perpendicular) {
|
---|
1054 | // mirror RightAngle marker, so it is inside the extrude
|
---|
1055 | double headingRefWS = activeMoveDirection.p1.heading(activeMoveDirection.p2);
|
---|
1056 | double headingMoveDir = Math.atan2(normalUnitVector.getY(), normalUnitVector.getX());
|
---|
1057 | double headingDiff = headingRefWS - headingMoveDir;
|
---|
1058 | if (headingDiff < 0)
|
---|
1059 | headingDiff += 2 * Math.PI;
|
---|
1060 | boolean mirrorRA = Math.abs(headingDiff - Math.PI) > 1e-5;
|
---|
1061 | Point pr1 = mv.getPoint(activeMoveDirection.p1);
|
---|
1062 | drawAngleSymbol(g2, pr1, normalUnitVector, mirrorRA);
|
---|
1063 | }
|
---|
1064 | }
|
---|
1065 | } else if (mode == Mode.translate || mode == Mode.translate_node) {
|
---|
1066 | g2.setColor(mainColor);
|
---|
1067 | if (p1.distance(p2) < 3) {
|
---|
1068 | g2.setStroke(mainStroke);
|
---|
1069 | g2.draw(new MapViewPath(mv).shapeAround(p1, SymbolShape.CIRCLE, symbolSize));
|
---|
1070 | } else {
|
---|
1071 | g2.setStroke(oldLineStroke);
|
---|
1072 | g2.draw(new MapViewPath(mv).moveTo(p1).lineTo(p2));
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | if (dualAlignActive) {
|
---|
1076 | // Draw reference ways
|
---|
1077 | drawReferenceSegment(g2, mv, dualAlignSegment1);
|
---|
1078 | drawReferenceSegment(g2, mv, dualAlignSegment2);
|
---|
1079 | } else if (activeMoveDirection != null) {
|
---|
1080 |
|
---|
1081 | g2.setColor(helperColor);
|
---|
1082 | g2.setStroke(helperStrokeDash);
|
---|
1083 | // Draw a guideline along the normal.
|
---|
1084 | Line2D normline;
|
---|
1085 | Point2D centerpoint = mv.getPoint2D(p1.interpolate(p2, .5));
|
---|
1086 | normline = createSemiInfiniteLine(centerpoint, normalUnitVector, g2);
|
---|
1087 | g2.draw(normline);
|
---|
1088 | // Draw right angle marker on initial position, only when moving at right angle
|
---|
1089 | if (activeMoveDirection.perpendicular) {
|
---|
1090 | // EastNorth units per pixel
|
---|
1091 | g2.setStroke(helperStrokeRA);
|
---|
1092 | g2.setColor(mainColor);
|
---|
1093 | drawAngleSymbol(g2, centerpoint, normalUnitVector, false);
|
---|
1094 | }
|
---|
1095 | }
|
---|
1096 | }
|
---|
1097 | }
|
---|
1098 | g2.setStroke(helperStrokeRA); // restore default stroke to prevent starnge occasional drawings
|
---|
1099 | }
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | private Point2D getNormalUniVector() {
|
---|
1103 | double fac = 1.0 / activeMoveDirection.en.length();
|
---|
1104 | // mult by factor to get unit vector.
|
---|
1105 | Point2D normalUnitVector = new Point2D.Double(activeMoveDirection.en.getX() * fac, activeMoveDirection.en.getY() * fac);
|
---|
1106 |
|
---|
1107 | // Check to see if our new N1 is in a positive direction with respect to the normalUnitVector.
|
---|
1108 | // Even if the x component is zero, we should still be able to discern using +0.0 and -0.0
|
---|
1109 | if (newN1en != null && ((newN1en.getX() > initialN1en.getX()) != (normalUnitVector.getX() > -0.0))) {
|
---|
1110 | // If not, use a sign-flipped version of the normalUnitVector.
|
---|
1111 | normalUnitVector = new Point2D.Double(-normalUnitVector.getX(), -normalUnitVector.getY());
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | //HACK: swap Y, because the target pixels are top down, but EastNorth is bottom-up.
|
---|
1115 | //This is normally done by MapView.getPoint, but it does not work on vectors.
|
---|
1116 | normalUnitVector.setLocation(normalUnitVector.getX(), -normalUnitVector.getY());
|
---|
1117 | return normalUnitVector;
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | /**
|
---|
1121 | * Determines if from1-to1 and from2-to2 vectors directions are opposite
|
---|
1122 | * @param from1 vector1 start
|
---|
1123 | * @param to1 vector1 end
|
---|
1124 | * @param from2 vector2 start
|
---|
1125 | * @param to2 vector2 end
|
---|
1126 | * @return true if from1-to1 and from2-to2 vectors directions are opposite
|
---|
1127 | */
|
---|
1128 | private static boolean isOppositeDirection(EastNorth from1, EastNorth to1, EastNorth from2, EastNorth to2) {
|
---|
1129 | return (from1.getX()-to1.getX())*(from2.getX()-to2.getX())
|
---|
1130 | +(from1.getY()-to1.getY())*(from2.getY()-to2.getY()) < 0;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | /**
|
---|
1134 | * Draws right angle symbol at specified position.
|
---|
1135 | * @param g2 the Graphics2D object used to draw on
|
---|
1136 | * @param center center point of angle
|
---|
1137 | * @param normal vector of normal
|
---|
1138 | * @param mirror {@code true} if symbol should be mirrored by the normal
|
---|
1139 | */
|
---|
1140 | private void drawAngleSymbol(Graphics2D g2, Point2D center, Point2D normal, boolean mirror) {
|
---|
1141 | // EastNorth units per pixel
|
---|
1142 | double factor = 1.0/g2.getTransform().getScaleX();
|
---|
1143 | double raoffsetx = symbolSize*factor*normal.getX();
|
---|
1144 | double raoffsety = symbolSize*factor*normal.getY();
|
---|
1145 |
|
---|
1146 | double cx = center.getX(), cy = center.getY();
|
---|
1147 | double k = mirror ? -1 : 1;
|
---|
1148 | Point2D ra1 = new Point2D.Double(cx + raoffsetx, cy + raoffsety);
|
---|
1149 | Point2D ra3 = new Point2D.Double(cx - raoffsety*k, cy + raoffsetx*k);
|
---|
1150 | Point2D ra2 = new Point2D.Double(ra1.getX() - raoffsety*k, ra1.getY() + raoffsetx*k);
|
---|
1151 |
|
---|
1152 | GeneralPath ra = new GeneralPath();
|
---|
1153 | ra.moveTo((float) ra1.getX(), (float) ra1.getY());
|
---|
1154 | ra.lineTo((float) ra2.getX(), (float) ra2.getY());
|
---|
1155 | ra.lineTo((float) ra3.getX(), (float) ra3.getY());
|
---|
1156 | g2.setStroke(helperStrokeRA);
|
---|
1157 | g2.draw(ra);
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | /**
|
---|
1161 | * Draws given reference segment.
|
---|
1162 | * @param g2 the Graphics2D object used to draw on
|
---|
1163 | * @param mv map view
|
---|
1164 | * @param seg the reference segment
|
---|
1165 | */
|
---|
1166 | private void drawReferenceSegment(Graphics2D g2, MapView mv, ReferenceSegment seg) {
|
---|
1167 | g2.setColor(helperColor);
|
---|
1168 | g2.setStroke(helperStrokeDash);
|
---|
1169 | g2.draw(new MapViewPath(mv).moveTo(seg.p1).lineTo(seg.p2));
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | /**
|
---|
1173 | * Creates a new Line that extends off the edge of the viewport in one direction
|
---|
1174 | * @param start The start point of the line
|
---|
1175 | * @param unitvector A unit vector denoting the direction of the line
|
---|
1176 | * @param g the Graphics2D object it will be used on
|
---|
1177 | * @return created line
|
---|
1178 | */
|
---|
1179 | private static Line2D createSemiInfiniteLine(Point2D start, Point2D unitvector, Graphics2D g) {
|
---|
1180 | Rectangle bounds = g.getDeviceConfiguration().getBounds();
|
---|
1181 | try {
|
---|
1182 | AffineTransform invtrans = g.getTransform().createInverse();
|
---|
1183 | Point2D widthpoint = invtrans.deltaTransform(new Point2D.Double(bounds.width, 0), null);
|
---|
1184 | Point2D heightpoint = invtrans.deltaTransform(new Point2D.Double(0, bounds.height), null);
|
---|
1185 |
|
---|
1186 | // Here we should end up with a gross overestimate of the maximum viewport diagonal in what
|
---|
1187 | // Graphics2D calls 'user space'. Essentially a manhattan distance of manhattan distances.
|
---|
1188 | // This can be used as a safe length of line to generate which will always go off-viewport.
|
---|
1189 | double linelength = Math.abs(widthpoint.getX()) + Math.abs(widthpoint.getY())
|
---|
1190 | + Math.abs(heightpoint.getX()) + Math.abs(heightpoint.getY());
|
---|
1191 |
|
---|
1192 | return new Line2D.Double(start, new Point2D.Double(start.getX() + (unitvector.getX() * linelength), start.getY()
|
---|
1193 | + (unitvector.getY() * linelength)));
|
---|
1194 | } catch (NoninvertibleTransformException e) {
|
---|
1195 | Main.debug(e);
|
---|
1196 | return new Line2D.Double(start, new Point2D.Double(start.getX() + (unitvector.getX() * 10), start.getY()
|
---|
1197 | + (unitvector.getY() * 10)));
|
---|
1198 | }
|
---|
1199 | }
|
---|
1200 | }
|
---|