Ignore:
Timestamp:
2009-07-03T23:35:00+02:00 (15 years ago)
Author:
stoecker
Message:

fixes for JOSM 1725 - utilsplugin JumpToAction still missing

Location:
applications/editors/josm/plugins/slippymap
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/slippymap/build.xml

    r15707 r16294  
    2626                <attribute name="Plugin-Description" value="Displays a slippy map grid in JOSM. Can load tiles from slippy map as background and request updates."/>
    2727                <attribute name="Plugin-Link" value="http://wiki.openstreetmap.org/index.php/JOSM/Plugins/SlippyMap"/>
    28                 <attribute name="Plugin-Mainversion" value="1646"/>
     28                <attribute name="Plugin-Mainversion" value="1725"/>
    2929                <attribute name="Plugin-Version" value="${version.entry.commit.revision}"/>
    3030            </manifest>
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapLayer.java

    r16164 r16294  
    4646 * @author LuVar <lubomir.varga@freemap.sk>
    4747 * @author Dave Hansen <dave@sr71.net>
    48  * 
     48 *
    4949 */
    5050public class SlippyMapLayer extends Layer implements ImageObserver,
     
    131131                    }
    132132                }));
    133        
     133
    134134        tileOptionMenu.add(new JMenuItem(
    135                         new AbstractAction(tr("Flush Tile Cache")) {
    136                                 public void actionPerformed(ActionEvent ae) {
    137                                         System.out.print("flushing all tiles...");
    138                                         for (SlippyMapTile t : tileStorage.values()) {
    139                                                 t.dropImage();
    140                                                 }
    141                                         System.out.println("done");
    142                                         }
    143                                 }));
     135                new AbstractAction(tr("Flush Tile Cache")) {
     136                    public void actionPerformed(ActionEvent ae) {
     137                        System.out.print("flushing all tiles...");
     138                        for (SlippyMapTile t : tileStorage.values()) {
     139                            t.dropImage();
     140                            }
     141                        System.out.println("done");
     142                        }
     143                    }));
    144144        // end of adding menu commands
    145145
     
    176176    /**
    177177     * Zoom in, go closer to map.
    178      * 
    179      * @return  true, if zoom increasing was successfull, false othervise
     178     *
     179     * @return    true, if zoom increasing was successfull, false othervise
    180180     */
    181181    public boolean increaseZoomLevel() {
     
    194194    /**
    195195     * Zoom out from map.
    196      * 
    197      * @return  true, if zoom increasing was successfull, false othervise
     196     *
     197     * @return    true, if zoom increasing was successfull, false othervise
    198198     */
    199199    public boolean decreaseZoomLevel() {
     
    271271
    272272    synchronized SlippyMapTile getTile(int x, int y, int zoom) {
    273                 SlippyMapKey key = new SlippyMapKey(x, y, zoom);
    274                 if (!key.valid) {
    275                         key = null;
    276                 }
    277                 return tileStorage.get(key);
    278         }
    279 
    280         synchronized SlippyMapTile putTile(SlippyMapTile tile, int x, int y, int zoom) {
    281                 SlippyMapKey key = new SlippyMapKey(x, y, zoom);
    282                 if (!key.valid) {
    283                         key = null;
    284                 }
    285                 return tileStorage.put(key, tile);
    286         }
    287 
    288         synchronized SlippyMapTile getOrCreateTile(int x, int y, int zoom) {
    289                 SlippyMapTile tile = getTile(x, y, zoom);
    290                 if (tile != null) {
    291                         return tile;
    292                 }
    293                 tile = new SlippyMapTile(x, y, zoom);
    294                 putTile(tile, x, y, zoom);
    295                 return tile;
    296         }
    297    
     273        SlippyMapKey key = new SlippyMapKey(x, y, zoom);
     274        if (!key.valid) {
     275            key = null;
     276        }
     277        return tileStorage.get(key);
     278    }
     279
     280    synchronized SlippyMapTile putTile(SlippyMapTile tile, int x, int y, int zoom) {
     281        SlippyMapKey key = new SlippyMapKey(x, y, zoom);
     282        if (!key.valid) {
     283            key = null;
     284        }
     285        return tileStorage.put(key, tile);
     286    }
     287
     288    synchronized SlippyMapTile getOrCreateTile(int x, int y, int zoom) {
     289        SlippyMapTile tile = getTile(x, y, zoom);
     290        if (tile != null) {
     291            return tile;
     292        }
     293        tile = new SlippyMapTile(x, y, zoom);
     294        putTile(tile, x, y, zoom);
     295        return tile;
     296    }
     297
    298298    void loadAllTiles() {
    299299        MapView mv = Main.map.mapView;
     
    302302
    303303        TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
    304        
     304
    305305        // if there is more than 18 tiles on screen in any direction, do not
    306306        // load all tiles!
    307307        if (ts.tilesSpanned() > (18*18)) {
    308                 System.out.println("Not downloading all tiles because there is more than 18 tiles on an axis!");
    309                 return;
     308            System.out.println("Not downloading all tiles because there is more than 18 tiles on an axis!");
     309            return;
    310310        }
    311311
    312312        for (Tile t : ts.allTiles()) {
    313                 SlippyMapTile tile = getOrCreateTile(t.x, t.y, currentZoomLevel);
    314                 if (tile.getImage() == null) {
    315                         this.loadSingleTile(tile);
    316                 }
     313            SlippyMapTile tile = getOrCreateTile(t.x, t.y, currentZoomLevel);
     314            if (tile.getImage() == null) {
     315                this.loadSingleTile(tile);
     316            }
    317317        }//end of for Tile t
    318318    }
    319319
    320         /*
    321         * Attempt to approximate how much the image is being scaled. For instance,
    322         * a 100x100 image being scaled to 50x50 would return 0.25.
    323         */
    324         Image lastScaledImage = null;
    325 
    326         double getImageScaling(Image img, Point p0, Point p1) {
    327                 int realWidth = img.getWidth(this);
    328                 int realHeight = img.getHeight(this);
    329                 if (realWidth == -1 || realHeight == -1) {
    330                         /*
    331                         * We need a good image against which to work. If
    332                         * the current one isn't loaded, then try the last one.
    333                         * Should be good enough. If we've never seen one, then
    334                         * guess.        
    335                         */
    336                         if (lastScaledImage != null) {
    337                                 return getImageScaling(lastScaledImage, p0, p1);
    338                         }
    339                         realWidth = 256;
    340                         realHeight = 256;
    341                 } else {
    342                         lastScaledImage = img;
    343                 }
    344                 /*
    345                 * If the zoom scale gets really, really off, these can get into
    346                 * the millions, so make this a double to prevent integer
    347                 * overflows.        
    348                 */
    349                 double drawWidth = p1.x - p0.x;
    350                 double drawHeight = p1.x - p0.x;
    351                 // stem.out.println("drawWidth: " + drawWidth + " drawHeight: " +
    352                 // drawHeight);
    353 
    354                 double drawArea = drawWidth * drawHeight;
     320    /*
     321    * Attempt to approximate how much the image is being scaled. For instance,
     322    * a 100x100 image being scaled to 50x50 would return 0.25.
     323    */
     324    Image lastScaledImage = null;
     325
     326    double getImageScaling(Image img, Point p0, Point p1) {
     327        int realWidth = img.getWidth(this);
     328        int realHeight = img.getHeight(this);
     329        if (realWidth == -1 || realHeight == -1) {
     330            /*
     331            * We need a good image against which to work. If
     332            * the current one isn't loaded, then try the last one.
     333            * Should be good enough. If we've never seen one, then
     334            * guess.        
     335            */
     336            if (lastScaledImage != null) {
     337                return getImageScaling(lastScaledImage, p0, p1);
     338            }
     339            realWidth = 256;
     340            realHeight = 256;
     341        } else {
     342            lastScaledImage = img;
     343        }
     344        /*
     345        * If the zoom scale gets really, really off, these can get into
     346        * the millions, so make this a double to prevent integer
     347        * overflows.        
     348        */
     349        double drawWidth = p1.x - p0.x;
     350        double drawHeight = p1.x - p0.x;
     351        // stem.out.println("drawWidth: " + drawWidth + " drawHeight: " +
     352        // drawHeight);
     353
     354        double drawArea = drawWidth * drawHeight;
    355355        double realArea = realWidth * realHeight;
    356356
     
    358358    }
    359359
    360         boolean imageLoaded(Image i) {
    361                 if (i == null)
    362                         return false;
    363 
    364                 int status = Toolkit.getDefaultToolkit().checkImage(i, -1, -1, this);
    365                 if ((status & ALLBITS) != 0)
    366                         return true;
    367                 return false;
    368         }
    369 
    370         Double paintTileImages(Graphics g, TileSet ts, int zoom) {
    371                 Double imageScale = null;
    372                 Image img = null;
    373                 for (Tile t : ts.allTiles()) {
    374                         SlippyMapTile tile = getTile(t.x, t.y, zoom);
    375                         if (tile == null) {
    376                                 // Don't trigger tile loading if this isn't
    377                                 // the exact zoom level we're looking for
    378                                 if (zoom != currentZoomLevel)
    379                                         continue;
    380                                 tile = getOrCreateTile(t.x, t.y, zoom);
    381                                 if (SlippyMapPreferences.getAutoloadTiles())
    382                                         loadSingleTile(tile);
    383                         }
    384                         img = tile.getImage();
    385                         if (img == null)
    386                                 continue;
    387                         if ((zoom != currentZoomLevel) && !tile.isDownloaded())
    388                                 continue;
    389                         Point p = t.pixelPos(zoom);
    390                         /*
    391                         * We need to get a box in which to draw, so advance by one tile in
    392                         * each direction to find the other corner of the box
    393                         */
    394                         Tile t2 = new Tile(t.x + 1, t.y + 1);
    395                         Point p2 = t2.pixelPos(zoom);
    396 
    397                         g.drawImage(img, p.x, p.y, p2.x - p.x, p2.y - p.y, this);
    398                         if (img == null)
    399                                 continue;
    400                         if (imageScale == null && zoom == currentZoomLevel)
    401                                 imageScale = new Double(getImageScaling(img, p, p2));
    402                         float fadeBackground = SlippyMapPreferences.getFadeBackground();
    403                         if (fadeBackground != 0f) {
    404                                 // dimm by painting opaque rect...
    405                                 g.setColor(new Color(1f, 1f, 1f, fadeBackground));
    406                                 g.fillRect(p.x, p.y, p2.x - p.x, p2.y - p.y);
    407                         }
    408                 }// end of for
    409                 return imageScale;
    410         }
    411 
    412         void paintTileText(TileSet ts, Graphics g, MapView mv, int zoom, Tile t) {
    413                 int fontHeight = g.getFontMetrics().getHeight();
    414 
    415                 SlippyMapTile tile = getTile(t.x, t.y, zoom);
    416                 if (tile == null) {
    417                         return;
    418                 }
    419                 if (tile.getImage() == null) {
    420                         loadSingleTile(tile);
    421                 }
    422                 Point p = t.pixelPos(zoom);
    423                 int texty = p.y + 2 + fontHeight;
    424 
    425                 if (SlippyMapPreferences.getDrawDebug()) {
    426                         g.drawString("x=" + t.x + " y=" + t.y + " z=" + zoom + "", p.x + 2, texty);
    427                         texty += 1 + fontHeight;
    428                         if ((t.x % 32 == 0) && (t.y % 32 == 0)) {
    429                                 g.drawString("x=" + t.x / 32 + " y=" + t.y / 32 + " z=7", p.x + 2, texty);
    430                                 texty += 1 + fontHeight;
    431                         }
    432                 }// end of if draw debug
    433 
    434                 String md = tile.getMetadata();
    435                 if (md != null) {
    436                         g.drawString(md, p.x + 2, texty);
    437                         texty += 1 + fontHeight;
    438                 }
    439 
    440                 Image tileImage = tile.getImage();
    441                 if (tileImage == null) {
    442                         g.drawString(tr("image not loaded"), p.x + 2, texty);
    443                         texty += 1 + fontHeight;
    444                 }
    445                 if (!imageLoaded(tileImage)) {
    446                         g.drawString(tr("image loading..."), p.x + 2, texty);
    447                         needRedraw = true;
    448                         Main.map.repaint(100);
    449                         texty += 1 + fontHeight;
    450                 }
    451 
    452                 if (SlippyMapPreferences.getDrawDebug()) {
    453                         if (ts.leftTile(t)) {
    454                                 if (t.y % 32 == 31) {
    455                                         g.fillRect(0, p.y - 1, mv.getWidth(), 3);
    456                                 } else {
    457                                         g.drawLine(0, p.y, mv.getWidth(), p.y);
    458                                 }
    459                         }
    460                 }// /end of if draw debug
    461         }
    462        
    463         private class Tile {
    464                 public int x;
    465                 public int y;
    466 
    467                 Tile(int x, int y) {
    468                         this.x = x;
    469                         this.y = y;
    470                 }
    471 
    472                 public Point pixelPos(int zoom) {
    473                         double lon = tileXToLon(this.x, zoom);
    474                         LatLon tmpLL = new LatLon(tileYToLat(this.y, zoom), lon);
    475                         MapView mv = Main.map.mapView;
    476                         return mv.getPoint(Main.proj.latlon2eastNorth(tmpLL));
    477                 }
    478         }
    479 
    480         private class TileSet {
    481                 int z12x0, z12x1, z12y0, z12y1;
    482                 int zoom;
    483 
    484                 TileSet(LatLon topLeft, LatLon botRight, int zoom) {
    485                         this.zoom = zoom;
    486                         z12x0 = lonToTileX(topLeft.lon(), zoom);
    487                         z12x1 = lonToTileX(botRight.lon(), zoom);
    488                         z12y0 = latToTileY(topLeft.lat(), zoom);
    489                         z12y1 = latToTileY(botRight.lat(), zoom);
    490                         if (z12x0 > z12x1) {
    491                                 int tmp = z12x0;
    492                                 z12x0 = z12x1;
    493                                 z12x1 = tmp;
    494                         }
    495                         if (z12y0 > z12y1) {
    496                                 int tmp = z12y0;
    497                                 z12y0 = z12y1;
    498                                 z12y1 = tmp;
    499                         }
    500                 }
    501 
    502                 int tilesSpanned() {
    503                         int x_span = z12x1 - z12x0;
    504                         int y_span = z12y1 - z12y0;
    505                         return x_span * y_span;
    506                 }
    507 
    508                 /*
    509                 * This is pretty silly. Should probably just be implemented as an
    510                 * iterator to keep from having to instantiate all these tiles.
    511                 */
    512                 List<Tile> allTiles()
     360    boolean imageLoaded(Image i) {
     361        if (i == null)
     362            return false;
     363
     364        int status = Toolkit.getDefaultToolkit().checkImage(i, -1, -1, this);
     365        if ((status & ALLBITS) != 0)
     366            return true;
     367        return false;
     368    }
     369
     370    Double paintTileImages(Graphics g, TileSet ts, int zoom) {
     371        Double imageScale = null;
     372        Image img = null;
     373        for (Tile t : ts.allTiles()) {
     374            SlippyMapTile tile = getTile(t.x, t.y, zoom);
     375            if (tile == null) {
     376                // Don't trigger tile loading if this isn't
     377                // the exact zoom level we're looking for
     378                if (zoom != currentZoomLevel)
     379                    continue;
     380                tile = getOrCreateTile(t.x, t.y, zoom);
     381                if (SlippyMapPreferences.getAutoloadTiles())
     382                    loadSingleTile(tile);
     383            }
     384            img = tile.getImage();
     385            if (img == null)
     386                continue;
     387            if ((zoom != currentZoomLevel) && !tile.isDownloaded())
     388                continue;
     389            Point p = t.pixelPos(zoom);
     390            /*
     391            * We need to get a box in which to draw, so advance by one tile in
     392            * each direction to find the other corner of the box
     393            */
     394            Tile t2 = new Tile(t.x + 1, t.y + 1);
     395            Point p2 = t2.pixelPos(zoom);
     396
     397            g.drawImage(img, p.x, p.y, p2.x - p.x, p2.y - p.y, this);
     398            if (img == null)
     399                continue;
     400            if (imageScale == null && zoom == currentZoomLevel)
     401                imageScale = new Double(getImageScaling(img, p, p2));
     402            float fadeBackground = SlippyMapPreferences.getFadeBackground();
     403            if (fadeBackground != 0f) {
     404                // dimm by painting opaque rect...
     405                g.setColor(new Color(1f, 1f, 1f, fadeBackground));
     406                g.fillRect(p.x, p.y, p2.x - p.x, p2.y - p.y);
     407            }
     408        }// end of for
     409        return imageScale;
     410    }
     411
     412    void paintTileText(TileSet ts, Graphics g, MapView mv, int zoom, Tile t) {
     413        int fontHeight = g.getFontMetrics().getHeight();
     414
     415        SlippyMapTile tile = getTile(t.x, t.y, zoom);
     416        if (tile == null) {
     417            return;
     418        }
     419        if (tile.getImage() == null) {
     420            loadSingleTile(tile);
     421        }
     422        Point p = t.pixelPos(zoom);
     423        int texty = p.y + 2 + fontHeight;
     424
     425        if (SlippyMapPreferences.getDrawDebug()) {
     426            g.drawString("x=" + t.x + " y=" + t.y + " z=" + zoom + "", p.x + 2, texty);
     427            texty += 1 + fontHeight;
     428            if ((t.x % 32 == 0) && (t.y % 32 == 0)) {
     429                g.drawString("x=" + t.x / 32 + " y=" + t.y / 32 + " z=7", p.x + 2, texty);
     430                texty += 1 + fontHeight;
     431            }
     432        }// end of if draw debug
     433
     434        String md = tile.getMetadata();
     435        if (md != null) {
     436            g.drawString(md, p.x + 2, texty);
     437            texty += 1 + fontHeight;
     438        }
     439
     440        Image tileImage = tile.getImage();
     441        if (tileImage == null) {
     442            g.drawString(tr("image not loaded"), p.x + 2, texty);
     443            texty += 1 + fontHeight;
     444        }
     445        if (!imageLoaded(tileImage)) {
     446            g.drawString(tr("image loading..."), p.x + 2, texty);
     447            needRedraw = true;
     448            Main.map.repaint(100);
     449            texty += 1 + fontHeight;
     450        }
     451
     452        if (SlippyMapPreferences.getDrawDebug()) {
     453            if (ts.leftTile(t)) {
     454                if (t.y % 32 == 31) {
     455                    g.fillRect(0, p.y - 1, mv.getWidth(), 3);
     456                } else {
     457                    g.drawLine(0, p.y, mv.getWidth(), p.y);
     458                }
     459            }
     460        }// /end of if draw debug
     461    }
     462
     463    private class Tile {
     464        public int x;
     465        public int y;
     466
     467        Tile(int x, int y) {
     468            this.x = x;
     469            this.y = y;
     470        }
     471
     472        public Point pixelPos(int zoom) {
     473            double lon = tileXToLon(this.x, zoom);
     474            LatLon tmpLL = new LatLon(tileYToLat(this.y, zoom), lon);
     475            MapView mv = Main.map.mapView;
     476            return mv.getPoint(tmpLL);
     477        }
     478    }
     479
     480    private class TileSet {
     481        int z12x0, z12x1, z12y0, z12y1;
     482        int zoom;
     483
     484        TileSet(LatLon topLeft, LatLon botRight, int zoom) {
     485            this.zoom = zoom;
     486            z12x0 = lonToTileX(topLeft.lon(), zoom);
     487            z12x1 = lonToTileX(botRight.lon(), zoom);
     488            z12y0 = latToTileY(topLeft.lat(), zoom);
     489            z12y1 = latToTileY(botRight.lat(), zoom);
     490            if (z12x0 > z12x1) {
     491                int tmp = z12x0;
     492                z12x0 = z12x1;
     493                z12x1 = tmp;
     494            }
     495            if (z12y0 > z12y1) {
     496                int tmp = z12y0;
     497                z12y0 = z12y1;
     498                z12y1 = tmp;
     499            }
     500        }
     501
     502        int tilesSpanned() {
     503            int x_span = z12x1 - z12x0;
     504            int y_span = z12y1 - z12y0;
     505            return x_span * y_span;
     506        }
     507
     508        /*
     509        * This is pretty silly. Should probably just be implemented as an
     510        * iterator to keep from having to instantiate all these tiles.
     511        */
     512        List<Tile> allTiles()
    513513        {
    514514            List<Tile> ret = new ArrayList<Tile>();
     
    526526        }
    527527
    528                 boolean topTile(Tile t) {
    529                         if (t.y == z12y0 - 1)
    530                                 return true;
    531                         return false;
    532                 }
    533 
    534                 boolean leftTile(Tile t) {
    535                         if (t.x == z12x0 - 1)
    536                                 return true;
    537                         return false;
    538                 }
    539         }
    540        
     528        boolean topTile(Tile t) {
     529            if (t.y == z12y0 - 1)
     530                return true;
     531            return false;
     532        }
     533
     534        boolean leftTile(Tile t) {
     535            if (t.x == z12x0 - 1)
     536                return true;
     537            return false;
     538        }
     539    }
     540
    541541    /**
    542542     */
    543543    @Override
    544544    public void paint(Graphics g, MapView mv) {
    545                 long start = System.currentTimeMillis();
    546                 LatLon topLeft = mv.getLatLon(0, 0);
    547                 LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
    548                 Graphics oldg = g;
    549 
    550                 if (botRight.lon() == 0.0 || botRight.lat() == 0) {
    551                         // probably still initializing
    552                         return;
    553                 }
    554                 if (lastTopLeft != null && lastBotRight != null && topLeft.equalsEpsilon(lastTopLeft)
    555                                 && botRight.equalsEpsilon(lastBotRight) && bufferImage != null
    556                                 && mv.getWidth() == bufferImage.getWidth(null) && mv.getHeight() == bufferImage.getHeight(null)
    557                                 && !needRedraw) {
    558 
    559                         g.drawImage(bufferImage, 0, 0, null);
    560                         return;
    561                 }
    562 
    563                 needRedraw = false;
    564                 lastTopLeft = topLeft;
    565                 lastBotRight = botRight;
    566                 bufferImage = mv.createImage(mv.getWidth(), mv.getHeight());
    567                 g = bufferImage.getGraphics();
    568 
    569                 TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
    570                 int zoom = currentZoomLevel;
    571 
    572                 if (ts.tilesSpanned() > (18*18)) {
    573                         System.out.println("too many tiles, decreasing zoom from " + currentZoomLevel);
    574                         if (decreaseZoomLevel()) {
    575                                 this.paint(oldg, mv);
    576                         }
    577                         return;
    578                 }//end of if more than 18*18
    579                
    580                 if (ts.tilesSpanned() <= 0) {
    581                         System.out.println("doesn't even cover one tile, increasing zoom from " + currentZoomLevel);
    582                         if (increaseZoomLevel()) {
    583                                 this.paint(oldg, mv);
    584                         }
    585                         return;
    586                 }
    587 
    588                 int fontHeight = g.getFontMetrics().getHeight();
    589 
    590                 g.setColor(Color.DARK_GRAY);
    591 
    592                 float fadeBackground = SlippyMapPreferences.getFadeBackground();
    593 
    594                 /*
    595                   * Go looking for tiles in zoom levels *other* than the current
    596                   * one.  Even if they might look bad, they look better than a
    597                   *  blank tile.
    598                   */
    599                 int otherZooms[] = {2, -2, 1, -1};
    600                 for (int zoomOff : otherZooms) {
    601                         int zoom2 = currentZoomLevel + zoomOff;
    602                         if ((zoom <= 0) || (zoom > SlippyMapPreferences.getMaxZoomLvl())) {
    603                                 continue;
    604                         }
    605                         TileSet ts2 = new TileSet(topLeft, botRight, zoom2);
    606                         this.paintTileImages(g, ts2, zoom2);
    607                         }
    608                 /*
    609                 * Save this for last since it will get painted over all the others
    610                 */
    611                 Double imageScale = this.paintTileImages(g, ts, currentZoomLevel);
    612                 g.setColor(Color.red);
    613                
    614                 for (Tile t : ts.allTiles()) {
    615                         // This draws the vertical lines for the entire
    616                         // column.  Only draw them for the top tile in
    617                         // the column.
    618                         if (ts.topTile(t)) {
    619                                 Point p = t.pixelPos(currentZoomLevel);
    620                                 if (SlippyMapPreferences.getDrawDebug()) {
    621                                         if (t.x % 32 == 0) {
    622                                                 // level 7 tile boundary
    623                                                 g.fillRect(p.x - 1, 0, 3, mv.getHeight());
    624                                         } else {
    625                                                 g.drawLine(p.x, 0, p.x, mv.getHeight());
    626                                         }
    627                                 }
    628                         }
    629                         this.paintTileText(ts, g, mv, currentZoomLevel, t);
    630                 }
    631 
    632                 oldg.drawImage(bufferImage, 0, 0, null);
    633 
    634                 if (imageScale != null) {
    635                         // If each source image pixel is being stretched into > 3
    636                         // drawn pixels, zoom in... getting too pixelated
    637                         if (imageScale > 3) {
    638                                 if (SlippyMapPreferences.getAutozoom()) {
    639                                         Main.debug("autozoom increase: scale: " + imageScale);
    640                                         increaseZoomLevel();
    641                                 }
    642                                 this.paint(oldg, mv);
    643                         }
    644 
    645                         // If each source image pixel is being squished into > 0.32
    646                         // of a drawn pixels, zoom out.
    647                         if (imageScale < 0.32) {
    648                                 if (SlippyMapPreferences.getAutozoom()) {
    649                                         Main.debug("autozoom decrease: scale: " + imageScale);
    650                                         decreaseZoomLevel();
    651                                 }
    652                                 this.paint(oldg, mv);
    653                         }
    654                 }
    655                 g.setColor(Color.black);
    656                 g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120);
    657         }// end of paint method
     545        long start = System.currentTimeMillis();
     546        LatLon topLeft = mv.getLatLon(0, 0);
     547        LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
     548        Graphics oldg = g;
     549
     550        if (botRight.lon() == 0.0 || botRight.lat() == 0) {
     551            // probably still initializing
     552            return;
     553        }
     554        if (lastTopLeft != null && lastBotRight != null && topLeft.equalsEpsilon(lastTopLeft)
     555                && botRight.equalsEpsilon(lastBotRight) && bufferImage != null
     556                && mv.getWidth() == bufferImage.getWidth(null) && mv.getHeight() == bufferImage.getHeight(null)
     557                && !needRedraw) {
     558
     559            g.drawImage(bufferImage, 0, 0, null);
     560            return;
     561        }
     562
     563        needRedraw = false;
     564        lastTopLeft = topLeft;
     565        lastBotRight = botRight;
     566        bufferImage = mv.createImage(mv.getWidth(), mv.getHeight());
     567        g = bufferImage.getGraphics();
     568
     569        TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
     570        int zoom = currentZoomLevel;
     571
     572        if (ts.tilesSpanned() > (18*18)) {
     573            System.out.println("too many tiles, decreasing zoom from " + currentZoomLevel);
     574            if (decreaseZoomLevel()) {
     575                this.paint(oldg, mv);
     576            }
     577            return;
     578        }//end of if more than 18*18
     579
     580        if (ts.tilesSpanned() <= 0) {
     581            System.out.println("doesn't even cover one tile, increasing zoom from " + currentZoomLevel);
     582            if (increaseZoomLevel()) {
     583                this.paint(oldg, mv);
     584            }
     585            return;
     586        }
     587
     588        int fontHeight = g.getFontMetrics().getHeight();
     589
     590        g.setColor(Color.DARK_GRAY);
     591
     592        float fadeBackground = SlippyMapPreferences.getFadeBackground();
     593
     594        /*
     595          * Go looking for tiles in zoom levels *other* than the current
     596          * one.  Even if they might look bad, they look better than a
     597          *  blank tile.
     598          */
     599        int otherZooms[] = {2, -2, 1, -1};
     600        for (int zoomOff : otherZooms) {
     601            int zoom2 = currentZoomLevel + zoomOff;
     602            if ((zoom <= 0) || (zoom > SlippyMapPreferences.getMaxZoomLvl())) {
     603                continue;
     604            }
     605            TileSet ts2 = new TileSet(topLeft, botRight, zoom2);
     606            this.paintTileImages(g, ts2, zoom2);
     607            }
     608        /*
     609        * Save this for last since it will get painted over all the others
     610        */
     611        Double imageScale = this.paintTileImages(g, ts, currentZoomLevel);
     612        g.setColor(Color.red);
     613
     614        for (Tile t : ts.allTiles()) {
     615            // This draws the vertical lines for the entire
     616            // column.  Only draw them for the top tile in
     617            // the column.
     618            if (ts.topTile(t)) {
     619                Point p = t.pixelPos(currentZoomLevel);
     620                if (SlippyMapPreferences.getDrawDebug()) {
     621                    if (t.x % 32 == 0) {
     622                        // level 7 tile boundary
     623                        g.fillRect(p.x - 1, 0, 3, mv.getHeight());
     624                    } else {
     625                        g.drawLine(p.x, 0, p.x, mv.getHeight());
     626                    }
     627                }
     628            }
     629            this.paintTileText(ts, g, mv, currentZoomLevel, t);
     630        }
     631
     632        oldg.drawImage(bufferImage, 0, 0, null);
     633
     634        if (imageScale != null) {
     635            // If each source image pixel is being stretched into > 3
     636            // drawn pixels, zoom in... getting too pixelated
     637            if (imageScale > 3) {
     638                if (SlippyMapPreferences.getAutozoom()) {
     639                    Main.debug("autozoom increase: scale: " + imageScale);
     640                    increaseZoomLevel();
     641                }
     642                this.paint(oldg, mv);
     643            }
     644
     645            // If each source image pixel is being squished into > 0.32
     646            // of a drawn pixels, zoom out.
     647            if (imageScale < 0.32) {
     648                if (SlippyMapPreferences.getAutozoom()) {
     649                    Main.debug("autozoom decrease: scale: " + imageScale);
     650                    decreaseZoomLevel();
     651                }
     652                this.paint(oldg, mv);
     653            }
     654        }
     655        g.setColor(Color.black);
     656        g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120);
     657    }// end of paint method
    658658
    659659    /**
     
    662662     */
    663663    SlippyMapTile getTileForPixelpos(int px, int py) {
    664         MapView mv = Main.map.mapView;
    665         Point clicked = new Point(px, py);
    666         LatLon topLeft = mv.getLatLon(0, 0);
    667         LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
    668         TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
    669         int z = currentZoomLevel;
    670        
    671         Tile clickedTile = null;
     664        MapView mv = Main.map.mapView;
     665        Point clicked = new Point(px, py);
     666        LatLon topLeft = mv.getLatLon(0, 0);
     667        LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
     668        TileSet ts = new TileSet(topLeft, botRight, currentZoomLevel);
     669        int z = currentZoomLevel;
     670
     671        Tile clickedTile = null;
    672672        for (Tile t1 : ts.allTiles()) {
    673673            Tile t2 = new Tile(t1.x+1, t1.y+1);
     
    735735
    736736    private int lonToTileX(double lon, int zoom) {
    737         return (int) (Math.pow(2.0, zoom - 3) * (lon + 180.0) / 45.0);
     737        return (int) (Math.pow(2.0, zoom - 3) * (lon + 180.0) / 45.0);
    738738    }
    739739
    740740    private double tileYToLat(int y, int zoom) {
    741741        return Math.atan(Math.sinh(Math.PI
    742                         - (Math.PI * y / Math.pow(2.0, zoom - 1))))
     742                - (Math.PI * y / Math.pow(2.0, zoom - 1))))
    743743                * 180 / Math.PI;
    744744    }
    745745
    746746    private double tileXToLon(int x, int zoom) {
    747         return x * 45.0 / Math.pow(2.0, zoom - 3) - 180.0;
     747        return x * 45.0 / Math.pow(2.0, zoom - 3) - 180.0;
    748748    }
    749749
    750750    private SlippyMapTile imgToTile(Image img) {
    751                 // we use the enumeration to avoid ConcurrentUpdateExceptions
    752                 // with other users of the tileStorage
    753                 Enumeration<SlippyMapTile> e = tileStorage.elements();
    754                 while (e.hasMoreElements()) {
    755                         SlippyMapTile t = e.nextElement();
    756                         if (t.getImageNoTimestamp() != img) {
    757                                 continue;
    758                         }
    759                         return t;
    760                 }
    761                 return null;
    762         }
    763    
     751        // we use the enumeration to avoid ConcurrentUpdateExceptions
     752        // with other users of the tileStorage
     753        Enumeration<SlippyMapTile> e = tileStorage.elements();
     754        while (e.hasMoreElements()) {
     755            SlippyMapTile t = e.nextElement();
     756            if (t.getImageNoTimestamp() != img) {
     757                continue;
     758            }
     759            return t;
     760        }
     761        return null;
     762    }
     763
    764764    private static int nr_loaded = 0;
    765765    private static int at_zoom = -1;
    766    
     766
    767767    public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
    768                 boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0);
    769                 SlippyMapTile imageTile = imgToTile(img);
    770                 if (imageTile == null) {
    771                         return false;
    772                 }
    773 
    774                 if ((infoflags & ERROR) != 0) {
    775                         String url; // = "unknown";
    776                         url = imageTile.getImageURL().toString();
    777                         System.err.println("imageUpdate(" + img + ") error " + url + ")");
    778                 }
    779                 if (((infoflags & ALLBITS) != 0)) {
    780                         int z = imageTile.getZoom();
    781                         if (z == at_zoom) {
    782                                 nr_loaded++;
    783                         } else {
    784                                 System.out.println("downloaded " + nr_loaded + " at: " + at_zoom + " now going to " + z);
    785                                 nr_loaded = 0;
    786                                 at_zoom = z;
    787                         }
    788                         imageTile.markAsDownloaded();
    789                 }
    790                 if ((infoflags & SOMEBITS) != 0) {
    791                         // if (y%100 == 0)
    792                         //System.out.println("imageUpdate("+img+") SOMEBITS ("+x+","+y+")");
    793                 }
    794                 // Repaint immediately if we are done, otherwise batch up
    795                 // repaint requests every 100 milliseconds
    796                 needRedraw = true;
    797                 Main.map.repaint(done ? 0 : 100);
    798                 return !done;
    799         }
     768        boolean done = ((infoflags & (ERROR | FRAMEBITS | ALLBITS)) != 0);
     769        SlippyMapTile imageTile = imgToTile(img);
     770        if (imageTile == null) {
     771            return false;
     772        }
     773
     774        if ((infoflags & ERROR) != 0) {
     775            String url; // = "unknown";
     776            url = imageTile.getImageURL().toString();
     777            System.err.println("imageUpdate(" + img + ") error " + url + ")");
     778        }
     779        if (((infoflags & ALLBITS) != 0)) {
     780            int z = imageTile.getZoom();
     781            if (z == at_zoom) {
     782                nr_loaded++;
     783            } else {
     784                System.out.println("downloaded " + nr_loaded + " at: " + at_zoom + " now going to " + z);
     785                nr_loaded = 0;
     786                at_zoom = z;
     787            }
     788            imageTile.markAsDownloaded();
     789        }
     790        if ((infoflags & SOMEBITS) != 0) {
     791            // if (y%100 == 0)
     792            //System.out.println("imageUpdate("+img+") SOMEBITS ("+x+","+y+")");
     793        }
     794        // Repaint immediately if we are done, otherwise batch up
     795        // repaint requests every 100 milliseconds
     796        needRedraw = true;
     797        Main.map.repaint(done ? 0 : 100);
     798        return !done;
     799    }
    800800
    801801    /*
Note: See TracChangeset for help on using the changeset viewer.