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