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