Changeset 576

Show
Ignore:
Timestamp:
06/22/08 14:55:06 (5 months ago)
Author:
tom
Message:

fixed some bugs with caching in the tweening branch

Files:

Legend:

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

    r528 r576  
    2020                        this.row = row; 
    2121                        this.column = column; 
    22                          
    23                         this.name = column+":"+row+":"+zoom; 
    2422                         
    2523                        // otherwise you'll get seams between tiles :( 
  • branches/tom-tweenlite/lib/com/modestmaps/core/TileGrid.as

    r574 r576  
    1313        import flash.events.IOErrorEvent; 
    1414        import flash.events.MouseEvent; 
     15        import flash.geom.ColorTransform; 
    1516        import flash.geom.Matrix; 
    1617        import flash.geom.Point; 
     
    146147                        this.draggable = draggable; 
    147148                        this.provider = provider; 
     149 
     150                        trace("initializing tilegrid for", map.name); 
    148151 
    149152                        // from provider: 
     
    237240                } 
    238241                 
    239                 public function onRender(event:Event=null):void 
    240                 { 
    241                         if (!dirty) { 
     242                protected function onRender(event:Event=null):void 
     243                { 
     244                        if (!dirty || !stage) { 
    242245                                return; 
    243246                        } 
     
    316319                                                if (!tile) { 
    317320                                                        tile = map.createTile(col, row, currentZoom); 
     321                                                        tile.name = key; 
    318322                                                        coord.row = tile.row; 
    319323                                                        coord.column = tile.column; 
     
    331335                                        visibleTiles.push(tile); 
    332336 
     337                                        var tileReady:Boolean = !(tile.alpha < 1 || layersNeeded[tile]); 
    333338                                        var foundParent:Boolean = false; 
    334339                                        var foundChildren:int = 0; 
     
    345350                                                 
    346351                                                // if it still doesn't have enough images yet, or it's fading in, try a double size parent instead 
    347                                                 if (MAX_PARENT_SEARCH > 0 && (tile.alpha < 1 || layersNeeded[tile]) && currentZoom > minZoom) { 
     352                                                if (MAX_PARENT_SEARCH > 0 && !tileReady && currentZoom > minZoom) { 
    348353                                                        pkey = parentKey(col, row, currentZoom, currentZoom-1); 
    349354                                                        if (alreadySeen[pkey] is Tile) { 
     
    361366                                                 
    362367                                                // if it doesn't have an image yet, see if we can make it from smaller images 
    363                                                 if (!foundParent && MAX_CHILD_SEARCH > 0 && (tile.alpha < 1 || layersNeeded[tile]) && currentZoom < maxZoom) { 
     368                                                if (!foundParent && MAX_CHILD_SEARCH > 0 && !tileReady && currentZoom < maxZoom) { 
    364369                                                        for (var czoom:int = currentZoom+1; czoom <= Math.min(maxZoom, currentZoom+MAX_CHILD_SEARCH); czoom++) { 
    365370                                                                var ckeys:Array = childKeys(col, row, currentZoom, czoom); 
     
    375380                                                                }  
    376381                                                        } // czoom 
    377                                                                                                                   
    378382                                                } 
    379383                                        } 
    380                                          
    381                                         var startZoomSearch:int = currentZoom -1; 
     384 
     385                                        var startZoomSearch:int = currentZoom - 1; 
    382386                                         
    383387                                        if (currentZoom > previousZoom) { 
     
    385389                                                startZoomSearch -= 1; 
    386390                                        } 
    387                                          
     391                                         
     392                                        var endZoomSearch:int = Math.max(minZoom, currentZoom-MAX_PARENT_SEARCH); 
     393 
     394                                        var stillNeedsAnImage:Boolean = !foundParent && foundChildren < 4;                                       
    388395                                        // if it still doesn't have an image yet, try more parent zooms 
    389                                         if (!foundParent && foundChildren < 4 && MAX_PARENT_SEARCH > 1 && (tile.alpha < 1 || layersNeeded[tile]) && currentZoom > minZoom) { 
    390                                                 for (pzoom = startZoomSearch; pzoom >= Math.max(minZoom, currentZoom-MAX_PARENT_SEARCH); pzoom--) { 
     396                                        if (stillNeedsAnImage && MAX_PARENT_SEARCH > 1 && tileReady && currentZoom > minZoom) { 
     397                                                for (pzoom = startZoomSearch; pzoom >= endZoomSearch; pzoom--) { 
    391398                                                        pkey = parentKey(col, row, currentZoom, pzoom); 
    392399                                                        if (alreadySeen[pkey] is Tile) { 
    393400                                                                ptile = ensureVisible(pkey); 
    394401                                                                ptile.alpha = 1;                                                                 
    395                                                                 foundParent = true; 
     402                                                                stillNeedsAnImage = false; 
    396403                                                                break; 
    397404                                                        } 
    398405                                                } 
    399                                         }  
    400                                          
    401                                         if (!foundParent && foundChildren < 4) { 
     406                                        } 
     407                                                                                
     408                                        if (stillNeedsAnImage) { 
    402409                                                blankCount++; 
    403410                                                //trace("sorry, no parent known for", key); 
     
    406413                                } 
    407414                        } 
    408                          
    409                         var i:int; 
    410                          
    411                         // prune children that are not currently visible 
    412                         // loop backwards so removal doesn't change i 
    413                         for (i = well.numChildren-1; i >= 0; i--) { 
     415 
     416                        // make absolutlely sure all our newly visible tiles are cached if they're done loading 
     417                        // TODO: this should probably happen onLoadEnd when there are no URLs left  
     418                        for each (tile in visibleTiles) { 
     419                                if (recentlySeen.indexOf(tile) < 0 && !layersNeeded[tile]) { 
     420                                        recentlySeen.push(tile); 
     421                                } 
     422                        } 
     423 
     424                        // prune tiles from the well if they shouldn't be there (not currently in visibleTiles) 
     425                        // (loop backwards so removal doesn't change i) 
     426                        for (var i:int = well.numChildren-1; i >= 0; i--) { 
    414427                                tile = well.getChildAt(i) as Tile; 
    415428                                if (visibleTiles.indexOf(tile) < 0) { 
    416429                                        well.removeChild(tile); 
    417430                                        tile.alpha = 0.0; 
     431                                        // if it's not in recentlySeen, that's because it wasn't done loading 
     432                                        // so we can give it back to the map now  
     433                                        if (recentlySeen.indexOf(tile) < 0) { 
     434                                                map.destroyTile(tile); 
     435                                        } 
     436                                        // otherwise, we'll keep it around for a while in case we want it 
     437                                        // back on the stage 
    418438                                } 
    419439                        } 
     
    422442                        // this means current is on top, +1 and -1 are next, then +2 and -2, etc. 
    423443                        visibleTiles.sort(distanceFromCurrentZoomCompare, Array.DESCENDING); 
    424  
    425444 
    426445                        // for fixing positions when we're between zoom levels: 
     
    442461                         
    443462                                // if we're done loading this one, move it to the end of recently seen: 
    444                                 if (layersNeeded[tile] == null) { 
     463                                if (!layersNeeded[tile]) { 
    445464                                        var ri:int = recentlySeen.indexOf(tile.name);  
    446465                                        if (ri >= 0) { 
    447466                                                recentlySeen.splice(ri, 1); 
    448467                                        } 
     468                                        //else { 
     469                                        //      trace('adding something to recently seen') 
     470                                        //} 
    449471                                        recentlySeen.push(tile.name); 
    450472                                } 
     
    454476                        // let's make sure we keep them around: 
    455477                        var maxRecentlySeen:int = Math.max(visibleTiles.length,MAX_TILES_TO_KEEP); 
     478/*                      trace(); 
     479                        trace('visibleTiles', visibleTiles.length); 
     480                        trace('maxRecentlySeen', maxRecentlySeen); 
     481                        trace('recentlySeen', recentlySeen.length); */ 
    456482                         
    457483                        // prune cache of already seen tiles if it's getting too big: 
     
    498524                         
    499525                        // process the queue 
    500                         var coord:Coordinate = new Coordinate(0,0,0); 
    501526                        while (openRequests.length < MAX_OPEN_REQUESTS && queue.length > 0) { 
    502527                                var tile:Tile = queue.shift() as Tile; 
    503                                 coord.row = tile.row; 
    504                                 coord.column = tile.column; 
    505                                 coord.zoom = tile.zoom; 
    506528                                // if it's still on the stage: 
    507529                                if (tile.parent) { 
    508                                         // TODO: add urls to Tile? 
    509                                         var urls:Array = layersNeeded[tile] as Array; 
    510                                         if (urls && urls.length > 0) { 
    511                                                 var url:* = urls.shift(); 
    512                                                 var tileLoader:Loader = new Loader();  
    513                                                 tileLoader.name = tile.name; 
    514                                                 try { 
    515                                                         tileLoader.load((url is URLRequest) ? url : new URLRequest(url)); 
    516                                                         tileLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadEnd, false, 0, true); 
    517                                                         tileLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError, false, 0, true); 
    518                                                         openRequests.push(tileLoader); 
    519                                                 } 
    520                                                 catch(error:Error) { 
    521                                                         tile.paintError(); 
    522                                                 } 
    523                                         } 
    524                                 } 
     530                                        loadNextURLForTile(tile); 
     531                                } 
     532                                // TODO: make sure that something else took care of calling map.destroyTile? 
     533                                // TODO: should that something also pull the tile out of the queue?  
    525534                        } 
    526535                         
     
    535544                        previousOpenRequests = openRequests.length; 
    536545                         
     546                } 
     547 
     548                private function loadNextURLForTile(tile:Tile):void 
     549                { 
     550                        // TODO: add urls to Tile? 
     551                        var urls:Array = layersNeeded[tile] as Array; 
     552                        if (urls && urls.length > 0) { 
     553                                var url:* = urls.shift(); 
     554                                var tileLoader:Loader = new Loader();  
     555                                tileLoader.name = tile.name; 
     556                                try { 
     557                                        tileLoader.load((url is URLRequest) ? url : new URLRequest(url)); 
     558                                        tileLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadEnd, false, 0, true); 
     559                                        tileLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError, false, 0, true); 
     560                                        openRequests.push(tileLoader); 
     561                                } 
     562                                catch(error:Error) { 
     563                                        tile.paintError(); 
     564                                } 
     565                        } 
     566                        else if (urls && urls.length == 0) { 
     567                                if (tile.zoom == currentZoom) { 
     568                                        tile.addedLoader(); 
     569                                } 
     570                                else { 
     571                                        tile.alpha = 1; 
     572                                }                                        
     573                                alreadySeen[tile.name] = tile; 
     574                                 
     575                                delete layersNeeded[tile]; 
     576                        }                        
    537577                } 
    538578 
     
    606646                        if (tile) {  
    607647                                tile.addChild(loader); 
    608  
    609                                 var urls:Array = layersNeeded[tile] as Array; 
    610                                 if (urls && urls.length) { 
    611                                         var url:String = urls.shift() as String; 
    612                                         var tileLoader:Loader = new Loader();  
    613                                         tileLoader.name = tile.name; 
    614                                         tileLoader.load(new URLRequest(url)); 
    615                                         tileLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadEnd, false, 0, true); 
    616                                         tileLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError, false, 0, true); 
    617                                         openRequests.push(tileLoader); 
    618                                 } 
    619                                 else { 
    620                                         if (urls) { 
    621                                                 delete layersNeeded[tile]; 
    622                                         } 
    623                                         if (tile.zoom == currentZoom) { 
    624                                                 tile.addedLoader(); 
    625                                         } 
    626                                         else { 
    627                                                 tile.alpha = 1; 
    628                                         }                                        
    629                                         alreadySeen[tile.name] = tile; 
    630                                 } 
    631  
     648                                loadNextURLForTile(tile); 
    632649                        } 
    633650                        else {