fix minor breakage
[potlatch2.git] / net / systemeD / halcyon / TileSet.as
index 2b51bc8f7b5651e5a377335498dc2875161b9004..f91bc467fcffe5ab525ddd103711a52477ffa16a 100755 (executable)
@@ -3,9 +3,8 @@ package net.systemeD.halcyon {
        import flash.display.*;
        import flash.events.*;
        import flash.net.*;
        import flash.display.*;
        import flash.events.*;
        import flash.net.*;
-       
-       import net.systemeD.halcyon.ImageURLLoader;
        import flash.system.LoaderContext;
        import flash.system.LoaderContext;
+       import net.systemeD.halcyon.MapEvent;
        
     public class TileSet extends Sprite {
 
        
     public class TileSet extends Sprite {
 
@@ -14,8 +13,8 @@ package net.systemeD.halcyon {
                public var tile_b:int;
                public var tile_t: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 requests:Array=[];
                private var tiles:Object={};            // key is "z,x,y"; value "true" (needed) or reference to sprite
@@ -27,16 +26,18 @@ package net.systemeD.halcyon {
 
         public function TileSet(map:Map) {
                        this.map=map;
 
         public function TileSet(map:Map) {
                        this.map=map;
+                       alpha=0.5;
                        createSprites();
                        createSprites();
+                       map.addEventListener(MapEvent.NUDGE_BACKGROUND, nudgeHandler);
                }
        
                }
        
-               public function init(url:String=null):void {
+               public function init(url:String=null, update:Boolean=false):void {
                        baseurl=url;
                        tiles={};
                        baseurl=url;
                        tiles={};
-                       if (!url) { 
-                               while (this.numChildren) { this.removeChildAt(0); }
-                               createSprites();
-                       }
+                       offset_lon=offset_lat=x=y=0;
+                       while (numChildren) { removeChildAt(0); }
+                       createSprites();
+                       if (update) { this.update(); }
                }
 
                private function createSprites():void {
                }
 
                private function createSprites():void {
@@ -45,21 +46,26 @@ package net.systemeD.halcyon {
                        }
                }
 
                        }
                }
 
+               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);
                        }
                public function changeScale(scale:uint):void {
                        for (var i:uint=map.MINSCALE; i<=map.MAXSCALE; i++) {
                                this.getChildAt(i-map.MINSCALE).visible=(scale==i);
                        }
-                       // ** Should also double or halve xoffset/yoffset accordingly
+                       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 {
                        if (!baseurl) { return; }
                }
                        
                // Update bounds - called on every move
                
                public function update():void {
                        if (!baseurl) { return; }
-                       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);
+                       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); }
                        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); }
@@ -94,7 +100,7 @@ package net.systemeD.halcyon {
                                        Sprite(l).addChild(loader);
                                        loader.x=map.lon2coord(tile2lon(tx));
                                        loader.y=map.lat2coord(tile2lat(ty));
                                        Sprite(l).addChild(loader);
                                        loader.x=map.lon2coord(tile2lon(tx));
                                        loader.y=map.lat2coord(tile2lat(ty));
-                                       loader.alpha=0.5;
+//                                     loader.alpha=0.5;
                                }
                        }
                }
                                }
                        }
                }
@@ -115,7 +121,21 @@ package net.systemeD.halcyon {
                private function tileURL(tx:int,ty:int):String {
                        return baseurl.replace('$z',map.scale).replace('$x',tx).replace('$y',ty);
                }
                private function tileURL(tx:int,ty:int):String {
                        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();
+               }
 
                
                // ------------------------------------------------------------------
 
                
                // ------------------------------------------------------------------