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