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 final 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 final EastNorth startEN;
|
---|
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 |
|
---|
81 | @Override
|
---|
82 | public int hashCode() {
|
---|
83 | final int prime = 31;
|
---|
84 | int result = super.hashCode();
|
---|
85 | result = prime * result + ((pivot == null) ? 0 : pivot.hashCode());
|
---|
86 | long temp;
|
---|
87 | temp = Double.doubleToLongBits(scalingFactor);
|
---|
88 | result = prime * result + (int) (temp ^ (temp >>> 32));
|
---|
89 | result = prime * result + ((startEN == null) ? 0 : startEN.hashCode());
|
---|
90 | return result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public boolean equals(Object obj) {
|
---|
95 | if (this == obj)
|
---|
96 | return true;
|
---|
97 | if (!super.equals(obj))
|
---|
98 | return false;
|
---|
99 | if (getClass() != obj.getClass())
|
---|
100 | return false;
|
---|
101 | ScaleCommand other = (ScaleCommand) obj;
|
---|
102 | if (pivot == null) {
|
---|
103 | if (other.pivot != null)
|
---|
104 | return false;
|
---|
105 | } else if (!pivot.equals(other.pivot))
|
---|
106 | return false;
|
---|
107 | if (Double.doubleToLongBits(scalingFactor) != Double.doubleToLongBits(other.scalingFactor))
|
---|
108 | return false;
|
---|
109 | if (startEN == null) {
|
---|
110 | if (other.startEN != null)
|
---|
111 | return false;
|
---|
112 | } else if (!startEN.equals(other.startEN))
|
---|
113 | return false;
|
---|
114 | return true;
|
---|
115 | }
|
---|
116 | }
|
---|