fix minor breakage
[potlatch2.git] / net / systemeD / halcyon / TileSet.as
index d71013d535b052b12613eec1a53aae604bea91cc..f91bc467fcffe5ab525ddd103711a52477ffa16a 100755 (executable)
@@ -1,51 +1,71 @@
 package net.systemeD.halcyon {
 
-       // ** Need to support different zoom levels
-       //    When zoom level changes: 
-       //              - double or halve xoffset/yoffset accordingly
-       //              - blank the tile queue
-
        import flash.display.*;
        import flash.events.*;
        import flash.net.*;
-       
-       import net.systemeD.halcyon.ImageURLLoader;
-       import net.systemeD.halcyon.Globals;
        import flash.system.LoaderContext;
+       import net.systemeD.halcyon.MapEvent;
        
     public class TileSet extends Sprite {
 
-               public var baseurl:String;
-
                public var tile_l:int;
                public var tile_r:int;
                public var tile_b:int;
                public var tile_t:int;
 
-               public var xoffset:Number=0;
-               public var yoffset:Number=0;
+               private var offset_lon:Number=0;
+               private var offset_lat:Number=0;
 
                private var requests:Array=[];
                private var tiles:Object={};            // key is "z,x,y"; value "true" (needed) or reference to sprite
                private var waiting:int=0;                      // number of tiles currently being downloaded
+               private var baseurl:String;                     // e.g. http://npe.openstreetmap.org/$z/$x/$y.png
 
                private var map:Map;
 
 
         public function TileSet(map:Map) {
                        this.map=map;
+                       alpha=0.5;
+                       createSprites();
+                       map.addEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
                }
        
-               public function init(url:String):void {
+               public function init(url:String=null, update:Boolean=false):void {
+                       baseurl=url;
+                       tiles={};
+                       offset_lon=offset_lat=x=y=0;
+                       while (numChildren) { removeChildAt(0); }
+                       createSprites();
+                       if (update) { this.update(); }
+               }
+
+               private function createSprites():void {
+                       for (var i:uint=map.MINSCALE; i<=map.MAXSCALE; i++) {
+                               this.addChild(new Sprite());
+                       }
+               }
+
+               public function setDimming(dim:Boolean):void {
+                       alpha=dim ? 0.5 : 1;
                }
 
+               public function changeScale(scale:uint):void {
+                       for (var i:uint=map.MINSCALE; i<=map.MAXSCALE; i++) {
+                               this.getChildAt(i-map.MINSCALE).visible=(scale==i);
+                       }
+                       x=map.lon2coord(map.centre_lon+offset_lon)-map.lon2coord(map.centre_lon);
+                       y=map.lat2coord(map.centre_lat+offset_lat)-map.lat2coord(map.centre_lat);
+               }
+                       
                // Update bounds - called on every move
                
                public function update():void {
-                       tile_l=lon2tile(map.edge_l+xoffset);
-                       tile_r=lon2tile(map.edge_r+xoffset);
-                       tile_t=lat2tile(map.edge_t+yoffset);
-                       tile_b=lat2tile(map.edge_b+yoffset);
+                       if (!baseurl) { return; }
+                       tile_l=lon2tile(map.edge_l-offset_lon);
+                       tile_r=lon2tile(map.edge_r-offset_lon);
+                       tile_t=lat2tile(map.edge_t-offset_lat);
+                       tile_b=lat2tile(map.edge_b-offset_lat);
                        for (var tx:int=tile_l; tx<=tile_r; tx++) {
                                for (var ty:int=tile_t; ty<=tile_b; ty++) {
                                        if (!tiles[map.scale+','+tx+','+ty]) { addRequest(tx,ty); }
@@ -64,25 +84,33 @@ package net.systemeD.halcyon {
                
                public function serviceQueue():void {
                        if (waiting==4 || requests.length==0) { return; }
-                       var r:Array, tx:int, ty:int, tz:int;
+                       var r:Array, tx:int, ty:int, tz:int, l:DisplayObject;
 
                        for (var i:uint=0; i<Math.min(requests.length, 4-waiting); i++) {
                                r=requests.shift(); tz=r[0]; tx=r[1]; ty=r[2];
                                if (tx>=tile_l && tx<=tile_r && ty>=tile_t && ty<=tile_b) {
                                        // Tile is on-screen, so load
+                                       waiting++;
                                        var loader:Loader = new Loader();
                                        loader.contentLoaderInfo.addEventListener(Event.INIT, doImgInit);
+                       loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, missingTileError);
                                        loader.load(new URLRequest(tileURL(tx,ty)), 
                                                    new LoaderContext(true));
-                                       this.addChild(loader);
+                                       l=this.getChildAt(map.scale-map.MINSCALE);
+                                       Sprite(l).addChild(loader);
                                        loader.x=map.lon2coord(tile2lon(tx));
                                        loader.y=map.lat2coord(tile2lat(ty));
-                                       loader.alpha=0.5;
+//                                     loader.alpha=0.5;
                                }
                        }
                }
 
-               protected function doImgInit(evt:Event):void {
+        private function missingTileError(event:Event):void {
+                       waiting--;
+                       return;
+               }
+
+               protected function doImgInit(event:Event):void {
                        waiting--;
                        return;
                }
@@ -91,10 +119,23 @@ package net.systemeD.halcyon {
                // Assemble tile URL
                
                private function tileURL(tx:int,ty:int):String {
-                       return "http://npe.openstreetmap.org/"+map.scale+"/"+tx+"/"+ty+".png";
-//                     return "http://andy.sandbox.cloudmade.com/tiles/cycle/"+map.scale+"/"+tx+"/"+ty+".png";
+                       return baseurl.replace('$z',map.scale).replace('$x',tx).replace('$y',ty);
                }
+               
+               public function get url():String {
+                       return baseurl ? baseurl : '';
+               }
+
 
+               // Update offset
+               
+               public function nudgeHandler(event:MapEvent):void {
+                       if (!baseurl) { return; }
+                       this.x+=event.params.x; this.y+=event.params.y;
+                       offset_lat=map.centre_lat-map.coord2lat(map.lat2coord(map.centre_lat)-this.y);
+                       offset_lon=map.centre_lon-map.coord2lon(map.lon2coord(map.centre_lon)-this.x);
+                       update();
+               }
 
                
                // ------------------------------------------------------------------