1 | // License: GPL. See LICENSE file for details.
|
---|
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.Color;
|
---|
9 | import java.awt.Cursor;
|
---|
10 | import java.awt.Graphics2D;
|
---|
11 | import java.awt.Point;
|
---|
12 | import java.awt.Stroke;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.awt.event.MouseEvent;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.LinkedHashSet;
|
---|
17 | import java.util.Set;
|
---|
18 |
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.Bounds;
|
---|
23 | import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
|
---|
24 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
25 | import org.openstreetmap.josm.data.SystemOfMeasurement;
|
---|
26 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
27 | import org.openstreetmap.josm.data.osm.Node;
|
---|
28 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
29 | import org.openstreetmap.josm.data.osm.Way;
|
---|
30 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
31 | import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
|
---|
32 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
33 | import org.openstreetmap.josm.gui.MapView;
|
---|
34 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
35 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
36 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
---|
37 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
38 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
39 | import org.openstreetmap.josm.gui.util.ModifierListener;
|
---|
40 | import org.openstreetmap.josm.tools.Geometry;
|
---|
41 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
42 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
43 |
|
---|
44 | //// TODO: (list below)
|
---|
45 | /* == Functionality ==
|
---|
46 | *
|
---|
47 | * 1. Use selected nodes as split points for the selected ways.
|
---|
48 | *
|
---|
49 | * The ways containing the selected nodes will be split and only the "inner"
|
---|
50 | * parts will be copied
|
---|
51 | *
|
---|
52 | * 2. Enter exact offset
|
---|
53 | *
|
---|
54 | * 3. Improve snapping
|
---|
55 | *
|
---|
56 | * 4. Visual cues could be better
|
---|
57 | *
|
---|
58 | * 5. Cursors (Half-done)
|
---|
59 | *
|
---|
60 | * 6. (long term) Parallelize and adjust offsets of existing ways
|
---|
61 | *
|
---|
62 | * == Code quality ==
|
---|
63 | *
|
---|
64 | * a) The mode, flags, and modifiers might be updated more than necessary.
|
---|
65 | *
|
---|
66 | * Not a performance problem, but better if they where more centralized
|
---|
67 | *
|
---|
68 | * b) Extract generic MapMode services into a super class and/or utility class
|
---|
69 | *
|
---|
70 | * c) Maybe better to simply draw our own source way highlighting?
|
---|
71 | *
|
---|
72 | * Current code doesn't not take into account that ways might been highlighted
|
---|
73 | * by other than us. Don't think that situation should ever happen though.
|
---|
74 | */
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * MapMode for making parallel ways.
|
---|
78 | *
|
---|
79 | * All calculations are done in projected coordinates.
|
---|
80 | *
|
---|
81 | * @author Ole Jørgen Brønner (olejorgenb)
|
---|
82 | */
|
---|
83 | public class ParallelWayAction extends MapMode implements ModifierListener, MapViewPaintable, PreferenceChangedListener {
|
---|
84 |
|
---|
85 | private enum Mode {
|
---|
86 | dragging, normal
|
---|
87 | }
|
---|
88 |
|
---|
89 | //// Preferences and flags
|
---|
90 | // See updateModeLocalPreferences for defaults
|
---|
91 | private Mode mode;
|
---|
92 | private boolean copyTags;
|
---|
93 | private boolean copyTagsDefault;
|
---|
94 |
|
---|
95 | private boolean snap;
|
---|
96 | private boolean snapDefault;
|
---|
97 |
|
---|
98 | private double snapThreshold;
|
---|
99 | private double snapDistanceMetric;
|
---|
100 | private double snapDistanceImperial;
|
---|
101 | private double snapDistanceChinese;
|
---|
102 | private double snapDistanceNautical;
|
---|
103 |
|
---|
104 | private transient ModifiersSpec snapModifierCombo;
|
---|
105 | private transient ModifiersSpec copyTagsModifierCombo;
|
---|
106 | private transient ModifiersSpec addToSelectionModifierCombo;
|
---|
107 | private transient ModifiersSpec toggleSelectedModifierCombo;
|
---|
108 | private transient ModifiersSpec setSelectedModifierCombo;
|
---|
109 |
|
---|
110 | private int initialMoveDelay;
|
---|
111 |
|
---|
112 | private final MapView mv;
|
---|
113 |
|
---|
114 | // Mouse tracking state
|
---|
115 | private Point mousePressedPos;
|
---|
116 | private boolean mouseIsDown;
|
---|
117 | private long mousePressedTime;
|
---|
118 | private boolean mouseHasBeenDragged;
|
---|
119 |
|
---|
120 | private transient WaySegment referenceSegment;
|
---|
121 | private transient ParallelWays pWays;
|
---|
122 | private transient Set<Way> sourceWays;
|
---|
123 | private EastNorth helperLineStart;
|
---|
124 | private EastNorth helperLineEnd;
|
---|
125 |
|
---|
126 | private transient Stroke helpLineStroke;
|
---|
127 | private transient Stroke refLineStroke;
|
---|
128 | private Color mainColor;
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Constructs a new {@code ParallelWayAction}.
|
---|
132 | * @param mapFrame Map frame
|
---|
133 | */
|
---|
134 | public ParallelWayAction(MapFrame mapFrame) {
|
---|
135 | super(tr("Parallel"), "parallel", tr("Make parallel copies of ways"),
|
---|
136 | Shortcut.registerShortcut("mapmode:parallel", tr("Mode: {0}",
|
---|
137 | tr("Parallel")), KeyEvent.VK_P, Shortcut.SHIFT),
|
---|
138 | mapFrame, ImageProvider.getCursor("normal", "parallel"));
|
---|
139 | putValue("help", ht("/Action/Parallel"));
|
---|
140 | mv = mapFrame.mapView;
|
---|
141 | updateModeLocalPreferences();
|
---|
142 | Main.pref.addPreferenceChangeListener(this);
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public void enterMode() {
|
---|
147 | // super.enterMode() updates the status line and cursor so we need our state to be set correctly
|
---|
148 | setMode(Mode.normal);
|
---|
149 | pWays = null;
|
---|
150 | updateAllPreferences(); // All default values should've been set now
|
---|
151 |
|
---|
152 | super.enterMode();
|
---|
153 |
|
---|
154 | mv.addMouseListener(this);
|
---|
155 | mv.addMouseMotionListener(this);
|
---|
156 | mv.addTemporaryLayer(this);
|
---|
157 |
|
---|
158 | helpLineStroke = GuiHelper.getCustomizedStroke(getStringPref("stroke.hepler-line", "1" ));
|
---|
159 | refLineStroke = GuiHelper.getCustomizedStroke(getStringPref("stroke.ref-line", "1 2 2"));
|
---|
160 | mainColor = Main.pref.getColor(marktr("make parallel helper line"), null);
|
---|
161 | if (mainColor == null) mainColor = PaintColors.SELECTED.get();
|
---|
162 |
|
---|
163 | //// Needed to update the mouse cursor if modifiers are changed when the mouse is motionless
|
---|
164 | Main.map.keyDetector.addModifierListener(this);
|
---|
165 | sourceWays = new LinkedHashSet<>(getCurrentDataSet().getSelectedWays());
|
---|
166 | for (Way w : sourceWays) {
|
---|
167 | w.setHighlighted(true);
|
---|
168 | }
|
---|
169 | mv.repaint();
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Override
|
---|
173 | public void exitMode() {
|
---|
174 | super.exitMode();
|
---|
175 | mv.removeMouseListener(this);
|
---|
176 | mv.removeMouseMotionListener(this);
|
---|
177 | mv.removeTemporaryLayer(this);
|
---|
178 | Main.map.statusLine.setDist(-1);
|
---|
179 | Main.map.statusLine.repaint();
|
---|
180 | Main.map.keyDetector.removeModifierListener(this);
|
---|
181 | removeWayHighlighting(sourceWays);
|
---|
182 | pWays = null;
|
---|
183 | sourceWays = null;
|
---|
184 | referenceSegment = null;
|
---|
185 | mv.repaint();
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public String getModeHelpText() {
|
---|
190 | // TODO: add more detailed feedback based on modifier state.
|
---|
191 | // TODO: dynamic messages based on preferences. (Could be problematic translation wise)
|
---|
192 | switch (mode) {
|
---|
193 | case normal:
|
---|
194 | return tr("Select ways as in Select mode. Drag selected ways or a single way to create a parallel copy (Alt toggles tag preservation)");
|
---|
195 | case dragging:
|
---|
196 | return tr("Hold Ctrl to toggle snapping");
|
---|
197 | }
|
---|
198 | return ""; // impossible ..
|
---|
199 | }
|
---|
200 |
|
---|
201 | // Separated due to "race condition" between default values
|
---|
202 | private void updateAllPreferences() {
|
---|
203 | updateModeLocalPreferences();
|
---|
204 | // @formatter:off
|
---|
205 | // @formatter:on
|
---|
206 | }
|
---|
207 |
|
---|
208 | private void updateModeLocalPreferences() {
|
---|
209 | // @formatter:off
|
---|
210 | snapThreshold = Main.pref.getDouble (prefKey("snap-threshold-percent"), 0.70);
|
---|
211 | snapDefault = Main.pref.getBoolean(prefKey("snap-default"), true);
|
---|
212 | copyTagsDefault = Main.pref.getBoolean(prefKey("copy-tags-default"), true);
|
---|
213 | initialMoveDelay = Main.pref.getInteger(prefKey("initial-move-delay"), 200);
|
---|
214 | snapDistanceMetric = Main.pref.getDouble(prefKey("snap-distance-metric"), 0.5);
|
---|
215 | snapDistanceImperial = Main.pref.getDouble(prefKey("snap-distance-imperial"), 1);
|
---|
216 | snapDistanceChinese = Main.pref.getDouble(prefKey("snap-distance-chinese"), 1);
|
---|
217 | snapDistanceNautical = Main.pref.getDouble(prefKey("snap-distance-nautical"), 0.1);
|
---|
218 |
|
---|
219 | snapModifierCombo = new ModifiersSpec(getStringPref("snap-modifier-combo", "?sC"));
|
---|
220 | copyTagsModifierCombo = new ModifiersSpec(getStringPref("copy-tags-modifier-combo", "As?"));
|
---|
221 | addToSelectionModifierCombo = new ModifiersSpec(getStringPref("add-to-selection-modifier-combo", "aSc"));
|
---|
222 | toggleSelectedModifierCombo = new ModifiersSpec(getStringPref("toggle-selection-modifier-combo", "asC"));
|
---|
223 | setSelectedModifierCombo = new ModifiersSpec(getStringPref("set-selection-modifier-combo", "asc"));
|
---|
224 | // @formatter:on
|
---|
225 | }
|
---|
226 |
|
---|
227 | @Override
|
---|
228 | public boolean layerIsSupported(Layer layer) {
|
---|
229 | return layer instanceof OsmDataLayer;
|
---|
230 | }
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | public void modifiersChanged(int modifiers) {
|
---|
234 | if (Main.map == null || mv == null || !mv.isActiveLayerDrawable())
|
---|
235 | return;
|
---|
236 |
|
---|
237 | // Should only get InputEvents due to the mask in enterMode
|
---|
238 | if (updateModifiersState(modifiers)) {
|
---|
239 | updateStatusLine();
|
---|
240 | updateCursor();
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | private boolean updateModifiersState(int modifiers) {
|
---|
245 | boolean oldAlt = alt, oldShift = shift, oldCtrl = ctrl;
|
---|
246 | updateKeyModifiers(modifiers);
|
---|
247 | return (oldAlt != alt || oldShift != shift || oldCtrl != ctrl);
|
---|
248 | }
|
---|
249 |
|
---|
250 | private void updateCursor() {
|
---|
251 | Cursor newCursor = null;
|
---|
252 | switch (mode) {
|
---|
253 | case normal:
|
---|
254 | if (matchesCurrentModifiers(setSelectedModifierCombo)) {
|
---|
255 | newCursor = ImageProvider.getCursor("normal", "parallel");
|
---|
256 | } else if (matchesCurrentModifiers(addToSelectionModifierCombo)) {
|
---|
257 | newCursor = ImageProvider.getCursor("normal", "parallel"); // FIXME
|
---|
258 | } else if (matchesCurrentModifiers(toggleSelectedModifierCombo)) {
|
---|
259 | newCursor = ImageProvider.getCursor("normal", "parallel"); // FIXME
|
---|
260 | } else {
|
---|
261 | // TODO: set to a cursor indicating an error
|
---|
262 | }
|
---|
263 | break;
|
---|
264 | case dragging:
|
---|
265 | if (snap) {
|
---|
266 | // TODO: snapping cursor?
|
---|
267 | newCursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
|
---|
268 | } else {
|
---|
269 | newCursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
|
---|
270 | }
|
---|
271 | }
|
---|
272 | if (newCursor != null) {
|
---|
273 | mv.setNewCursor(newCursor, this);
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | private void setMode(Mode mode) {
|
---|
278 | this.mode = mode;
|
---|
279 | updateCursor();
|
---|
280 | updateStatusLine();
|
---|
281 | }
|
---|
282 |
|
---|
283 | private boolean sanityCheck() {
|
---|
284 | // @formatter:off
|
---|
285 | boolean areWeSane =
|
---|
286 | mv.isActiveLayerVisible() &&
|
---|
287 | mv.isActiveLayerDrawable() &&
|
---|
288 | ((Boolean) this.getValue("active"));
|
---|
289 | // @formatter:on
|
---|
290 | assert (areWeSane); // mad == bad
|
---|
291 | return areWeSane;
|
---|
292 | }
|
---|
293 |
|
---|
294 | @Override
|
---|
295 | public void mousePressed(MouseEvent e) {
|
---|
296 | requestFocusInMapView();
|
---|
297 | updateModifiersState(e.getModifiers());
|
---|
298 | // Other buttons are off limit, but we still get events.
|
---|
299 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
300 | return;
|
---|
301 |
|
---|
302 | if (!sanityCheck())
|
---|
303 | return;
|
---|
304 |
|
---|
305 | updateFlagsOnlyChangeableOnPress();
|
---|
306 | updateFlagsChangeableAlways();
|
---|
307 |
|
---|
308 | // Since the created way is left selected, we need to unselect again here
|
---|
309 | if (pWays != null && pWays.getWays() != null) {
|
---|
310 | getCurrentDataSet().clearSelection(pWays.getWays());
|
---|
311 | pWays = null;
|
---|
312 | }
|
---|
313 |
|
---|
314 | mouseIsDown = true;
|
---|
315 | mousePressedPos = e.getPoint();
|
---|
316 | mousePressedTime = System.currentTimeMillis();
|
---|
317 |
|
---|
318 | }
|
---|
319 |
|
---|
320 | @Override
|
---|
321 | public void mouseReleased(MouseEvent e) {
|
---|
322 | updateModifiersState(e.getModifiers());
|
---|
323 | // Other buttons are off limit, but we still get events.
|
---|
324 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
325 | return;
|
---|
326 |
|
---|
327 | if (!mouseHasBeenDragged) {
|
---|
328 | // use point from press or click event? (or are these always the same)
|
---|
329 | Way nearestWay = mv.getNearestWay(e.getPoint(), OsmPrimitive.isSelectablePredicate);
|
---|
330 | if (nearestWay == null) {
|
---|
331 | if (matchesCurrentModifiers(setSelectedModifierCombo)) {
|
---|
332 | clearSourceWays();
|
---|
333 | }
|
---|
334 | resetMouseTrackingState();
|
---|
335 | return;
|
---|
336 | }
|
---|
337 | boolean isSelected = nearestWay.isSelected();
|
---|
338 | if (matchesCurrentModifiers(addToSelectionModifierCombo)) {
|
---|
339 | if (!isSelected) {
|
---|
340 | addSourceWay(nearestWay);
|
---|
341 | }
|
---|
342 | } else if (matchesCurrentModifiers(toggleSelectedModifierCombo)) {
|
---|
343 | if (isSelected) {
|
---|
344 | removeSourceWay(nearestWay);
|
---|
345 | } else {
|
---|
346 | addSourceWay(nearestWay);
|
---|
347 | }
|
---|
348 | } else if (matchesCurrentModifiers(setSelectedModifierCombo)) {
|
---|
349 | clearSourceWays();
|
---|
350 | addSourceWay(nearestWay);
|
---|
351 | } // else -> invalid modifier combination
|
---|
352 | } else if (mode == Mode.dragging) {
|
---|
353 | clearSourceWays();
|
---|
354 | }
|
---|
355 |
|
---|
356 | setMode(Mode.normal);
|
---|
357 | resetMouseTrackingState();
|
---|
358 | mv.repaint();
|
---|
359 | }
|
---|
360 |
|
---|
361 | private void removeWayHighlighting(Collection<Way> ways) {
|
---|
362 | if (ways == null)
|
---|
363 | return;
|
---|
364 | for (Way w : ways) {
|
---|
365 | w.setHighlighted(false);
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | @Override
|
---|
370 | public void mouseDragged(MouseEvent e) {
|
---|
371 | // WTF.. the event passed here doesn't have button info?
|
---|
372 | // Since we get this event from other buttons too, we must check that
|
---|
373 | // _BUTTON1_ is down.
|
---|
374 | if (!mouseIsDown)
|
---|
375 | return;
|
---|
376 |
|
---|
377 | boolean modifiersChanged = updateModifiersState(e.getModifiers());
|
---|
378 | updateFlagsChangeableAlways();
|
---|
379 |
|
---|
380 | if (modifiersChanged) {
|
---|
381 | // Since this could be remotely slow, do it conditionally
|
---|
382 | updateStatusLine();
|
---|
383 | updateCursor();
|
---|
384 | }
|
---|
385 |
|
---|
386 | if ((System.currentTimeMillis() - mousePressedTime) < initialMoveDelay)
|
---|
387 | return;
|
---|
388 | // Assuming this event only is emitted when the mouse has moved
|
---|
389 | // Setting this after the check above means we tolerate clicks with some movement
|
---|
390 | mouseHasBeenDragged = true;
|
---|
391 |
|
---|
392 | Point p = e.getPoint();
|
---|
393 | if (mode == Mode.normal) {
|
---|
394 | // Should we ensure that the copyTags modifiers are still valid?
|
---|
395 |
|
---|
396 | // Important to use mouse position from the press, since the drag
|
---|
397 | // event can come quite late
|
---|
398 | if (!isModifiersValidForDragMode())
|
---|
399 | return;
|
---|
400 | if (!initParallelWays(mousePressedPos, copyTags))
|
---|
401 | return;
|
---|
402 | setMode(Mode.dragging);
|
---|
403 | }
|
---|
404 |
|
---|
405 | // Calculate distance to the reference line
|
---|
406 | EastNorth enp = mv.getEastNorth((int) p.getX(), (int) p.getY());
|
---|
407 | EastNorth nearestPointOnRefLine = Geometry.closestPointToLine(referenceSegment.getFirstNode().getEastNorth(),
|
---|
408 | referenceSegment.getSecondNode().getEastNorth(), enp);
|
---|
409 |
|
---|
410 | // Note: d is the distance in _projected units_
|
---|
411 | double d = enp.distance(nearestPointOnRefLine);
|
---|
412 | double realD = mv.getProjection().eastNorth2latlon(enp).greatCircleDistance(mv.getProjection().eastNorth2latlon(nearestPointOnRefLine));
|
---|
413 | double snappedRealD = realD;
|
---|
414 |
|
---|
415 | // TODO: abuse of isToTheRightSideOfLine function.
|
---|
416 | boolean toTheRight = Geometry.isToTheRightSideOfLine(referenceSegment.getFirstNode(),
|
---|
417 | referenceSegment.getFirstNode(), referenceSegment.getSecondNode(), new Node(enp));
|
---|
418 |
|
---|
419 | if (snap) {
|
---|
420 | // TODO: Very simple snapping
|
---|
421 | // - Snap steps relative to the distance?
|
---|
422 | double snapDistance;
|
---|
423 | SystemOfMeasurement som = NavigatableComponent.getSystemOfMeasurement();
|
---|
424 | if (som.equals(SystemOfMeasurement.CHINESE)) {
|
---|
425 | snapDistance = snapDistanceChinese * SystemOfMeasurement.CHINESE.aValue;
|
---|
426 | } else if (som.equals(SystemOfMeasurement.IMPERIAL)) {
|
---|
427 | snapDistance = snapDistanceImperial * SystemOfMeasurement.IMPERIAL.aValue;
|
---|
428 | } else if (som.equals(SystemOfMeasurement.NAUTICAL_MILE)) {
|
---|
429 | snapDistance = snapDistanceNautical * SystemOfMeasurement.NAUTICAL_MILE.aValue;
|
---|
430 | } else {
|
---|
431 | snapDistance = snapDistanceMetric; // Metric system by default
|
---|
432 | }
|
---|
433 | double closestWholeUnit;
|
---|
434 | double modulo = realD % snapDistance;
|
---|
435 | if (modulo < snapDistance/2.0) {
|
---|
436 | closestWholeUnit = realD - modulo;
|
---|
437 | } else {
|
---|
438 | closestWholeUnit = realD + (snapDistance-modulo);
|
---|
439 | }
|
---|
440 | if (Math.abs(closestWholeUnit - realD) < (snapThreshold * snapDistance)) {
|
---|
441 | snappedRealD = closestWholeUnit;
|
---|
442 | } else {
|
---|
443 | snappedRealD = closestWholeUnit + Math.signum(realD - closestWholeUnit) * snapDistance;
|
---|
444 | }
|
---|
445 | }
|
---|
446 | d = snappedRealD * (d/realD); // convert back to projected distance. (probably ok on small scales)
|
---|
447 | helperLineStart = nearestPointOnRefLine;
|
---|
448 | helperLineEnd = enp;
|
---|
449 | if (toTheRight) {
|
---|
450 | d = -d;
|
---|
451 | }
|
---|
452 | pWays.changeOffset(d);
|
---|
453 |
|
---|
454 | Main.map.statusLine.setDist(Math.abs(snappedRealD));
|
---|
455 | Main.map.statusLine.repaint();
|
---|
456 | mv.repaint();
|
---|
457 | }
|
---|
458 |
|
---|
459 | private boolean matchesCurrentModifiers(ModifiersSpec spec) {
|
---|
460 | return spec.matchWithKnown(alt, shift, ctrl);
|
---|
461 | }
|
---|
462 |
|
---|
463 | @Override
|
---|
464 | public void paint(Graphics2D g, MapView mv, Bounds bbox) {
|
---|
465 | if (mode == Mode.dragging) {
|
---|
466 | // sanity checks
|
---|
467 | if (mv == null)
|
---|
468 | return;
|
---|
469 |
|
---|
470 | // FIXME: should clip the line (gets insanely slow when zoomed in on a very long line
|
---|
471 | g.setStroke(refLineStroke);
|
---|
472 | g.setColor(mainColor);
|
---|
473 | Point p1 = mv.getPoint(referenceSegment.getFirstNode().getEastNorth());
|
---|
474 | Point p2 = mv.getPoint(referenceSegment.getSecondNode().getEastNorth());
|
---|
475 | g.drawLine(p1.x, p1.y, p2.x, p2.y);
|
---|
476 |
|
---|
477 | g.setStroke(helpLineStroke);
|
---|
478 | g.setColor(mainColor);
|
---|
479 | p1 = mv.getPoint(helperLineStart);
|
---|
480 | p2 = mv.getPoint(helperLineEnd);
|
---|
481 | g.drawLine(p1.x, p1.y, p2.x, p2.y);
|
---|
482 | }
|
---|
483 | }
|
---|
484 |
|
---|
485 | private boolean isModifiersValidForDragMode() {
|
---|
486 | return (!alt && !shift && !ctrl) || matchesCurrentModifiers(snapModifierCombo)
|
---|
487 | || matchesCurrentModifiers(copyTagsModifierCombo);
|
---|
488 | }
|
---|
489 |
|
---|
490 | private void updateFlagsOnlyChangeableOnPress() {
|
---|
491 | copyTags = copyTagsDefault != matchesCurrentModifiers(copyTagsModifierCombo);
|
---|
492 | }
|
---|
493 |
|
---|
494 | private void updateFlagsChangeableAlways() {
|
---|
495 | snap = snapDefault != matchesCurrentModifiers(snapModifierCombo);
|
---|
496 | }
|
---|
497 |
|
---|
498 | //// We keep the source ways and the selection in sync so the user can see the source way's tags
|
---|
499 | private void addSourceWay(Way w) {
|
---|
500 | assert (sourceWays != null);
|
---|
501 | getCurrentDataSet().addSelected(w);
|
---|
502 | w.setHighlighted(true);
|
---|
503 | sourceWays.add(w);
|
---|
504 | }
|
---|
505 |
|
---|
506 | private void removeSourceWay(Way w) {
|
---|
507 | assert (sourceWays != null);
|
---|
508 | getCurrentDataSet().clearSelection(w);
|
---|
509 | w.setHighlighted(false);
|
---|
510 | sourceWays.remove(w);
|
---|
511 | }
|
---|
512 |
|
---|
513 | private void clearSourceWays() {
|
---|
514 | assert (sourceWays != null);
|
---|
515 | getCurrentDataSet().clearSelection(sourceWays);
|
---|
516 | for (Way w : sourceWays) {
|
---|
517 | w.setHighlighted(false);
|
---|
518 | }
|
---|
519 | sourceWays.clear();
|
---|
520 | }
|
---|
521 |
|
---|
522 | private void resetMouseTrackingState() {
|
---|
523 | mouseIsDown = false;
|
---|
524 | mousePressedPos = null;
|
---|
525 | mouseHasBeenDragged = false;
|
---|
526 | }
|
---|
527 |
|
---|
528 | // TODO: rename
|
---|
529 | private boolean initParallelWays(Point p, boolean copyTags) {
|
---|
530 | referenceSegment = mv.getNearestWaySegment(p, Way.isUsablePredicate, true);
|
---|
531 | if (referenceSegment == null)
|
---|
532 | return false;
|
---|
533 |
|
---|
534 | if (!sourceWays.contains(referenceSegment.way)) {
|
---|
535 | clearSourceWays();
|
---|
536 | addSourceWay(referenceSegment.way);
|
---|
537 | }
|
---|
538 |
|
---|
539 | try {
|
---|
540 | int referenceWayIndex = -1;
|
---|
541 | int i = 0;
|
---|
542 | for (Way w : sourceWays) {
|
---|
543 | if (w == referenceSegment.way) {
|
---|
544 | referenceWayIndex = i;
|
---|
545 | break;
|
---|
546 | }
|
---|
547 | i++;
|
---|
548 | }
|
---|
549 | pWays = new ParallelWays(sourceWays, copyTags, referenceWayIndex);
|
---|
550 | pWays.commit();
|
---|
551 | getCurrentDataSet().setSelected(pWays.getWays());
|
---|
552 | return true;
|
---|
553 | } catch (IllegalArgumentException e) {
|
---|
554 | // TODO: Not ideal feedback. Maybe changing the cursor could be a good mechanism?
|
---|
555 | JOptionPane.showMessageDialog(
|
---|
556 | Main.parent,
|
---|
557 | tr("ParallelWayAction\n" +
|
---|
558 | "The ways selected must form a simple branchless path"),
|
---|
559 | tr("Make parallel way error"),
|
---|
560 | JOptionPane.INFORMATION_MESSAGE);
|
---|
561 | // The error dialog prevents us from getting the mouseReleased event
|
---|
562 | resetMouseTrackingState();
|
---|
563 | pWays = null;
|
---|
564 | return false;
|
---|
565 | }
|
---|
566 | }
|
---|
567 |
|
---|
568 | private String prefKey(String subKey) {
|
---|
569 | return "edit.make-parallel-way-action." + subKey;
|
---|
570 | }
|
---|
571 |
|
---|
572 | private String getStringPref(String subKey, String def) {
|
---|
573 | return Main.pref.get(prefKey(subKey), def);
|
---|
574 | }
|
---|
575 |
|
---|
576 | @Override
|
---|
577 | public void preferenceChanged(PreferenceChangeEvent e) {
|
---|
578 | if (e.getKey().startsWith(prefKey(""))) {
|
---|
579 | updateAllPreferences();
|
---|
580 | }
|
---|
581 | }
|
---|
582 |
|
---|
583 | @Override
|
---|
584 | public void destroy() {
|
---|
585 | super.destroy();
|
---|
586 | Main.pref.removePreferenceChangeListener(this);
|
---|
587 | }
|
---|
588 | }
|
---|