1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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.Cursor;
|
---|
12 | import java.awt.Graphics2D;
|
---|
13 | import java.awt.Point;
|
---|
14 | import java.awt.Rectangle;
|
---|
15 | import java.awt.Stroke;
|
---|
16 | import java.awt.Toolkit;
|
---|
17 | import java.awt.event.AWTEventListener;
|
---|
18 | import java.awt.event.ActionEvent;
|
---|
19 | import java.awt.event.InputEvent;
|
---|
20 | import java.awt.event.KeyEvent;
|
---|
21 | import java.awt.event.MouseEvent;
|
---|
22 | import java.awt.geom.AffineTransform;
|
---|
23 | import java.awt.geom.GeneralPath;
|
---|
24 | import java.awt.geom.Line2D;
|
---|
25 | import java.awt.geom.NoninvertibleTransformException;
|
---|
26 | import java.awt.geom.Point2D;
|
---|
27 | import java.util.ArrayList;
|
---|
28 | import java.util.Collection;
|
---|
29 | import java.util.LinkedList;
|
---|
30 | import java.util.List;
|
---|
31 |
|
---|
32 | import org.openstreetmap.josm.Main;
|
---|
33 | import org.openstreetmap.josm.command.AddCommand;
|
---|
34 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
35 | import org.openstreetmap.josm.command.Command;
|
---|
36 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
37 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
38 | import org.openstreetmap.josm.data.Bounds;
|
---|
39 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
40 | import org.openstreetmap.josm.data.osm.Node;
|
---|
41 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
42 | import org.openstreetmap.josm.data.osm.Way;
|
---|
43 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
44 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
45 | import org.openstreetmap.josm.gui.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.GuiHelper;
|
---|
51 | import org.openstreetmap.josm.tools.Geometry;
|
---|
52 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
53 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Makes a rectangle from a line, or modifies a rectangle.
|
---|
57 | */
|
---|
58 | public class ExtrudeAction extends MapMode implements MapViewPaintable {
|
---|
59 |
|
---|
60 | enum Mode { extrude, translate, select, create_new, translate_node }
|
---|
61 |
|
---|
62 | private Mode mode = Mode.select;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * If true, when extruding create new node even if segments parallel.
|
---|
66 | */
|
---|
67 | private boolean alwaysCreateNodes = false;
|
---|
68 |
|
---|
69 | private long mouseDownTime = 0;
|
---|
70 | private WaySegment selectedSegment = null;
|
---|
71 | private Node selectedNode = null;
|
---|
72 | private Color mainColor;
|
---|
73 | private Stroke mainStroke;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * drawing settings for helper lines
|
---|
77 | */
|
---|
78 | private Color helperColor;
|
---|
79 | private Stroke helperStrokeDash;
|
---|
80 | private Stroke helperStrokeRA;
|
---|
81 |
|
---|
82 | private Stroke oldLineStroke;
|
---|
83 | private double symbolSize;
|
---|
84 | /**
|
---|
85 | * Possible directions to move to.
|
---|
86 | */
|
---|
87 | private List<ReferenceSegment> possibleMoveDirections;
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Collection of nodes that is moved
|
---|
92 | */
|
---|
93 | private Collection<OsmPrimitive> movingNodeList;
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * The direction that is currently active.
|
---|
97 | */
|
---|
98 | private ReferenceSegment activeMoveDirection;
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * The position of the mouse cursor when the drag action was initiated.
|
---|
102 | */
|
---|
103 | private Point initialMousePos;
|
---|
104 | /**
|
---|
105 | * The time which needs to pass between click and release before something
|
---|
106 | * counts as a move, in milliseconds
|
---|
107 | */
|
---|
108 | private int initialMoveDelay = 200;
|
---|
109 | /**
|
---|
110 | * The initial EastNorths of node1 and node2
|
---|
111 | */
|
---|
112 | private EastNorth initialN1en;
|
---|
113 | private EastNorth initialN2en;
|
---|
114 | /**
|
---|
115 | * The new EastNorths of node1 and node2
|
---|
116 | */
|
---|
117 | private EastNorth newN1en;
|
---|
118 | private EastNorth newN2en;
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * the command that performed last move.
|
---|
122 | */
|
---|
123 | private MoveCommand moveCommand;
|
---|
124 |
|
---|
125 | /** The cursor for the 'create_new' mode. */
|
---|
126 | private final Cursor cursorCreateNew;
|
---|
127 |
|
---|
128 | /** The cursor for the 'translate' mode. */
|
---|
129 | private final Cursor cursorTranslate;
|
---|
130 |
|
---|
131 | /** The cursor for the 'alwaysCreateNodes' submode. */
|
---|
132 | private final Cursor cursorCreateNodes;
|
---|
133 |
|
---|
134 | private class ReferenceSegment {
|
---|
135 | public final EastNorth en;
|
---|
136 | public final EastNorth p1;
|
---|
137 | public final EastNorth p2;
|
---|
138 | public final boolean perpendicular;
|
---|
139 |
|
---|
140 | public ReferenceSegment(EastNorth en, EastNorth p1, EastNorth p2, boolean perpendicular) {
|
---|
141 | this.en = en;
|
---|
142 | this.p1 = p1;
|
---|
143 | this.p2 = p2;
|
---|
144 | this.perpendicular = perpendicular;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * This listener is used to indicate the 'create_new' mode, if the Alt modifier is pressed.
|
---|
150 | */
|
---|
151 | private final AWTEventListener altKeyListener = new AWTEventListener() {
|
---|
152 | @Override
|
---|
153 | public void eventDispatched(AWTEvent e) {
|
---|
154 | if(Main.map == null || Main.map.mapView == null || !Main.map.mapView.isActiveLayerDrawable())
|
---|
155 | return;
|
---|
156 | InputEvent ie = (InputEvent) e;
|
---|
157 | boolean alt = (ie.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
|
---|
158 | boolean ctrl = (ie.getModifiers() & (ActionEvent.CTRL_MASK)) != 0;
|
---|
159 | boolean shift = (ie.getModifiers() & (ActionEvent.SHIFT_MASK)) != 0;
|
---|
160 | if (mode == Mode.select) {
|
---|
161 | Main.map.mapView.setNewCursor(ctrl ? cursorTranslate : alt ? cursorCreateNew : shift ? cursorCreateNodes : cursor, this);
|
---|
162 | }
|
---|
163 | }
|
---|
164 | };
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Create a new SelectAction
|
---|
168 | * @param mapFrame The MapFrame this action belongs to.
|
---|
169 | */
|
---|
170 | public ExtrudeAction(MapFrame mapFrame) {
|
---|
171 | super(tr("Extrude"), "extrude/extrude", tr("Create areas"),
|
---|
172 | Shortcut.registerShortcut("mapmode:extrude", tr("Mode: {0}", tr("Extrude")), KeyEvent.VK_X, Shortcut.DIRECT),
|
---|
173 | mapFrame,
|
---|
174 | ImageProvider.getCursor("normal", "rectangle"));
|
---|
175 | putValue("help", ht("/Action/Extrude"));
|
---|
176 | cursorCreateNew = ImageProvider.getCursor("normal", "rectangle_plus");
|
---|
177 | cursorTranslate = ImageProvider.getCursor("normal", "rectangle_move");
|
---|
178 | cursorCreateNodes = ImageProvider.getCursor("normal", "rectangle_plussmall");
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override public String getModeHelpText() {
|
---|
182 | if (mode == Mode.translate)
|
---|
183 | return tr("Move a segment along its normal, then release the mouse button.");
|
---|
184 | else if (mode == Mode.extrude)
|
---|
185 | return tr("Draw a rectangle of the desired size, then release the mouse button.");
|
---|
186 | else if (mode == Mode.create_new)
|
---|
187 | return tr("Draw a rectangle of the desired size, then release the mouse button.");
|
---|
188 | else
|
---|
189 | return tr("Drag a way segment to make a rectangle. Ctrl-drag to move a segment along its normal, " +
|
---|
190 | "Alt-drag to create a new rectangle, double click to add a new node.");
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override public boolean layerIsSupported(Layer l) {
|
---|
194 | return l instanceof OsmDataLayer;
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Override public void enterMode() {
|
---|
198 | super.enterMode();
|
---|
199 | Main.map.mapView.addMouseListener(this);
|
---|
200 | Main.map.mapView.addMouseMotionListener(this);
|
---|
201 | try {
|
---|
202 | Toolkit.getDefaultToolkit().addAWTEventListener(altKeyListener, AWTEvent.KEY_EVENT_MASK);
|
---|
203 | } catch (SecurityException ex) {
|
---|
204 | }
|
---|
205 | initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay",200);
|
---|
206 | mainColor = Main.pref.getColor(marktr("Extrude: main line"), null);
|
---|
207 | if (mainColor == null) mainColor = PaintColors.SELECTED.get();
|
---|
208 | helperColor = Main.pref.getColor(marktr("Extrude: helper line"), Color.ORANGE);
|
---|
209 | helperStrokeDash = GuiHelper.getCustomizedStroke(Main.pref.get("extrude.stroke.helper-line", "1 4"));
|
---|
210 | helperStrokeRA = new BasicStroke(1);
|
---|
211 | symbolSize = Main.pref.getDouble("extrude.angle-symbol-radius", 8);
|
---|
212 |
|
---|
213 | oldLineStroke = GuiHelper.getCustomizedStroke(Main.pref.get("extrude.ctrl.stroke.old-line", "1"));
|
---|
214 | mainStroke = GuiHelper.getCustomizedStroke(Main.pref.get("extrude.stroke.main", "3"));
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Override public void exitMode() {
|
---|
218 | Main.map.mapView.removeMouseListener(this);
|
---|
219 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
220 | Main.map.mapView.removeTemporaryLayer(this);
|
---|
221 | try {
|
---|
222 | Toolkit.getDefaultToolkit().removeAWTEventListener(altKeyListener);
|
---|
223 | } catch (SecurityException ex) {
|
---|
224 | }
|
---|
225 | super.exitMode();
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * If the left mouse button is pressed over a segment, switch
|
---|
230 | * to either extrude, translate or create_new mode depending on whether Ctrl or Alt is held.
|
---|
231 | */
|
---|
232 | @Override public void mousePressed(MouseEvent e) {
|
---|
233 | if(!Main.map.mapView.isActiveLayerVisible())
|
---|
234 | return;
|
---|
235 | if (!(Boolean)this.getValue("active"))
|
---|
236 | return;
|
---|
237 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
238 | return;
|
---|
239 |
|
---|
240 | updateKeyModifiers(e);
|
---|
241 |
|
---|
242 | selectedNode = Main.map.mapView.getNearestNode(e.getPoint(), OsmPrimitive.isSelectablePredicate);
|
---|
243 | selectedSegment = Main.map.mapView.getNearestWaySegment(e.getPoint(), OsmPrimitive.isSelectablePredicate);
|
---|
244 |
|
---|
245 | // If nothing gets caught, stay in select mode
|
---|
246 | if (selectedSegment == null && selectedNode == null) return;
|
---|
247 |
|
---|
248 | if (selectedNode != null) {
|
---|
249 | movingNodeList = new ArrayList<OsmPrimitive>();
|
---|
250 | movingNodeList.add(selectedNode);
|
---|
251 | calculatePossibleDirectionsByNode();
|
---|
252 | if (possibleMoveDirections.isEmpty()) {
|
---|
253 | // if no directions fould, do not enter dragging mode
|
---|
254 | return;
|
---|
255 | }
|
---|
256 | mode = Mode.translate_node;
|
---|
257 | } else {
|
---|
258 | // Otherwise switch to another mode
|
---|
259 | if (ctrl) {
|
---|
260 | mode = Mode.translate;
|
---|
261 | movingNodeList = new ArrayList<OsmPrimitive>();
|
---|
262 | movingNodeList.add(selectedSegment.getFirstNode());
|
---|
263 | movingNodeList.add(selectedSegment.getSecondNode());
|
---|
264 | } else if (alt) {
|
---|
265 | mode = Mode.create_new;
|
---|
266 | // create a new segment and then select and extrude the new segment
|
---|
267 | getCurrentDataSet().setSelected(selectedSegment.way);
|
---|
268 | alwaysCreateNodes = true;
|
---|
269 | } else {
|
---|
270 | mode = Mode.extrude;
|
---|
271 | getCurrentDataSet().setSelected(selectedSegment.way);
|
---|
272 | alwaysCreateNodes = shift;
|
---|
273 | }
|
---|
274 | calculatePossibleDirectionsBySegment();
|
---|
275 | }
|
---|
276 |
|
---|
277 | // Signifies that nothing has happened yet
|
---|
278 | newN1en = null;
|
---|
279 | newN2en = null;
|
---|
280 | moveCommand = null;
|
---|
281 |
|
---|
282 | Main.map.mapView.addTemporaryLayer(this);
|
---|
283 |
|
---|
284 | updateStatusLine();
|
---|
285 | Main.map.mapView.repaint();
|
---|
286 |
|
---|
287 | // Make note of time pressed
|
---|
288 | mouseDownTime = System.currentTimeMillis();
|
---|
289 |
|
---|
290 | // Make note of mouse position
|
---|
291 | initialMousePos = e.getPoint();
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Perform action depending on what mode we're in.
|
---|
296 | */
|
---|
297 | @Override public void mouseDragged(MouseEvent e) {
|
---|
298 | if(!Main.map.mapView.isActiveLayerVisible())
|
---|
299 | return;
|
---|
300 |
|
---|
301 | // do not count anything as a drag if it lasts less than 100 milliseconds.
|
---|
302 | if (System.currentTimeMillis() - mouseDownTime < initialMoveDelay)
|
---|
303 | return;
|
---|
304 |
|
---|
305 | if (mode == Mode.select) {
|
---|
306 | // Just sit tight and wait for mouse to be released.
|
---|
307 | } else {
|
---|
308 | //move, create new and extrude mode - move the selected segment
|
---|
309 |
|
---|
310 | EastNorth mouseEn = Main.map.mapView.getEastNorth(e.getPoint().x, e.getPoint().y);
|
---|
311 | EastNorth bestMovement = calculateBestMovement(mouseEn);
|
---|
312 |
|
---|
313 | newN1en = new EastNorth(initialN1en.getX() + bestMovement.getX(), initialN1en.getY() + bestMovement.getY());
|
---|
314 | newN2en = new EastNorth(initialN2en.getX() + bestMovement.getX(), initialN2en.getY() + bestMovement.getY());
|
---|
315 |
|
---|
316 | // find out the movement distance, in metres
|
---|
317 | double distance = Main.getProjection().eastNorth2latlon(initialN1en).greatCircleDistance(Main.getProjection().eastNorth2latlon(newN1en));
|
---|
318 | Main.map.statusLine.setDist(distance);
|
---|
319 | updateStatusLine();
|
---|
320 |
|
---|
321 | Main.map.mapView.setNewCursor(Cursor.MOVE_CURSOR, this);
|
---|
322 |
|
---|
323 | if (mode == Mode.extrude || mode == Mode.create_new) {
|
---|
324 | //nothing here
|
---|
325 | } else if (mode == Mode.translate_node || mode == Mode.translate) {
|
---|
326 | //move nodes to new position
|
---|
327 | if (moveCommand == null) {
|
---|
328 | //make a new move command
|
---|
329 | moveCommand = new MoveCommand(movingNodeList, bestMovement.getX(), bestMovement.getY());
|
---|
330 | Main.main.undoRedo.add(moveCommand);
|
---|
331 | } else {
|
---|
332 | //reuse existing move command
|
---|
333 | moveCommand.moveAgainTo(bestMovement.getX(), bestMovement.getY());
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | Main.map.mapView.repaint();
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Do anything that needs to be done, then switch back to select mode
|
---|
343 | */
|
---|
344 | @Override public void mouseReleased(MouseEvent e) {
|
---|
345 |
|
---|
346 | if(!Main.map.mapView.isActiveLayerVisible())
|
---|
347 | return;
|
---|
348 |
|
---|
349 | if (mode == Mode.select) {
|
---|
350 | // Nothing to be done
|
---|
351 | } else {
|
---|
352 | if (mode == Mode.create_new) {
|
---|
353 | if (e.getPoint().distance(initialMousePos) > 10 && newN1en != null) {
|
---|
354 | createNewRectangle();
|
---|
355 | }
|
---|
356 | } else if (mode == Mode.extrude) {
|
---|
357 | if( e.getClickCount() == 2 && e.getPoint().equals(initialMousePos) ) {
|
---|
358 | // double click adds a new node
|
---|
359 | addNewNode(e);
|
---|
360 | }
|
---|
361 | else if (e.getPoint().distance(initialMousePos) > 10 && newN1en != null && selectedSegment != null) {
|
---|
362 | // main extrusion commands
|
---|
363 | performExtrusion();
|
---|
364 | }
|
---|
365 | } else if (mode == Mode.translate || mode == Mode.translate_node) {
|
---|
366 | //Commit translate
|
---|
367 | //the move command is already committed in mouseDragged
|
---|
368 | }
|
---|
369 |
|
---|
370 | boolean alt = (e.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
|
---|
371 | boolean ctrl = (e.getModifiers() & (ActionEvent.CTRL_MASK)) != 0;
|
---|
372 | boolean shift = (e.getModifiers() & (ActionEvent.SHIFT_MASK)) != 0;
|
---|
373 | // Switch back into select mode
|
---|
374 | Main.map.mapView.setNewCursor(ctrl ? cursorTranslate : alt ? cursorCreateNew : shift ? cursorCreateNodes : cursor, this);
|
---|
375 | Main.map.mapView.removeTemporaryLayer(this);
|
---|
376 | selectedSegment = null;
|
---|
377 | moveCommand = null;
|
---|
378 | mode = Mode.select;
|
---|
379 |
|
---|
380 | updateStatusLine();
|
---|
381 | Main.map.mapView.repaint();
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Insert node into nearby segment
|
---|
387 | * @param e - current mouse point
|
---|
388 | */
|
---|
389 | private void addNewNode(MouseEvent e) {
|
---|
390 | // Should maybe do the same as in DrawAction and fetch all nearby segments?
|
---|
391 | WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint(), OsmPrimitive.isSelectablePredicate);
|
---|
392 | if (ws != null) {
|
---|
393 | Node n = new Node(Main.map.mapView.getLatLon(e.getX(), e.getY()));
|
---|
394 | EastNorth A = ws.getFirstNode().getEastNorth();
|
---|
395 | EastNorth B = ws.getSecondNode().getEastNorth();
|
---|
396 | n.setEastNorth(Geometry.closestPointToSegment(A, B, n.getEastNorth()));
|
---|
397 | Way wnew = new Way(ws.way);
|
---|
398 | wnew.addNode(ws.lowerIndex+1, n);
|
---|
399 | SequenceCommand cmds = new SequenceCommand(tr("Add a new node to an existing way"),
|
---|
400 | new AddCommand(n), new ChangeCommand(ws.way, wnew));
|
---|
401 | Main.main.undoRedo.add(cmds);
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | private void createNewRectangle() {
|
---|
406 | if (selectedSegment == null) return;
|
---|
407 | // crete a new rectangle
|
---|
408 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
409 | Node third = new Node(newN2en);
|
---|
410 | Node fourth = new Node(newN1en);
|
---|
411 | Way wnew = new Way();
|
---|
412 | wnew.addNode(selectedSegment.getFirstNode());
|
---|
413 | wnew.addNode(selectedSegment.getSecondNode());
|
---|
414 | wnew.addNode(third);
|
---|
415 | wnew.addNode(fourth);
|
---|
416 | // ... and close the way
|
---|
417 | wnew.addNode(selectedSegment.getFirstNode());
|
---|
418 | // undo support
|
---|
419 | cmds.add(new AddCommand(third));
|
---|
420 | cmds.add(new AddCommand(fourth));
|
---|
421 | cmds.add(new AddCommand(wnew));
|
---|
422 | Command c = new SequenceCommand(tr("Extrude Way"), cmds);
|
---|
423 | Main.main.undoRedo.add(c);
|
---|
424 | getCurrentDataSet().setSelected(wnew);
|
---|
425 | }
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Do actual extrusion of @field selectedSegment
|
---|
429 | */
|
---|
430 | private void performExtrusion() {
|
---|
431 | // create extrusion
|
---|
432 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
433 | Way wnew = new Way(selectedSegment.way);
|
---|
434 | int insertionPoint = selectedSegment.lowerIndex + 1;
|
---|
435 |
|
---|
436 | //find if the new points overlap existing segments (in case of 90 degree angles)
|
---|
437 | Node prevNode = getPreviousNode(selectedSegment.lowerIndex);
|
---|
438 | boolean nodeOverlapsSegment = prevNode != null && Geometry.segmentsParallel(initialN1en, prevNode.getEastNorth(), initialN1en, newN1en);
|
---|
439 | boolean hasOtherWays = this.hasNodeOtherWays(selectedSegment.getFirstNode(), selectedSegment.way);
|
---|
440 |
|
---|
441 | if (nodeOverlapsSegment && !alwaysCreateNodes && !hasOtherWays) {
|
---|
442 | //move existing node
|
---|
443 | Node n1Old = selectedSegment.getFirstNode();
|
---|
444 | cmds.add(new MoveCommand(n1Old, Main.getProjection().eastNorth2latlon(newN1en)));
|
---|
445 | } else {
|
---|
446 | //introduce new node
|
---|
447 | Node n1New = new Node(Main.getProjection().eastNorth2latlon(newN1en));
|
---|
448 | wnew.addNode(insertionPoint, n1New);
|
---|
449 | insertionPoint ++;
|
---|
450 | cmds.add(new AddCommand(n1New));
|
---|
451 | }
|
---|
452 |
|
---|
453 | //find if the new points overlap existing segments (in case of 90 degree angles)
|
---|
454 | Node nextNode = getNextNode(selectedSegment.lowerIndex + 1);
|
---|
455 | nodeOverlapsSegment = nextNode != null && Geometry.segmentsParallel(initialN2en, nextNode.getEastNorth(), initialN2en, newN2en);
|
---|
456 | hasOtherWays = hasNodeOtherWays(selectedSegment.getSecondNode(), selectedSegment.way);
|
---|
457 |
|
---|
458 | if (nodeOverlapsSegment && !alwaysCreateNodes && !hasOtherWays) {
|
---|
459 | //move existing node
|
---|
460 | Node n2Old = selectedSegment.getSecondNode();
|
---|
461 | cmds.add(new MoveCommand(n2Old, Main.getProjection().eastNorth2latlon(newN2en)));
|
---|
462 | } else {
|
---|
463 | //introduce new node
|
---|
464 | Node n2New = new Node(Main.getProjection().eastNorth2latlon(newN2en));
|
---|
465 | wnew.addNode(insertionPoint, n2New);
|
---|
466 | insertionPoint ++;
|
---|
467 | cmds.add(new AddCommand(n2New));
|
---|
468 | }
|
---|
469 |
|
---|
470 | //the way was a single segment, close the way
|
---|
471 | if (wnew.getNodesCount() == 4) {
|
---|
472 | wnew.addNode(selectedSegment.getFirstNode());
|
---|
473 | }
|
---|
474 |
|
---|
475 | cmds.add(new ChangeCommand(selectedSegment.way, wnew));
|
---|
476 | Command c = new SequenceCommand(tr("Extrude Way"), cmds);
|
---|
477 | Main.main.undoRedo.add(c);
|
---|
478 | }
|
---|
479 |
|
---|
480 | /**
|
---|
481 | * This method tests if a node has other ways apart from the given one.
|
---|
482 | * @param node
|
---|
483 | * @param myWay
|
---|
484 | * @return true of node belongs only to myWay, false if there are more ways.
|
---|
485 | */
|
---|
486 | private boolean hasNodeOtherWays(Node node, Way myWay) {
|
---|
487 | for (OsmPrimitive p : node.getReferrers()) {
|
---|
488 | if (p instanceof Way && p.isUsable() && p != myWay)
|
---|
489 | return true;
|
---|
490 | }
|
---|
491 | return false;
|
---|
492 | }
|
---|
493 |
|
---|
494 | /**
|
---|
495 | * Determine best movenemnt from initialMousePos to current position @param mouseEn,
|
---|
496 | * choosing one of the directions @field possibleMoveDirections
|
---|
497 | * @return movement vector
|
---|
498 | */
|
---|
499 | private EastNorth calculateBestMovement(EastNorth mouseEn) {
|
---|
500 |
|
---|
501 | EastNorth initialMouseEn = Main.map.mapView.getEastNorth(initialMousePos.x, initialMousePos.y);
|
---|
502 | EastNorth mouseMovement = initialMouseEn.sub(mouseEn);
|
---|
503 |
|
---|
504 | double bestDistance = Double.POSITIVE_INFINITY;
|
---|
505 | EastNorth bestMovement = null;
|
---|
506 | activeMoveDirection = null;
|
---|
507 |
|
---|
508 | //find the best movement direction and vector
|
---|
509 | for (ReferenceSegment direction : possibleMoveDirections) {
|
---|
510 | EastNorth movement = calculateSegmentOffset(initialN1en, initialN2en, direction.en, mouseEn);
|
---|
511 | if (movement == null) {
|
---|
512 | //if direction parallel to segment.
|
---|
513 | continue;
|
---|
514 | }
|
---|
515 |
|
---|
516 | double distanceFromMouseMovement = movement.distance(mouseMovement);
|
---|
517 | if (bestDistance > distanceFromMouseMovement) {
|
---|
518 | bestDistance = distanceFromMouseMovement;
|
---|
519 | activeMoveDirection = direction;
|
---|
520 | bestMovement = movement;
|
---|
521 | }
|
---|
522 | }
|
---|
523 | return bestMovement;
|
---|
524 | }
|
---|
525 |
|
---|
526 | /***
|
---|
527 | * This method calculates offset amount by witch to move the given segment perpendicularly for it to be in line with mouse position.
|
---|
528 | * @param segmentP1
|
---|
529 | * @param segmentP2
|
---|
530 | * @param targetPos
|
---|
531 | * @return offset amount of P1 and P2.
|
---|
532 | */
|
---|
533 | private static EastNorth calculateSegmentOffset(EastNorth segmentP1, EastNorth segmentP2, EastNorth moveDirection,
|
---|
534 | EastNorth targetPos) {
|
---|
535 | EastNorth intersectionPoint;
|
---|
536 | if (segmentP1.distanceSq(segmentP2)>1e-7) {
|
---|
537 | intersectionPoint = Geometry.getLineLineIntersection(segmentP1, segmentP2, targetPos, targetPos.add(moveDirection));
|
---|
538 | } else {
|
---|
539 | intersectionPoint = Geometry.closestPointToLine(targetPos, targetPos.add(moveDirection), segmentP1);
|
---|
540 | }
|
---|
541 |
|
---|
542 | if (intersectionPoint == null)
|
---|
543 | return null;
|
---|
544 | else
|
---|
545 | //return distance form base to target position
|
---|
546 | return intersectionPoint.sub(targetPos);
|
---|
547 | }
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Gather possible move directions - perpendicular to the selected segment and parallel to neighbor segments
|
---|
551 | */
|
---|
552 | private void calculatePossibleDirectionsBySegment() {
|
---|
553 | // remember initial positions for segment nodes.
|
---|
554 | initialN1en = selectedSegment.getFirstNode().getEastNorth();
|
---|
555 | initialN2en = selectedSegment.getSecondNode().getEastNorth();
|
---|
556 |
|
---|
557 | //add direction perpendicular to the selected segment
|
---|
558 | possibleMoveDirections = new ArrayList<ReferenceSegment>();
|
---|
559 | possibleMoveDirections.add(new ReferenceSegment(new EastNorth(
|
---|
560 | initialN1en.getY() - initialN2en.getY(),
|
---|
561 | initialN2en.getX() - initialN1en.getX()
|
---|
562 | ), initialN1en, initialN2en, true));
|
---|
563 |
|
---|
564 |
|
---|
565 | //add directions parallel to neighbor segments
|
---|
566 | Node prevNode = getPreviousNode(selectedSegment.lowerIndex);
|
---|
567 | if (prevNode != null) {
|
---|
568 | EastNorth en = prevNode.getEastNorth();
|
---|
569 | possibleMoveDirections.add(new ReferenceSegment(new EastNorth(
|
---|
570 | initialN1en.getX() - en.getX(),
|
---|
571 | initialN1en.getY() - en.getY()
|
---|
572 | ), initialN1en, en, false));
|
---|
573 | }
|
---|
574 |
|
---|
575 | Node nextNode = getNextNode(selectedSegment.lowerIndex + 1);
|
---|
576 | if (nextNode != null) {
|
---|
577 | EastNorth en = nextNode.getEastNorth();
|
---|
578 | possibleMoveDirections.add(new ReferenceSegment(new EastNorth(
|
---|
579 | initialN2en.getX() - en.getX(),
|
---|
580 | initialN2en.getY() - en.getY()
|
---|
581 | ), initialN2en, en, false));
|
---|
582 | }
|
---|
583 | }
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * Gather possible move directions - along all adjacent segments
|
---|
587 | */
|
---|
588 | private void calculatePossibleDirectionsByNode() {
|
---|
589 | // remember initial positions for segment nodes.
|
---|
590 | initialN1en = selectedNode.getEastNorth();
|
---|
591 | initialN2en = initialN1en;
|
---|
592 | possibleMoveDirections = new ArrayList<ReferenceSegment>();
|
---|
593 | for (OsmPrimitive p: selectedNode.getReferrers()) {
|
---|
594 | if (p instanceof Way && p.isUsable()) {
|
---|
595 | for (Node neighbor: ((Way) p).getNeighbours(selectedNode)) {
|
---|
596 | EastNorth en = neighbor.getEastNorth();
|
---|
597 | possibleMoveDirections.add(new ReferenceSegment(new EastNorth(
|
---|
598 | initialN1en.getX() - en.getX(),
|
---|
599 | initialN1en.getY() - en.getY()
|
---|
600 | ), initialN1en, en, false));
|
---|
601 | }
|
---|
602 | }
|
---|
603 | }
|
---|
604 | }
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * Gets a node from selected way before given index.
|
---|
608 | * @param index index of current node
|
---|
609 | * @return index of previous node or -1 if there are no nodes there.
|
---|
610 | */
|
---|
611 | private int getPreviousNodeIndex(int index) {
|
---|
612 | if (index > 0)
|
---|
613 | return index - 1;
|
---|
614 | else if (selectedSegment.way.isClosed())
|
---|
615 | return selectedSegment.way.getNodesCount() - 2;
|
---|
616 | else
|
---|
617 | return -1;
|
---|
618 | }
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * Gets a node from selected way before given index.
|
---|
622 | * @param index index of current node
|
---|
623 | * @return previous node or null if there are no nodes there.
|
---|
624 | */
|
---|
625 | private Node getPreviousNode(int index) {
|
---|
626 | int indexPrev = getPreviousNodeIndex(index);
|
---|
627 | if (indexPrev >= 0)
|
---|
628 | return selectedSegment.way.getNode(indexPrev);
|
---|
629 | else
|
---|
630 | return null;
|
---|
631 | }
|
---|
632 |
|
---|
633 |
|
---|
634 | /**
|
---|
635 | * Gets a node from selected way after given index.
|
---|
636 | * @param index index of current node
|
---|
637 | * @return index of next node or -1 if there are no nodes there.
|
---|
638 | */
|
---|
639 | private int getNextNodeIndex(int index) {
|
---|
640 | int count = selectedSegment.way.getNodesCount();
|
---|
641 | if (index < count - 1)
|
---|
642 | return index + 1;
|
---|
643 | else if (selectedSegment.way.isClosed())
|
---|
644 | return 1;
|
---|
645 | else
|
---|
646 | return -1;
|
---|
647 | }
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * Gets a node from selected way after given index.
|
---|
651 | * @param index index of current node
|
---|
652 | * @return next node or null if there are no nodes there.
|
---|
653 | */
|
---|
654 | private Node getNextNode(int index) {
|
---|
655 | int indexNext = getNextNodeIndex(index);
|
---|
656 | if (indexNext >= 0)
|
---|
657 | return selectedSegment.way.getNode(indexNext);
|
---|
658 | else
|
---|
659 | return null;
|
---|
660 | }
|
---|
661 |
|
---|
662 | @Override
|
---|
663 | public void paint(Graphics2D g, MapView mv, Bounds box) {
|
---|
664 | Graphics2D g2 = g;
|
---|
665 | if (mode == Mode.select) {
|
---|
666 | // Nothing to do
|
---|
667 | } else {
|
---|
668 | if (newN1en != null) {
|
---|
669 |
|
---|
670 | Point p1 = mv.getPoint(initialN1en);
|
---|
671 | Point p2 = mv.getPoint(initialN2en);
|
---|
672 | Point p3 = mv.getPoint(newN1en);
|
---|
673 | Point p4 = mv.getPoint(newN2en);
|
---|
674 |
|
---|
675 | EastNorth normalUnitVector = getNormalUniVector();
|
---|
676 |
|
---|
677 | if (mode == Mode.extrude || mode == Mode.create_new) {
|
---|
678 | g2.setColor(mainColor);
|
---|
679 | g2.setStroke(mainStroke);
|
---|
680 | // Draw rectangle around new area.
|
---|
681 | GeneralPath b = new GeneralPath();
|
---|
682 | b.moveTo(p1.x, p1.y); b.lineTo(p3.x, p3.y);
|
---|
683 | b.lineTo(p4.x, p4.y); b.lineTo(p2.x, p2.y);
|
---|
684 | b.lineTo(p1.x, p1.y);
|
---|
685 | g2.draw(b);
|
---|
686 |
|
---|
687 | if (activeMoveDirection != null) {
|
---|
688 | // Draw reference way
|
---|
689 | Point pr1 = mv.getPoint(activeMoveDirection.p1);
|
---|
690 | Point pr2 = mv.getPoint(activeMoveDirection.p2);
|
---|
691 | b = new GeneralPath();
|
---|
692 | b.moveTo(pr1.x, pr1.y);
|
---|
693 | b.lineTo(pr2.x, pr2.y);
|
---|
694 | g2.setColor(helperColor);
|
---|
695 | g2.setStroke(helperStrokeDash);
|
---|
696 | g2.draw(b);
|
---|
697 |
|
---|
698 | // Draw right angle marker on first node position, only when moving at right angle
|
---|
699 | if (activeMoveDirection.perpendicular) {
|
---|
700 | // mirror RightAngle marker, so it is inside the extrude
|
---|
701 | double headingRefWS = activeMoveDirection.p1.heading(activeMoveDirection.p2);
|
---|
702 | double headingMoveDir = Math.atan2(normalUnitVector.getY(), normalUnitVector.getX());
|
---|
703 | double headingDiff = headingRefWS - headingMoveDir;
|
---|
704 | if (headingDiff < 0) headingDiff += 2 * Math.PI;
|
---|
705 | boolean mirrorRA = Math.abs(headingDiff - Math.PI) > 1e-5;
|
---|
706 | drawAngleSymbol(g2, pr1, normalUnitVector, mirrorRA);
|
---|
707 | }
|
---|
708 | }
|
---|
709 | } else if (mode == Mode.translate || mode == Mode.translate_node) {
|
---|
710 | g2.setColor(mainColor);
|
---|
711 | if (p1.distance(p2) < 3) {
|
---|
712 | g2.setStroke(mainStroke);
|
---|
713 | g2.drawOval((int)(p1.x-symbolSize/2), (int)(p1.y-symbolSize/2),
|
---|
714 | (int)(symbolSize), (int)(symbolSize));
|
---|
715 | } else {
|
---|
716 | Line2D oldline = new Line2D.Double(p1, p2);
|
---|
717 | g2.setStroke(oldLineStroke);
|
---|
718 | g2.draw(oldline);
|
---|
719 | }
|
---|
720 |
|
---|
721 | if (activeMoveDirection != null) {
|
---|
722 |
|
---|
723 | g2.setColor(helperColor);
|
---|
724 | g2.setStroke(helperStrokeDash);
|
---|
725 | // Draw a guideline along the normal.
|
---|
726 | Line2D normline;
|
---|
727 | Point2D centerpoint = new Point2D.Double((p1.getX()+p2.getX())*0.5, (p1.getY()+p2.getY())*0.5);
|
---|
728 | normline = createSemiInfiniteLine(centerpoint, normalUnitVector, g2);
|
---|
729 | g2.draw(normline);
|
---|
730 | // Draw right angle marker on initial position, only when moving at right angle
|
---|
731 | if (activeMoveDirection.perpendicular) {
|
---|
732 | // EastNorth units per pixel
|
---|
733 | g2.setStroke(helperStrokeRA);
|
---|
734 | g2.setColor(mainColor);
|
---|
735 | drawAngleSymbol(g2, centerpoint, normalUnitVector, false);
|
---|
736 | }
|
---|
737 | }
|
---|
738 | }
|
---|
739 | }
|
---|
740 | g2.setStroke(helperStrokeRA); // restore default stroke to prevent starnge occasional drawings
|
---|
741 | }
|
---|
742 | }
|
---|
743 |
|
---|
744 | private EastNorth getNormalUniVector() {
|
---|
745 | double fac = 1.0 / activeMoveDirection.en.distance(0,0);
|
---|
746 | // mult by factor to get unit vector.
|
---|
747 | EastNorth normalUnitVector = new EastNorth(activeMoveDirection.en.getX() * fac, activeMoveDirection.en.getY() * fac);
|
---|
748 |
|
---|
749 | // Check to see if our new N1 is in a positive direction with respect to the normalUnitVector.
|
---|
750 | // Even if the x component is zero, we should still be able to discern using +0.0 and -0.0
|
---|
751 | if (newN1en != null && ((newN1en.getX() > initialN1en.getX()) != (normalUnitVector.getX() > -0.0))) {
|
---|
752 | // If not, use a sign-flipped version of the normalUnitVector.
|
---|
753 | normalUnitVector = new EastNorth(-normalUnitVector.getX(), -normalUnitVector.getY());
|
---|
754 | }
|
---|
755 |
|
---|
756 | //HACK: swap Y, because the target pixels are top down, but EastNorth is bottom-up.
|
---|
757 | //This is normally done by MapView.getPoint, but it does not work on vectors.
|
---|
758 | normalUnitVector.setLocation(normalUnitVector.getX(), -normalUnitVector.getY());
|
---|
759 | return normalUnitVector;
|
---|
760 | }
|
---|
761 |
|
---|
762 | private void drawAngleSymbol(Graphics2D g2, Point2D center, EastNorth normal, boolean mirror) {
|
---|
763 | // EastNorth units per pixel
|
---|
764 | double factor = 1.0/g2.getTransform().getScaleX();
|
---|
765 | double raoffsetx = symbolSize*factor*normal.getX();
|
---|
766 | double raoffsety = symbolSize*factor*normal.getY();
|
---|
767 |
|
---|
768 | double cx = center.getX(), cy = center.getY();
|
---|
769 | double k = (mirror ? -1 : 1);
|
---|
770 | Point2D ra1 = new Point2D.Double(cx + raoffsetx, cy + raoffsety);
|
---|
771 | Point2D ra3 = new Point2D.Double(cx - raoffsety*k, cy + raoffsetx*k);
|
---|
772 | Point2D ra2 = new Point2D.Double(ra1.getX() - raoffsety*k, ra1.getY() + raoffsetx*k);
|
---|
773 |
|
---|
774 | GeneralPath ra = new GeneralPath();
|
---|
775 | ra.moveTo((float)ra1.getX(), (float)ra1.getY());
|
---|
776 | ra.lineTo((float)ra2.getX(), (float)ra2.getY());
|
---|
777 | ra.lineTo((float)ra3.getX(), (float)ra3.getY());
|
---|
778 | g2.setStroke(helperStrokeRA);
|
---|
779 | g2.draw(ra);
|
---|
780 | }
|
---|
781 |
|
---|
782 | /**
|
---|
783 | * Create a new Line that extends off the edge of the viewport in one direction
|
---|
784 | * @param start The start point of the line
|
---|
785 | * @param unitvector A unit vector denoting the direction of the line
|
---|
786 | * @param g the Graphics2D object it will be used on
|
---|
787 | */
|
---|
788 | static private Line2D createSemiInfiniteLine(Point2D start, Point2D unitvector, Graphics2D g) {
|
---|
789 | Rectangle bounds = g.getDeviceConfiguration().getBounds();
|
---|
790 | try {
|
---|
791 | AffineTransform invtrans = g.getTransform().createInverse();
|
---|
792 | Point2D widthpoint = invtrans.deltaTransform(new Point2D.Double(bounds.width,0), null);
|
---|
793 | Point2D heightpoint = invtrans.deltaTransform(new Point2D.Double(0,bounds.height), null);
|
---|
794 |
|
---|
795 | // Here we should end up with a gross overestimate of the maximum viewport diagonal in what
|
---|
796 | // Graphics2D calls 'user space'. Essentially a manhattan distance of manhattan distances.
|
---|
797 | // This can be used as a safe length of line to generate which will always go off-viewport.
|
---|
798 | double linelength = Math.abs(widthpoint.getX()) + Math.abs(widthpoint.getY()) + Math.abs(heightpoint.getX()) + Math.abs(heightpoint.getY());
|
---|
799 |
|
---|
800 | return new Line2D.Double(start, new Point2D.Double(start.getX() + (unitvector.getX() * linelength) , start.getY() + (unitvector.getY() * linelength)));
|
---|
801 | }
|
---|
802 | catch (NoninvertibleTransformException e) {
|
---|
803 | return new Line2D.Double(start, new Point2D.Double(start.getX() + (unitvector.getX() * 10) , start.getY() + (unitvector.getY() * 10)));
|
---|
804 | }
|
---|
805 | }
|
---|
806 | }
|
---|