001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command;
003
004import static org.openstreetmap.josm.tools.I18n.trn;
005
006import java.util.Collection;
007import java.util.Objects;
008
009import org.openstreetmap.josm.data.coor.EastNorth;
010import org.openstreetmap.josm.data.osm.Node;
011import org.openstreetmap.josm.data.osm.OsmPrimitive;
012
013/**
014 * Command, to scale a given set of primitives.
015 * The relative distance of the nodes will be increased/decreased.
016 */
017public class ScaleCommand extends TransformNodesCommand {
018    /**
019     * Pivot point
020     */
021    private final EastNorth pivot;
022
023    /**
024     * Current scaling factor applied
025     */
026    private double scalingFactor;
027
028    /**
029     * scaling factor applied previously
030     */
031    private double deltaScalingFactor;
032
033    /**
034     * World position of the mouse when the user started the command.
035     */
036    private EastNorth startEN;
037
038    /**
039     * Creates a ScaleCommand.
040     * Assign the initial object set, compute pivot point.
041     * Computation of pivot point is done by the same rules that are used in
042     * the "align nodes in circle" action.
043     * @param objects objects to fetch nodes from
044     * @param currentEN current east/north
045     */
046    public ScaleCommand(Collection<? extends OsmPrimitive> objects, EastNorth currentEN) {
047        super(objects);
048
049        pivot = getNodesCenter();
050
051        // We remember the very first position of the mouse for this action.
052        // Note that SelectAction will keep the same ScaleCommand when the user
053        // releases the button and presses it again with the same modifiers.
054        // The very first point of this operation is stored here.
055        startEN = currentEN;
056
057        handleEvent(currentEN);
058    }
059
060    /**
061     * Compute new scaling factor and transform nodes accordingly.
062     */
063    @Override
064    public final void handleEvent(EastNorth currentEN) {
065        setScalingFactor(deltaScalingFactor + calcScalingFactor(currentEN));
066        transformNodes();
067    }
068
069    /**
070     * Handle a repeated scaling action where the mouse was moved to a different position
071     * see #24695
072     * @param newStartEN start cursor position of a repeated scaling
073     * @since 19566
074     */
075    public void handleUpdate(EastNorth newStartEN) {
076        startEN = newStartEN;
077        deltaScalingFactor = scalingFactor - calcScalingFactor(newStartEN);
078    }
079
080    private double calcScalingFactor(EastNorth currentEN) {
081        double startAngle = Math.atan2(startEN.east() - pivot.east(), startEN.north() - pivot.north());
082        double endAngle = Math.atan2(currentEN.east() - pivot.east(), currentEN.north() - pivot.north());
083        double startDistance = pivot.distance(startEN);
084        double currentDistance = pivot.distance(currentEN);
085        return Math.cos(startAngle - endAngle) * currentDistance / startDistance;
086    }
087
088    /**
089     * Set the scaling factor
090     * @param scalingFactor The scaling factor.
091     */
092    protected void setScalingFactor(double scalingFactor) {
093        this.scalingFactor = scalingFactor;
094    }
095
096    /**
097     * Returns the scaling factor.
098     * @return The scaling factor
099     */
100    public double getScalingFactor() {
101        return scalingFactor;
102    }
103
104    /**
105     * Scale nodes.
106     */
107    @Override
108    protected void transformNodes() {
109        for (Node n : nodes) {
110            EastNorth oldEastNorth = oldStates.get(n).getEastNorth();
111            double dx = oldEastNorth.east() - pivot.east();
112            double dy = oldEastNorth.north() - pivot.north();
113            double nx = pivot.east() + scalingFactor * dx;
114            double ny = pivot.north() + scalingFactor * dy;
115            n.setEastNorth(new EastNorth(nx, ny));
116        }
117    }
118
119    @Override
120    public String getDescriptionText() {
121        return trn("Scale {0} node", "Scale {0} nodes", nodes.size(), nodes.size());
122    }
123
124    @Override
125    public int hashCode() {
126        return Objects.hash(super.hashCode(), pivot, scalingFactor, startEN, deltaScalingFactor);
127    }
128
129    @Override
130    public boolean equals(Object obj) {
131        if (this == obj) return true;
132        if (obj == null || getClass() != obj.getClass()) return false;
133        if (!super.equals(obj)) return false;
134        ScaleCommand that = (ScaleCommand) obj;
135        return Double.compare(that.scalingFactor, scalingFactor) == 0 &&
136                Objects.equals(pivot, that.pivot) &&
137                Objects.equals(startEN, that.startEN) &&
138                Objects.equals(deltaScalingFactor, that.deltaScalingFactor);
139    }
140}