source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSAdjustAction.java@ 19424

Last change on this file since 19424 was 19387, checked in by pieren, 15 years ago

More about raster image rotation

File size: 7.8 KB
Line 
1// License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
2package cadastre_fr;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Color;
7import java.awt.Cursor;
8import java.awt.Graphics2D;
9import java.awt.Toolkit;
10import java.awt.event.ActionEvent;
11import java.awt.event.MouseEvent;
12import java.awt.event.MouseListener;
13import java.awt.event.MouseMotionListener;
14import java.util.ArrayList;
15
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.MapFrame;
20import org.openstreetmap.josm.gui.MapView;
21import org.openstreetmap.josm.actions.mapmode.MapMode;
22import org.openstreetmap.josm.data.coor.EastNorth;
23import org.openstreetmap.josm.tools.ImageProvider;
24import org.openstreetmap.josm.gui.layer.Layer;
25
26public class WMSAdjustAction extends MapMode implements
27 MouseListener, MouseMotionListener{
28
29 private static final long serialVersionUID = 1L;
30 private ArrayList<WMSLayer> modifiedLayers = new ArrayList<WMSLayer>();
31 WMSLayer selectedLayer;
32 private boolean rasterMoved;
33 private EastNorth prevEastNorth;
34 enum Mode { moveXY, moveZ, rotate}
35 private static Mode mode = null;
36 private static EastNorth[] croppedRaster = new EastNorth[5];;
37
38 public WMSAdjustAction(MapFrame mapFrame) {
39 super(tr("Adjust WMS"), "adjustxywms",
40 tr("Adjust the position of the WMS layer (raster images only)"), mapFrame,
41 ImageProvider.getCursor("normal", "move"));
42 }
43
44 @Override public void enterMode() {
45 if (Main.map != null) {
46 selectedLayer = null;
47 WMSLayer possibleLayer = null;
48 int cRasterLayers = 0;
49 for (Layer l : Main.map.mapView.getAllLayers()) {
50 if (l instanceof WMSLayer && ((WMSLayer)l).isRaster()) {
51 possibleLayer = (WMSLayer)l;
52 cRasterLayers++;
53 }
54 }
55 Layer activeLayer = Main.map.mapView.getActiveLayer();
56 if (activeLayer instanceof WMSLayer && ((WMSLayer)activeLayer).isRaster()) {
57 selectedLayer = (WMSLayer)activeLayer;
58 } else if (cRasterLayers == 1) {
59 selectedLayer = possibleLayer;
60 }
61 if (selectedLayer != null) {
62 super.enterMode();
63 Main.map.mapView.addMouseListener(this);
64 Main.map.mapView.addMouseMotionListener(this);
65 rasterMoved = false;
66 selectedLayer.adjustModeEnabled = true;
67 } else {
68 JOptionPane.showMessageDialog(Main.parent,tr("This mode works only if active layer is\n"
69 +"a cadastre \"plan image\" (raster image)"));
70 }
71 }
72 }
73
74 @Override public void exitMode() {
75 super.exitMode();
76 Main.map.mapView.removeMouseListener(this);
77 Main.map.mapView.removeMouseMotionListener(this);
78 if (rasterMoved && CacheControl.cacheEnabled) {
79 int reply = JOptionPane.showConfirmDialog(null,
80 "Save the changes in cache ?",
81 "Update cache",
82 JOptionPane.YES_NO_OPTION);
83 if (reply == JOptionPane.OK_OPTION) {
84 saveModifiedLayers();
85 }
86 }
87 modifiedLayers.clear();
88 selectedLayer.adjustModeEnabled = false;
89 selectedLayer = null;
90 }
91
92 @Override
93 public void mousePressed(MouseEvent e) {
94 if (e.getButton() != MouseEvent.BUTTON1)
95 return;
96 boolean ctrl = (e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0;
97 // boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
98 boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
99 if (shift && !ctrl)
100 mode = Mode.moveZ;
101 else if (shift && ctrl)
102 mode = Mode.rotate;
103 else
104 mode = Mode.moveXY;
105 rasterMoved = true;
106 prevEastNorth = Main.map.mapView.getEastNorth(e.getX(), e.getY());
107 Main.map.mapView.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
108 }
109
110 @Override public void mouseDragged(MouseEvent e) {
111 EastNorth newEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
112 if (mode == Mode.rotate) {
113 rotateFrameOnly(prevEastNorth, newEastNorth);
114 } else {
115 if (mode == Mode.moveXY) {
116 displace(prevEastNorth, newEastNorth);
117 } else if (mode == Mode.moveZ) {
118 resize(newEastNorth);
119 }
120 prevEastNorth = newEastNorth;
121 }
122 if (!modifiedLayers.contains(selectedLayer))
123 modifiedLayers.add(selectedLayer);
124 Main.map.mapView.repaint();
125 }
126
127 public static void paintAdjustFrames(Graphics2D g, final MapView mv) {
128 if (mode == Mode.rotate && croppedRaster != null) {
129 g.setColor(Color.red);
130 for (int i=0; i<4; i++)
131 g.drawLine(mv.getPoint(croppedRaster[i]).x,
132 mv.getPoint(croppedRaster[i]).y,
133 mv.getPoint(croppedRaster[i+1]).x,
134 mv.getPoint(croppedRaster[i+1]).y);
135 }
136 }
137
138 private void displace(EastNorth start, EastNorth end) {
139 selectedLayer.displace(end.east()-start.east(), end.north()-start.north());
140 }
141
142 private void resize(EastNorth newEastNorth) {
143 EastNorth center = selectedLayer.getRasterCenter();
144 double dPrev = prevEastNorth.distance(center.east(), center.north());
145 double dNew = newEastNorth.distance(center.east(), center.north());
146 selectedLayer.resize(center, dNew/dPrev);
147 }
148
149 private void rotate(EastNorth start, EastNorth end) {
150 EastNorth pivot = selectedLayer.getRasterCenter();
151 double startAngle = Math.atan2(start.east()-pivot.east(), start.north()-pivot.north());
152 double endAngle = Math.atan2(end.east()-pivot.east(), end.north()-pivot.north());
153 double rotationAngle = endAngle - startAngle;
154 selectedLayer.rotate(pivot, rotationAngle);
155 }
156
157 private void rotateFrameOnly(EastNorth start, EastNorth end) {
158 if (start != null && end != null) {
159 EastNorth pivot = selectedLayer.getRasterCenter();
160 double startAngle = Math.atan2(start.east()-pivot.east(), start.north()-pivot.north());
161 double endAngle = Math.atan2(end.east()-pivot.east(), end.north()-pivot.north());
162 double rotationAngle = endAngle - startAngle;
163 if (selectedLayer.images.get(0).orgCroppedRaster != null) {
164 for (int i=0; i<4; i++) {
165 croppedRaster[i] = selectedLayer.images.get(0).orgCroppedRaster[i].rotate(pivot, rotationAngle);
166 }
167 croppedRaster[4] = croppedRaster[0];
168 }
169 }
170 }
171
172 @Override public void mouseReleased(MouseEvent e) {
173 //Main.map.mapView.repaint();
174 if (mode == Mode.rotate) {
175 EastNorth newEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
176 rotate(prevEastNorth, newEastNorth);
177 Main.map.mapView.repaint();
178 }
179 Main.map.mapView.setCursor(Cursor.getDefaultCursor());
180 prevEastNorth = null;
181 mode = null;
182 }
183
184 public void mouseEntered(MouseEvent e) {
185 }
186 public void mouseExited(MouseEvent e) {
187 }
188 public void mouseMoved(MouseEvent e) {
189 }
190
191 @Override public void mouseClicked(MouseEvent e) {
192 }
193
194 private void saveModifiedLayers() {
195 for (WMSLayer wmsLayer : modifiedLayers) {
196 wmsLayer.saveNewCache();
197 }
198 }
199}
Note: See TracBrowser for help on using the repository browser.