Changeset 611

Show
Ignore:
Timestamp:
07/17/08 13:25:11 (2 months ago)
Author:
tom
Message:

breaking TileGrid's onRender function into modular pieces

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/tom-tweenlite/lib/com/modestmaps/core/TileGrid.as

    r609 r611  
    325325                        minRow -= tileBuffer; 
    326326                        maxRow += tileBuffer;  
    327                          
    328                         visibleTiles = []; 
     327 
     328                        // loop over all tiles and find parent or child tiles from cache to compensate for unloaded tiles:                       
     329                        repopulateVisibleTiles(minCol, maxCol, minRow, maxRow); 
     330 
     331                        // make absolutlely sure all our newly visible tiles are cached if they're done loading 
     332                        // TODO: should this happen onLoadEnd when there are no URLs left?  
     333                        for each (var visibleTile:Tile in visibleTiles) { 
     334                                // if we're done loading this one, add/move it to the end of recently seen: 
     335                                if (!layersNeeded[visibleTile]) { 
     336                                        var ri:int = recentlySeen.indexOf(visibleTile.name);  
     337                                        if (ri >= 0) { 
     338                                                recentlySeen.splice(ri, 1); 
     339                                        } 
     340                                        recentlySeen.push(visibleTile.name); 
     341                                }                                
     342                        } 
     343 
     344                        // prune tiles from the well if they shouldn't be there (not currently in visibleTiles) 
     345                        // (loop backwards so removal doesn't change i) 
     346                        for (var i:int = well.numChildren-1; i >= 0; i--) { 
     347                                var wellTile:Tile = well.getChildAt(i) as Tile; 
     348                                if (visibleTiles.indexOf(wellTile) < 0) { 
     349                                        well.removeChild(wellTile); 
     350                                        wellTile.hide(); 
     351                                        if (!tileCache.containsKey(wellTile.name)) { 
     352                                                //trace("destroying tile that was in the well but never cached"); 
     353                                                map.destroyTile(wellTile); 
     354                                        } 
     355                                } 
     356                        } 
     357 
     358                        // position tiles such that currentZoom is approximately scale 1 
     359                        // and x and y make sense in pixels relative to realMinCol and realMinRow, topleft 
     360                        positionTiles(realMinCol, realMinRow); 
     361 
     362                        // all the visible tiles will be at the end of recentlySeen 
     363                        // let's make sure we keep them around: 
     364                        var maxRecentlySeen:int = Math.max(visibleTiles.length, maxTilesToKeep); 
     365                         
     366                        // prune cache of already seen tiles if it's getting too big: 
     367                        if (recentlySeen.length > maxRecentlySeen) { 
     368 
     369                                // throw away keys at the beginning of recentlySeen 
     370                                recentlySeen = recentlySeen.slice(recentlySeen.length - maxRecentlySeen, recentlySeen.length); 
     371                                 
     372                                // loop over our internal tile cache  
     373                                // and throw out tiles not in recentlySeen 
     374                                tileCache.retainKeys(recentlySeen);  
     375                        } 
     376                         
     377                        // update center position:                       
     378                        centerRow = (realMaxRow+realMinRow)/2; 
     379                        centerColumn = (realMaxCol+realMinCol)/2.0; 
     380 
     381                        dirty = false; 
     382                } 
     383                 
     384                /** 
     385                 * loops over given cols and rows and adds tiles to visibleTiles array and the well 
     386                 * using child or parent tiles to compensate for tiles not yet available in the tileCache 
     387                 */ 
     388                private function repopulateVisibleTiles(minCol:int, maxCol:int, minRow:int, maxRow:int):void 
     389                { 
     390                        visibleTiles = [];  
     391                         
    329392                        blankCount = 0; // keep count of how many tiles we missed? 
    330393                 
    331394                        // for use in loops etc. 
    332                         var tile:Tile; 
    333                         var key:String; 
    334395                        var coord:Coordinate = new Coordinate(0,0,0); 
    335396 
     
    339400                                         
    340401                                        // create a string key for this tile 
    341                                         key = tileKey(col, row, currentZoom); 
     402                                        var key:String = tileKey(col, row, currentZoom); 
    342403                                         
    343404                                        // see if we already have this tile 
    344                                         tile = well.getChildByName(key) as Tile; 
     405                                        var tile:Tile = well.getChildByName(key) as Tile; 
    345406                                                                                 
    346407                                        // create it if not, and add it to the load queue 
     
    387448                                                var foundChildren:int = 0; 
    388449         
    389                                                 // for searching parents, reused further down too 
    390                                                 var pzoom:int; 
    391                                                 var pkey:String; 
    392                                                  
    393                                                 // for fixing row/cols so they're positioned at currentZoom 
    394                                                 var scaleFactor:Number; 
    395                                                  
    396450                                                if (currentZoom > previousZoom) { 
    397451                                                         
    398452                                                        // if it still doesn't have enough images yet, or it's fading in, try a double size parent instead 
    399453                                                        if (maxParentSearch > 0 && currentZoom > minZoom) { 
    400                                                                 pkey = parentKey(col, row, currentZoom, currentZoom-1); 
    401                                                                 if (ensureVisible(pkey)) { 
     454                                                                if (ensureVisible(parentKey(col, row, currentZoom, currentZoom-1))) { 
    402455                                                                        foundParent = true; 
    403456                                                                } 
     
    438491                                                // if it still doesn't have an image yet, try more parent zooms 
    439492                                                if (stillNeedsAnImage && maxParentSearch > 1 && currentZoom > minZoom) { 
    440                                                         for (pzoom = startZoomSearch; pzoom >= endZoomSearch; pzoom--) { 
    441                                                                 pkey = parentKey(col, row, currentZoom, pzoom); 
    442                                                                 if (ensureVisible(pkey)) {                                                               
     493                                                        for (var pzoom:int = startZoomSearch; pzoom >= endZoomSearch; pzoom--) { 
     494                                                                if (ensureVisible(parentKey(col, row, currentZoom, pzoom))) {                                                            
    443495                                                                        stillNeedsAnImage = false; 
    444496                                                                        break; 
     
    453505                                        } // if !tileReady 
    454506                                         
    455                                 } 
    456                         } 
    457  
    458 /*                      trace((maxCol - minCol + 1) * (maxRow - minRow + 1), "tiles visible at currentZoom"); 
    459                         trace(visibleTiles.length, " tiles visible"); */ 
    460  
    461                         // make absolutlely sure all our newly visible tiles are cached if they're done loading 
    462                         // TODO: this should probably happen onLoadEnd when there are no URLs left?  
    463                         for each (tile in visibleTiles) { 
    464                                 // if we're done loading this one, add/move it to the end of recently seen: 
    465                                 if (!layersNeeded[tile]) { 
    466                                         var ri:int = recentlySeen.indexOf(tile.name);  
    467                                         if (ri >= 0) { 
    468                                                 recentlySeen.splice(ri, 1); 
    469                                         } 
    470                                         recentlySeen.push(tile.name); 
    471                                 }                                
    472                         } 
    473  
    474                         // prune tiles from the well if they shouldn't be there (not currently in visibleTiles) 
    475                         // (loop backwards so removal doesn't change i) 
    476                         for (var i:int = well.numChildren-1; i >= 0; i--) { 
    477                                 tile = well.getChildAt(i) as Tile; 
    478                                 if (visibleTiles.indexOf(tile) < 0) { 
    479                                         well.removeChild(tile); 
    480                                         tile.hide(); 
    481                                         if (!tileCache.containsKey(tile.name)) { 
    482                                                 //trace("destroying tile that was in the well but never cached"); 
    483                                                 map.destroyTile(tile); 
    484                                         } 
    485                                 } 
    486                         } 
    487  
    488                         //trace(well.numChildren, " tiles in the well"); 
    489                                                  
     507                                } // for row 
     508                        } // for col 
     509                         
     510                } // repopulateVisibleTiles 
     511                 
     512                private function positionTiles(realMinCol:Number, realMinRow:Number):void 
     513                { 
    490514                        // sort children by difference from current zoom level 
    491515                        // this means current is on top, +1 and -1 are next, then +2 and -2, etc. 
     
    496520                         
    497521                        // apply the sorted depths, position all the tiles and also keep recentlySeen updated: 
    498                         for each (tile in visibleTiles) { 
     522                        for each (var tile:Tile in visibleTiles) { 
    499523                         
    500524                                // if we set them all to numChildren-1, descending, they should end up correctly sorted 
     
    502526 
    503527                                // position tile according to current transform 
    504                                 scaleFactor = Math.pow(2.0, currentZoom-tile.zoom); 
     528                                var scaleFactor:Number = Math.pow(2.0, currentZoom-tile.zoom); 
    505529                                var positionCol:Number = (scaleFactor*tile.column) - realMinCol; 
    506530                                var positionRow:Number = (scaleFactor*tile.row) - realMinRow; 
     
    511535                                tileScale = Math.ceil(tileScale * tileWidth) / tileWidth; // round up to the nearest pixel 
    512536                                tile.scaleX = tile.scaleY = tileScale; 
    513                         } 
    514                          
    515                         // all the visible tiles will be at the end of recentlySeen 
    516                         // let's make sure we keep them around: 
    517                         var maxRecentlySeen:int = Math.max(visibleTiles.length, maxTilesToKeep); 
    518                          
    519 /*                      trace("maxRecentlySeen is", maxRecentlySeen); 
    520                         trace("recentlySeen.length is", recentlySeen.length); */ 
    521                          
    522                         // prune cache of already seen tiles if it's getting too big: 
    523                         if (recentlySeen.length > maxRecentlySeen) { 
    524                                 //trace("... and that's too big");                               
    525  
    526                                 // throw away keys at the beginning of recentlySeen 
    527                                 recentlySeen = recentlySeen.slice(recentlySeen.length - maxRecentlySeen, recentlySeen.length); 
    528                                  
    529 /*                              trace("recentlySeen.length is now", recentlySeen.length); 
    530                                  
    531                                 trace("trimming cache from", tileCache.size); */ 
    532  
    533                                 // loop over our internal tile cache  
    534                                 // and throw out tiles not in recentlySeen 
    535                                 tileCache.retainKeys(recentlySeen);  
    536  
    537 //                              trace("to", tileCache.size); 
    538                         } 
    539                          
    540                         // update center position:                       
    541                         centerRow = (realMaxRow+realMinRow)/2; 
    542                         centerColumn = (realMaxCol+realMinCol)/2.0; 
    543  
    544                         dirty = false; 
     537                        }                        
    545538                } 
    546539                 
     
    764757                        return _invertedMatrix; 
    765758                } 
    766               protected function set invertedMatrix(m:Matrix):void 
     759/*            protected function set invertedMatrix(m:Matrix):void 
    767760                { 
    768761                        throw new Error("invertedMatrix is read only"); 
    769                 } 
     762                } */ 
    770763 
    771764                /** derived from map provider by calculateBounds(), read-only here for convenience */ 
     
    806799                        return _topLeftCoordinate; 
    807800                } 
    808               public function set topLeftCoordinate(c:Coordinate):void 
     801/*            public function set topLeftCoordinate(c:Coordinate):void 
    809802                { 
    810803                        throw new Error("topLeftCoordinate is read only"); 
    811                 } 
     804                } */ 
    812805 
    813806                public function get bottomRightCoordinate():Coordinate 
     
    819812                        return _bottomRightCoordinate; 
    820813                } 
    821               public function set bottomRightCoordinate(c:Coordinate):void 
     814/*            public function set bottomRightCoordinate(c:Coordinate):void 
    822815                { 
    823816                        throw new Error("bottomRightCoordinate is read only"); 
    824                 } 
     817                } */ 
    825818                                                 
    826819                // for backward compatibility: