Changeset 30444 in osm for applications/editors/josm/plugins/gpsblam
- Timestamp:
- 2014-05-12T00:55:10+02:00 (11 years ago)
- Location:
- applications/editors/josm/plugins/gpsblam
- Files:
-
- 4 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/gpsblam/src/org/openstreetmap/josm/plugins/gpsblam/GPSBlamMode.java
r28741 r30444 16 16 package org.openstreetmap.josm.plugins.gpsblam; 17 17 18 import java.awt.Color;19 18 import static org.openstreetmap.josm.tools.I18n.tr; 20 19 21 20 import java.awt.AWTEvent; 22 21 import java.awt.BasicStroke; 22 import java.awt.Color; 23 23 import java.awt.Cursor; 24 24 import java.awt.Graphics2D; … … 29 29 import java.awt.event.InputEvent; 30 30 import java.awt.event.KeyEvent; 31 import java.awt.event.KeyListener;32 31 import java.awt.event.MouseEvent; 33 32 import java.awt.event.MouseWheelEvent; … … 46 45 public class GPSBlamMode extends MapMode implements LayerChangeListener, MouseWheelListener, AWTEventListener { 47 46 48 49 50 51 52 53 54 55 47 Point pointPressed; 48 MapFrame frame; 49 private Point oldP2; 50 int radius; 51 MouseWheelListener mapViewWheelListeners[]; 52 GPSBlamLayer currentBlamLayer; 53 54 public GPSBlamMode(MapFrame mapFrame, String name, String desc) { 56 55 super(name, "gpsblam_mode.png", desc, mapFrame, Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 57 56 radius = 10; 58 57 } 59 58 60 59 @Override public void enterMode() { … … 71 70 72 71 public void eventDispatched(AWTEvent e) { 73 if (e instanceof KeyEvent) 74 { 75 KeyEvent ke = (KeyEvent)e; 76 if (ke.getKeyCode()==KeyEvent.VK_UP) 77 changeRadiusBy(1); 78 else if (ke.getKeyCode()==KeyEvent.VK_DOWN) 79 changeRadiusBy(-1); 80 } 81 } 82 83 72 if (e instanceof KeyEvent) { 73 KeyEvent ke = (KeyEvent)e; 74 if (ke.getKeyCode()==KeyEvent.VK_UP) 75 changeRadiusBy(1); 76 else if (ke.getKeyCode()==KeyEvent.VK_DOWN) 77 changeRadiusBy(-1); 78 } 79 } 80 84 81 @Override public void exitMode() { 85 82 super.exitMode(); … … 90 87 } 91 88 92 93 89 @Override public void mousePressed(MouseEvent e) { 94 95 96 97 98 99 100 101 102 103 90 if (e.getButton() == MouseEvent.BUTTON1) { 91 pointPressed = new Point(e.getPoint()); 92 // gain exclusive use of mouse wheel for now 93 mapViewWheelListeners = Main.map.mapView.getMouseWheelListeners(); 94 for (MouseWheelListener l : mapViewWheelListeners) { 95 if (l != this) 96 Main.map.mapView.removeMouseWheelListener(l); 97 } 98 paintBox(pointPressed, radius); 99 } 104 100 } 105 101 106 102 @Override public void mouseDragged(MouseEvent e) { 107 103 if ( (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK) { 108 104 //if button1 is hold, draw the line 109 105 paintBox(e.getPoint(), radius); … … 115 111 return; 116 112 } 117 118 119 120 121 113 // give mapView back its mouse wheel 114 for (MouseWheelListener l : mapViewWheelListeners) { 115 if (l != this) 116 Main.map.mapView.addMouseWheelListener(l); 117 } 122 118 123 119 xorDrawBox(pointPressed, oldP2, radius); // clear box 124 125 // Collection <CachedLatLon> selectedCLLs = getSelectedGPXCLLs(pointPressed, e.getPoint());120 121 // Collection <CachedLatLon> selectedCLLs = getSelectedGPXCLLs(pointPressed, e.getPoint()); 126 122 GPSBlamInputData inputData = new GPSBlamInputData(pointPressed, e.getPoint(), radius); 127 if (!inputData.isEmpty()) 128 { 123 if (!inputData.isEmpty()) { 129 124 if(currentBlamLayer == null) { 130 125 currentBlamLayer = new GPSBlamLayer(tr("GPSBlam")); 131 126 Main.main.addLayer(currentBlamLayer); 132 127 } 133 128 currentBlamLayer.addBlamMarker(new GPSBlamMarker(inputData)); 134 129 Main.map.mapView.repaint(); 135 130 } 136 131 137 132 pointPressed = oldP2 = null; 138 133 } 139 140 141 134 142 135 public void changeRadiusBy(int delta) { 143 if (pointPressed != null) 144 { 145 int new_radius = radius + delta; 146 if (new_radius < 1) 147 new_radius = 1; 148 paintBox(oldP2, new_radius); 149 radius = new_radius; 150 } 136 if (pointPressed != null) { 137 int new_radius = radius + delta; 138 if (new_radius < 1) 139 new_radius = 1; 140 paintBox(oldP2, new_radius); 141 radius = new_radius; 142 } 151 143 } 152 144 153 145 @Override public void mouseWheelMoved(MouseWheelEvent e) { 154 if ( (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK) { 155 changeRadiusBy(-e.getWheelRotation()); 156 157 } 158 } 159 146 if ( (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK) { 147 changeRadiusBy(-e.getWheelRotation()); 148 } 149 } 150 160 151 private void xorDrawBox(Point p1, Point p2, int radius){ 161 152 if (frame != null) { 162 // Graphics2D g = (Graphics2D)frame.getGraphics(); 163 Graphics2D g = (Graphics2D)Main.map.mapView.getGraphics(); 153 Graphics2D g = (Graphics2D)Main.map.mapView.getGraphics(); 164 154 g.setXORMode(Color.BLACK); 165 155 g.setColor(Color.WHITE); 166 156 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);// AA+XOR broken in some versions of Java 167 157 g.setStroke(new BasicStroke(2.0f)); 168 if (p2==null)169 158 if (p2==null) 159 p2 = p1; 170 160 double length = Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)); 171 if (length > 0) 172 { 173 double dir_x = (p2.x-p1.x)/length, dir_y = (p2.y-p1.y)/length; // unit direction vector from p1.x,p1.y to p2.x, p2.y 174 double perpdir_x = dir_y, perpdir_y = -dir_x; // unit vector 90deg CW from direction vector 175 double angle = Math.atan2(-perpdir_y, perpdir_x); // polar angle of perpdir 176 double ofs_x = radius * perpdir_x, ofs_y = radius * perpdir_y; // radius vector, 90deg CW from dir vector 177 178 Path2D path = new Path2D.Double(); 179 path.append(new Line2D.Double(p1.x+ofs_x, p1.y+ofs_y,p2.x+ofs_x, p2.y+ofs_y), false); 180 path.append(new Arc2D.Double(p2.x-radius, p2.y-radius,radius*2, radius*2, 181 Math.toDegrees(angle), -180, Arc2D.OPEN), true); 182 path.append(new Line2D.Double(p2.x-ofs_x, p2.y-ofs_y,p1.x-ofs_x, p1.y-ofs_y), true); 183 path.append(new Arc2D.Double(p1.x-radius, p1.y-radius,radius*2, radius*2, 184 Math.toDegrees(angle)-180, -180, Arc2D.OPEN), true); 185 path.closePath(); 186 g.draw(path); 187 188 } 189 else 190 { 191 g.setXORMode(Color.BLACK); 192 g.setColor(Color.WHITE); 193 g.drawOval((int)Math.round(p2.x-radius), (int)Math.round(p2.y-radius), 194 (int)Math.round(radius*2), (int)Math.round(radius*2)); 195 } 196 } 197 } 198 161 if (length > 0) { 162 double dir_x = (p2.x-p1.x)/length, dir_y = (p2.y-p1.y)/length; // unit direction vector from p1.x,p1.y to p2.x, p2.y 163 double perpdir_x = dir_y, perpdir_y = -dir_x; // unit vector 90deg CW from direction vector 164 double angle = Math.atan2(-perpdir_y, perpdir_x); // polar angle of perpdir 165 double ofs_x = radius * perpdir_x, ofs_y = radius * perpdir_y; // radius vector, 90deg CW from dir vector 166 167 Path2D path = new Path2D.Double(); 168 path.append(new Line2D.Double(p1.x+ofs_x, p1.y+ofs_y,p2.x+ofs_x, p2.y+ofs_y), false); 169 path.append(new Arc2D.Double(p2.x-radius, p2.y-radius,radius*2, radius*2, 170 Math.toDegrees(angle), -180, Arc2D.OPEN), true); 171 path.append(new Line2D.Double(p2.x-ofs_x, p2.y-ofs_y,p1.x-ofs_x, p1.y-ofs_y), true); 172 path.append(new Arc2D.Double(p1.x-radius, p1.y-radius,radius*2, radius*2, 173 Math.toDegrees(angle)-180, -180, Arc2D.OPEN), true); 174 path.closePath(); 175 g.draw(path); 176 } else { 177 try { 178 g.setXORMode(Color.BLACK); 179 g.setColor(Color.WHITE); 180 g.drawOval((int)Math.round(p2.x-radius), (int)Math.round(p2.y-radius), 181 (int)Math.round(radius*2), (int)Math.round(radius*2)); 182 } catch (InternalError e) { 183 // Robustness against Java bug https://bugs.openjdk.java.net/browse/JDK-8041647 184 Main.error(e); 185 Main.error("Java bug JDK-8041647 occured. To avoid this bug, please consult https://bugs.openjdk.java.net/browse/JDK-8041647." 186 +" If the bug is fixed, please update your Java Runtime Environment."); 187 } 188 } 189 } 190 } 199 191 200 192 private void paintBox(Point p2, int new_radius) { 201 202 193 if (frame != null) { 203 194 if (oldP2 != null) { … … 208 199 } 209 200 } 210 201 211 202 public void setFrame(MapFrame mapFrame) { 212 203 frame = mapFrame; 213 204 } 214 205 215 216 @Override 217 public void activeLayerChange(Layer arg0, Layer arg1) { 218 } 219 220 @Override 221 public void layerAdded(Layer arg0) { 222 } 223 224 @Override 206 @Override 207 public void activeLayerChange(Layer arg0, Layer arg1) { 208 } 209 210 @Override 211 public void layerAdded(Layer arg0) { 212 } 213 214 @Override 225 215 public void layerRemoved(Layer oldLayer) { 226 216 if (oldLayer instanceof GPSBlamLayer) { … … 231 221 } 232 222 233 234 223 @Override 235 224 public void destroy() { … … 237 226 MapView.removeLayerChangeListener(this); 238 227 } 239 240 228 }
Note:
See TracChangeset
for help on using the changeset viewer.