Changeset 26163 in osm for applications/editors/josm/plugins
- Timestamp:
- 2011-06-20T18:30:38+02:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/turbopen/src/org/openstreetmap/josm/plugins/fastdraw/FastDrawingMode.java
r26111 r26163 5 5 * Licence: GPL v2 or later 6 6 * Author: Alexei Kasatkin, 2011 7 * Thanks to authors of BuildingTools, ImproveWayAccuracy and LakeWalker 7 * Thanks to authors of BuildingTools, ImproveWayAccuracy and LakeWalker 8 8 * for good sample code 9 9 */ … … 53 53 AWTEventListener { 54 54 private static final String SIMPLIFYMODE_MESSAGE= 55 "Press Enter to simplify or save, Ctrl-Enter to save with tags, Up/Down to tune simplification";55 tr("Press Enter to simplify or save, Ctrl-Enter to save with tags, Up/Down to tune simplification"); 56 56 private static final String DRAWINGMODE_MESSAGE= 57 "Click or Click&drag to continue, Ctrl-Click to add fixed node, Shift-Click to delete, Enter to simplify or save, Ctrl-Shift-Click to start new line";58 57 tr("Click or Click&drag to continue, Ctrl-Click to add fixed node, Shift-Click to delete, Enter to simplify or save, Ctrl-Shift-Click to start new line"); 58 59 59 private Color COLOR_FIXED; 60 60 private Color COLOR_NORMAL; … … 62 62 private Color COLOR_SELECTEDFRAGMENT; 63 63 private Color COLOR_EDITEDFRAGMENT; 64 64 65 65 private double maxDist; 66 66 private double epsilonMult; … … 70 70 /// Initial tolerance for Douglas-Pecker algorithm 71 71 private double startingEps; 72 72 73 73 private DrawnPolyLine line; 74 74 private MapView mv; … … 92 92 private Stroke strokeForDelete; 93 93 private int dragNode=-1; 94 94 95 95 96 96 FastDrawingMode(MapFrame mapFrame) { … … 121 121 mv = Main.map.mapView; 122 122 line.setMv(mv); 123 123 124 124 if (getCurrentDataSet() == null) return; 125 125 126 126 Main.map.mapView.addMouseListener(this); 127 127 Main.map.mapView.addMouseMotionListener(this); 128 128 Main.map.mapView.addTemporaryLayer(this); 129 129 130 130 Main.map.mapView.setCursor(cursorDraw); 131 131 … … 166 166 167 167 ////////// Event listener methods 168 168 169 169 @Override 170 170 public void paint(Graphics2D g, MapView mv, Bounds bbox) { 171 171 LinkedList<LatLon> pts=line.getPoints(); 172 172 if (pts.isEmpty()) return; 173 173 174 174 if (line.wasSimplified()) { 175 175 // we are drawing simplified version, that exists … … 178 178 g.setStroke(strokeForOriginal); 179 179 } 180 180 181 181 Point p1, p2; 182 182 LatLon pp1, pp2; … … 246 246 if (!isEnabled()) return; 247 247 if (e.getButton() != MouseEvent.BUTTON1) return; 248 249 248 249 250 250 int idx=line.findClosestPoint(e.getPoint(),maxDist); 251 251 if (idx==0 && !line.isClosed()) { … … 256 256 updateCursor(); 257 257 return; 258 } 259 258 } 259 260 260 if (ctrl && shift) newDrawing(); 261 261 if (!ctrl && shift) { 262 262 if (idx>=0) {line.deleteNode(idx); nearestIdx=-1;} 263 else line.tryToDeleteSegment(e.getPoint()); 263 else line.tryToDeleteSegment(e.getPoint()); 264 264 return; 265 265 } … … 267 267 if (ctrl) { 268 268 // toggle fixed point 269 line.toggleFixed(idx); 269 line.toggleFixed(idx); 270 270 } 271 271 // node dragging … … 276 276 //if (line.isClosed()) { setStatusLine(tr(SIMPLIFYMODE_MESSAGE));return; } 277 277 drawing = true; 278 278 279 279 LatLon p = getLatLon(e); 280 280 Node nd1 = getNearestNode(e.getPoint(), maxDist); … … 284 284 p=nd1.getCoor(); 285 285 line.fixPoint(p); 286 } 287 286 } 287 288 288 line.addLast(p); 289 289 if (ctrl) line.fixPoint(p); 290 290 line.clearSimplifiedVersion(); 291 291 292 292 setStatusLine(tr("Please move the mouse to draw new way")); 293 293 repaint(); 294 294 295 295 } 296 296 297 297 @Override 298 298 public void mouseReleased(MouseEvent e) { … … 302 302 drawing = false; 303 303 highlighted=null; 304 if (!line.isClosed()) setStatusLine( tr(DRAWINGMODE_MESSAGE));304 if (!line.isClosed()) setStatusLine(DRAWINGMODE_MESSAGE); 305 305 repaint(); 306 306 } … … 315 315 if (!isEnabled()) return; 316 316 Node nd1 = getNearestNode(e.getPoint(), maxDist); 317 boolean nearpoint2=nd1!=null; 317 boolean nearpoint2=nd1!=null; 318 318 if (nearpoint!=nearpoint2) {nearpoint=nearpoint2;updateCursor();} 319 319 320 320 nearestIdx=line.findClosestPoint(e.getPoint(),maxDist); 321 321 322 322 if (!drawing) { 323 323 if (dragNode>=0) { … … 333 333 highlighted=h2; 334 334 repaint(); 335 } 335 } 336 336 } 337 337 return; 338 338 } 339 if (line.isClosed()) setStatusLine( tr(SIMPLIFYMODE_MESSAGE));340 339 if (line.isClosed()) setStatusLine(SIMPLIFYMODE_MESSAGE); 340 341 341 // do not draw points close to existing points - we do not want self-intersections 342 342 if (nearestIdx>=0) { return; } 343 343 344 344 Point lastP = line.getLastPoint(); // last point of line fragment being edited 345 345 346 346 if (nearpoint){ 347 347 if ( Math.hypot(e.getX() - lastP.x, e.getY() - lastP.y) > 1e-2) { 348 348 line.addFixed(nd1.getCoor()); // snap to node coords 349 349 repaint(); 350 } 350 } 351 351 } else { 352 352 if (Math.hypot(e.getX() - lastP.x, e.getY() - lastP.y) > minPixelsBetweenPoints) { … … 355 355 } 356 356 } 357 357 358 358 //statusText = getLatLon(e).toString(); updateStatusLine(); 359 359 } … … 373 373 if (!line.wasSimplified()) { 374 374 line.simplify(eps); 375 setStatusLine( tr(SIMPLIFYMODE_MESSAGE));375 setStatusLine(SIMPLIFYMODE_MESSAGE); 376 376 } else saveAsWay(); 377 377 } … … 391 391 line.moveToTheEnd(); 392 392 } 393 393 394 394 395 395 } … … 421 421 int n = pts.size(); 422 422 if (n == 0) return; 423 423 424 424 Collection<Command> cmds = new LinkedList<Command>(); 425 425 int i = 0; … … 427 427 LatLon first=pts.get(0); 428 428 Node firstNode=null; 429 429 430 430 for (LatLon p : pts) { 431 431 Node nd=null; … … 482 482 //System.out.println(tr("Eps={0}", eps)); 483 483 eps*=k; 484 setStatusLine(tr("Eps={0}", eps));484 /* I18N: Eps = Epsilon, the tolerance parameter */ setStatusLine(tr("Eps={0}", eps)); 485 485 line.simplify(eps); 486 486 repaint(); 487 487 } 488 488 489 489 private void setStatusLine(String tr) { 490 490 statusText=tr; … … 509 509 }*/ 510 510 511 511 512 512 void loadPrefs() { 513 513 COLOR_DELETE = Main.pref.getColor("fastdraw.color.delete", Color.red); … … 523 523 eps=startingEps; 524 524 } 525 525 526 526 void savePrefs() { 527 527 Main.pref.putColor("fastdraw.color.delete", COLOR_DELETE ); … … 546 546 if (nearpoint) Main.map.mapView.setCursor(cursorCtrl); else 547 547 Main.map.mapView.setCursor(cursorDraw); 548 549 548 549 550 550 } 551 551 // </editor-fold> 552 552 553 553 // <editor-fold defaultstate="collapsed" desc="Helper functions"> 554 554 555 555 private Node getNearestNode(Point point, double maxDist) { 556 556 Node nd = Main.map.mapView.getNearestNode(point, OsmPrimitive.isUsablePredicate); 557 if (nd!=null && line.getPoint(nd.getCoor()).distance(point)<=maxDist) return nd; 557 if (nd!=null && line.getPoint(nd.getCoor()).distance(point)<=maxDist) return nd; 558 558 else return null; 559 559 } … … 563 563 } 564 564 // </editor-fold> 565 565 566 566 }
Note:
See TracChangeset
for help on using the changeset viewer.