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