Ignore:
Timestamp:
2009-06-26T22:30:49+02:00 (15 years ago)
Author:
lvarga
Message:

Last two patches. Read previous logs.

Same drill, this time with the tile debugging text code.
This makes the paint() function a *ton* smaller and
much easier to read.

This moves over the last user of the class variables:
z12x0, z12x1, z12y0, z12y1 and removes them.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapLayer.java

    r16161 r16164  
    1010import java.awt.Point;
    1111import java.awt.Rectangle;
     12import java.awt.Toolkit;
    1213import java.awt.event.ActionEvent;
    1314import java.awt.event.MouseAdapter;
     
    5960    LatLon lastTopLeft;
    6061    LatLon lastBotRight;
    61     int z12x0, z12x1, z12y0, z12y1;
    6262    private Image bufferImage;
    6363    private SlippyMapTile clickedTile;
     
    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       
    360463        private class Tile {
    361464                public int x;
     
    440543    @Override
    441544    public void paint(Graphics g, MapView mv) {
    442         long start = System.currentTimeMillis();
    443         LatLon topLeft = mv.getLatLon(0, 0);
    444         LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
    445         Graphics oldg = g;
    446 
    447         if (botRight.lon() == 0.0 || botRight.lat() == 0) {
    448                 // probably still initializing
    449                 return;
    450         }
    451         if (lastTopLeft != null && lastBotRight != null
    452                 && topLeft.equalsEpsilon(lastTopLeft)
    453                 && botRight.equalsEpsilon(lastBotRight) && bufferImage != null
    454                 && mv.getWidth() == bufferImage.getWidth(null)
    455                 && mv.getHeight() == bufferImage.getHeight(null) && !needRedraw) {
    456 
    457             g.drawImage(bufferImage, 0, 0, null);
    458             return;
    459         }
    460 
    461         needRedraw = false;
    462         lastTopLeft = topLeft;
    463         lastBotRight = botRight;
    464         bufferImage = mv.createImage(mv.getWidth(), mv.getHeight());
    465         g = bufferImage.getGraphics();
    466 
    467         int zoom = currentZoomLevel;
    468         z12x0 = lonToTileX(topLeft.lon(), zoom);
    469         z12x1 = lonToTileX(botRight.lon(), zoom);
    470         z12y0 = latToTileY(topLeft.lat(), zoom);
    471         z12y1 = latToTileY(botRight.lat(), zoom);
    472 
    473         if (z12x0 > z12x1) {
    474             int tmp = z12x0;
    475             z12x0 = z12x1;
    476             z12x1 = tmp;
    477         }
    478         if (z12y0 > z12y1) {
    479             int tmp = z12y0;
    480             z12y0 = z12y1;
    481             z12y1 = tmp;
    482         }
    483 
    484         if (z12x1 - z12x0 > 18)
    485             return;
    486         if (z12y1 - z12y0 > 18)
    487             return;
    488 
    489         for (int x = z12x0 - 1; x <= z12x1 + 1; x++) {
    490             double lon = tileXToLon(x, zoom);
    491             for (int y = z12y0 - 1; y <= z12y1 + 1; y++) {
    492                 LatLon tmpLL = new LatLon(tileYToLat(y, zoom), lon);
    493                 pixelpos[x - z12x0 + 1][y - z12y0 + 1] = mv.getPoint(Main.proj
    494                         .latlon2eastNorth(tmpLL));
    495             }
    496         }
    497 
    498         int fontHeight = g.getFontMetrics().getHeight();
    499 
    500         g.setColor(Color.DARK_GRAY);
    501 
    502         float fadeBackground = SlippyMapPreferences.getFadeBackground();
    503 
    504         Double imageScale = null;
    505         int count = 0;
    506         for (int x = z12x0 - 1; x <= z12x1; x++) {
    507             for (int y = z12y0 - 1; y <= z12y1; y++) {
    508                 SlippyMapKey key = new SlippyMapKey(x, y, currentZoomLevel);
    509                 SlippyMapTile tile;
    510                 tile = tileStorage.get(key);
    511                 if (!key.valid) {
    512                         System.out.println("loadAllTiles() made invalid key");
    513                         continue;
    514                 }
    515                 if (tile == null) {
    516                     tile = new SlippyMapTile(x, y, currentZoomLevel);
    517                     tileStorage.put(key, tile);
    518                     if (SlippyMapPreferences.getAutoloadTiles()) {
    519                         // TODO probably do on background
    520                         loadSingleTile(tile);
    521                     }
    522                     checkTileStorage();
    523                 }
    524                 Image img = tile.getImage();
    525 
    526                 if (img != null) {
    527                     Point p = pixelpos[x - z12x0 + 1][y - z12y0 + 1];
    528                     Point p2 = pixelpos[x - z12x0 + 2][y - z12y0 + 2];
    529                     g.drawImage(img, p.x, p.y, p2.x - p.x, p2.y - p.y, this);
    530                     if (imageScale == null)
    531                         imageScale = new Double(getImageScaling(img, p, p2));
    532                     count++;
    533                     if (fadeBackground != 0f) {
    534                         // dimm by painting opaque rect...
    535                         g.setColor(new Color(1f, 1f, 1f, fadeBackground));
    536                         g.fillRect(p.x, p.y, p2.x - p.x, p2.y - p.y);
    537                     }// end of if dim != 0
    538                 }// end of if img != null
    539             }// end of for
    540         }// end of for
    541         g.setColor(Color.red);
    542         for (int x = z12x0 - 1; x <= z12x1; x++) {
    543             Point p = pixelpos[x - z12x0 + 1][0];
    544 
    545             if(SlippyMapPreferences.getDrawDebug()) {
    546                 if (x % 32 == 0) {
    547                     // level 7 tile boundary
    548                     g.fillRect(p.x - 1, 0, 3, mv.getHeight());
    549                 } else {
    550                     g.drawLine(p.x, 0, p.x, mv.getHeight());
    551                 }
    552             }//end of if draw debug
    553 
    554             for (int y = z12y0 - 1; y <= z12y1; y++) {
    555                 SlippyMapKey key = new SlippyMapKey(currentZoomLevel, x, y);
    556                 int texty = p.y + 2 + fontHeight;
    557                 SlippyMapTile tile = tileStorage.get(key);
    558                 if (tile == null) {
    559                     continue;
    560                 }
    561                 if (!key.valid) {
    562                         System.out.println("paint-0() made invalid key");
    563                         continue;
    564                 }
    565                 if (tile.getImage() == null) {
    566                         loadSingleTile(tile);
    567                 }
    568                 p = pixelpos[x - z12x0 + 1][y - z12y0 + 2];
    569 
    570                 if(SlippyMapPreferences.getDrawDebug()) {
    571                     g.drawString("x=" + x + " y=" + y + " z=" + currentZoomLevel
    572                             + "", p.x + 2, texty);
    573                     texty += 1 + fontHeight;
    574                     if ((x % 32 == 0) && (y % 32 == 0)) {
    575                         g.drawString("x=" + x / 32 + " y=" + y / 32 + " z=7",
    576                                 p.x + 2, texty);
    577                         texty += 1 + fontHeight;
    578                     }
    579                 }//end of if draw debug
    580 
    581                 String md = tile.getMetadata();
    582                 if (md != null) {
    583                     g.drawString(md, p.x + 2, texty);
    584                     texty += 1 + fontHeight;
    585                 }
    586 
    587                 if (tile.getImage() == null) {
    588                     g.drawString(tr("image not loaded"), p.x + 2, texty);
    589                     texty += 1 + fontHeight;
    590                 }
    591 
    592                 if(SlippyMapPreferences.getDrawDebug()) {
    593                     if (x == z12x0 - 1) {
    594                         if (y % 32 == 31) {
    595                             g.fillRect(0, p.y - 1, mv.getWidth(), 3);
    596                         } else {
    597                             g.drawLine(0, p.y, mv.getWidth(), p.y);
    598                         }
    599                     }
    600                 }//end of if draw debug
    601             } // end of for
    602         }
    603 
    604         oldg.drawImage(bufferImage, 0, 0, null);
    605 
    606         if (imageScale != null) {
    607             // If each source image pixel is being stretched into > 3
    608             // drawn pixels, zoom in... getting too pixelated
    609             if (imageScale > 3) {
    610                 if (SlippyMapPreferences.getAutozoom()) {
    611                     Main.debug("autozoom increase: "+z12x1+" " + z12x0 + " " + z12y1 + " " + z12y0
    612                                     + topLeft + " " + botRight + " scale: " + imageScale);
    613                     increaseZoomLevel();
    614                 }
    615                 this.paint(oldg, mv);
    616             }
    617 
    618             // If each source image pixel is being squished into > 0.32
    619             // of a drawn pixels, zoom out.
    620             if (imageScale < 0.32) {
    621                 if (SlippyMapPreferences.getAutozoom()) {
    622                     Main.debug("autozoom decrease: "+z12x1+" " + z12x0 + " " + z12y1 + " " + z12y0
    623                                     + topLeft + " " + botRight + " scale: " + imageScale);
    624                     decreaseZoomLevel();
    625                 }
    626                 this.paint(oldg, mv);
    627             }
    628         }
    629         g.setColor(Color.black);
    630         g.drawString("currentZoomLevel=" + currentZoomLevel, 120, 120);
    631     }// 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
    632658
    633659    /**
Note: See TracChangeset for help on using the changeset viewer.