1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.command;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
9 | import org.openstreetmap.josm.data.osm.Node;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 |
|
---|
12 | public class ScaleCommand extends TransformNodesCommand {
|
---|
13 | /**
|
---|
14 | * Pivot point
|
---|
15 | */
|
---|
16 | private EastNorth pivot;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Current scaling factor applied
|
---|
20 | */
|
---|
21 | private double scalingFactor;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * World position of the mouse when the user started the command.
|
---|
25 | */
|
---|
26 | private EastNorth startEN = null;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Creates a ScaleCommand.
|
---|
30 | * Assign the initial object set, compute pivot point.
|
---|
31 | * Computation of pivot point is done by the same rules that are used in
|
---|
32 | * the "align nodes in circle" action.
|
---|
33 | */
|
---|
34 | public ScaleCommand(Collection<OsmPrimitive> objects, EastNorth currentEN) {
|
---|
35 | super(objects);
|
---|
36 |
|
---|
37 | pivot = getNodesCenter();
|
---|
38 |
|
---|
39 | // We remember the very first position of the mouse for this action.
|
---|
40 | // Note that SelectAction will keep the same ScaleCommand when the user
|
---|
41 | // releases the button and presses it again with the same modifiers.
|
---|
42 | // The very first point of this operation is stored here.
|
---|
43 | startEN = currentEN;
|
---|
44 |
|
---|
45 | handleEvent(currentEN);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Compute new scaling factor and transform nodes accordingly.
|
---|
50 | */
|
---|
51 | @Override
|
---|
52 | public final void handleEvent(EastNorth currentEN) {
|
---|
53 | double startAngle = Math.atan2(startEN.east()-pivot.east(), startEN.north()-pivot.north());
|
---|
54 | double endAngle = Math.atan2(currentEN.east()-pivot.east(), currentEN.north()-pivot.north());
|
---|
55 | double startDistance = pivot.distance(startEN);
|
---|
56 | double currentDistance = pivot.distance(currentEN);
|
---|
57 | scalingFactor = Math.cos(startAngle-endAngle) * currentDistance / startDistance;
|
---|
58 | transformNodes();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Scale nodes.
|
---|
63 | */
|
---|
64 | @Override
|
---|
65 | protected void transformNodes() {
|
---|
66 | for (Node n : nodes) {
|
---|
67 | EastNorth oldEastNorth = oldStates.get(n).getEastNorth();
|
---|
68 | double dx = oldEastNorth.east() - pivot.east();
|
---|
69 | double dy = oldEastNorth.north() - pivot.north();
|
---|
70 | double nx = pivot.east() + scalingFactor * dx;
|
---|
71 | double ny = pivot.north() + scalingFactor * dy;
|
---|
72 | n.setEastNorth(new EastNorth(nx, ny));
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public String getDescriptionText() {
|
---|
78 | return trn("Scale {0} node", "Scale {0} nodes", nodes.size(), nodes.size());
|
---|
79 | }
|
---|
80 | }
|
---|