Changeset 621
- Timestamp:
- 07/20/08 14:47:18 (2 months ago)
- Files:
-
- branches/tom-tweenlite/lib/com/modestmaps/core/Tile.as (modified) (3 diffs)
- branches/tom-tweenlite/lib/com/modestmaps/core/TileGrid.as (modified) (17 diffs)
- branches/tom-tweenlite/lib/com/modestmaps/core/TweenTile.as (modified) (1 diff)
- branches/tom-tweenlite/lib/com/modestmaps/Map.as (modified) (9 diffs)
- branches/tom-tweenlite/lib/com/modestmaps/TweenMap.as (modified) (3 diffs)
- branches/tom-tweenlite/samples/as3/ModestMapsSample.as (modified) (1 diff)
- branches/tom-tweenlite/swc/bin/ModestMapsSWC.swc (modified) (previous)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/tom-tweenlite/lib/com/modestmaps/core/Tile.as
r597 r621 6 6 package com.modestmaps.core 7 7 { 8 import flash.display.Loader; 8 9 import flash.display.Sprite; 9 10 10 11 public class Tile extends Sprite 11 12 { 13 public static var count:int = 0; 14 12 15 // not a coordinate, because it's very important these are ints 13 16 public var zoom:int; … … 17 20 public function Tile(column:int, row:int, zoom:int) 18 21 { 19 this.zoom = zoom; 20 this.row = row; 21 this.column = column; 22 init(column, row, zoom); 22 23 23 24 // otherwise you'll get seams between tiles :( 24 25 this.cacheAsBitmap = false; 25 26 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; 26 36 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 } 29 55 30 56 public function isShowing():Boolean … … 89 115 } 90 116 } 117 118 91 119 } 92 120 branches/tom-tweenlite/lib/com/modestmaps/core/TileGrid.as
r620 r621 1 1 package com.modestmaps.core 2 2 { 3 import com.modestmaps.Map;4 3 import com.modestmaps.events.MapEvent; 5 4 import com.modestmaps.mapproviders.IMapProvider; … … 24 23 public class TileGrid extends Sprite 25 24 { 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 31 25 // TILE_WIDTH and TILE_HEIGHT are now tileWidth and tileHeight 32 26 // this was needed for the NASA DailyPlanetProvider which has 512x512px tiles … … 68 62 69 63 protected var tileCache:TileCache; 64 65 protected var tilePool:TilePool; 70 66 71 67 // per-tile, the array of images we're going to load, which can be empty … … 153 149 protected var pmouse:Point; 154 150 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) 156 152 { 157 153 doubleClickEnabled = true; 158 154 159 this.map = map;155 //this.map = map; 160 156 this.draggable = draggable; 161 157 … … 170 166 calculateBounds(); 171 167 168 this.tilePool = new TilePool(Tile); 172 169 this.tileQueue = new TileQueue(); 173 this.tileCache = new TileCache( map);170 this.tileCache = new TileCache(tilePool); 174 171 175 172 this.mapWidth = w; … … 223 220 removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage); 224 221 addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 222 } 223 224 public function setTileClass(tileClass:Class):void 225 { 226 tilePool.setTileClass(tileClass); 225 227 } 226 228 … … 254 256 + "\nfinished (cached) tiles: " + tileCache.size 255 257 + "\nrecently used tiles: " + recentlySeen.length 256 + "\nTiles created: " + map.tileCount258 + "\nTiles created: " + Tile.count 257 259 + "\nmemory: " + (System.totalMemory/1048576).toFixed(1) + "MB"; 258 260 debugField.width = debugField.textWidth+8; … … 274 276 275 277 if (zooming && panning) { 276 map.onExtentChanged(map.getExtent());278 dispatchEvent(new Event(Event.CHANGE, false, false)); 277 279 } 278 280 else if (panning) { … … 292 294 } 293 295 else if (boundsEnforced) { 294 map.onExtentChanged(map.getExtent());296 dispatchEvent(new Event(Event.CHANGE, false, false)); 295 297 } 296 298 … … 369 371 tileQueue.remove(wellTile); 370 372 } 371 map.destroyTile(wellTile);373 tilePool.returnTile(wellTile); 372 374 } 373 375 } … … 427 429 tile = tileCache.getTile(key); 428 430 if (!tile) { 429 tile = map.createTile(col, row, currentZoom);431 tile = tilePool.getTile(col, row, currentZoom); 430 432 tile.name = key; 431 433 coord.row = tile.row; … … 543 545 well.setChildIndex(tile, well.numChildren-1); 544 546 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 545 552 // position tile according to current transform 546 553 var scaleFactor:Number = Math.pow(2.0, currentZoom-tile.zoom); 547 554 var positionCol:Number = (scaleFactor*tile.column) - realMinCol; 548 555 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); 555 558 } 556 559 } … … 1011 1014 if (!tileCache.containsKey(tile.name)) { 1012 1015 delete layersNeeded[tile.name]; 1013 map.destroyTile(tile);1016 tilePool.returnTile(tile); 1014 1017 } 1015 1018 } … … 1287 1290 // Tiles we've already seen and fully loaded, by key (.name) 1288 1291 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; 1294 1297 alreadySeen = new Dictionary(); 1295 1298 } … … 1327 1330 for (var key:String in alreadySeen) { 1328 1331 if (keys.indexOf(key) < 0) { 1329 map.destroyTile(alreadySeen[key] as Tile);1332 tilePool.returnTile(alreadySeen[key] as Tile); 1330 1333 delete alreadySeen[key]; 1331 1334 } … … 1336 1339 { 1337 1340 for (var key:String in alreadySeen) { 1338 map.destroyTile(alreadySeen[key] as Tile);1341 tilePool.returnTile(alreadySeen[key] as Tile); 1339 1342 delete alreadySeen[key]; 1340 1343 } … … 1342 1345 } 1343 1346 } 1347 1348 class 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 38 38 this.alpha = 1; 39 39 } 40 40 41 } 41 42 branches/tom-tweenlite/lib/com/modestmaps/Map.as
r618 r621 104 104 105 105 // 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); 107 108 addChild(grid); 108 109 … … 178 179 public function setExtent(extent:MapExtent):void 179 180 { 180 onExtentChanging( this.getExtent());181 onExtentChanging(); 181 182 var position:MapPosition = extentPosition(extent); 182 183 // tell grid what the rock is cooking 183 184 grid.resetTiles(position.coord, position.point); 184 onExtentChanged( this.getExtent());185 onExtentChanged(); 185 186 requestCopyrightUpdate(); 186 187 } … … 203 204 } 204 205 else { 205 onExtentChanging( this.getExtent());206 onExtentChanging(); 206 207 zoom = Math.min(Math.max(zoom, grid.minZoom), grid.maxZoom); 207 208 var center:MapPosition = coordinatePosition(mapProvider.locationCoordinate(location).zoomTo(zoom)); 208 209 // tell grid what the rock is cooking 209 210 grid.resetTiles(center.coord, center.point); 210 onExtentChanged( this.getExtent());211 onExtentChanged(); 211 212 requestCopyrightUpdate(); 212 213 } … … 229 230 } 230 231 else { // else hard reset 231 onExtentChanging( this.getExtent());232 onExtentChanging(); 232 233 var center:MapPosition = coordinatePosition(grid.centerCoordinate().zoomTo(zoom)); 233 234 // tell grid what the rock is cooking 234 235 grid.resetTiles(center.coord, center.point); 235 onExtentChanged( this.getExtent());236 onExtentChanged(); 236 237 requestCopyrightUpdate(); 237 238 } … … 329 330 var extent:MapExtent = new MapExtent(); 330 331 331 if(!mapProvider) 332 return extent; 332 if(!mapProvider) { 333 throw new Error("WHOAH, no mapProvider in getExtent!"); 334 } 333 335 334 336 extent.northWest = mapProvider.coordinateLocation(grid.topLeftCoordinate); … … 606 608 public function setCenter(location:Location):void 607 609 { 608 onExtentChanging( this.getExtent());610 onExtentChanging(); 609 611 var center:MapPosition = coordinatePosition(mapProvider.locationCoordinate(location).zoomTo(grid.zoomLevel)); 610 612 // tell grid what the rock is cooking 611 613 grid.resetTiles(center.coord, center.point); 612 onExtentChanged( this.getExtent());614 onExtentChanged(); 613 615 requestCopyrightUpdate(); 614 616 } … … 783 785 * @see com.modestmaps.events.MapEvent.EXTENT_CHANGED 784 786 */ 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 } 788 792 } 789 793 … … 794 798 * @see com.modestmaps.events.MapEvent.BEGIN_EXTENT_CHANGE 795 799 */ 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 } 799 805 } 800 806 … … 863 869 } 864 870 } 865 866 protected var tilePool:Array = [];867 public var tileCount:int = 0;868 869 protected function getTileFromPool(column:int, row:int, zoom:int):Tile870 {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):Tile884 {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):void895 {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 // meh904 }905 }906 }907 tile.graphics.clear();908 tilePool.push(tile);909 }910 871 911 872 } branches/tom-tweenlite/lib/com/modestmaps/TweenMap.as
r609 r621 12 12 import com.modestmaps.core.MapExtent; 13 13 import com.modestmaps.core.MapPosition; 14 import com.modestmaps.core.Tile;15 14 import com.modestmaps.core.TweenTile; 16 15 import com.modestmaps.geo.Location; … … 52 51 public function TweenMap(width:Number=320, height:Number=240, draggable:Boolean=true, provider:IMapProvider=null, ... rest) 53 52 { 54 super(width, height, draggable, provider, rest); 53 super(width, height, draggable, provider, rest); 54 grid.setTileClass(TweenTile); 55 55 } 56 56 … … 258 258 } 259 259 260 override public function createTile(column:int, row:int, zoom:int):Tile261 {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 270 260 } 271 261 } branches/tom-tweenlite/samples/as3/ModestMapsSample.as
r620 r621 75 75 map.x = map.y = PADDING; 76 76 77 //map.addChild(map.grid.debugField);77 map.addChild(map.grid.debugField); 78 78 79 79 // if you want to wait for the Google version info to load
