Changeset 32548 in osm for applications/editors/josm/plugins/FastDraw/src
- Timestamp:
- 2016-07-04T03:46:58+02:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw/DrawnPolyLine.java
r30737 r32548 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.plugins.fastdraw; 2 3 … … 25 26 clear(); 26 27 } 28 27 29 public void setMv(MapView mv) { 28 30 this.mv = mv; … … 35 37 double getLength() { 36 38 List<LatLon> pts = getPoints(); 37 Iterator<LatLon> it1, it2;38 LatLon pp1, pp2;39 if (pts.size() <2) return 0;40 it1 =pts.listIterator(0);41 it2 =pts.listIterator(1);42 double len =0;39 Iterator<LatLon> it1, it2; 40 LatLon pp1, pp2; 41 if (pts.size() < 2) return 0; 42 it1 = pts.listIterator(0); 43 it2 = pts.listIterator(1); 44 double len = 0; 43 45 for (int i = 0; i < pts.size() - 1; i++) { 44 45 46 len+=pp1.greatCircleDistance(pp2);46 pp1 = it1.next(); 47 pp2 = it2.next(); 48 len += pp1.greatCircleDistance(pp2); 47 49 } 48 50 return len; … … 50 52 51 53 LinkedList<LatLon> getPoints() { 52 if (simplePoints !=null) return simplePoints; else return points;54 if (simplePoints != null) return simplePoints; else return points; 53 55 } 54 56 55 57 boolean wasSimplified() { 56 return (simplePoints !=null && simplePoints.size()>0);58 return (simplePoints != null && simplePoints.size() > 0); 57 59 } 58 60 59 61 int findClosestPoint(Point p, double d) { 60 double x =p.x, y=p.y;61 int n =points.size();62 int idx =-1;63 double dist, minD=1e10;64 for (int i =0;i<n;i++) {65 dist = Math.sqrt(getPoint(points.get(i)).distanceSq(x, y));66 if (dist <d && dist<minD) {67 idx =i;68 minD =dist;69 } ;62 double x = p.x, y = p.y; 63 int n = points.size(); 64 int idx = -1; 65 double dist, minD = 1e10; 66 for (int i = 0; i < n; i++) { 67 dist = Math.sqrt(getPoint(points.get(i)).distanceSq(x, y)); 68 if (dist < d && dist < minD) { 69 idx = i; 70 minD = dist; 71 } 70 72 } 71 73 return idx; … … 74 76 void clear() { 75 77 points.clear(); 76 used =null;77 lastIdx =0;78 closedFlag =false;78 used = null; 79 lastIdx = 0; 80 closedFlag = false; 79 81 fixed.clear(); 80 simplePoints =null;82 simplePoints = null; 81 83 } 82 84 83 85 void undo() { 84 86 //if (points.size() > 0) points.removeLast(); 85 if (lastIdx >0 && lastIdx<points.size()){86 points.remove(lastIdx);87 lastIdx--;87 if (lastIdx > 0 && lastIdx < points.size()) { 88 points.remove(lastIdx); 89 lastIdx--; 88 90 } 89 91 } … … 103 105 104 106 void addLast(LatLon coor) { 105 if (closedFlag && lastIdx >points.size()-1) return;106 if (lastIdx >=points.size()-1) {107 if (closedFlag && lastIdx > points.size()-1) return; 108 if (lastIdx >= points.size()-1) { 107 109 // 108 110 if (points.isEmpty() || !coor.equals(points.getLast())) { 109 111 points.addLast(coor); 110 if (points.size() >1) lastIdx++;111 112 if (points.size() > 1) lastIdx++; 113 } 112 114 } else { 113 115 // insert point into midlle of the line … … 120 122 121 123 Point getLastPoint() { 122 if (lastIdx <points.size()) return getPoint(points.get(lastIdx));124 if (lastIdx < points.size()) return getPoint(points.get(lastIdx)); 123 125 else return null; 124 126 } … … 129 131 130 132 int getSimplePointsCount() { 131 if (simplePoints !=null)return simplePoints.size(); else return -1;133 if (simplePoints != null) return simplePoints.size(); else return -1; 132 134 } 133 135 … … 135 137 * Increase epsilon to fit points count in maxPKM point per 1 km 136 138 */ 137 double autoSimplify(double initEpsilon, double ekf,int k,double maxPKM) {138 double e =initEpsilon;139 if (e <1e-3) e=1e-3;140 if (ekf <1+1e-2) ekf=1.01;139 double autoSimplify(double initEpsilon, double ekf, int k, double maxPKM) { 140 double e = initEpsilon; 141 if (e < 1e-3) e = 1e-3; 142 if (ekf < 1+1e-2) ekf = 1.01; 141 143 simplify(e); 142 while (getNodesPerKm(k) >maxPKM && e<1e3) {143 e=e*ekf;144 145 144 while (getNodesPerKm(k) > maxPKM && e < 1e3) { 145 e = e*ekf; 146 simplify(e); 147 //System.out.printf("eps=%f n=%d\n", e,simplePoints.size()); 146 148 } 147 149 return e; … … 217 219 */ 218 220 public double pointLineDistance(Point p1, Point p2, Point p3) { 219 double x0 = p1.x; 220 double x1 = p2.x; 221 double x2 = p3.x; 221 double x0 = p1.x; double y0 = p1.y; 222 double x1 = p2.x; double y1 = p2.y; 223 double x2 = p3.x; double y2 = p3.y; 222 224 if (x2 == x1 && y2 == y1) { 223 225 return Math.hypot(x1 - x0, y1 - y0); 224 226 } else { 225 return Math.abs((x2-x1)*(y1-y0)-(x1-x0)*(y2-y1))/Math.hypot(x2 - x1, y2 - y1);227 return Math.abs((x2-x1)*(y1-y0)-(x1-x0)*(y2-y1))/Math.hypot(x2 - x1, y2 - y1); 226 228 } 227 229 } … … 229 231 void closeLine() { 230 232 points.add(points.getFirst()); 231 closedFlag=true; 232 } 233 closedFlag = true; 234 } 235 233 236 boolean isClosed() { 234 237 return closedFlag; … … 236 239 237 240 void deleteNode(int idx) { 238 if (idx <=lastIdx) lastIdx--;241 if (idx <= lastIdx) lastIdx--; 239 242 fixed.remove(points.get(idx)); 240 243 points.remove(idx); 241 244 } 245 242 246 void tryToDeleteSegment(Point p) { 243 if (points.size() <3) return;247 if (points.size() < 3) return; 244 248 245 249 LatLon start; 246 250 start = findBigSegment(p); 247 ListIterator<LatLon> it = points.listIterator();251 ListIterator<LatLon> it = points.listIterator(); 248 252 LatLon pp; 249 boolean f =false;250 int i =0,idx=-1;253 boolean f = false; 254 int i = 0, idx = -1; 251 255 while (it.hasNext()) { 252 pp =it.next();253 if (f && (fixed.contains(pp))) {256 pp = it.next(); 257 if (f && (fixed.contains(pp))) { 254 258 // if end of line fragment reached 255 lastIdx =idx;259 lastIdx = idx; 256 260 return; 257 }if (f &&(!it.hasNext())) { 261 } 262 if (f && !it.hasNext()) { 258 263 // if end of whole line reached 259 closedFlag =false;264 closedFlag = false; 260 265 it.remove(); 261 lastIdx =points.size()-1;266 lastIdx = points.size()-1; 262 267 return; 263 268 } … … 265 270 // if we are deleting this segment 266 271 if (f) it.remove(); 267 if (pp == start) {f=true;idx=i;} // next node should be removed 272 if (pp == start) { 273 f = true; 274 idx = i; 275 } // next node should be removed 268 276 i++; 269 277 } 270 lastIdx =points.size()-1;271 278 lastIdx = points.size()-1; 279 } 272 280 273 281 /** find starting point of the polyline line fragment close to p 274 282 * line fragment = segments between two fixed (green) nodes 275 * @param p276 * @return277 283 */ 278 284 LatLon findBigSegment(Point p) { 279 if (points.size() <2) return null;285 if (points.size() < 2) return null; 280 286 Iterator<LatLon> it1 = points.listIterator(0); 281 287 Iterator<LatLon> it2 = points.listIterator(1); 282 Point p1, p2;283 LatLon pp1, pp2,start=null;284 start =points.getFirst();288 Point p1, p2; 289 LatLon pp1, pp2, start = null; 290 start = points.getFirst(); 285 291 do { 286 pp1=it1.next(); 287 pp2=it2.next(); 288 p1 = getPoint(pp1); 289 p2 = getPoint(pp2); 290 // maintain segment start end end 291 if (fixed.contains(pp1) ) { start=pp1; } 292 if (pointSegmentDistance(p,p1,p2) < 5) { 293 return start; 294 } 292 pp1 = it1.next(); 293 pp2 = it2.next(); 294 p1 = getPoint(pp1); 295 p2 = getPoint(pp2); 296 // maintain segment start end end 297 if (fixed.contains(pp1)) { 298 start = pp1; 299 } 300 if (pointSegmentDistance(p, p1, p2) < 5) { 301 return start; 302 } 295 303 } while (it2.hasNext()); 296 304 return null; 297 298 305 } 299 306 300 307 private double pointSegmentDistance(Point p, Point p1, Point p2) { 301 double a,b,x,y,l,kt,kn,dist; 302 x=p.x-p1.x; y=p.y-p1.y; 303 a=p2.x-p1.x; b=p2.y-p1.y; 304 l=Math.hypot(a,b); 305 if (l==0) return Math.hypot(x, y); // p1 = p2 306 kt=(x*a+y*b)/l; 307 kn=Math.abs((-x*b+y*a)/l); 308 if (kt>=0 && kt<l) dist=kn; else { 309 dist=Math.min(Math.hypot(x, y), Math.hypot(x-a, y-b)); 308 double a, b, x, y, l, kt, kn, dist; 309 x = p.x-p1.x; 310 y = p.y-p1.y; 311 a = p2.x-p1.x; 312 b = p2.y-p1.y; 313 l = Math.hypot(a, b); 314 if (l == 0) return Math.hypot(x, y); // p1 = p2 315 kt = (x*a+y*b)/l; 316 kn = Math.abs((-x*b+y*a)/l); 317 if (kt >= 0 && kt < l) dist = kn; else { 318 dist = Math.min(Math.hypot(x, y), Math.hypot(x-a, y-b)); 310 319 } 311 320 return dist; … … 313 322 314 323 void clearSimplifiedVersion() { 315 simplePoints =null;324 simplePoints = null; 316 325 } 317 326 318 327 boolean isLastPoint(int i) { 319 return (lastIdx ==i);328 return (lastIdx == i); 320 329 } 321 330 322 331 void moveToTheEnd() { 323 lastIdx =points.size()-1;332 lastIdx = points.size()-1; 324 333 } 325 334 … … 341 350 } 342 351 if (fixed.contains(dragged)) { 343 344 345 352 fixed.remove(dragged); 353 fixed.add(coor); 354 } 346 355 } 347 356 … … 353 362 public double getNodesPerKm(int k) { 354 363 List<LatLon> pts = simplePoints; 355 if (!wasSimplified()) pts =points;356 int n =pts.size();357 if (n <2) return 0;358 if (k <2) k=2;359 if (k >n) k=n;360 361 LatLon pp1, pp2 =null;362 Iterator<LatLon> it1, it2;363 it1 =pts.listIterator(0);364 it2 =pts.listIterator(1);365 double lens[]=new double[n];364 if (!wasSimplified()) pts = points; 365 int n = pts.size(); 366 if (n < 2) return 0; 367 if (k < 2) k = 2; 368 if (k > n) k = n; 369 370 LatLon pp1, pp2 = null; 371 Iterator<LatLon> it1, it2; 372 it1 = pts.listIterator(0); 373 it2 = pts.listIterator(1); 374 double[] lens = new double[n]; 366 375 for (int i = 0; i < n-1; i++) { 367 368 369 370 371 lens[i]=pp1.greatCircleDistance(pp2);372 373 double pkm =0,maxpkm=0;374 double len =0;375 int seg =0; // averaged segments counts376 pp1 = it1.next(); 377 //p1 = getPoint(pp1); 378 pp2 = it2.next(); 379 //p2 =sa getPoint(pp2); 380 lens[i] = pp1.greatCircleDistance(pp2); 381 } 382 double pkm = 0, maxpkm = 0; 383 double len = 0; 384 int seg = 0; // averaged segments counts 376 385 for (int i = 1; i < n; i++) { 377 len+=lens[i-1]; // add next next point 378 // remove old segment 379 if (i>k) {seg=k; len-=lens[i-k-1];} else seg=i; 380 if (i>=k || i==n-1) { 381 // len is length of points[i-windowSize] .. points[i] 382 if (len>0) pkm = seg / len * 1000; 383 //System.out.println("i="+i+" pkm="+len+" pkm="+pkm); 384 if (pkm > maxpkm) maxpkm=pkm; 385 } 386 } 386 len += lens[i-1]; // add next next point 387 // remove old segment 388 if (i > k) { 389 seg = k; 390 len -= lens[i-k-1]; 391 } else 392 seg = i; 393 if (i >= k || i == n-1) { 394 // len is length of points[i-windowSize] .. points[i] 395 if (len > 0) pkm = seg / len * 1000; 396 //System.out.println("i="+i+" pkm="+len+" pkm="+pkm); 397 if (pkm > maxpkm) maxpkm = pkm; 398 } 399 } 387 400 return Math.round(maxpkm); 388 389 401 } 390 402 … … 392 404 return points.size(); 393 405 } 394 395 406 } -
applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw/FDSettings.java
r31265 r32548 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.plugins.fastdraw; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 2 5 3 6 import java.awt.Color; 4 7 import java.awt.Stroke; 5 8 import java.io.IOException; 9 6 10 import org.openstreetmap.josm.Main; 7 11 import org.openstreetmap.josm.gui.util.GuiHelper; 8 import static org.openstreetmap.josm.tools.I18n.tr;9 12 10 13 public class FDSettings { … … 15 18 public Color COLOR_EDITEDFRAGMENT; 16 19 public Color COLOR_SIMPLIFIED; 17 20 18 21 public double maxDist; 19 22 public double epsilonMult; … … 22 25 public double minPixelsBetweenPoints; 23 26 /// Initial tolerance for Douglas-Pecker algorithm 24 public double startingEps; 27 public double startingEps; 25 28 /// Maximum number of points per 1 km of way 26 29 public double maxPointsPerKm; … … 28 31 public boolean drawLastSegment; 29 32 // snap to nodes 30 public boolean snapNodes; 33 public boolean snapNodes; 31 34 // add fixed foints on mouse click 32 public boolean fixedClick; 35 public boolean fixedClick; 33 36 // add fixed foints on spacebar 34 37 public boolean fixedSpacebar; … … 46 49 public int dotSize; 47 50 public int bigDotSize; 48 51 49 52 public void loadPrefs() { 50 53 COLOR_DELETE = Main.pref.getColor("fastdraw.color.delete", Color.red); … … 54 57 COLOR_SELECTEDFRAGMENT = Main.pref.getColor("fastdraw.color.select", Color.blue); 55 58 COLOR_SIMPLIFIED = Main.pref.getColor("fastdraw.color.simplified", Color.orange); 56 59 57 60 normalStroke = GuiHelper.getCustomizedStroke(Main.pref.get("fastdraw.stroke.normal", "2")); 58 61 deleteStroke = GuiHelper.getCustomizedStroke(Main.pref.get("fastdraw.stroke.delete", "3")); 59 62 simplifiedStroke = GuiHelper.getCustomizedStroke(Main.pref.get("fastdraw.stroke.simplified", "2")); 60 63 61 64 bigDotSize = Main.pref.getInteger("fastdraw.point.bigsize", 7); 62 65 dotSize = Main.pref.getInteger("fastdraw.point.normalsize", 5); 63 66 64 67 maxDist = Main.pref.getDouble("fastdraw.maxdist", 5); 65 68 epsilonMult = Main.pref.getDouble("fastdraw.epsilonmult", 1.1); … … 73 76 fixedClick = Main.pref.getBoolean("fastdraw.fixedclick", false); 74 77 fixedSpacebar = Main.pref.getBoolean("fastdraw.fixedspacebar", false); 75 drawClosed = 78 drawClosed = Main.pref.getBoolean("fastdraw.drawclosed", false); 76 79 simplifyMode = Main.pref.getInteger("fastdraw.simplifymode", 0) % 3; 77 80 allowEditExistingWays = Main.pref.getBoolean("fastdraw.alloweditexisting", false); … … 81 84 82 85 public void savePrefs() { 83 Main.pref.putDouble("fastdraw.maxdist", maxDist); 84 Main.pref.putDouble("fastdraw.epsilonmult", epsilonMult); 85 //Main.pref.putDouble("fastdraw.deltasearch", deltaLatLon); 86 Main.pref.putDouble("fastdraw.mindelta",minPixelsBetweenPoints); 87 Main.pref.putDouble("fastdraw.startingEps",startingEps); 88 Main.pref.putDouble("fastdraw.maxpkm",maxPointsPerKm); 89 Main.pref.putInteger("fastdraw.pkmblocksize",pkmBlockSize); 90 Main.pref.put("fastdraw.drawlastsegment",drawLastSegment); 91 Main.pref.put("fastdraw.snapnodes", snapNodes); 92 Main.pref.put("fastdraw.fixedclick", fixedClick); 93 Main.pref.put("fastdraw.fixedspacebar", fixedSpacebar); 94 Main.pref.put("fastdraw.drawclosed", drawClosed); 95 Main.pref.putInteger("fastdraw.simplifymode", simplifyMode); 96 Main.pref.put("fastdraw.autotags", autoTags); 97 Main.pref.put("fastdraw.alloweditexisting", allowEditExistingWays); 98 try {Main.pref.save();} catch (IOException e) { 99 System.err.println(tr("Can not save preferences")); 100 } 86 Main.pref.putDouble("fastdraw.maxdist", maxDist); 87 Main.pref.putDouble("fastdraw.epsilonmult", epsilonMult); 88 //Main.pref.putDouble("fastdraw.deltasearch", deltaLatLon); 89 Main.pref.putDouble("fastdraw.mindelta", minPixelsBetweenPoints); 90 Main.pref.putDouble("fastdraw.startingEps", startingEps); 91 Main.pref.putDouble("fastdraw.maxpkm", maxPointsPerKm); 92 Main.pref.putInteger("fastdraw.pkmblocksize", pkmBlockSize); 93 Main.pref.put("fastdraw.drawlastsegment", drawLastSegment); 94 Main.pref.put("fastdraw.snapnodes", snapNodes); 95 Main.pref.put("fastdraw.fixedclick", fixedClick); 96 Main.pref.put("fastdraw.fixedspacebar", fixedSpacebar); 97 Main.pref.put("fastdraw.drawclosed", drawClosed); 98 Main.pref.putInteger("fastdraw.simplifymode", simplifyMode); 99 Main.pref.put("fastdraw.autotags", autoTags); 100 Main.pref.put("fastdraw.alloweditexisting", allowEditExistingWays); 101 try { 102 Main.pref.save(); 103 } catch (IOException e) { 104 System.err.println(tr("Can not save preferences")); 105 } 101 106 } 102 107 } -
applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw/FastDrawConfigDialog.java
r31224 r32548 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.plugins.fastdraw; 2 3 … … 28 29 public class FastDrawConfigDialog extends ExtendedDialog { 29 30 30 private final JLabel label1 =new JLabel(tr("Epsilon multiplier"));31 private final JLabel label2 =new JLabel(tr("Starting Epsilon"));32 private final JLabel label3 =new JLabel(tr("Max points count per 1 km"));33 private final JLabel label4 =new JLabel(/* I18n: Combobox to select what a press to return key does */ tr("Enter key mode"));34 private final JLabel label5 =new JLabel(tr("Auto add tags"));35 private final JFormattedTextField text1 =new JFormattedTextField(NumberFormat.getInstance());36 private final JFormattedTextField text2 =newJFormattedTextField(NumberFormat.getInstance());37 private final JFormattedTextField text3 =newJFormattedTextField(NumberFormat.getInstance());38 private final JComboBox<String> combo1 =new JComboBox<>(new String[]{tr("Autosimplify"),39 tr("Simplify with initial epsilon"),tr("Save as is")});40 private final JCheckBox snapCb =new JCheckBox(tr("Snap to nodes"));31 private final JLabel label1 = new JLabel(tr("Epsilon multiplier")); 32 private final JLabel label2 = new JLabel(tr("Starting Epsilon")); 33 private final JLabel label3 = new JLabel(tr("Max points count per 1 km")); 34 private final JLabel label4 = new JLabel(/* I18n: Combobox to select what a press to return key does */ tr("Enter key mode")); 35 private final JLabel label5 = new JLabel(tr("Auto add tags")); 36 private final JFormattedTextField text1 = new JFormattedTextField(NumberFormat.getInstance()); 37 private final JFormattedTextField text2 = new JFormattedTextField(NumberFormat.getInstance()); 38 private final JFormattedTextField text3 = new JFormattedTextField(NumberFormat.getInstance()); 39 private final JComboBox<String> combo1 = new JComboBox<>(new String[]{tr("Autosimplify"), 40 tr("Simplify with initial epsilon"), tr("Save as is")}); 41 private final JCheckBox snapCb = new JCheckBox(tr("Snap to nodes")); 41 42 private final JCheckBox fixedClickCb = new JCheckBox(tr("Add fixed points on click")); 42 43 private final JCheckBox fixedSpaceCb = new JCheckBox(tr("Add fixed points on spacebar")); … … 47 48 48 49 public FastDrawConfigDialog(FDSettings settings) { 49 super(Main.parent, tr("FastDraw configuration"),new String[] {tr("Ok"), tr("Cancel")});50 super(Main.parent, tr("FastDraw configuration"), new String[] {tr("Ok"), tr("Cancel")}); 50 51 this.settings = settings; 51 52 52 53 JPanel all = new JPanel(); 53 54 GridBagLayout layout = new GridBagLayout(); … … 57 58 public void actionPerformed(ActionEvent e) { 58 59 String s = Utils.getClipboardContent(); 59 if (TextTagParser.getValidatedTagsFromText(s) !=null) {60 if (TextTagParser.getValidatedTagsFromText(s) != null) { 60 61 addTags.setText(s); 61 62 } … … 63 64 }); 64 65 pasteButton.setToolTipText(tr("Try copying tags from properties table")); 65 66 66 67 ArrayList<String> history = new ArrayList<>(Main.pref.getCollection("fastdraw.tags-history")); 67 68 while (history.remove("")) { }; 68 69 addTags.setPossibleItems(history); 69 70 all.add(label1, GBC.std().insets(10,0,0,0));71 all.add(text1, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0,0,5));72 all.add(label2, GBC.std().insets(10,0,0,0));73 all.add(text2, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0,0,5));74 all.add(label3, GBC.std().insets(10,0,0,0));75 all.add(text3, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0,0,5));76 all.add(label4, GBC.std().insets(10,0,0,0));77 all.add(combo1, GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0,0,5));78 79 all.add(label5, GBC.std().insets(10,0,0,0));80 all.add(pasteButton, GBC.eop().insets(0, 0,0,5));81 82 all.add(addTags, GBC.eop().fill(GBC.HORIZONTAL).insets(10, 0,5,10));83 84 all.add(snapCb, GBC.eop().insets(20,0,0,0));85 86 all.add(fixedClickCb, GBC.eop().insets(20,0,0,0));87 all.add(fixedSpaceCb, GBC.eop().insets(20,0,0,0));88 all.add(drawClosedCb, GBC.eop().insets(20,0,0,0));89 90 all.add(allowEditExistingWaysCb, GBC.eop().insets(20,0,0,0));91 70 71 all.add(label1, GBC.std().insets(10, 0, 0, 0)); 72 all.add(text1, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5)); 73 all.add(label2, GBC.std().insets(10, 0, 0, 0)); 74 all.add(text2, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5)); 75 all.add(label3, GBC.std().insets(10, 0, 0, 0)); 76 all.add(text3, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5)); 77 all.add(label4, GBC.std().insets(10, 0, 0, 0)); 78 all.add(combo1, GBC.eop().fill(GBC.HORIZONTAL).insets(5, 0, 0, 5)); 79 80 all.add(label5, GBC.std().insets(10, 0, 0, 0)); 81 all.add(pasteButton, GBC.eop().insets(0, 0, 0, 5)); 82 83 all.add(addTags, GBC.eop().fill(GBC.HORIZONTAL).insets(10, 0, 5, 10)); 84 85 all.add(snapCb, GBC.eop().insets(20, 0, 0, 0)); 86 87 all.add(fixedClickCb, GBC.eop().insets(20, 0, 0, 0)); 88 all.add(fixedSpaceCb, GBC.eop().insets(20, 0, 0, 0)); 89 all.add(drawClosedCb, GBC.eop().insets(20, 0, 0, 0)); 90 91 all.add(allowEditExistingWaysCb, GBC.eop().insets(20, 0, 0, 0)); 92 92 93 addTags.setText(settings.autoTags); 93 94 text1.setValue(settings.epsilonMult); … … 100 101 allowEditExistingWaysCb.setSelected(settings.allowEditExistingWays); 101 102 combo1.setSelectedIndex(settings.simplifyMode); 102 103 103 104 setContent(all, false); 104 105 setButtonIcons(new String[] {"ok.png", "cancel.png"}); … … 116 117 if (getValue() == 1) { 117 118 try { 118 settings.epsilonMult =NumberFormat.getInstance().parse(text1.getText()).doubleValue();119 settings.startingEps =NumberFormat.getInstance().parse(text2.getText()).doubleValue();120 settings.maxPointsPerKm =NumberFormat.getInstance().parse(text3.getText()).doubleValue();121 settings.snapNodes =snapCb.isSelected();122 settings.fixedClick =fixedClickCb.isSelected();123 settings.fixedSpacebar =fixedSpaceCb.isSelected();124 settings.allowEditExistingWays =allowEditExistingWaysCb.isSelected();125 settings.drawClosed =drawClosedCb.isSelected();126 settings.simplifyMode =combo1.getSelectedIndex();127 settings.autoTags =addTags.getText();119 settings.epsilonMult = NumberFormat.getInstance().parse(text1.getText()).doubleValue(); 120 settings.startingEps = NumberFormat.getInstance().parse(text2.getText()).doubleValue(); 121 settings.maxPointsPerKm = NumberFormat.getInstance().parse(text3.getText()).doubleValue(); 122 settings.snapNodes = snapCb.isSelected(); 123 settings.fixedClick = fixedClickCb.isSelected(); 124 settings.fixedSpacebar = fixedSpaceCb.isSelected(); 125 settings.allowEditExistingWays = allowEditExistingWaysCb.isSelected(); 126 settings.drawClosed = drawClosedCb.isSelected(); 127 settings.simplifyMode = combo1.getSelectedIndex(); 128 settings.autoTags = addTags.getText(); 128 129 if (!settings.autoTags.isEmpty()) { 129 130 addTags.addCurrentItemToHistory(); … … 132 133 settings.savePrefs(); 133 134 } catch (ParseException e) { 134 JOptionPane.showMessageDialog(Main.parent,135 tr("Can not read settings"));135 JOptionPane.showMessageDialog(Main.parent, 136 tr("Can not read settings")); 136 137 } 137 138 } 138 139 return result; 139 140 140 } 141 142 141 } -
applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw/FastDrawingMode.java
r32444 r32548 1 /* 2 * Licence: GPL v2 or later 3 * Author: Alexei Kasatkin, 2011 4 * Thanks to authors of BuildingTools, ImproveWayAccuracy and LakeWalker 5 * for good sample code 6 */ 1 // License: GPL. For details, see LICENSE file. 7 2 package org.openstreetmap.josm.plugins.fastdraw; 8 3 … … 52 47 import org.openstreetmap.josm.tools.TextTagParser; 53 48 54 class FastDrawingMode extends MapMode implements MapViewPaintable, 55 KeyPressReleaseListener, ModifierListener {56 private static final String SIMPLIFYMODE_MESSAGE =49 class FastDrawingMode extends MapMode implements MapViewPaintable, KeyPressReleaseListener, ModifierListener { 50 // CHECKSTYLE.OFF: LineLength 51 private static final String SIMPLIFYMODE_MESSAGE = 57 52 tr("Q=Options, Enter=save, Ctrl-Enter=save with tags, Up/Down=tune"); 58 private static final String DRAWINGMODE_MESSAGE= 59 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"); 53 private static final String DRAWINGMODE_MESSAGE = 54 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"); 55 // CHECKSTYLE.ON: LineLength 60 56 61 57 private FDSettings settings; … … 75 71 private LatLon highlightedFragmentStart; 76 72 private int nearestPointIndex; 77 private int dragNode =-1;73 private int dragNode = -1; 78 74 private List<Node> oldNodes; 79 75 80 76 private boolean lineWasSaved; 81 77 private boolean deltaChanged; 82 78 private Way oldWay; 83 79 84 80 FastDrawingMode(MapFrame mapFrame) { 85 super(tr("FastDrawing"), "turbopen.png", tr("Fast drawing mode"), 86 Shortcut.registerShortcut("mapmode:fastdraw", tr("Mode: {0}", tr("Fast drawing mode")), KeyEvent.VK_F, Shortcut.SHIFT) 87 ,mapFrame, Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));88 line =new DrawnPolyLine();81 super(tr("FastDrawing"), "turbopen.png", tr("Fast drawing mode"), 82 Shortcut.registerShortcut("mapmode:fastdraw", tr("Mode: {0}", tr("Fast drawing mode")), KeyEvent.VK_F, Shortcut.SHIFT), 83 mapFrame, Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 84 line = new DrawnPolyLine(); 89 85 cursorDraw = ImageProvider.getCursor("crosshair", null); 90 86 cursorCtrl = ImageProvider.getCursor("crosshair", "fixed"); … … 96 92 } 97 93 98 // <editor-fold defaultstate="collapsed" desc="Event listeners">94 // <editor-fold defaultstate="collapsed" desc="Event listeners"> 99 95 100 96 @Override … … 102 98 if (!isEnabled()) return; 103 99 super.enterMode(); 104 lineWasSaved =false;105 settings =new FDSettings();100 lineWasSaved = false; 101 settings = new FDSettings(); 106 102 settings.loadPrefs(); 107 103 settings.savePrefs(); 108 104 109 eps =settings.startingEps;105 eps = settings.startingEps; 110 106 mv = Main.map.mapView; 111 107 line.setMv(mv); 112 108 113 109 if (getLayerManager().getEditDataSet() == null) return; 114 110 … … 116 112 Main.map.mapView.addMouseMotionListener(this); 117 113 Main.map.mapView.addTemporaryLayer(this); 118 114 119 115 Main.map.keyDetector.addKeyListener(this); 120 116 Main.map.keyDetector.addModifierListener(this); … … 130 126 131 127 Main.map.mapView.removeTemporaryLayer(this); 132 128 133 129 Main.map.keyDetector.removeKeyListener(this); 134 130 Main.map.keyDetector.removeModifierListener(this); … … 149 145 } 150 146 151 private final ArrayList<Point> fixedPoints = new ArrayList<>(3000); // tamporyrary storate for paint152 147 private final ArrayList<Point> fixedPoints = new ArrayList<>(3000); // temporary storate for paint 148 153 149 ////////// Event listener methods 154 150 @Override 155 151 public void paint(Graphics2D g, MapView mv, Bounds bbox) { 156 LinkedList<LatLon> pts =line.getPoints();152 LinkedList<LatLon> pts = line.getPoints(); 157 153 if (pts.isEmpty()) return; 158 154 … … 163 159 g.setStroke(settings.normalStroke); 164 160 } 165 161 166 162 int bigDotSize = settings.bigDotSize; 167 163 … … 171 167 g.setColor(settings.COLOR_FIXED); 172 168 g.fillOval(p1.x - bigDotSize/2, p1.y - bigDotSize/2, bigDotSize, bigDotSize); 173 Color lineColor, initLineColor;174 initLineColor = line.wasSimplified() ? settings.COLOR_SIMPLIFIED : settings.COLOR_NORMAL;169 Color lineColor, initLineColor; 170 initLineColor = line.wasSimplified() ? settings.COLOR_SIMPLIFIED : settings.COLOR_NORMAL; 175 171 lineColor = initLineColor; 176 int rp, dp;177 dp =line.wasSimplified() ? settings.bigDotSize : settings.dotSize; rp=dp/2;172 int rp, dp; 173 dp = line.wasSimplified() ? settings.bigDotSize : settings.dotSize; rp = dp/2; 178 174 if (pts.size() > 1) { 179 Iterator<LatLon> it1, it2;180 it1 =pts.listIterator(0);181 it2 =pts.listIterator(1);175 Iterator<LatLon> it1, it2; 176 it1 = pts.listIterator(0); 177 it2 = pts.listIterator(1); 182 178 fixedPoints.clear(); 183 179 for (int i = 0; i < pts.size() - 1; i++) { … … 186 182 pp2 = it2.next(); 187 183 p2 = line.getPoint(pp2); 188 if (shift && highlightedFragmentStart==pp1 && nearestPointIndex<0) {lineColor=settings.COLOR_SELECTEDFRAGMENT;} 189 if (!shift && line.isLastPoint(i)) { lineColor=settings.COLOR_EDITEDFRAGMENT; } 184 if (shift && highlightedFragmentStart == pp1 && nearestPointIndex < 0) { 185 lineColor = settings.COLOR_SELECTEDFRAGMENT; 186 } 187 if (!shift && line.isLastPoint(i)) { 188 lineColor = settings.COLOR_EDITEDFRAGMENT; 189 } 190 190 g.setColor(lineColor); 191 191 g.drawLine(p1.x, p1.y, p2.x, p2.y); 192 if (line.isFixed(pp2)) { 193 lineColor =initLineColor;192 if (line.isFixed(pp2)) { 193 lineColor = initLineColor; 194 194 fixedPoints.add(p2); 195 195 } else { … … 197 197 } 198 198 if (!drawing) { 199 if (!line.wasSimplified() && nearestPointIndex ==i+1) {200 if (shift) {201 // highlight node to delete202 g.setStroke(settings.deleteStroke);203 g.setColor(settings.COLOR_DELETE);204 g.drawLine(p2.x - 5, p2.y - 5,p2.x + 5, p2.y + 5);205 g.drawLine(p2.x - 5, p2.y + 5,p2.x + 5, p2.y - 5);206 g.setStroke(settings.normalStroke);207 } else if (ctrl) {208 // highlight node to toggle fixation209 g.setStroke(settings.deleteStroke);210 g.setColor( line.isFixed(pp2) ? settings.COLOR_NORMAL: settings.COLOR_FIXED);211 g.fillOval(p2.x - bigDotSize/2-2, p2.y - bigDotSize/2-2, bigDotSize+4, bigDotSize+4);212 g.setStroke(settings.normalStroke);213 }199 if (!line.wasSimplified() && nearestPointIndex == i+1) { 200 if (shift) { 201 // highlight node to delete 202 g.setStroke(settings.deleteStroke); 203 g.setColor(settings.COLOR_DELETE); 204 g.drawLine(p2.x - 5, p2.y - 5, p2.x + 5, p2.y + 5); 205 g.drawLine(p2.x - 5, p2.y + 5, p2.x + 5, p2.y - 5); 206 g.setStroke(settings.normalStroke); 207 } else if (ctrl) { 208 // highlight node to toggle fixation 209 g.setStroke(settings.deleteStroke); 210 g.setColor(line.isFixed(pp2) ? settings.COLOR_NORMAL : settings.COLOR_FIXED); 211 g.fillOval(p2.x - bigDotSize/2-2, p2.y - bigDotSize/2-2, bigDotSize+4, bigDotSize+4); 212 g.setStroke(settings.normalStroke); 213 } 214 214 } 215 215 } … … 220 220 } 221 221 } 222 if (settings.drawLastSegment && !drawing && dragNode <0 && !shift &&223 nearestPointIndex <=0 && !line.wasSimplified()) {222 if (settings.drawLastSegment && !drawing && dragNode < 0 && !shift && 223 nearestPointIndex <= 0 && !line.wasSimplified()) { 224 224 // draw line to current point 225 225 g.setColor(lineColor); 226 Point lp =line.getLastPoint();227 Point mp =mv.getMousePosition();228 if (lp !=null && mp!=null) g.drawLine(lp.x,lp.y,mp.x,mp.y);226 Point lp = line.getLastPoint(); 227 Point mp = mv.getMousePosition(); 228 if (lp != null && mp != null) g.drawLine(lp.x, lp.y, mp.x, mp.y); 229 229 } 230 230 if (deltaChanged) { 231 231 g.setColor(lineColor); 232 Point lp=line.getLastPoint(); 233 int r=(int) settings.minPixelsBetweenPoints; 234 if (lp!=null) g.drawOval(lp.x-r,lp.y-r,2*r,2*r); 235 } 236 } 237 232 Point lp = line.getLastPoint(); 233 int r = (int) settings.minPixelsBetweenPoints; 234 if (lp != null) g.drawOval(lp.x-r, lp.y-r, 2*r, 2*r); 235 } 236 } 238 237 239 238 @Override … … 242 241 if (e.getButton() != MouseEvent.BUTTON1) return; 243 242 updateKeyModifiers(e); 244 243 245 244 requestFocusInMapView(); 246 245 247 int idx =line.findClosestPoint(e.getPoint(),settings.maxDist);248 if (idx ==0 && !line.isClosed()) {246 int idx = line.findClosestPoint(e.getPoint(), settings.maxDist); 247 if (idx == 0 && !line.isClosed()) { 249 248 line.closeLine(); 250 249 // the way should become closed 251 drawing =false;252 dragNode =0;250 drawing = false; 251 dragNode = 0; 253 252 updateCursor(); 254 253 return; … … 256 255 autoCloseIfNeeded(); 257 256 258 if (ctrl && shift) {newDrawing();repaint();return;} 257 if (ctrl && shift) { 258 newDrawing(); 259 repaint(); 260 return; 261 } 259 262 if (!ctrl && shift) { 260 if (idx>=0) {line.deleteNode(idx); nearestPointIndex=-1;} 261 else line.tryToDeleteSegment(e.getPoint()); 263 if (idx >= 0) { 264 line.deleteNode(idx); 265 nearestPointIndex = -1; 266 } else 267 line.tryToDeleteSegment(e.getPoint()); 262 268 return; 263 269 } 264 if (idx >=0) {270 if (idx >= 0) { 265 271 if (ctrl) { 266 272 // toggle fixed point … … 268 274 } 269 275 // node dragging 270 dragNode =idx;276 dragNode = idx; 271 277 return; 272 278 } 273 startDrawing(e.getPoint(), settings.fixedClick);279 startDrawing(e.getPoint(), settings.fixedClick); 274 280 } 275 281 … … 277 283 //if (line.isClosed()) { setStatusLine(tr(SIMPLIFYMODE_MESSAGE));return; } 278 284 drawing = true; 279 if (line.wasSimplified()) { 285 if (line.wasSimplified()) { 280 286 // new line started after simplification 281 287 // we need to save old line … … 288 294 if (settings.snapNodes) { // find existing node near point and use it 289 295 Node nd1 = getNearestNode(point, settings.maxDist); 290 if (nd1 !=null) {296 if (nd1 != null) { 291 297 // found node, make it fixed point of the line 292 298 //System.out.println("node "+nd1); 293 p =nd1.getCoor();299 p = nd1.getCoor(); 294 300 line.fixPoint(p); 295 301 } … … 298 304 line.addLast(p); 299 305 if (ctrl || fixFlag) line.fixPoint(p); 300 306 301 307 setStatusLine(tr("Please move the mouse to draw new way")); 302 308 repaint(); … … 308 314 stopDrawing(); 309 315 } 310 316 311 317 private void stopDrawing() { 312 318 if (!isEnabled()) return; 313 319 dragNode = -1; 314 320 drawing = false; 315 highlightedFragmentStart =null;321 highlightedFragmentStart = null; 316 322 if (!line.isClosed()) setStatusLine(DRAWINGMODE_MESSAGE); 317 323 updateCursor(); … … 328 334 if (!isEnabled()) return; 329 335 updateKeyModifiers(e); 330 deltaChanged =false;336 deltaChanged = false; 331 337 Node nd1 = getNearestNode(e.getPoint(), settings.maxDist); 332 boolean nearSomeNode2 =nd1!=null;333 boolean needRepaint =false;334 if (nearSomeNode !=nearSomeNode2) {335 nearSomeNode =nearSomeNode2;338 boolean nearSomeNode2 = nd1 != null; 339 boolean needRepaint = false; 340 if (nearSomeNode != nearSomeNode2) { 341 nearSomeNode = nearSomeNode2; 336 342 updateCursor(); 337 needRepaint =true;338 } 339 340 int nearestIdx2 =line.findClosestPoint(e.getPoint(),settings.maxDist);343 needRepaint = true; 344 } 345 346 int nearestIdx2 = line.findClosestPoint(e.getPoint(), settings.maxDist); 341 347 if (nearestPointIndex != nearestIdx2) { 342 nearestPointIndex =nearestIdx2;348 nearestPointIndex = nearestIdx2; 343 349 updateCursor(); 344 needRepaint =true;350 needRepaint = true; 345 351 } 346 352 if (settings.drawLastSegment) { 347 needRepaint =true;348 } 349 353 needRepaint = true; 354 } 355 350 356 if (!drawing) { 351 if (dragNode >=0) {352 line.moveNode(dragNode, getLatLon(e));357 if (dragNode >= 0) { 358 line.moveNode(dragNode, getLatLon(e)); 353 359 repaint(); 354 360 return; … … 357 363 if (shift && nearestPointIndex == -1) { 358 364 // find line fragment to highlight 359 LatLon h2 =line.findBigSegment(e.getPoint());360 if (highlightedFragmentStart !=h2) {361 highlightedFragmentStart =h2;362 needRepaint =true;363 } 364 } 365 365 LatLon h2 = line.findBigSegment(e.getPoint()); 366 if (highlightedFragmentStart != h2) { 367 highlightedFragmentStart = h2; 368 needRepaint = true; 369 } 370 } 371 366 372 if (needRepaint) { 367 373 repaint(); … … 372 378 373 379 // do not draw points close to existing points - we do not want self-intersections 374 if (nearestPointIndex>=0) { return; } 380 if (nearestPointIndex >= 0) { 381 return; 382 } 375 383 376 384 Point lastP = line.getLastPoint(); // last point of line fragment being edited 377 385 378 386 // free mouse-drawing 379 if (nearSomeNode) {387 if (nearSomeNode) { 380 388 if (settings.snapNodes && Math.hypot(e.getX() - lastP.x, e.getY() - lastP.y) > 1e-2) { 381 389 line.addFixed(nd1.getCoor()); // snap to node coords … … 405 413 line.clearSimplifiedVersion(); 406 414 repaint(); 407 eps =settings.startingEps;415 eps = settings.startingEps; 408 416 } 409 417 back(); 410 break;418 break; 411 419 case KeyEvent.VK_ENTER: 412 420 e.consume(); … … 415 423 //line.simplify(eps); 416 424 switch(settings.simplifyMode) { 417 418 eps = line.autoSimplify(settings.startingEps, settings.epsilonMult,419 settings.pkmBlockSize, settings.maxPointsPerKm);420 421 422 423 424 } 425 if (settings.simplifyMode ==2) {425 case 0: //case 1: 426 eps = line.autoSimplify(settings.startingEps, settings.epsilonMult, 427 settings.pkmBlockSize, settings.maxPointsPerKm); 428 break; 429 case 1: //case 2: case 3: 430 line.simplify(eps); 431 break; 432 } 433 if (settings.simplifyMode == 2) { 426 434 // autosave 427 435 saveAsWay(true); … … 430 438 showSimplifyHint(); 431 439 } 432 } else {saveAsWay(true);} 433 break; 440 } else { 441 saveAsWay(true); 442 } 443 break; 434 444 case KeyEvent.VK_DOWN: 435 445 if (ctrl || shift || alt) return; … … 438 448 if (line.wasSimplified()) changeEpsilon(settings.epsilonMult); 439 449 else changeDelta(1/1.1); 440 break;450 break; 441 451 case KeyEvent.VK_UP: 442 452 if (ctrl || shift || alt) return; … … 445 455 if (line.wasSimplified()) changeEpsilon(1/settings.epsilonMult); 446 456 else changeDelta(1.1); 447 break;457 break; 448 458 case KeyEvent.VK_ESCAPE: 449 459 e.consume(); 450 460 Point lastPoint = line.getLastPoint(); 451 461 if (!line.isClosed()) line.moveToTheEnd(); 452 if (lastPoint ==null || lastPoint.equals(line.getLastPoint())) {453 if (line.getPoints().size()>5) {462 if (lastPoint == null || lastPoint.equals(line.getLastPoint())) { 463 if (line.getPoints().size() > 5) { 454 464 boolean answer = ConditionalOptionPaneUtil.showConfirmationDialog( 455 "delete_drawn_line", Main.parent,456 tr("Are you sure you do not want to save the line containing {0} points?",457 line.getPoints().size()), tr("Delete confirmation"),458 JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_OPTION);459 if (!answer) break;465 "delete_drawn_line", Main.parent, 466 tr("Are you sure you do not want to save the line containing {0} points?", 467 line.getPoints().size()), tr("Delete confirmation"), 468 JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_OPTION); 469 if (!answer) break; 460 470 } 461 471 newDrawing(); // stop drawing … … 463 473 Main.map.selectSelectTool(false); 464 474 } 465 break;466 475 break; 476 467 477 case KeyEvent.VK_I: 468 JOptionPane.showMessageDialog(Main.parent,469 tr("{0} m - length of the line\n{1} nodes\n{2} points per km (maximum)\n{3} points per km (average)",470 line.getLength(),line.getPoints().size(),line.getNodesPerKm(settings.pkmBlockSize),471 line.getNodesPerKm(1000000)),472 tr("Line information"),JOptionPane.INFORMATION_MESSAGE);473 break;478 JOptionPane.showMessageDialog(Main.parent, 479 tr("{0} m - length of the line\n{1} nodes\n{2} points per km (maximum)\n{3} points per km (average)", 480 line.getLength(), line.getPoints().size(), line.getNodesPerKm(settings.pkmBlockSize), 481 line.getNodesPerKm(1000000)), 482 tr("Line information"), JOptionPane.INFORMATION_MESSAGE); 483 break; 474 484 case KeyEvent.VK_Q: 475 485 // less details … … 477 487 new FastDrawConfigDialog(settings).showDialog(); 478 488 if (line.wasSimplified()) { 479 eps = line.autoSimplify(settings.startingEps, settings.epsilonMult, settings.pkmBlockSize, settings.maxPointsPerKm);489 eps = line.autoSimplify(settings.startingEps, settings.epsilonMult, settings.pkmBlockSize, settings.maxPointsPerKm); 480 490 showSimplifyHint(); 481 491 } 482 492 repaint(); 483 break;493 break; 484 494 case KeyEvent.VK_SPACE: 485 495 e.consume(); 486 496 if (!drawing) { 487 497 Point p = Main.map.mapView.getMousePosition(); 488 if (p !=null) startDrawing(p,settings.fixedSpacebar);489 } 490 break;491 } 492 } 493 498 if (p != null) startDrawing(p, settings.fixedSpacebar); 499 } 500 break; 501 } 502 } 503 494 504 @Override 495 505 public void doKeyReleased(KeyEvent keyEvent) { 496 506 //System.out.println("released "+keyEvent); 497 if (keyEvent.getKeyCode() ==KeyEvent.VK_SPACE) stopDrawing();507 if (keyEvent.getKeyCode() == KeyEvent.VK_SPACE) stopDrawing(); 498 508 updateCursor(); 499 509 } … … 504 514 updateCursor(); 505 515 } 506 516 507 517 @Override 508 518 protected void updateStatusLine() { … … 510 520 Main.map.statusLine.repaint(); 511 521 } 512 // </editor-fold>513 514 // <editor-fold defaultstate="collapsed" desc="Different action helper methods">522 // </editor-fold> 523 524 // <editor-fold defaultstate="collapsed" desc="Different action helper methods"> 515 525 public void newDrawing() { 516 oldWay =null; oldNodes=null;517 eps =settings.startingEps;526 oldWay = null; oldNodes = null; 527 eps = settings.startingEps; 518 528 line.clear(); 519 529 } 520 530 521 531 private void saveAsWay(boolean autoExit) { 522 List<LatLon> pts =line.getPoints();532 List<LatLon> pts = line.getPoints(); 523 533 int n = pts.size(); 524 534 if (n < 2) return; //do not save oversimplified lines 525 if (line.isClosed() && n ==2) return;526 if (line.isClosed() && n ==3) pts.remove(2); // two-point way can not be closed535 if (line.isClosed() && n == 2) return; 536 if (line.isClosed() && n == 3) pts.remove(2); // two-point way can not be closed 527 537 528 538 Collection<Command> cmds = new LinkedList<>(); 529 539 int i = 0; 530 540 531 541 Way w; 532 if (oldWay ==null) {542 if (oldWay == null) { 533 543 w = new Way(); 534 544 } else { … … 536 546 w.setNodes(new ArrayList<Node>()); // nodes will be created frosm scratch 537 547 } 538 539 LatLon first =pts.get(0);540 Node firstNode =null;548 549 LatLon first = pts.get(0); 550 Node firstNode = null; 541 551 542 552 for (LatLon p : pts) { 543 553 Node nd = Main.map.mapView.getNearestNode(line.getPoint(p), OsmPrimitive.isSelectablePredicate); 544 554 // there may be a node with the same coords! 545 546 if (nd !=null && p.greatCircleDistance(nd.getCoor())>0.01) nd=null;547 if (nd ==null) {548 if (i >0 && p.equals(first)) {549 nd =firstNode;555 556 if (nd != null && p.greatCircleDistance(nd.getCoor()) > 0.01) nd = null; 557 if (nd == null) { 558 if (i > 0 && p.equals(first)) { 559 nd = firstNode; 550 560 } else { 551 561 nd = new Node(p); … … 558 568 return; 559 569 } 560 if (i ==0) {561 firstNode =nd;570 if (i == 0) { 571 firstNode = nd; 562 572 } 563 573 w.addNode(nd); … … 566 576 if (ctrl) { 567 577 // paste tags - from ctrl-shift-v 568 Set 578 Set<OsmPrimitive> ts = new HashSet<>(); 569 579 ts.add(w); 570 580 TagPaster tp = new TagPaster(Main.pasteBuffer.getDirectlyAdded(), ts); … … 580 590 } 581 591 } 582 if (oldWay !=null) {592 if (oldWay != null) { 583 593 List<Node> nodes = w.getNodes(); 584 594 cmds.add(new ChangeCommand(oldWay, w)); 585 595 for (Node nd: oldNodes) { 586 // node from old way but not in new way 596 // node from old way but not in new way 587 597 if (!nodes.contains(nd)) { 588 598 List<OsmPrimitive> refs = nd.getReferrers(); 589 599 // does someone need this node? if no-delete it. 590 if (refs.size() ==1 && !nd.isDeleted() && nd.isUsable() && !nd.isTagged()) cmds.add(new DeleteCommand(nd));600 if (refs.size() == 1 && !nd.isDeleted() && nd.isUsable() && !nd.isTagged()) cmds.add(new DeleteCommand(nd)); 591 601 } 592 602 } … … 613 623 void changeEpsilon(double k) { 614 624 //System.out.println(tr("Eps={0}", eps)); 615 eps *=k;625 eps *= k; 616 626 line.simplify(eps); 617 /* I18N: Eps = Epsilon, the tolerance parameter */ 627 /* I18N: Eps = Epsilon, the tolerance parameter */ 618 628 showSimplifyHint(); 619 629 repaint(); … … 621 631 622 632 void changeDelta(double k) { 623 settings.minPixelsBetweenPoints *=k;624 deltaChanged =true;625 626 setStatusLine(tr("min distance={0} px ({1} m)", (int)settings.minPixelsBetweenPoints,633 settings.minPixelsBetweenPoints *= k; 634 deltaChanged = true; 635 636 setStatusLine(tr("min distance={0} px ({1} m)", (int) settings.minPixelsBetweenPoints, 627 637 mv.getDist100Pixel()/100*settings.minPixelsBetweenPoints)); 628 638 repaint(); 629 639 } 630 631 640 632 641 /*private Node findClosestNode(LatLon p, double d) { … … 648 657 649 658 private void loadFromWay(Way w) { 650 659 651 660 Object[] nodes = w.getNodes().toArray(); 652 int n =nodes.length;661 int n = nodes.length; 653 662 if (w.isClosed()) n--; 654 for (int i =0;i<n;i++) {655 Node nd =(Node) nodes[i];663 for (int i = 0; i < n; i++) { 664 Node nd = (Node) nodes[i]; 656 665 List<OsmPrimitive> refs = nd.getReferrers(); 657 if (refs.size() >1 || nd.isTagged()) {666 if (refs.size() > 1 || nd.isTagged()) { 658 667 line.addFixed(nd.getCoor()); 659 668 } else { … … 667 676 668 677 private void setStatusLine(String tr) { 669 statusText =tr;678 statusText = tr; 670 679 updateStatusLine(); 671 680 } 672 681 673 682 private void showSimplifyHint() { 674 setStatusLine(tr("Eps={0}, {1} points, {2} p/km",675 eps, line.getSimplePointsCount(), line.getNodesPerKm(settings.pkmBlockSize))+" "676 +SIMPLIFYMODE_MESSAGE);677 } 678 683 setStatusLine(tr("Eps={0}, {1} points, {2} p/km", 684 eps, line.getSimplePointsCount(), line.getNodesPerKm(settings.pkmBlockSize))+" " 685 +SIMPLIFYMODE_MESSAGE); 686 } 687 679 688 private void updateCursor() { 680 689 if (shift) Main.map.mapView.setCursor(cursorShift); else 681 if (line.isClosed() || (nearestPointIndex==0)) Main.map.mapView.setCursor(cursorReady); else682 if (ctrl) Main.map.mapView.setCursor(cursorCtrl); else683 if (nearSomeNode && settings.snapNodes) Main.map.mapView.setCursor(cursorCtrl); else684 if (drawing) Main.map.mapView.setCursor(cursorDrawing); else685 Main.map.mapView.setCursor(cursorDraw);690 if (line.isClosed() || (nearestPointIndex == 0)) Main.map.mapView.setCursor(cursorReady); else 691 if (ctrl) Main.map.mapView.setCursor(cursorCtrl); else 692 if (nearSomeNode && settings.snapNodes) Main.map.mapView.setCursor(cursorCtrl); else 693 if (drawing) Main.map.mapView.setCursor(cursorDrawing); else 694 Main.map.mapView.setCursor(cursorDraw); 686 695 } 687 696 … … 689 698 Main.map.mapView.repaint(); 690 699 } 691 700 692 701 private void tryToLoadWay() { 693 702 updateCursor(); 694 703 Collection<Way> selectedWays = Main.getLayerManager().getEditDataSet().getSelectedWays(); 695 if (selectedWays !=null // if there is a selection696 && selectedWays.size()==1 // and one way is selected697 && line.getPoints().size()==0) /* and ther is no already drawn line */ {704 if (selectedWays != null // if there is a selection 705 && selectedWays.size() == 1 // and one way is selected 706 && line.getPoints().size() == 0) /* and ther is no already drawn line */ { 698 707 // we can start drawing new way starting from old one 699 708 Way w = selectedWays.iterator().next(); 700 709 701 710 if (w.isNew() || settings.allowEditExistingWays) loadFromWay(w); 702 711 } … … 704 713 705 714 private void autoCloseIfNeeded() { 706 if (settings.drawClosed && line.getPointCount() >1 && !line.isClosed()) {715 if (settings.drawClosed && line.getPointCount() > 1 && !line.isClosed()) { 707 716 line.closeLine(); 708 717 } 709 718 } 710 // </editor-fold>711 712 // <editor-fold defaultstate="collapsed" desc="Helper functions">719 // </editor-fold> 720 721 // <editor-fold defaultstate="collapsed" desc="Helper functions"> 713 722 714 723 private Node getNearestNode(Point point, double maxDist) { 715 Node nd = Main.map.mapView.getNearestNode(point, OsmPrimitive.isSelectablePredicate);716 if (nd!=null && line.getPoint(nd.getCoor()).distance(point)<=maxDist) return nd;717 else return null;724 Node nd = Main.map.mapView.getNearestNode(point, OsmPrimitive.isSelectablePredicate); 725 if (nd != null && line.getPoint(nd.getCoor()).distance(point) <= maxDist) return nd; 726 else return null; 718 727 } 719 728 … … 721 730 return mv.getLatLon(e.getX(), e.getY()); 722 731 } 723 // </editor-fold> 724 732 // </editor-fold> 725 733 } -
applications/editors/josm/plugins/FastDraw/src/org/openstreetmap/josm/plugins/fastdraw/FastDrawingPlugin.java
r29453 r32548 1 /* 2 * Licence: GPL v2 or later 3 * Author: Alexei Kasatkin, 2011 4 * Ideas: Kotelnikov, Michael Barabanov (ticket #3840) 5 */ 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.fastdraw; 6 3 7 package org.openstreetmap.josm.plugins.fastdraw;8 4 import org.openstreetmap.josm.Main; 9 5 import org.openstreetmap.josm.gui.IconToggleButton; … … 11 7 import org.openstreetmap.josm.plugins.Plugin; 12 8 import org.openstreetmap.josm.plugins.PluginInformation; 9 13 10 public class FastDrawingPlugin extends Plugin { 14 11
Note:
See TracChangeset
for help on using the changeset viewer.