1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.utilsplugin2.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.LinkedList;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
16 | import org.openstreetmap.josm.command.Command;
|
---|
17 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
18 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
19 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
20 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
25 | import org.openstreetmap.josm.gui.Notification;
|
---|
26 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Mirror the selected ways nodes or ways along line given by two first selected points
|
---|
30 | *
|
---|
31 | * Note: If a ways are selected, their nodes are mirrored
|
---|
32 | *
|
---|
33 | * @author Alexei Kasatkin, based on much copy+paste from other MirrorAction :)
|
---|
34 | */
|
---|
35 | public final class SymmetryAction extends JosmAction {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Constructs a new {@code SymmetryAction}.
|
---|
39 | */
|
---|
40 | public SymmetryAction() {
|
---|
41 | super(tr("Symmetry"), "symmetry", tr("Mirror selected nodes and ways."),
|
---|
42 | Shortcut.registerShortcut("tools:symmetry", tr("More tools: {0}", tr("Symmetry")),
|
---|
43 | KeyEvent.VK_S, Shortcut.ALT_SHIFT), true);
|
---|
44 | putValue("help", ht("/Action/Symmetry"));
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public void actionPerformed(ActionEvent e) {
|
---|
49 | Collection<OsmPrimitive> sel = getLayerManager().getEditDataSet().getSelected();
|
---|
50 | HashSet<Node> nodes = new HashSet<>();
|
---|
51 | EastNorth p1 = null, p2 = null;
|
---|
52 |
|
---|
53 | for (OsmPrimitive osm : sel) {
|
---|
54 | if (osm instanceof Node) {
|
---|
55 | if (p1 == null) p1 = ((Node) osm).getEastNorth(); else
|
---|
56 | if (p2 == null) p2 = ((Node) osm).getEastNorth(); else
|
---|
57 | nodes.add((Node) osm);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | for (OsmPrimitive osm : sel) {
|
---|
61 | if (osm instanceof Way) {
|
---|
62 | nodes.addAll(((Way) osm).getNodes());
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (p1 == null || p2 == null || nodes.size() < 1) {
|
---|
67 | new Notification(
|
---|
68 | tr("Please select at least two nodes for symmetry axis and something else to mirror.")
|
---|
69 | ).setIcon(JOptionPane.WARNING_MESSAGE).show();
|
---|
70 | return;
|
---|
71 | }
|
---|
72 |
|
---|
73 | double ne, nn, l, e0, n0;
|
---|
74 | e0 = p1.east();
|
---|
75 | n0 = p1.north();
|
---|
76 | ne = -(p2.north() - p1.north());
|
---|
77 | nn = (p2.east() - p1.east());
|
---|
78 | l = Math.hypot(ne, nn);
|
---|
79 | ne /= l; nn /= l; // normal unit vector
|
---|
80 |
|
---|
81 | Collection<Command> cmds = new LinkedList<>();
|
---|
82 |
|
---|
83 | for (Node n : nodes) {
|
---|
84 | EastNorth c = n.getEastNorth();
|
---|
85 | double pr = (c.east()-e0)*ne + (c.north()-n0)*nn;
|
---|
86 | //pr=10;
|
---|
87 | cmds.add(new MoveCommand(n, -2*ne*pr, -2*nn*pr));
|
---|
88 | }
|
---|
89 |
|
---|
90 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Symmetry"), cmds));
|
---|
91 | MainApplication.getMap().repaint();
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | protected void updateEnabledState() {
|
---|
96 | updateEnabledStateOnCurrentSelection();
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
101 | setEnabled(selection != null && !selection.isEmpty());
|
---|
102 | }
|
---|
103 | }
|
---|