source: josm/trunk/src/org/openstreetmap/josm/command/ScaleCommand.java@ 8378

Last change on this file since 8378 was 8285, checked in by Don-vip, 9 years ago

fix sonar squid:S2039 - Member variable visibility should be specified

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.openstreetmap.josm.tools.I18n.trn;
5
6import java.util.Collection;
7
8import javax.swing.Icon;
9
10import org.openstreetmap.josm.data.coor.EastNorth;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.tools.ImageProvider;
14
15public 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 private EastNorth startEN = null;
30
31 /**
32 * Creates a ScaleCommand.
33 * Assign the initial object set, compute pivot point.
34 * Computation of pivot point is done by the same rules that are used in
35 * the "align nodes in circle" action.
36 */
37 public ScaleCommand(Collection<OsmPrimitive> objects, EastNorth currentEN) {
38 super(objects);
39
40 pivot = getNodesCenter();
41
42 // We remember the very first position of the mouse for this action.
43 // Note that SelectAction will keep the same ScaleCommand when the user
44 // releases the button and presses it again with the same modifiers.
45 // The very first point of this operation is stored here.
46 startEN = currentEN;
47
48 handleEvent(currentEN);
49 }
50
51 /**
52 * Compute new scaling factor and transform nodes accordingly.
53 */
54 @Override
55 public final void handleEvent(EastNorth currentEN) {
56 double startAngle = Math.atan2(startEN.east()-pivot.east(), startEN.north()-pivot.north());
57 double endAngle = Math.atan2(currentEN.east()-pivot.east(), currentEN.north()-pivot.north());
58 double startDistance = pivot.distance(startEN);
59 double currentDistance = pivot.distance(currentEN);
60 scalingFactor = Math.cos(startAngle-endAngle) * currentDistance / startDistance;
61 transformNodes();
62 }
63
64 /**
65 * Scale nodes.
66 */
67 @Override
68 protected void transformNodes() {
69 for (Node n : nodes) {
70 EastNorth oldEastNorth = oldStates.get(n).getEastNorth();
71 double dx = oldEastNorth.east() - pivot.east();
72 double dy = oldEastNorth.north() - pivot.north();
73 double nx = pivot.east() + scalingFactor * dx;
74 double ny = pivot.north() + scalingFactor * dy;
75 n.setEastNorth(new EastNorth(nx, ny));
76 }
77 }
78
79 @Override
80 public String getDescriptionText() {
81 return trn("Scale {0} node", "Scale {0} nodes", nodes.size(), nodes.size());
82 }
83
84 @Override
85 public Icon getDescriptionIcon() {
86 return ImageProvider.get("data", "node");
87 }
88}
Note: See TracBrowser for help on using the repository browser.