1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Cursor;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.Rectangle;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.LinkedList;
|
---|
15 |
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.actions.MergeNodesAction;
|
---|
20 | import org.openstreetmap.josm.command.AddCommand;
|
---|
21 | import org.openstreetmap.josm.command.Command;
|
---|
22 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
23 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
24 | import org.openstreetmap.josm.command.RotateCommand;
|
---|
25 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
26 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
27 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
28 | import org.openstreetmap.josm.data.osm.Node;
|
---|
29 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
30 | import org.openstreetmap.josm.data.osm.Way;
|
---|
31 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
32 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
---|
33 | import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;
|
---|
34 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
35 | import org.openstreetmap.josm.gui.MapView;
|
---|
36 | import org.openstreetmap.josm.gui.SelectionManager;
|
---|
37 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
---|
38 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
39 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Move is an action that can move all kind of OsmPrimitives (except keys for now).
|
---|
43 | *
|
---|
44 | * If an selected object is under the mouse when dragging, move all selected objects.
|
---|
45 | * If an unselected object is under the mouse when dragging, it becomes selected
|
---|
46 | * and will be moved.
|
---|
47 | * If no object is under the mouse, move all selected objects (if any)
|
---|
48 | *
|
---|
49 | * @author imi
|
---|
50 | */
|
---|
51 | public class SelectAction extends MapMode implements SelectionEnded {
|
---|
52 |
|
---|
53 | enum Mode { move, rotate, select }
|
---|
54 | private Mode mode = null;
|
---|
55 | private long mouseDownTime = 0;
|
---|
56 | private boolean didMove = false;
|
---|
57 | Node virtualNode = null;
|
---|
58 | WaySegment virtualWay = null;
|
---|
59 | SequenceCommand virtualCmds = null;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * The old cursor before the user pressed the mouse button.
|
---|
63 | */
|
---|
64 | private Cursor oldCursor;
|
---|
65 | /**
|
---|
66 | * The position of the mouse before the user moves a node.
|
---|
67 | */
|
---|
68 | private Point mousePos;
|
---|
69 | private SelectionManager selectionManager;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * The time which needs to pass between click and release before something
|
---|
73 | * counts as a move, in milliseconds
|
---|
74 | */
|
---|
75 | private int initialMoveDelay;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * The screen distance which needs to be travelled before something
|
---|
79 | * counts as a move, in pixels
|
---|
80 | */
|
---|
81 | private int initialMoveThreshold;
|
---|
82 | private boolean initialMoveThresholdExceeded = false;
|
---|
83 | /**
|
---|
84 | * Create a new SelectAction
|
---|
85 | * @param mapFrame The MapFrame this action belongs to.
|
---|
86 | */
|
---|
87 | public SelectAction(MapFrame mapFrame) {
|
---|
88 | super(tr("Select"), "move/move", tr("Select, move and rotate objects"),
|
---|
89 | Shortcut.registerShortcut("mapmode:select", tr("Mode: {0}", tr("Select")), KeyEvent.VK_S, Shortcut.GROUP_EDIT),
|
---|
90 | mapFrame,
|
---|
91 | getCursor("normal", "selection", Cursor.DEFAULT_CURSOR));
|
---|
92 | putValue("help", "Action/Move/Move");
|
---|
93 | selectionManager = new SelectionManager(this, false, mapFrame.mapView);
|
---|
94 | initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay",200);
|
---|
95 | initialMoveThreshold = Main.pref.getInteger("edit.initial-move-threshold",5);
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static Cursor getCursor(String name, String mod, int def) {
|
---|
99 | try {
|
---|
100 | return ImageProvider.getCursor(name, mod);
|
---|
101 | } catch (Exception e) {
|
---|
102 | }
|
---|
103 | return Cursor.getPredefinedCursor(def);
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void setCursor(Cursor c) {
|
---|
107 | if (oldCursor == null) {
|
---|
108 | oldCursor = Main.map.mapView.getCursor();
|
---|
109 | Main.map.mapView.setCursor(c);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | private void restoreCursor() {
|
---|
114 | if (oldCursor != null) {
|
---|
115 | Main.map.mapView.setCursor(oldCursor);
|
---|
116 | oldCursor = null;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override public void enterMode() {
|
---|
121 | super.enterMode();
|
---|
122 | Main.map.mapView.addMouseListener(this);
|
---|
123 | Main.map.mapView.addMouseMotionListener(this);
|
---|
124 | Main.map.mapView.enableVirtualNodes(
|
---|
125 | Main.pref.getInteger("mappaint.node.virtual-size", 8) != 0);
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override public void exitMode() {
|
---|
129 | super.exitMode();
|
---|
130 | selectionManager.unregister(Main.map.mapView);
|
---|
131 | Main.map.mapView.removeMouseListener(this);
|
---|
132 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
133 | Main.map.mapView.enableVirtualNodes(false);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * If the left mouse button is pressed, move all currently selected
|
---|
138 | * objects (if one of them is under the mouse) or the current one under the
|
---|
139 | * mouse (which will become selected).
|
---|
140 | */
|
---|
141 | @Override public void mouseDragged(MouseEvent e) {
|
---|
142 | if (mode == Mode.select) return;
|
---|
143 |
|
---|
144 | // do not count anything as a move if it lasts less than 100 milliseconds.
|
---|
145 | if ((mode == Mode.move) && (System.currentTimeMillis() - mouseDownTime < initialMoveDelay)) return;
|
---|
146 |
|
---|
147 | if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
|
---|
148 | return;
|
---|
149 |
|
---|
150 | if (mode == Mode.move) {
|
---|
151 | setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (mousePos == null) {
|
---|
155 | mousePos = e.getPoint();
|
---|
156 | return;
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (!initialMoveThresholdExceeded) {
|
---|
160 | int dxp = mousePos.x - e.getX();
|
---|
161 | int dyp = mousePos.y - e.getY();
|
---|
162 | int dp = (int) Math.sqrt(dxp*dxp+dyp*dyp);
|
---|
163 | if (dp < initialMoveThreshold) return;
|
---|
164 | initialMoveThresholdExceeded = true;
|
---|
165 | }
|
---|
166 |
|
---|
167 | EastNorth mouseEN = Main.map.mapView.getEastNorth(e.getX(), e.getY());
|
---|
168 | EastNorth mouseStartEN = Main.map.mapView.getEastNorth(mousePos.x, mousePos.y);
|
---|
169 | double dx = mouseEN.east() - mouseStartEN.east();
|
---|
170 | double dy = mouseEN.north() - mouseStartEN.north();
|
---|
171 | if (dx == 0 && dy == 0)
|
---|
172 | return;
|
---|
173 |
|
---|
174 | if (virtualWay != null) {
|
---|
175 | Collection<Command> virtualCmds = new LinkedList<Command>();
|
---|
176 | virtualCmds.add(new AddCommand(virtualNode));
|
---|
177 | Way w = virtualWay.way;
|
---|
178 | Way wnew = new Way(w);
|
---|
179 | wnew.addNode(virtualWay.lowerIndex+1, virtualNode);
|
---|
180 | virtualCmds.add(new ChangeCommand(w, wnew));
|
---|
181 | virtualCmds.add(new MoveCommand(virtualNode, dx, dy));
|
---|
182 | Main.main.undoRedo.add(new SequenceCommand(tr("Add and move a virtual new node to way"), virtualCmds));
|
---|
183 | selectPrims(Collections.singleton((OsmPrimitive)virtualNode), false, false);
|
---|
184 | virtualWay = null;
|
---|
185 | virtualNode = null;
|
---|
186 | } else {
|
---|
187 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
---|
188 | Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
|
---|
189 |
|
---|
190 | // when rotating, having only one node makes no sense - quit silently
|
---|
191 | if (mode == Mode.rotate && affectedNodes.size() < 2)
|
---|
192 | return;
|
---|
193 |
|
---|
194 | Command c = !Main.main.undoRedo.commands.isEmpty()
|
---|
195 | ? Main.main.undoRedo.commands.getLast() : null;
|
---|
196 | if (c instanceof SequenceCommand)
|
---|
197 | c = ((SequenceCommand)c).getLastCommand();
|
---|
198 |
|
---|
199 | if (mode == Mode.move) {
|
---|
200 | if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).objects))
|
---|
201 | ((MoveCommand)c).moveAgain(dx,dy);
|
---|
202 | else
|
---|
203 | Main.main.undoRedo.add(
|
---|
204 | c = new MoveCommand(selection, dx, dy));
|
---|
205 |
|
---|
206 | for (Node n : affectedNodes) {
|
---|
207 | if (n.coor.isOutSideWorld()) {
|
---|
208 | // Revert move
|
---|
209 | ((MoveCommand) c).moveAgain(-dx, -dy);
|
---|
210 |
|
---|
211 | JOptionPane.showMessageDialog(Main.parent,
|
---|
212 | tr("Cannot move objects outside of the world."));
|
---|
213 | return;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | } else if (mode == Mode.rotate) {
|
---|
217 | if (c instanceof RotateCommand && affectedNodes.equals(((RotateCommand)c).objects))
|
---|
218 | ((RotateCommand)c).rotateAgain(mouseStartEN, mouseEN);
|
---|
219 | else
|
---|
220 | Main.main.undoRedo.add(new RotateCommand(selection, mouseStartEN, mouseEN));
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | Main.map.mapView.repaint();
|
---|
225 | mousePos = e.getPoint();
|
---|
226 |
|
---|
227 | didMove = true;
|
---|
228 | }
|
---|
229 |
|
---|
230 | private Collection<OsmPrimitive> getNearestCollectionVirtual(Point p) {
|
---|
231 | MapView c = Main.map.mapView;
|
---|
232 | int snapDistance = Main.pref.getInteger("mappaint.node.virtual-snap-distance", 8);
|
---|
233 | snapDistance *= snapDistance;
|
---|
234 | OsmPrimitive osm = c.getNearestNode(p);
|
---|
235 | virtualWay = null;
|
---|
236 | virtualNode = null;
|
---|
237 |
|
---|
238 | if (osm == null)
|
---|
239 | {
|
---|
240 | WaySegment nearestWaySeg = c.getNearestWaySegment(p);
|
---|
241 | if (nearestWaySeg != null)
|
---|
242 | {
|
---|
243 | osm = nearestWaySeg.way;
|
---|
244 | if(Main.pref.getInteger("mappaint.node.virtual-size", 8) > 0)
|
---|
245 | {
|
---|
246 | Way w = (Way)osm;
|
---|
247 | Point p1 = c.getPoint(w.nodes.get(nearestWaySeg.lowerIndex).eastNorth);
|
---|
248 | Point p2 = c.getPoint(w.nodes.get(nearestWaySeg.lowerIndex+1).eastNorth);
|
---|
249 | if(SimplePaintVisitor.isLargeSegment(p1, p2, Main.pref.getInteger("mappaint.node.virtual-space", 70)))
|
---|
250 | {
|
---|
251 | Point pc = new Point((p1.x+p2.x)/2, (p1.y+p2.y)/2);
|
---|
252 | if (p.distanceSq(pc) < snapDistance)
|
---|
253 | {
|
---|
254 | virtualWay = nearestWaySeg;
|
---|
255 | virtualNode = new Node(Main.map.mapView.getLatLon(pc.x, pc.y));
|
---|
256 | osm = w;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 | if (osm == null)
|
---|
263 | return Collections.emptySet();
|
---|
264 | return Collections.singleton(osm);
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Look, whether any object is selected. If not, select the nearest node.
|
---|
269 | * If there are no nodes in the dataset, do nothing.
|
---|
270 | *
|
---|
271 | * If the user did not press the left mouse button, do nothing.
|
---|
272 | *
|
---|
273 | * Also remember the starting position of the movement and change the mouse
|
---|
274 | * cursor to movement.
|
---|
275 | */
|
---|
276 | @Override public void mousePressed(MouseEvent e) {
|
---|
277 | if (! (Boolean)this.getValue("active")) return;
|
---|
278 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
279 | return;
|
---|
280 | boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
---|
281 | // boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
|
---|
282 | boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
---|
283 |
|
---|
284 | mouseDownTime = System.currentTimeMillis();
|
---|
285 | didMove = false;
|
---|
286 | initialMoveThresholdExceeded = false;
|
---|
287 |
|
---|
288 | Collection<OsmPrimitive> osmColl = getNearestCollectionVirtual(e.getPoint());
|
---|
289 |
|
---|
290 | if (ctrl && shift) {
|
---|
291 | if (Main.ds.getSelected().isEmpty()) selectPrims(osmColl, true, false);
|
---|
292 | mode = Mode.rotate;
|
---|
293 | setCursor(ImageProvider.getCursor("rotate", null));
|
---|
294 | } else if (!osmColl.isEmpty()) {
|
---|
295 | // Don't replace the selection now if the user clicked on a
|
---|
296 | // selected object (this would break moving of selected groups).
|
---|
297 | // We'll do that later in mouseReleased if the user didn't try to
|
---|
298 | // move.
|
---|
299 | selectPrims(osmColl,
|
---|
300 | shift || Main.ds.getSelected().containsAll(osmColl),
|
---|
301 | ctrl);
|
---|
302 | mode = Mode.move;
|
---|
303 | } else {
|
---|
304 | mode = Mode.select;
|
---|
305 | oldCursor = Main.map.mapView.getCursor();
|
---|
306 | selectionManager.register(Main.map.mapView);
|
---|
307 | selectionManager.mousePressed(e);
|
---|
308 | }
|
---|
309 | if(mode != Mode.move || shift || ctrl)
|
---|
310 | {
|
---|
311 | virtualNode = null;
|
---|
312 | virtualWay = null;
|
---|
313 | }
|
---|
314 |
|
---|
315 | updateStatusLine();
|
---|
316 | Main.map.mapView.repaint();
|
---|
317 |
|
---|
318 | mousePos = e.getPoint();
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Restore the old mouse cursor.
|
---|
323 | */
|
---|
324 | @Override public void mouseReleased(MouseEvent e) {
|
---|
325 | if (mode == Mode.select) {
|
---|
326 | selectionManager.unregister(Main.map.mapView);
|
---|
327 | }
|
---|
328 | restoreCursor();
|
---|
329 |
|
---|
330 | if (mode == Mode.move) {
|
---|
331 | boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
---|
332 | boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
---|
333 | if (!didMove) {
|
---|
334 | selectPrims(
|
---|
335 | Main.map.mapView.getNearestCollection(e.getPoint()),
|
---|
336 | shift, ctrl);
|
---|
337 | } else {
|
---|
338 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
---|
339 | if (ctrl) {
|
---|
340 | Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
|
---|
341 | Collection<Node> nn = Main.map.mapView.getNearestNodes(e.getPoint(), affectedNodes);
|
---|
342 | if (nn != null) {
|
---|
343 | Node n = nn.iterator().next();
|
---|
344 | LinkedList<Node> selNodes = new LinkedList<Node>();
|
---|
345 | for (OsmPrimitive osm : selection)
|
---|
346 | if (osm instanceof Node)
|
---|
347 | selNodes.add((Node)osm);
|
---|
348 | if (selNodes.size() > 0) {
|
---|
349 | selNodes.add(n);
|
---|
350 | MergeNodesAction.mergeNodes(selNodes, n);
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 | DataSet.fireSelectionChanged(selection);
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | updateStatusLine();
|
---|
359 | mode = null;
|
---|
360 | updateStatusLine();
|
---|
361 | }
|
---|
362 |
|
---|
363 | public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
|
---|
364 | selectPrims(selectionManager.getObjectsInRectangle(r, alt), shift, ctrl);
|
---|
365 | }
|
---|
366 |
|
---|
367 | public void selectPrims(Collection<OsmPrimitive> selectionList, boolean shift, boolean ctrl) {
|
---|
368 | if (shift && ctrl)
|
---|
369 | return; // not allowed together
|
---|
370 |
|
---|
371 | Collection<OsmPrimitive> curSel;
|
---|
372 | if (!ctrl && !shift)
|
---|
373 | curSel = new LinkedList<OsmPrimitive>(); // new selection will replace the old.
|
---|
374 | else
|
---|
375 | curSel = Main.ds.getSelected();
|
---|
376 |
|
---|
377 | for (OsmPrimitive osm : selectionList)
|
---|
378 | if (ctrl)
|
---|
379 | curSel.remove(osm);
|
---|
380 | else
|
---|
381 | curSel.add(osm);
|
---|
382 | Main.ds.setSelected(curSel);
|
---|
383 | Main.map.mapView.repaint();
|
---|
384 | }
|
---|
385 |
|
---|
386 | @Override public String getModeHelpText() {
|
---|
387 | if (mode == Mode.select) {
|
---|
388 | return tr("Release the mouse button to select the objects in the rectangle.");
|
---|
389 | } else if (mode == Mode.move) {
|
---|
390 | return tr("Release the mouse button to stop moving. Ctrl to merge with nearest node.");
|
---|
391 | } else if (mode == Mode.rotate) {
|
---|
392 | return tr("Release the mouse button to stop rotating.");
|
---|
393 | } else {
|
---|
394 | return tr("Move objects by dragging; Shift to add to selection (Ctrl to remove); Shift-Ctrl to rotate selected; or change selection");
|
---|
395 | }
|
---|
396 | }
|
---|
397 | }
|
---|