Changeset 13961 in osm for applications
- Timestamp:
- 2009-03-03T19:00:43+01:00 (16 years ago)
- Location:
- applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapLayer.java
r13921 r13961 40 40 */ 41 41 public class SlippyMapLayer extends Layer implements ImageObserver, 42 PreferenceChangedListener 43 { 44 public int currentZoomLevel = 14; 45 //ArrayList<HashMap<SlippyMapKey, SlippyMapTile>> tileStorage = null; 42 PreferenceChangedListener { 43 /** 44 * Actual zoom lvl. Initial zoom lvl is set to 45 * {@link SlippyMapPreferences#getMinZoomLvl()}. 46 */ 47 public int currentZoomLevel = SlippyMapPreferences.getMinZoomLvl(); 46 48 private HashMap<SlippyMapKey, SlippyMapTile>[] tileStorage = null; 47 49 48 Point[][] pixelpos = new Point[21][21]; 49 LatLon lastTopLeft; 50 LatLon lastBotRight; 51 int z12x0, z12x1, z12y0, z12y1; 52 private Image bufferImage; 53 private SlippyMapTile clickedTile; 54 private boolean needRedraw; 55 private JPopupMenu tileOptionMenu; 56 57 @SuppressWarnings("serial") 58 public SlippyMapLayer() 59 { 60 super(tr("Slippy Map")); 61 background = true; 62 63 clearTileStorage(); 64 65 tileOptionMenu = new JPopupMenu(); 66 tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Load Tile")) 67 { 68 public void actionPerformed(ActionEvent ae) 69 { 70 if (clickedTile != null) 71 { 72 clickedTile.loadImage(); 73 needRedraw = true; 74 Main.map.repaint(); 75 } 76 } 77 })); 78 79 tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Show Tile Status")) 80 { 81 public void actionPerformed(ActionEvent ae) 82 { 83 if (clickedTile != null) 84 { 85 clickedTile.loadMetadata(); 86 needRedraw = true; 87 Main.map.repaint(); 88 } 89 } 90 })); 91 92 tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Request Update")) 93 { 94 public void actionPerformed(ActionEvent ae) 95 { 96 if (clickedTile != null) 97 { 98 clickedTile.requestUpdate(); 99 needRedraw = true; 100 Main.map.repaint(); 101 } 102 } 103 })); 104 105 tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Load All Tiles")) 106 { 107 public void actionPerformed(ActionEvent ae) 108 { 109 loadAllTiles(); 110 needRedraw = true; 111 Main.map.repaint(); 112 } 113 })); 114 115 //increase and decrease commands 116 tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Increase zoom")) 117 { 118 public void actionPerformed(ActionEvent ae) 119 { 120 increaseZoomLevel(); 121 needRedraw = true; 122 Main.map.repaint(); 123 } 124 })); 125 126 tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Decrease zoom")) 127 { 128 public void actionPerformed(ActionEvent ae) 129 { 130 decreaseZoomLevel(); 131 needRedraw = true; 132 Main.map.repaint(); 133 } 134 })); 135 //end of adding menu commands 136 137 SwingUtilities.invokeLater(new Runnable() 138 { 139 public void run() 140 { 141 Main.map.mapView.addMouseListener(new MouseAdapter() 142 { 143 @Override 144 public void mouseClicked(MouseEvent e) 145 { 146 if (e.getButton() != MouseEvent.BUTTON3) 147 return; 148 clickedTile = getTileForPixelpos(e.getX(), e.getY()); 149 tileOptionMenu.show(e.getComponent(), e.getX(), e 150 .getY()); 151 } 152 }); 153 154 listeners.add(new LayerChangeListener() 155 { 156 public void activeLayerChange(Layer oldLayer, 157 Layer newLayer) 158 { 159 } 160 161 public void layerAdded(Layer newLayer) 162 { 163 } 164 165 public void layerRemoved(Layer oldLayer) 166 { 167 Main.pref.listener.remove(SlippyMapLayer.this); 168 } 169 }); 170 } 171 }); 172 173 Main.pref.listener.add(this); 174 } 175 176 /** 177 * Zoom in, go closer to map. 50 Point[][] pixelpos = new Point[21][21]; 51 LatLon lastTopLeft; 52 LatLon lastBotRight; 53 int z12x0, z12x1, z12y0, z12y1; 54 private Image bufferImage; 55 private SlippyMapTile clickedTile; 56 private boolean needRedraw; 57 private JPopupMenu tileOptionMenu; 58 59 @SuppressWarnings("serial") 60 public SlippyMapLayer() { 61 super(tr("Slippy Map")); 62 background = true; 63 64 clearTileStorage(); 65 66 tileOptionMenu = new JPopupMenu(); 67 tileOptionMenu.add(new JMenuItem(new AbstractAction(tr("Load Tile")) { 68 public void actionPerformed(ActionEvent ae) { 69 if (clickedTile != null) { 70 clickedTile.loadImage(); 71 needRedraw = true; 72 Main.map.repaint(); 73 } 74 } 75 })); 76 77 tileOptionMenu.add(new JMenuItem(new AbstractAction( 78 tr("Show Tile Status")) { 79 public void actionPerformed(ActionEvent ae) { 80 if (clickedTile != null) { 81 clickedTile.loadMetadata(); 82 needRedraw = true; 83 Main.map.repaint(); 84 } 85 } 86 })); 87 88 tileOptionMenu.add(new JMenuItem(new AbstractAction( 89 tr("Request Update")) { 90 public void actionPerformed(ActionEvent ae) { 91 if (clickedTile != null) { 92 clickedTile.requestUpdate(); 93 needRedraw = true; 94 Main.map.repaint(); 95 } 96 } 97 })); 98 99 tileOptionMenu.add(new JMenuItem(new AbstractAction( 100 tr("Load All Tiles")) { 101 public void actionPerformed(ActionEvent ae) { 102 loadAllTiles(); 103 needRedraw = true; 104 Main.map.repaint(); 105 } 106 })); 107 108 // increase and decrease commands 109 tileOptionMenu.add(new JMenuItem( 110 new AbstractAction(tr("Increase zoom")) { 111 public void actionPerformed(ActionEvent ae) { 112 increaseZoomLevel(); 113 needRedraw = true; 114 Main.map.repaint(); 115 } 116 })); 117 118 tileOptionMenu.add(new JMenuItem( 119 new AbstractAction(tr("Decrease zoom")) { 120 public void actionPerformed(ActionEvent ae) { 121 decreaseZoomLevel(); 122 needRedraw = true; 123 Main.map.repaint(); 124 } 125 })); 126 // end of adding menu commands 127 128 SwingUtilities.invokeLater(new Runnable() { 129 public void run() { 130 Main.map.mapView.addMouseListener(new MouseAdapter() { 131 @Override 132 public void mouseClicked(MouseEvent e) { 133 if (e.getButton() != MouseEvent.BUTTON3) 134 return; 135 clickedTile = getTileForPixelpos(e.getX(), e.getY()); 136 tileOptionMenu.show(e.getComponent(), e.getX(), e 137 .getY()); 138 } 139 }); 140 141 listeners.add(new LayerChangeListener() { 142 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 143 } 144 145 public void layerAdded(Layer newLayer) { 146 } 147 148 public void layerRemoved(Layer oldLayer) { 149 Main.pref.listener.remove(SlippyMapLayer.this); 150 } 151 }); 152 } 153 }); 154 155 Main.pref.listener.add(this); 156 } 157 158 /** 159 * Zoom in, go closer to map. 160 */ 161 public void increaseZoomLevel() { 162 if (currentZoomLevel < SlippyMapPreferences.getMaxZoomLvl()) { 163 currentZoomLevel++; 164 } else { 165 System.err 166 .println("current zoom lvl couldnt be increased. MaxZoomLvl reached."); 167 } 168 } 169 170 /** 171 * Zoom out from map. 172 */ 173 public void decreaseZoomLevel() { 174 if (currentZoomLevel > SlippyMapPreferences.getMinZoomLvl()) { 175 currentZoomLevel--; 176 } else { 177 System.err 178 .println("current zoom lvl couldnt be decreased. MinZoomLvl reached."); 179 } 180 } 181 182 public void clearTileStorage() { 183 // when max zoom lvl is begin saved, this method is called and probably 184 // the setting isnt saved yet. 185 int maxZoom = 30; // SlippyMapPreferences.getMaxZoomLvl(); 186 // +1 because of array indexed from 0. 187 tileStorage = new HashMap[maxZoom + 1]; 188 189 for (int i = 0; i < maxZoom + 1; i++) 190 tileStorage[i] = new HashMap<SlippyMapKey, SlippyMapTile>(); 191 } 192 193 void loadAllTiles() { 194 MapView mv = Main.map.mapView; 195 LatLon topLeft = mv.getLatLon(0, 0); 196 LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight()); 197 z12x0 = lonToTileX(topLeft.lon()); 198 z12x1 = lonToTileX(botRight.lon()); 199 z12y0 = latToTileY(topLeft.lat()); 200 z12y1 = latToTileY(botRight.lat()); 201 if (z12x0 > z12x1) { 202 int tmp = z12x0; 203 z12x0 = z12x1; 204 z12x1 = tmp; 205 } 206 if (z12y0 > z12y1) { 207 int tmp = z12y0; 208 z12y0 = z12y1; 209 z12y1 = tmp; 210 } 211 // if there is more than 18 tiles on screen in any direction, do not 212 // load all tiles! 213 if (z12x1 - z12x0 > 18) { 214 System.out 215 .println("Not downloading all tiles because there is more than 18 tiles on X axis!"); 216 return; 217 } 218 if (z12y1 - z12y0 > 18) { 219 System.out 220 .println("Not downloading all tiles because there is more than 18 tiles on Y axis!"); 221 return; 222 } 223 224 for (int x = z12x0 - 1; x <= z12x1; x++) { 225 for (int y = z12y0 - 1; y <= z12y1; y++) { 226 SlippyMapKey key = new SlippyMapKey(x, y); 227 228 SlippyMapTile tile = tileStorage[currentZoomLevel].get(key); 229 230 if (tile == null) 231 tileStorage[currentZoomLevel].put(key, 232 tile = new SlippyMapTile(x, y, currentZoomLevel)); 233 234 if (tile.getImage() == null) 235 tile.loadImage(); 236 } 237 } 238 } 239 240 /** 178 241 */ 179 public void increaseZoomLevel() { 180 if(currentZoomLevel < SlippyMapPreferences.getMaxZoomLvl()) { 181 currentZoomLevel++; 182 //if(SlippyMapPreferences.getAutoloadTiles()) { 183 // loadAllTiles(); 184 //} 185 } 186 } 187 188 /** 189 * Zoom out from map. 190 */ 191 public void decreaseZoomLevel() { 192 if(currentZoomLevel > 10) { 193 currentZoomLevel--; 194 //if(SlippyMapPreferences.getAutoloadTiles()) { 195 // loadAllTiles(); 196 //} 197 } 198 } 199 200 public void clearTileStorage() 201 { 202 int maxZoom = SlippyMapPreferences.getMaxZoomLvl(); 203 // +1 because of array indexed from 0. 204 tileStorage = new HashMap[maxZoom+1]; 205 206 for (int i = 0; i < maxZoom+1; i++) 207 tileStorage[i] = new HashMap<SlippyMapKey, SlippyMapTile>(); 208 } 209 210 void loadAllTiles() 211 { 212 MapView mv = Main.map.mapView; 213 LatLon topLeft = mv.getLatLon(0, 0); 214 LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight()); 215 z12x0 = lonToTileX(topLeft.lon()); 216 z12x1 = lonToTileX(botRight.lon()); 217 z12y0 = latToTileY(topLeft.lat()); 218 z12y1 = latToTileY(botRight.lat()); 219 if (z12x0 > z12x1) 220 { 221 int tmp = z12x0; 222 z12x0 = z12x1; 223 z12x1 = tmp; 224 } 225 if (z12y0 > z12y1) 226 { 227 int tmp = z12y0; 228 z12y0 = z12y1; 229 z12y1 = tmp; 230 } 231 //if there is more than 18 tiles on screen in any direction, do not load all tiles! 232 if (z12x1 - z12x0 > 18) { 233 System.out.println("Not downloading all tiles because there is more than 18 tiles on X axis!"); 234 return; 235 } 236 if (z12y1 - z12y0 > 18) { 237 System.out.println("Not downloading all tiles because there is more than 18 tiles on Y axis!"); 238 return; 239 } 240 241 for (int x = z12x0 - 1; x <= z12x1; x++) 242 { 243 for (int y = z12y0 - 1; y <= z12y1; y++) 244 { 245 SlippyMapKey key = new SlippyMapKey(x,y); 246 247 SlippyMapTile tile = tileStorage[currentZoomLevel].get(key); 248 249 if (tile == null) 250 tileStorage[currentZoomLevel].put(key, 251 tile = new SlippyMapTile(x, y, currentZoomLevel)); 252 253 if (tile.getImage() == null) 254 tile.loadImage(); 255 } 256 } 257 } 258 259 /** 260 */ 261 @Override 262 public void paint(Graphics g, MapView mv) 263 { 264 LatLon topLeft = mv.getLatLon(0, 0); 265 LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight()); 266 Graphics oldg = g; 267 268 if (lastTopLeft != null && lastBotRight != null 269 && topLeft.equalsEpsilon(lastTopLeft) 270 && botRight.equalsEpsilon(lastBotRight) && bufferImage != null 271 && mv.getWidth() == bufferImage.getWidth(null) 272 && mv.getHeight() == bufferImage.getHeight(null) && !needRedraw) 273 { 274 275 g.drawImage(bufferImage, 0, 0, null); 276 return; 277 } 278 279 needRedraw = false; 280 lastTopLeft = topLeft; 281 lastBotRight = botRight; 282 bufferImage = mv.createImage(mv.getWidth(), mv.getHeight()); 283 g = bufferImage.getGraphics(); 284 285 z12x0 = lonToTileX(topLeft.lon()); 286 z12x1 = lonToTileX(botRight.lon()); 287 z12y0 = latToTileY(topLeft.lat()); 288 z12y1 = latToTileY(botRight.lat()); 289 290 if (z12x0 > z12x1) 291 { 292 int tmp = z12x0; 293 z12x0 = z12x1; 294 z12x1 = tmp; 295 } 296 if (z12y0 > z12y1) 297 { 298 int tmp = z12y0; 299 z12y0 = z12y1; 300 z12y1 = tmp; 301 } 302 303 if (z12x1 - z12x0 > 18) 304 return; 305 if (z12y1 - z12y0 > 18) 306 return; 307 308 for (int x = z12x0 - 1; x <= z12x1 + 1; x++) 309 { 310 double lon = tileXToLon(x); 311 for (int y = z12y0 - 1; y <= z12y1 + 1; y++) 312 { 313 LatLon tmpLL = new LatLon(tileYToLat(y), lon); 314 pixelpos[x - z12x0 + 1][y - z12y0 + 1] = mv.getPoint(Main.proj 315 .latlon2eastNorth(tmpLL)); 316 } 317 } 318 319 int fontHeight = g.getFontMetrics().getHeight(); 320 321 g.setColor(Color.DARK_GRAY); 322 323 float fadeBackground = SlippyMapPreferences.getFadeBackground(); 324 325 for (int x = z12x0 - 1; x <= z12x1; x++) 326 { 327 for (int y = z12y0 - 1; y <= z12y1; y++) 328 { 329 SlippyMapKey key = new SlippyMapKey(x,y); 330 SlippyMapTile tile; 331 try 332 { 333 tile = tileStorage[currentZoomLevel].get(key); 334 } catch (IndexOutOfBoundsException ex) { 335 throw new RuntimeException("currentZoomLevel=" + currentZoomLevel + " and tile storage array have just size=" + tileStorage.length + " and maxZoomLvl in preferences is " + SlippyMapPreferences.getMaxZoomLvl() + ".", ex); 336 } 337 338 if (tile == null) 339 { 340 tile = new SlippyMapTile(x, y, currentZoomLevel); 341 tileStorage[currentZoomLevel].put(key, tile); 342 if(SlippyMapPreferences.getAutoloadTiles()) { 343 //TODO probably do on background 344 tile.loadImage(); 345 } 346 } 347 Image img = tile.getImage(); 348 349 if (img != null) 350 { 351 Point p = pixelpos[x - z12x0 + 1][y - z12y0 + 1]; 352 Point p2 = pixelpos[x - z12x0 + 2][y - z12y0 + 2]; 353 g.drawImage(img, p.x, p.y, p2.x - p.x, p2.y - p.y, this); 354 355 if(fadeBackground != 0f) { 356 //dimm by painting opaque rect... 357 g.setColor(new Color(1f,1f,1f,fadeBackground)); 358 g.fillRect(p.x, p.y, p2.x - p.x, p2.y - p.y); 359 }//end of if dim != 0 360 }//end of if img != null 361 }//end of for 362 }//end of for 363 364 for (int x = z12x0 - 1; x <= z12x1; x++) 365 { 366 Point p = pixelpos[x - z12x0 + 1][0]; 367 368 if (x % 32 == 0) 369 { 370 // level 7 tile boundary 371 g.fillRect(p.x - 1, 0, 3, mv.getHeight()); 372 } 373 else 374 { 375 g.drawLine(p.x, 0, p.x, mv.getHeight()); 376 } 377 378 for (int y = z12y0 - 1; y <= z12y1; y++) 379 { 380 SlippyMapKey key = new SlippyMapKey(x,y); 381 int texty = p.y + 2 + fontHeight; 382 SlippyMapTile tile = tileStorage[currentZoomLevel].get(key); 383 if(tile == null) { 384 continue; 385 } 386 p = pixelpos[x - z12x0 + 1][y - z12y0 + 2]; 387 g.drawString("x=" + x + " y=" + y + " z=" + currentZoomLevel + "", p.x + 2, texty); 388 texty += 1 + fontHeight; 389 if ((x % 32 == 0) && (y % 32 == 0)) 390 { 391 g.drawString("x=" + x / 32 + " y=" + y / 32 + " z=7", 392 p.x + 2, texty); 393 texty += 1 + fontHeight; 394 } 395 396 String md = tile.getMetadata(); 397 if (md != null) 398 { 399 g.drawString(md, p.x + 2, texty); 400 texty += 1 + fontHeight; 401 } 402 403 if (tile.getImage() == null) 404 { 405 g.drawString(tr("image not loaded"), p.x + 2, texty); 406 texty += 1 + fontHeight; 407 } 408 409 if (x == z12x0 - 1) 410 { 411 if (y % 32 == 31) 412 { 413 g.fillRect(0, p.y - 1, mv.getWidth(), 3); 414 } 415 else 416 { 417 g.drawLine(0, p.y, mv.getWidth(), p.y); 418 } 419 } 420 } //end of for 421 } 422 423 oldg.drawImage(bufferImage, 0, 0, null); 424 425 if((z12x1 - z12x0 < 2) || (z12y1 - z12y0 < 2)) { 426 if(SlippyMapPreferences.getAutozoom()) { 427 increaseZoomLevel(); 428 } 429 this.paint(oldg, mv); 430 } 431 432 if((z12x1 - z12x0 > 6) || (z12y1 - z12y0 > 6)) { 433 if(SlippyMapPreferences.getAutozoom()) { 434 decreaseZoomLevel(); 435 } 436 this.paint(oldg, mv); 437 } 438 439 g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120); 440 }//end of paint metod 441 442 SlippyMapTile getTileForPixelpos(int px, int py) 443 { 444 int tilex = z12x1; 445 int tiley = z12y1; 446 for (int x = z12x0; x <= z12x1; x++) 447 { 448 449 if (pixelpos[x - z12x0 + 1][0].x > px) 450 { 451 tilex = x - 1; 452 break; 453 } 454 } 455 if (tilex == -1) 456 return null; 457 for (int y = z12y0; y <= z12y1; y++) 458 { 459 460 if (pixelpos[0][y - z12y0 + 1].y > py) 461 { 462 tiley = y - 1; 463 break; 464 } 465 } 466 if (tiley == -1) 467 return null; 468 469 SlippyMapKey key = new SlippyMapKey(tilex,tiley); 470 SlippyMapTile tile = tileStorage[currentZoomLevel].get(key); 471 if (tile == null) 472 tileStorage[currentZoomLevel].put(key, 473 tile = new SlippyMapTile(tilex, tiley, currentZoomLevel)); 474 return tile; 475 } 476 477 @Override 478 public Icon getIcon() 479 { 480 return ImageProvider.get("slippymap"); 481 } 482 483 @Override 484 public Object getInfoComponent() 485 { 486 return null; 487 } 488 489 @Override 490 public Component[] getMenuEntries() 491 { 492 return new Component[] 493 { 494 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), 495 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 496 new JSeparator(), 497 // color, 498 new JMenuItem(new RenameLayerAction(associatedFile, this)), 499 new JSeparator(), 500 new JMenuItem(new LayerListPopup.InfoAction(this)) }; 501 } 502 503 @Override 504 public String getToolTipText() 505 { 506 return null; 507 } 508 509 @Override 510 public boolean isMergable(Layer other) 511 { 512 return false; 513 } 514 515 @Override 516 public void mergeFrom(Layer from) 517 { 518 } 519 520 @Override 521 public void visitBoundingBox(BoundingXYVisitor v) 522 { 523 } 524 525 private int latToTileY(double lat) 526 { 527 double l = lat / 180 * Math.PI; 528 double pf = Math.log(Math.tan(l) + (1 / Math.cos(l))); 529 return (int) (Math.pow(2.0, currentZoomLevel-1) * (Math.PI - pf) / Math.PI); 530 } 531 532 private int lonToTileX(double lon) 533 { 534 return (int) (Math.pow(2.0, currentZoomLevel-3) * (lon + 180.0) / 45.0); 535 } 536 537 private double tileYToLat(int y) 538 { 539 return Math.atan(Math.sinh(Math.PI - (Math.PI * y / Math.pow(2.0, currentZoomLevel-1)))) * 180 540 / Math.PI; 541 } 542 543 private double tileXToLon(int x) 544 { 545 return x * 45.0 / Math.pow(2.0, currentZoomLevel-3) - 180.0; 546 } 547 548 public boolean imageUpdate(Image img, int infoflags, int x, int y, 549 int width, int height) 550 { 551 552 boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0); 553 // Repaint immediately if we are done, otherwise batch up 554 // repaint requests every 100 milliseconds 555 needRedraw = true; 556 Main.map.repaint(done ? 0 : 100); 557 return !done; 558 } 559 560 /* 561 * (non-Javadoc) 562 * 563 * @see org.openstreetmap.josm.data.Preferences.PreferenceChangedListener#preferenceChanged(java.lang.String, 564 * java.lang.String) 565 */ 566 public void preferenceChanged(String key, String newValue) 567 { 568 if (key.startsWith(SlippyMapPreferences.PREFERENCE_PREFIX)) 569 { 570 // System.err.println(this + ".preferenceChanged('" + key + "', '" 571 // + newValue + "') called"); 572 573 clearTileStorage(); 574 } 575 } 576 577 @Override 578 public void destroy() 579 { 580 Main.pref.listener.remove(SlippyMapLayer.this); 581 } 242 @Override 243 public void paint(Graphics g, MapView mv) { 244 LatLon topLeft = mv.getLatLon(0, 0); 245 LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight()); 246 Graphics oldg = g; 247 248 if (lastTopLeft != null && lastBotRight != null 249 && topLeft.equalsEpsilon(lastTopLeft) 250 && botRight.equalsEpsilon(lastBotRight) && bufferImage != null 251 && mv.getWidth() == bufferImage.getWidth(null) 252 && mv.getHeight() == bufferImage.getHeight(null) && !needRedraw) { 253 254 g.drawImage(bufferImage, 0, 0, null); 255 return; 256 } 257 258 needRedraw = false; 259 lastTopLeft = topLeft; 260 lastBotRight = botRight; 261 bufferImage = mv.createImage(mv.getWidth(), mv.getHeight()); 262 g = bufferImage.getGraphics(); 263 264 z12x0 = lonToTileX(topLeft.lon()); 265 z12x1 = lonToTileX(botRight.lon()); 266 z12y0 = latToTileY(topLeft.lat()); 267 z12y1 = latToTileY(botRight.lat()); 268 269 if (z12x0 > z12x1) { 270 int tmp = z12x0; 271 z12x0 = z12x1; 272 z12x1 = tmp; 273 } 274 if (z12y0 > z12y1) { 275 int tmp = z12y0; 276 z12y0 = z12y1; 277 z12y1 = tmp; 278 } 279 280 if (z12x1 - z12x0 > 18) 281 return; 282 if (z12y1 - z12y0 > 18) 283 return; 284 285 for (int x = z12x0 - 1; x <= z12x1 + 1; x++) { 286 double lon = tileXToLon(x); 287 for (int y = z12y0 - 1; y <= z12y1 + 1; y++) { 288 LatLon tmpLL = new LatLon(tileYToLat(y), lon); 289 pixelpos[x - z12x0 + 1][y - z12y0 + 1] = mv.getPoint(Main.proj 290 .latlon2eastNorth(tmpLL)); 291 } 292 } 293 294 int fontHeight = g.getFontMetrics().getHeight(); 295 296 g.setColor(Color.DARK_GRAY); 297 298 float fadeBackground = SlippyMapPreferences.getFadeBackground(); 299 300 for (int x = z12x0 - 1; x <= z12x1; x++) { 301 for (int y = z12y0 - 1; y <= z12y1; y++) { 302 SlippyMapKey key = new SlippyMapKey(x, y); 303 SlippyMapTile tile; 304 try { 305 tile = tileStorage[currentZoomLevel].get(key); 306 } catch (IndexOutOfBoundsException ex) { 307 throw new RuntimeException("currentZoomLevel=" 308 + currentZoomLevel 309 + " and tile storage array have just size=" 310 + tileStorage.length 311 + " and maxZoomLvl in preferences is " 312 + SlippyMapPreferences.getMaxZoomLvl() + ".", ex); 313 } 314 315 if (tile == null) { 316 tile = new SlippyMapTile(x, y, currentZoomLevel); 317 tileStorage[currentZoomLevel].put(key, tile); 318 if (SlippyMapPreferences.getAutoloadTiles()) { 319 // TODO probably do on background 320 tile.loadImage(); 321 } 322 } 323 Image img = tile.getImage(); 324 325 if (img != null) { 326 Point p = pixelpos[x - z12x0 + 1][y - z12y0 + 1]; 327 Point p2 = pixelpos[x - z12x0 + 2][y - z12y0 + 2]; 328 g.drawImage(img, p.x, p.y, p2.x - p.x, p2.y - p.y, this); 329 330 if (fadeBackground != 0f) { 331 // dimm by painting opaque rect... 332 g.setColor(new Color(1f, 1f, 1f, fadeBackground)); 333 g.fillRect(p.x, p.y, p2.x - p.x, p2.y - p.y); 334 }// end of if dim != 0 335 }// end of if img != null 336 }// end of for 337 }// end of for 338 339 g.setColor(Color.red); 340 for (int x = z12x0 - 1; x <= z12x1; x++) { 341 Point p = pixelpos[x - z12x0 + 1][0]; 342 343 if(SlippyMapPreferences.getDrawDebug()) { 344 if (x % 32 == 0) { 345 // level 7 tile boundary 346 g.fillRect(p.x - 1, 0, 3, mv.getHeight()); 347 } else { 348 g.drawLine(p.x, 0, p.x, mv.getHeight()); 349 } 350 }//end of if draw debug 351 352 for (int y = z12y0 - 1; y <= z12y1; y++) { 353 SlippyMapKey key = new SlippyMapKey(x, y); 354 int texty = p.y + 2 + fontHeight; 355 SlippyMapTile tile = tileStorage[currentZoomLevel].get(key); 356 if (tile == null) { 357 continue; 358 } 359 p = pixelpos[x - z12x0 + 1][y - z12y0 + 2]; 360 361 if(SlippyMapPreferences.getDrawDebug()) { 362 g.drawString("x=" + x + " y=" + y + " z=" + currentZoomLevel 363 + "", p.x + 2, texty); 364 texty += 1 + fontHeight; 365 if ((x % 32 == 0) && (y % 32 == 0)) { 366 g.drawString("x=" + x / 32 + " y=" + y / 32 + " z=7", 367 p.x + 2, texty); 368 texty += 1 + fontHeight; 369 } 370 }//end of if draw debug 371 372 String md = tile.getMetadata(); 373 if (md != null) { 374 g.drawString(md, p.x + 2, texty); 375 texty += 1 + fontHeight; 376 } 377 378 if (tile.getImage() == null) { 379 g.drawString(tr("image not loaded"), p.x + 2, texty); 380 texty += 1 + fontHeight; 381 } 382 383 if(SlippyMapPreferences.getDrawDebug()) { 384 if (x == z12x0 - 1) { 385 if (y % 32 == 31) { 386 g.fillRect(0, p.y - 1, mv.getWidth(), 3); 387 } else { 388 g.drawLine(0, p.y, mv.getWidth(), p.y); 389 } 390 } 391 }//end of if draw debug 392 } // end of for 393 } 394 395 oldg.drawImage(bufferImage, 0, 0, null); 396 397 // TODO do autozoom nicer 398 if ((z12x1 - z12x0 < 2) || (z12y1 - z12y0 < 2)) { 399 if (SlippyMapPreferences.getAutozoom()) { 400 increaseZoomLevel(); 401 } 402 this.paint(oldg, mv); 403 } 404 405 if ((z12x1 - z12x0 > 6) || (z12y1 - z12y0 > 6)) { 406 if (SlippyMapPreferences.getAutozoom()) { 407 decreaseZoomLevel(); 408 } 409 this.paint(oldg, mv); 410 } 411 412 g.setColor(Color.black); 413 g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120); 414 }// end of paint metod 415 416 SlippyMapTile getTileForPixelpos(int px, int py) { 417 int tilex = z12x1; 418 int tiley = z12y1; 419 for (int x = z12x0; x <= z12x1; x++) { 420 421 if (pixelpos[x - z12x0 + 1][0].x > px) { 422 tilex = x - 1; 423 break; 424 } 425 } 426 if (tilex == -1) 427 return null; 428 for (int y = z12y0; y <= z12y1; y++) { 429 430 if (pixelpos[0][y - z12y0 + 1].y > py) { 431 tiley = y - 1; 432 break; 433 } 434 } 435 if (tiley == -1) 436 return null; 437 438 SlippyMapKey key = new SlippyMapKey(tilex, tiley); 439 SlippyMapTile tile = tileStorage[currentZoomLevel].get(key); 440 if (tile == null) 441 tileStorage[currentZoomLevel].put(key, tile = new SlippyMapTile( 442 tilex, tiley, currentZoomLevel)); 443 return tile; 444 } 445 446 @Override 447 public Icon getIcon() { 448 return ImageProvider.get("slippymap"); 449 } 450 451 @Override 452 public Object getInfoComponent() { 453 return null; 454 } 455 456 @Override 457 public Component[] getMenuEntries() { 458 return new Component[] { 459 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), 460 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 461 new JSeparator(), 462 // color, 463 new JMenuItem(new RenameLayerAction(associatedFile, this)), 464 new JSeparator(), 465 new JMenuItem(new LayerListPopup.InfoAction(this)) }; 466 } 467 468 @Override 469 public String getToolTipText() { 470 return null; 471 } 472 473 @Override 474 public boolean isMergable(Layer other) { 475 return false; 476 } 477 478 @Override 479 public void mergeFrom(Layer from) { 480 } 481 482 @Override 483 public void visitBoundingBox(BoundingXYVisitor v) { 484 } 485 486 private int latToTileY(double lat) { 487 double l = lat / 180 * Math.PI; 488 double pf = Math.log(Math.tan(l) + (1 / Math.cos(l))); 489 return (int) (Math.pow(2.0, currentZoomLevel - 1) * (Math.PI - pf) / Math.PI); 490 } 491 492 private int lonToTileX(double lon) { 493 return (int) (Math.pow(2.0, currentZoomLevel - 3) * (lon + 180.0) / 45.0); 494 } 495 496 private double tileYToLat(int y) { 497 return Math.atan(Math.sinh(Math.PI 498 - (Math.PI * y / Math.pow(2.0, currentZoomLevel - 1)))) 499 * 180 / Math.PI; 500 } 501 502 private double tileXToLon(int x) { 503 return x * 45.0 / Math.pow(2.0, currentZoomLevel - 3) - 180.0; 504 } 505 506 public boolean imageUpdate(Image img, int infoflags, int x, int y, 507 int width, int height) { 508 509 boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0); 510 // Repaint immediately if we are done, otherwise batch up 511 // repaint requests every 100 milliseconds 512 needRedraw = true; 513 Main.map.repaint(done ? 0 : 100); 514 return !done; 515 } 516 517 /* 518 * (non-Javadoc) 519 * 520 * @seeorg.openstreetmap.josm.data.Preferences.PreferenceChangedListener# 521 * preferenceChanged(java.lang.String, java.lang.String) 522 */ 523 public void preferenceChanged(String key, String newValue) { 524 if (key.startsWith(SlippyMapPreferences.PREFERENCE_PREFIX)) { 525 // System.err.println(this + ".preferenceChanged('" + key + "', '" 526 // + newValue + "') called"); 527 // when fade background changed, no need to clear tile storage 528 // TODO move this code to SlippyMapPreferences class. 529 if (!key.equals(SlippyMapPreferences.PREFERENCE_FADE_BACKGROUND)) { 530 clearTileStorage(); 531 } 532 } 533 } 534 535 @Override 536 public void destroy() { 537 Main.pref.listener.remove(SlippyMapLayer.this); 538 } 582 539 } -
applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferences.java
r13921 r13961 14 14 public static final String PREFERENCE_PREFIX = "slippymap"; 15 15 16 private static final String PREFERENCE_TILE_URL = PREFERENCE_PREFIX + ".tile_url"; 17 private static final String PREFERENCE_AUTOZOOM = PREFERENCE_PREFIX + ".autozoom"; 18 private static final String PREFERENCE_AUTOLOADTILES = PREFERENCE_PREFIX + ".autoload_tiles"; 19 private static final String PREFERENCE_MAX_ZOOM_LVL = PREFERENCE_PREFIX + ".max_zoom_lvl"; 20 private static final String PREFERENCE_FADE_BACKGROUND = PREFERENCE_PREFIX + ".fade_background"; 16 public static final String PREFERENCE_TILE_URL = PREFERENCE_PREFIX + ".tile_url"; 17 public static final String PREFERENCE_AUTOZOOM = PREFERENCE_PREFIX + ".autozoom"; 18 public static final String PREFERENCE_AUTOLOADTILES = PREFERENCE_PREFIX + ".autoload_tiles"; 19 public static final String PREFERENCE_MIN_ZOOM_LVL = PREFERENCE_PREFIX + ".min_zoom_lvl"; 20 public static final String PREFERENCE_MAX_ZOOM_LVL = PREFERENCE_PREFIX + ".max_zoom_lvl"; 21 public static final String PREFERENCE_FADE_BACKGROUND = PREFERENCE_PREFIX + ".fade_background"; 22 public static final String PREFERENCE_DRAW_DEBUG = PREFERENCE_PREFIX + ".draw_debug"; 21 23 22 24 public static String getMapUrl() … … 52 54 public static void setAutozoom(boolean autozoom) { 53 55 Main.pref.put(SlippyMapPreferences.PREFERENCE_AUTOZOOM, autozoom); 56 } 57 58 public static void setDrawDebug(boolean drawDebug) { 59 Main.pref.put(SlippyMapPreferences.PREFERENCE_DRAW_DEBUG, drawDebug); 60 } 61 62 public static boolean getDrawDebug() 63 { 64 String drawDebug = Main.pref.get(PREFERENCE_DRAW_DEBUG); 65 66 if (drawDebug == null || "".equals(drawDebug)) 67 { 68 drawDebug = "false"; 69 Main.pref.put(PREFERENCE_DRAW_DEBUG, drawDebug); 70 } 71 72 return Boolean.parseBoolean(drawDebug); 54 73 } 55 74 … … 127 146 navrat = 30; 128 147 } 148 //if(navrat < SlippyMapPreferences.getMinZoomLvl()) { 149 // System.err.println("maxZoomLvl shouldnt be more than minZoomLvl! Setting to minZoomLvl."); 150 // navrat = SlippyMapPreferences.getMinZoomLvl(); 151 //} 129 152 return navrat; 130 153 } … … 135 158 maxZoomLvl = 30; 136 159 } 160 if(maxZoomLvl < SlippyMapPreferences.getMinZoomLvl()) { 161 System.err.println("maxZoomLvl shouldnt be more than minZoomLvl! Setting to minZoomLvl."); 162 maxZoomLvl = SlippyMapPreferences.getMinZoomLvl(); 163 } 137 164 Main.pref.put(SlippyMapPreferences.PREFERENCE_MAX_ZOOM_LVL, "" + maxZoomLvl); 165 } 166 167 public static int getMinZoomLvl() 168 { 169 String minZoomLvl = Main.pref.get(PREFERENCE_MIN_ZOOM_LVL); 170 171 if (minZoomLvl == null || "".equals(minZoomLvl)) 172 { 173 minZoomLvl = "" + (SlippyMapPreferences.getMaxZoomLvl() - 4); 174 Main.pref.put(PREFERENCE_MAX_ZOOM_LVL, minZoomLvl); 175 } 176 177 int navrat; 178 try { 179 navrat = Integer.parseInt(minZoomLvl); 180 } catch (Exception ex) { 181 throw new RuntimeException("Problem while converting string to int. Converting value of prefetrences " + PREFERENCE_MIN_ZOOM_LVL + ". Value=\"" + minZoomLvl + "\". Should be an integer. Error: " + ex.getMessage(), ex); 182 } 183 if(navrat < 2) { 184 System.err.println("minZoomLvl shouldnt be lees than 2! Setting to 2."); 185 navrat = 2; 186 } 187 //if(navrat > SlippyMapPreferences.getMaxZoomLvl()) { 188 // System.err.println("minZoomLvl shouldnt be more than maxZoomLvl! Setting to maxZoomLvl."); 189 // navrat = SlippyMapPreferences.getMaxZoomLvl(); 190 //} 191 return navrat; 192 } 193 194 public static void setMinZoomLvl(int minZoomLvl) { 195 if(minZoomLvl < 2) { 196 System.err.println("minZoomLvl shouldnt be lees than 2! Setting to 2."); 197 minZoomLvl = 2; 198 } 199 if(minZoomLvl > SlippyMapPreferences.getMaxZoomLvl()) { 200 System.err.println("minZoomLvl shouldnt be more than maxZoomLvl! Setting to maxZoomLvl."); 201 minZoomLvl = SlippyMapPreferences.getMaxZoomLvl(); 202 } 203 Main.pref.put(SlippyMapPreferences.PREFERENCE_MIN_ZOOM_LVL, "" + minZoomLvl); 138 204 } 139 205
Note:
See TracChangeset
for help on using the changeset viewer.