Changeset 621

Show
Ignore:
Timestamp:
07/20/08 14:47:18 (2 months ago)
Author:
tom
Message:

removed TileGrid's dependence on Map in tweenlite branch, TilePool? now belongs to grid

Files:

Legend:

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

    r597 r621  
    66package com.modestmaps.core 
    77{ 
     8        import flash.display.Loader; 
    89        import flash.display.Sprite; 
    910         
    1011        public class Tile extends Sprite 
    1112        {                
     13                public static var count:int = 0; 
     14                 
    1215                // not a coordinate, because it's very important these are ints 
    1316                public var zoom:int; 
     
    1720                public function Tile(column:int, row:int, zoom:int) 
    1821                { 
    19                         this.zoom = zoom; 
    20                         this.row = row; 
    21                         this.column = column; 
     22                        init(column, row, zoom); 
    2223                         
    2324                        // otherwise you'll get seams between tiles :( 
    2425                        this.cacheAsBitmap = false; 
    2526                         
     27                        count++; 
     28                }  
     29                 
     30                /** override this in a subclass and call grid.setTileClass if you want to draw on your tiles */ 
     31            public function init(column:int, row:int, zoom:int):void 
     32            { 
     33                        this.zoom = zoom; 
     34                        this.row = row; 
     35                        this.column = column;                    
    2636                        hide(); 
    27                          
    28                 }  
     37            }         
     38 
     39                /** once TileGrid is done with a tile, it will call destroy and possibly reuse it later */ 
     40            public function destroy():void 
     41            { 
     42                while (numChildren > 0) { 
     43                        var loader:Loader = removeChildAt(0) as Loader; 
     44                        if (loader) { 
     45                                try { 
     46                                        loader.unload(); 
     47                                } 
     48                                catch (error:Error) { 
     49                                        // meh 
     50                                } 
     51                        } 
     52                } 
     53                graphics.clear(); 
     54            }         
    2955                 
    3056                public function isShowing():Boolean 
     
    89115                    }                    
    90116                } 
     117                 
     118                 
    91119        } 
    92120 
  • branches/tom-tweenlite/lib/com/modestmaps/core/TileGrid.as

    r620 r621  
    11package com.modestmaps.core  
    22{ 
    3         import com.modestmaps.Map; 
    43        import com.modestmaps.events.MapEvent; 
    54        import com.modestmaps.mapproviders.IMapProvider; 
     
    2423        public class TileGrid extends Sprite 
    2524        { 
    26                 // we need a reference to our map for createTile() only,  
    27                 // slightly messy but better than a whole TileFactory hierarchy! 
    28                 // TODO: can we just use a function? how would TweenMap override it? 
    29                 private var map:Map; 
    30  
    3125        // TILE_WIDTH and TILE_HEIGHT are now tileWidth and tileHeight 
    3226        // this was needed for the NASA DailyPlanetProvider which has 512x512px tiles 
     
    6862 
    6963                protected var tileCache:TileCache; 
     64 
     65                protected var tilePool:TilePool; 
    7066                 
    7167                // per-tile, the array of images we're going to load, which can be empty 
     
    153149                protected var pmouse:Point; 
    154150                 
    155                 public function TileGrid(map:Map, w:Number, h:Number, draggable:Boolean, provider:IMapProvider) 
     151                public function TileGrid(w:Number, h:Number, draggable:Boolean, provider:IMapProvider) 
    156152                { 
    157153                        doubleClickEnabled = true; 
    158154                         
    159                         this.map = map; 
     155                        //this.map = map; 
    160156                        this.draggable = draggable; 
    161157 
     
    170166                        calculateBounds(); 
    171167                         
     168                        this.tilePool = new TilePool(Tile); 
    172169                        this.tileQueue = new TileQueue(); 
    173                         this.tileCache = new TileCache(map); 
     170                        this.tileCache = new TileCache(tilePool); 
    174171 
    175172                        this.mapWidth = w; 
     
    223220                        removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage); 
    224221                        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
     222                } 
     223 
     224                public function setTileClass(tileClass:Class):void 
     225                { 
     226                        tilePool.setTileClass(tileClass); 
    225227                } 
    226228                 
     
    254256                                                + "\nfinished (cached) tiles: " + tileCache.size 
    255257                                                + "\nrecently used tiles: " + recentlySeen.length 
    256                                                 + "\nTiles created: " + map.tileCount 
     258                                                + "\nTiles created: " + Tile.count 
    257259                                                + "\nmemory: " + (System.totalMemory/1048576).toFixed(1) + "MB";  
    258260                                debugField.width = debugField.textWidth+8; 
     
    274276 
    275277                        if (zooming && panning) { 
    276                                 map.onExtentChanged(map.getExtent()); 
     278                                dispatchEvent(new Event(Event.CHANGE, false, false)); 
    277279                        }  
    278280                        else if (panning) { 
     
    292294                        } 
    293295                        else if (boundsEnforced) { 
    294                                 map.onExtentChanged(map.getExtent()); 
     296                                dispatchEvent(new Event(Event.CHANGE, false, false)); 
    295297                        } 
    296298                         
     
    369371                                                        tileQueue.remove(wellTile); 
    370372                                                } 
    371                                                 map.destroyTile(wellTile); 
     373                                                tilePool.returnTile(wellTile); 
    372374                                        } 
    373375                                } 
     
    427429                                                tile = tileCache.getTile(key); 
    428430                                                if (!tile) { 
    429                                                         tile = map.createTile(col, row, currentZoom); 
     431                                                        tile = tilePool.getTile(col, row, currentZoom); 
    430432                                                        tile.name = key; 
    431433                                                        coord.row = tile.row; 
     
    543545                                well.setChildIndex(tile, well.numChildren-1); 
    544546 
     547                                // scale to compensate for zoom differences                              
     548                                var tileScale:Number = Math.pow(2, zoomLevel-tile.zoom); 
     549                                tileScale = Math.ceil(tileScale * tileWidth) / tileWidth; // round up to the nearest pixel 
     550                                tile.scaleX = tile.scaleY = tileScale; 
     551 
    545552                                // position tile according to current transform 
    546553                                var scaleFactor:Number = Math.pow(2.0, currentZoom-tile.zoom); 
    547554                                var positionCol:Number = (scaleFactor*tile.column) - realMinCol; 
    548555                                var positionRow:Number = (scaleFactor*tile.row) - realMinRow; 
    549                                 tile.x = positionCol*tileWidth*positionScaleCompensation; 
    550                                 tile.y = positionRow*tileHeight*positionScaleCompensation; 
    551                                  
    552                                 var tileScale:Number = Math.pow(2, zoomLevel-tile.zoom); 
    553                                 tileScale = Math.ceil(tileScale * tileWidth) / tileWidth; // round up to the nearest pixel 
    554                                 tile.scaleX = tile.scaleY = tileScale; 
     556                                tile.x = Math.floor(positionCol*tileWidth*positionScaleCompensation); 
     557                                tile.y = Math.floor(positionRow*tileHeight*positionScaleCompensation); 
    555558                        }                        
    556559                } 
     
    10111014                                if (!tileCache.containsKey(tile.name)) { 
    10121015                                        delete layersNeeded[tile.name]; 
    1013                                         map.destroyTile(tile); 
     1016                                        tilePool.returnTile(tile); 
    10141017                                } 
    10151018                        } 
     
    12871290        // Tiles we've already seen and fully loaded, by key (.name) 
    12881291        protected var alreadySeen:Dictionary; 
    1289         protected var map:Map; // for handing tiles back! 
    1290          
    1291         public function TileCache(map:Map
    1292         { 
    1293                 this.map = map
     1292        protected var tilePool:TilePool; // for handing tiles back! 
     1293         
     1294        public function TileCache(tilePool:TilePool
     1295        { 
     1296                this.tilePool = tilePool
    12941297                alreadySeen = new Dictionary(); 
    12951298        } 
     
    13271330                for (var key:String in alreadySeen) { 
    13281331                        if (keys.indexOf(key) < 0) { 
    1329                                 map.destroyTile(alreadySeen[key] as Tile); 
     1332                                tilePool.returnTile(alreadySeen[key] as Tile); 
    13301333                                delete alreadySeen[key]; 
    13311334                        } 
     
    13361339        { 
    13371340                for (var key:String in alreadySeen) { 
    1338                         map.destroyTile(alreadySeen[key] as Tile); 
     1341                        tilePool.returnTile(alreadySeen[key] as Tile); 
    13391342                        delete alreadySeen[key]; 
    13401343                } 
     
    13421345        } 
    13431346} 
     1347 
     1348class TilePool  
     1349{ 
     1350        protected var pool:Array = []; 
     1351        protected var tileClass:Class; 
     1352         
     1353        public function TilePool(tileClass:Class) 
     1354        { 
     1355                this.tileClass = tileClass; 
     1356        } 
     1357 
     1358        public function setTileClass(tileClass:Class):void 
     1359        { 
     1360                this.tileClass = tileClass; 
     1361                pool = []; 
     1362        } 
     1363 
     1364        public function getTile(column:int, row:int, zoom:int):Tile 
     1365        { 
     1366        if (pool.length > 0) { 
     1367                trace("reusing a tile"); 
     1368                var tile:Tile = pool.shift() as Tile; 
     1369                tile.init(column, row, zoom); 
     1370                return tile; 
     1371        }                                                
     1372                trace("creating a tile"); 
     1373        return new tileClass(column, row, zoom) as Tile; 
     1374        } 
     1375 
     1376        public function returnTile(tile:Tile):void 
     1377        { 
     1378                tile.destroy(); 
     1379        pool.push(tile); 
     1380        } 
     1381         
     1382} 
  • branches/tom-tweenlite/lib/com/modestmaps/core/TweenTile.as

    r612 r621  
    3838                        this.alpha = 1; 
    3939                }                
     40                                 
    4041        } 
    4142 
  • branches/tom-tweenlite/lib/com/modestmaps/Map.as

    r618 r621  
    104104 
    105105                        // initialize the grid (so point/location/coordinate functions should be valid after this) 
    106                         grid = new TileGrid(this, mapWidth, mapHeight, draggable, mapProvider); 
     106                        grid = new TileGrid(mapWidth, mapHeight, draggable, mapProvider); 
     107                        grid.addEventListener(Event.CHANGE, onExtentChanged); 
    107108                addChild(grid); 
    108109 
     
    178179            public function setExtent(extent:MapExtent):void 
    179180            { 
    180                 onExtentChanging(this.getExtent()); 
     181                onExtentChanging(); 
    181182                var position:MapPosition = extentPosition(extent); 
    182183                // tell grid what the rock is cooking 
    183184                grid.resetTiles(position.coord, position.point); 
    184                 onExtentChanged(this.getExtent()); 
     185                onExtentChanged(); 
    185186            requestCopyrightUpdate(); 
    186187            } 
     
    203204                } 
    204205                else { 
    205                         onExtentChanging(this.getExtent()); 
     206                        onExtentChanging(); 
    206207                        zoom = Math.min(Math.max(zoom, grid.minZoom), grid.maxZoom); 
    207208                var center:MapPosition = coordinatePosition(mapProvider.locationCoordinate(location).zoomTo(zoom)); 
    208209                // tell grid what the rock is cooking 
    209210                grid.resetTiles(center.coord, center.point); 
    210                 onExtentChanged(this.getExtent()); 
     211                onExtentChanged(); 
    211212                requestCopyrightUpdate(); 
    212213            } 
     
    229230                        } 
    230231                        else { // else hard reset 
    231                                 onExtentChanging(this.getExtent()); 
     232                                onExtentChanging(); 
    232233                                var center:MapPosition = coordinatePosition(grid.centerCoordinate().zoomTo(zoom)); 
    233234                                // tell grid what the rock is cooking 
    234235                                grid.resetTiles(center.coord, center.point); 
    235                                 onExtentChanged(this.getExtent()); 
     236                                onExtentChanged(); 
    236237                                requestCopyrightUpdate(); 
    237238                        } 
     
    329330                var extent:MapExtent = new MapExtent(); 
    330331                 
    331                 if(!mapProvider) 
    332                     return extent; 
     332                if(!mapProvider) { 
     333                        throw new Error("WHOAH, no mapProvider in getExtent!"); 
     334                } 
    333335         
    334336                extent.northWest = mapProvider.coordinateLocation(grid.topLeftCoordinate); 
     
    606608                public function setCenter(location:Location):void 
    607609                { 
    608                         onExtentChanging(this.getExtent()); 
     610                        onExtentChanging(); 
    609611                        var center:MapPosition = coordinatePosition(mapProvider.locationCoordinate(location).zoomTo(grid.zoomLevel)); 
    610612                        // tell grid what the rock is cooking 
    611613                        grid.resetTiles(center.coord, center.point); 
    612                         onExtentChanged(this.getExtent()); 
     614                        onExtentChanged(); 
    613615                        requestCopyrightUpdate(); 
    614616                } 
     
    783785            * @see com.modestmaps.events.MapEvent.EXTENT_CHANGED 
    784786            */ 
    785             public function onExtentChanged(extent:MapExtent):void 
    786             { 
    787                 dispatchEvent(new MapEvent(MapEvent.EXTENT_CHANGED, extent)); 
     787            protected function onExtentChanged(event:Event=null):void 
     788            { 
     789                if (hasEventListener(MapEvent.EXTENT_CHANGED)) { 
     790                        dispatchEvent(new MapEvent(MapEvent.EXTENT_CHANGED, getExtent())); 
     791                    } 
    788792            } 
    789793 
     
    794798            * @see com.modestmaps.events.MapEvent.BEGIN_EXTENT_CHANGE 
    795799            */ 
    796             public function onExtentChanging(extent:MapExtent):void 
    797             { 
    798                 dispatchEvent(new MapEvent(MapEvent.BEGIN_EXTENT_CHANGE, extent)); 
     800            protected function onExtentChanging():void 
     801            { 
     802                if (hasEventListener(MapEvent.BEGIN_EXTENT_CHANGE)) { 
     803                        dispatchEvent(new MapEvent(MapEvent.BEGIN_EXTENT_CHANGE, getExtent())); 
     804                } 
    799805            }        
    800806             
     
    863869            } 
    864870        } 
    865  
    866                 protected var tilePool:Array = []; 
    867                 public var tileCount:int = 0; 
    868  
    869                 protected function getTileFromPool(column:int, row:int, zoom:int):Tile 
    870                 { 
    871                 if (tilePool.length > 0) { 
    872                         var tile:Tile = tilePool.shift() as Tile; 
    873                         tile.row = row; 
    874                         tile.column = column; 
    875                         tile.zoom = zoom; 
    876                         tile.hide(); 
    877                         return tile; 
    878                 }                                                
    879                 return null; 
    880                 } 
    881  
    882                 /** override this if you want to create your own tiles */ 
    883             public function createTile(column:int, row:int, zoom:int):Tile 
    884             { 
    885                 var tile:Tile = getTileFromPool(column, row, zoom); 
    886                 if (tile) { 
    887                         return tile; 
    888                 } 
    889                 //trace("made", tileCount++, "tiles"); 
    890                 return new Tile(column, row, zoom); 
    891             }         
    892  
    893                 /** once TileGrid is done with a tile, it will hand it back to you */ 
    894             public function destroyTile(tile:Tile):void 
    895             { 
    896                 while (tile.numChildren > 0) { 
    897                         var loader:Loader = tile.removeChildAt(0) as Loader; 
    898                         if (loader) { 
    899                                 try { 
    900                                         loader.unload(); 
    901                                 } 
    902                                 catch (error:Error) { 
    903                                         // meh 
    904                                 } 
    905                         } 
    906                 } 
    907                 tile.graphics.clear(); 
    908                 tilePool.push(tile); 
    909             }         
    910871                 
    911872        } 
  • branches/tom-tweenlite/lib/com/modestmaps/TweenMap.as

    r609 r621  
    1212        import com.modestmaps.core.MapExtent; 
    1313        import com.modestmaps.core.MapPosition; 
    14         import com.modestmaps.core.Tile; 
    1514        import com.modestmaps.core.TweenTile; 
    1615        import com.modestmaps.geo.Location; 
     
    5251            public function TweenMap(width:Number=320, height:Number=240, draggable:Boolean=true, provider:IMapProvider=null, ... rest) 
    5352            { 
    54                 super(width, height, draggable, provider, rest);                 
     53                super(width, height, draggable, provider, rest); 
     54                grid.setTileClass(TweenTile); 
    5555        } 
    5656 
     
    258258            } 
    259259 
    260                 override public function createTile(column:int, row:int, zoom:int):Tile 
    261                 { 
    262                         var tile:Tile = getTileFromPool(column, row, zoom); 
    263                 if (tile) { 
    264                         return tile; 
    265                 }                        
    266                 tileCount++; 
    267                         return new TweenTile(column, row, zoom); 
    268                 } 
    269  
    270260        } 
    271261} 
  • branches/tom-tweenlite/samples/as3/ModestMapsSample.as

    r620 r621  
    7575            map.x = map.y = PADDING; 
    7676             
    77             //map.addChild(map.grid.debugField); 
     77            map.addChild(map.grid.debugField); 
    7878             
    7979            // if you want to wait for the Google version info to load