working (though incomplete) 900913;
authorRichard Fairhurst <richard@systemed.net>
Sun, 20 Sep 2009 00:16:45 +0000 (00:16 +0000)
committerRichard Fairhurst <richard@systemed.net>
Sun, 20 Sep 2009 00:16:45 +0000 (00:16 +0000)
do the RIGHT SCALE dammit (I hate projection code. Not looking forward to making this projection-independent...);
make Map display list a bit less fragile

halcyon_viewer.as
net/systemeD/halcyon/Elastic.as
net/systemeD/halcyon/ImageURLLoader.as [moved from net/systemeD/halcyon/ImageLoader.as with 78% similarity]
net/systemeD/halcyon/Map.as
net/systemeD/halcyon/POI.as
net/systemeD/halcyon/TileSet.as
net/systemeD/halcyon/WayUI.as
net/systemeD/halcyon/styleparser/RuleSet.as

index da1818b..d0fb05f 100755 (executable)
@@ -22,6 +22,11 @@ package {
        
                private function init(e:Event):void {
 
        
                private function init(e:Event):void {
 
+                       theMap = new Map(this.loaderInfo.parameters);
+            theMap.updateSize(stage.stageWidth, stage.stageHeight);
+                       addChild(theMap);
+                       Globals.vars.root=theMap;
+
                        // add debug field
                        var t:TextField=new TextField();
                        t.width=400; t.height=100; t.x=400; t.border=true;
                        // add debug field
                        var t:TextField=new TextField();
                        t.width=400; t.height=100; t.x=400; t.border=true;
@@ -30,11 +35,6 @@ package {
                        Globals.vars.debug=t;
                        t.visible = true;
 
                        Globals.vars.debug=t;
                        t.visible = true;
 
-                       theMap = new Map(this.loaderInfo.parameters);
-            theMap.updateSize(stage.stageWidth, stage.stageHeight);
-                       addChild(theMap);
-                       Globals.vars.root=theMap;
-
                        stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
                        stage.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
                        stage.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
                        stage.addEventListener(MouseEvent.MOUSE_UP, theMap.mouseUpHandler);
                        stage.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
                        stage.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
index b7b3343..134ea6d 100755 (executable)
@@ -139,7 +139,7 @@ package net.systemeD.halcyon {
                // Add object (stroke/fill/roadname) to layer sprite
                
                private function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void {
                // Add object (stroke/fill/roadname) to layer sprite
                
                private function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void {
-                       var l:DisplayObject=Map(map).getChildAt(5);
+                       var l:DisplayObject=Map(map).getChildAt(map.WAYSPRITE+5);
                        var o:DisplayObject=Sprite(l).getChildAt(t);
                        if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); }
                        Sprite(o).addChild(s);
                        var o:DisplayObject=Sprite(l).getChildAt(t);
                        if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); }
                        Sprite(o).addChild(s);
similarity index 78%
rename from net/systemeD/halcyon/ImageLoader.as
rename to net/systemeD/halcyon/ImageURLLoader.as
index d729201..2379e48 100755 (executable)
@@ -10,8 +10,9 @@ package net.systemeD.halcyon {
 
        import flash.events.*;
        import flash.net.*;
 
        import flash.events.*;
        import flash.net.*;
+       import flash.display.*;
 
 
-       public class ImageLoader extends URLLoader {
+       public class ImageURLLoader extends URLLoader {
                public var filename:*;
        }
 
                public var filename:*;
        }
 
index 39d13b3..4e3004f 100755 (executable)
@@ -74,6 +74,11 @@ package net.systemeD.halcyon {
                
                public var connection:Connection;                               // server connection
 
                
                public var connection:Connection;                               // server connection
 
+               public const TILESPRITE:uint=0;
+               public const WAYSPRITE:uint=1;
+               public const POISPRITE:uint=12;
+               public const NAMESPRITE:uint=13;
+
                // ------------------------------------------------------------------------------------------
                // Map constructor function
 
                // ------------------------------------------------------------------------------------------
                // Map constructor function
 
@@ -89,7 +94,7 @@ package net.systemeD.halcyon {
                        // [layer][1]                   - casing
                        // [layer][0]                   - fill
 
                        // [layer][1]                   - casing
                        // [layer][0]                   - fill
 
-                       for (var l:int=0; l<13; l++) {                          // 11 layers (10 is +5, 0 is -5)
+                       for (var l:int=0; l<13; l++) {                          // 11 layers (11 is +5, 1 is -5)
                                var s:Sprite = getHitSprite();          //  |
                                s.addChild(getPaintSprite());                   //      | 0 fill
                                s.addChild(getPaintSprite());                   //      | 1 casing
                                var s:Sprite = getHitSprite();          //  |
                                s.addChild(getPaintSprite());                   //      | 0 fill
                                s.addChild(getPaintSprite());                   //      | 1 casing
@@ -102,14 +107,16 @@ package net.systemeD.halcyon {
                                s.addChild(getHitSprite());                         //  | 4 entity hit tests
                                addChild(s);                                                    //  |
                        }
                                s.addChild(getHitSprite());                         //  | 4 entity hit tests
                                addChild(s);                                                    //  |
                        }
-                       addChild(getPaintSprite());                             // 11 - POIs
-                       addChild(getPaintSprite());                             // 12 - shields and POI names
+                       addChild(getPaintSprite());                             // 12 - POIs
+                       addChild(getPaintSprite());                             // 13 - shields and POI names
 
                        this.initparams=initparams;
                        connection = Connection.getConnection(initparams);
             connection.addEventListener(Connection.NEW_WAY, newWayCreated);
             connection.addEventListener(Connection.NEW_POI, newPOICreated);
                        gotEnvironment(null);
 
                        this.initparams=initparams;
                        connection = Connection.getConnection(initparams);
             connection.addEventListener(Connection.NEW_WAY, newWayCreated);
             connection.addEventListener(Connection.NEW_POI, newPOICreated);
                        gotEnvironment(null);
+
+                       addEventListener(Event.ENTER_FRAME, everyFrame);
         }
 
         private function getPaintSprite():Sprite {
         }
 
         private function getPaintSprite():Sprite {
@@ -156,7 +163,7 @@ package net.systemeD.halcyon {
                        //updateSize();
 
                        scale=startscale;
                        //updateSize();
 
                        scale=startscale;
-                       scalefactor=MASTERSCALE/Math.pow(2,14-scale);
+                       scalefactor=MASTERSCALE/Math.pow(2,13-scale);
                        baselon    =startlon          -(mapwidth /2)/scalefactor;
                        basey      =lat2latp(startlat)+(mapheight/2)/scalefactor;
                        addDebug("Baselon "+baselon+", basey "+basey);
                        baselon    =startlon          -(mapwidth /2)/scalefactor;
                        basey      =lat2latp(startlat)+(mapheight/2)/scalefactor;
                        addDebug("Baselon "+baselon+", basey "+basey);
@@ -171,13 +178,14 @@ package net.systemeD.halcyon {
                public function updateCoords(tx:Number,ty:Number):void {
                        x=tx; y=ty;
 
                public function updateCoords(tx:Number,ty:Number):void {
                        x=tx; y=ty;
 
-                       // ** calculate tile_l etc.
                        edge_t=coord2lat(-y          );
                        edge_b=coord2lat(-y+mapheight);
                        edge_l=coord2lon(-x          );
                        edge_r=coord2lon(-x+mapwidth );
                        addDebug("Lon "+edge_l+"-"+edge_r);
                        addDebug("Lat "+edge_b+"-"+edge_t);
                        edge_t=coord2lat(-y          );
                        edge_b=coord2lat(-y+mapheight);
                        edge_l=coord2lon(-x          );
                        edge_r=coord2lon(-x+mapwidth );
                        addDebug("Lon "+edge_l+"-"+edge_r);
                        addDebug("Lat "+edge_b+"-"+edge_t);
+
+                       tileset.update();
                }
                
                public function updateCoordsFromLatLon(lat:Number,lon:Number):void {
                }
                
                public function updateCoordsFromLatLon(lat:Number,lon:Number):void {
@@ -348,6 +356,13 @@ package net.systemeD.halcyon {
                        lastxmouse=mouseX; lastymouse=mouseY;
                }
         
                        lastxmouse=mouseX; lastymouse=mouseY;
                }
         
+               // ------------------------------------------------------------------------------------------
+               // Do every frame
+
+               private function everyFrame(event:Event):void {
+                       tileset.serviceQueue();
+               }
+
                // ------------------------------------------------------------------------------------------
                // Miscellaneous events
                
                // ------------------------------------------------------------------------------------------
                // Miscellaneous events
                
index 92f2303..73e10b2 100644 (file)
@@ -36,6 +36,7 @@ package net.systemeD.halcyon {
                        // ** apply :hover etc.
                        var sl:StyleList=map.ruleset.getStyles(this.node,tags);
                        var r:Boolean=false;    // ** rendered
                        // ** apply :hover etc.
                        var sl:StyleList=map.ruleset.getStyles(this.node,tags);
                        var r:Boolean=false;    // ** rendered
+                       var l:DisplayObject;
                        for (var sublayer:uint=0; sublayer<10; sublayer++) {
 
                                if (sl.pointStyles[sublayer]) {
                        for (var sublayer:uint=0; sublayer<10; sublayer++) {
 
                                if (sl.pointStyles[sublayer]) {
@@ -45,6 +46,8 @@ package net.systemeD.halcyon {
                                        if (s.icon_image!=iconname) {
                                                // 'load' icon (actually just from library)
                                                if (map.ruleset.images[s.icon_image]) {
                                        if (s.icon_image!=iconname) {
                                                // 'load' icon (actually just from library)
                                                if (map.ruleset.images[s.icon_image]) {
+//                                                     l=map.getChildAt(map.POISPRITE);
+//                                                     Sprite(l).addChild(map.ruleset.images[s.icon_image]);
                                                        var loader:Loader = new Loader();
                                                        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
                                                        loader.loadBytes(map.ruleset.images[s.icon_image]);
                                                        var loader:Loader = new Loader();
                                                        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
                                                        loader.loadBytes(map.ruleset.images[s.icon_image]);
@@ -63,7 +66,7 @@ package net.systemeD.halcyon {
                                        // create name sprite
                                        if (!name) {
                                                name=new Sprite();
                                        // create name sprite
                                        if (!name) {
                                                name=new Sprite();
-                                               var c:DisplayObject=map.getChildAt(12);
+                                               var c:DisplayObject=map.getChildAt(map.NAMESPRITE);
                                                Sprite(c).addChild(name);
                                        }
                                        t.writeNameLabel(name,tags[t.text],map.lon2coord(node.lon),map.latp2coord(node.latp));
                                                Sprite(c).addChild(name);
                                        }
                                        t.writeNameLabel(name,tags[t.text],map.lon2coord(node.lon),map.latp2coord(node.latp));
@@ -71,7 +74,7 @@ package net.systemeD.halcyon {
                        }
                        if (!r && iconname!='') {
                                // not rendered any more, so remove
                        }
                        if (!r && iconname!='') {
                                // not rendered any more, so remove
-                               var l:DisplayObject=map.getChildAt(11);
+                               l=map.getChildAt(map.POISPRITE);
                                Sprite(l).removeChild(icon);
                                iconname='';
                        }
                                Sprite(l).removeChild(icon);
                                iconname='';
                        }
@@ -79,7 +82,7 @@ package net.systemeD.halcyon {
 
                private function loadedIcon(event:Event):void {
                        icon = Bitmap(event.target.content);
 
                private function loadedIcon(event:Event):void {
                        icon = Bitmap(event.target.content);
-                       var l:DisplayObject=map.getChildAt(11);
+                       var l:DisplayObject=map.getChildAt(map.POISPRITE);
                        Sprite(l).addChild(icon);
                        updatePosition();
                }
                        Sprite(l).addChild(icon);
                        updatePosition();
                }
index 2c9a75d..d71013d 100755 (executable)
@@ -5,15 +5,15 @@ package net.systemeD.halcyon {
        //              - double or halve xoffset/yoffset accordingly
        //              - blank the tile queue
 
        //              - double or halve xoffset/yoffset accordingly
        //              - blank the tile queue
 
-       import flash.display.DisplayObjectContainer;
-       import flash.display.Bitmap;
+       import flash.display.*;
        import flash.events.*;
        import flash.net.*;
        
        import flash.events.*;
        import flash.net.*;
        
-       import net.systemeD.halcyon.ImageLoader;
+       import net.systemeD.halcyon.ImageURLLoader;
        import net.systemeD.halcyon.Globals;
        import net.systemeD.halcyon.Globals;
-
-    public class TileSet extends DisplayObjectContainer {
+       import flash.system.LoaderContext;
+       
+    public class TileSet extends Sprite {
 
                public var baseurl:String;
 
 
                public var baseurl:String;
 
@@ -22,8 +22,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;
-               public var yoffset:Number;
+               public var xoffset:Number=0;
+               public var yoffset: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
@@ -42,11 +42,10 @@ package net.systemeD.halcyon {
                // Update bounds - called on every move
                
                public function update():void {
                // Update bounds - called on every move
                
                public function update():void {
-                       tile_l=lon2tile(map.coord2lon(-xoffset-map.x));
-                       tile_r=lon2tile(map.coord2lon(-xoffset-map.x+map.mapwidth));
-                       tile_t=lat2tile(map.coord2lat(-yoffset-map.y));
-                       tile_b=lat2tile(map.coord2lat(-yoffset-map.y+map.mapheight));
-
+                       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);
                        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); }
@@ -66,50 +65,36 @@ package net.systemeD.halcyon {
                public function serviceQueue():void {
                        if (waiting==4 || requests.length==0) { return; }
                        var r:Array, tx:int, ty:int, tz:int;
                public function serviceQueue():void {
                        if (waiting==4 || requests.length==0) { return; }
                        var r:Array, tx:int, ty:int, tz:int;
-                       var loader:ImageLoader, urlreq:URLRequest;
 
                        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
 
                        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
-                                       urlreq=new URLRequest(tileURL(tx,ty));
-                                       loader=new ImageLoader();
-                                       loader.dataFormat=URLLoaderDataFormat.BINARY;
-                                       loader.filename=[tz,tx,ty];
-                                       loader.addEventListener(Event.COMPLETE,                                         loadedTile,                             false, 0, true);
-                                       loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
-                                       loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
-                                       loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
-                                       loader.load(urlreq);
+                                       var loader:Loader = new Loader();
+                                       loader.contentLoaderInfo.addEventListener(Event.INIT, doImgInit);
+                                       loader.load(new URLRequest(tileURL(tx,ty)), 
+                                                   new LoaderContext(true));
+                                       this.addChild(loader);
+                                       loader.x=map.lon2coord(tile2lon(tx));
+                                       loader.y=map.lat2coord(tile2lat(ty));
+                                       loader.alpha=0.5;
                                }
                        }
                }
 
                                }
                        }
                }
 
-               // Tile has loaded, so place on display list
-
-               private function loadedTile(event:Event):void {
-                       var r:Array=event.target.filename as Array;
-                       var tz:int=r[0]; var tx:int=r[1]; var ty:int=r[2];
-                       
-                       var image:Bitmap = event.target.content as Bitmap;
-                       addChild(image);
-                       image.x=map.lon2coord(tile2lon(tx));
-                       image.y=map.lat2coord(tile2lat(ty));
-
+               protected function doImgInit(evt:Event):void {
                        waiting--;
                        waiting--;
+                       return;
                }
 
                
                // Assemble tile URL
                
                private function tileURL(tx:int,ty:int):String {
                }
 
                
                // Assemble tile URL
                
                private function tileURL(tx:int,ty:int):String {
-                       // ***** to do
-                       return '';
+                       return "http://npe.openstreetmap.org/"+map.scale+"/"+tx+"/"+ty+".png";
+//                     return "http://andy.sandbox.cloudmade.com/tiles/cycle/"+map.scale+"/"+tx+"/"+ty+".png";
                }
 
                }
 
-               private function httpStatusHandler( event:HTTPStatusEvent ):void { }
-               private function securityErrorHandler( event:SecurityErrorEvent ):void { Globals.vars.root.addDebug("securityerrorevent"); }
-               private function ioErrorHandler( event:IOErrorEvent ):void { Globals.vars.root.addDebug("ioerrorevent"); }
 
                
                // ------------------------------------------------------------------
 
                
                // ------------------------------------------------------------------
@@ -119,7 +104,7 @@ package net.systemeD.halcyon {
                        return (Math.floor((lon+180)/360*Math.pow(2,map.scale)));
                }
                private function lat2tile(lat:Number):int { 
                        return (Math.floor((lon+180)/360*Math.pow(2,map.scale)));
                }
                private function lat2tile(lat:Number):int { 
-                       return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,map.scale))); 
+                       return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 *Math.pow(2,map.scale)));
                }
                private function tile2lon(t:int):Number {
                        return (t/Math.pow(2,map.scale)*360-180);
                }
                private function tile2lon(t:int):Number {
                        return (t/Math.pow(2,map.scale)*360-180);
index 51c9f06..abad93a 100755 (executable)
@@ -397,7 +397,7 @@ package net.systemeD.halcyon {
                // Add object (stroke/fill/roadname) to layer sprite
                
                private function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void {
                // Add object (stroke/fill/roadname) to layer sprite
                
                private function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void {
-                       var l:DisplayObject=Map(map).getChildAt(layer);
+                       var l:DisplayObject=Map(map).getChildAt(map.WAYSPRITE+layer);
                        var o:DisplayObject=Sprite(l).getChildAt(t);
                        if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); }
                        Sprite(o).addChild(s);
                        var o:DisplayObject=Sprite(l).getChildAt(t);
                        if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); }
                        Sprite(o).addChild(s);
index 4afcfcf..517856f 100644 (file)
@@ -4,7 +4,7 @@ package net.systemeD.halcyon.styleparser {
        import flash.net.*;
        import net.systemeD.halcyon.Globals;
        import net.systemeD.halcyon.Map;
        import flash.net.*;
        import net.systemeD.halcyon.Globals;
        import net.systemeD.halcyon.Map;
-       import net.systemeD.halcyon.ImageLoader;
+       import net.systemeD.halcyon.ImageURLLoader;
     import net.systemeD.halcyon.connection.Entity;
 //     import bustin.dev.Inspector;
        
     import net.systemeD.halcyon.connection.Entity;
 //     import bustin.dev.Inspector;
        
@@ -73,7 +73,7 @@ package net.systemeD.halcyon.styleparser {
                                
                                        iconsToLoad++;
                                        var request:URLRequest=new URLRequest(filename);
                                
                                        iconsToLoad++;
                                        var request:URLRequest=new URLRequest(filename);
-                                       var loader:ImageLoader=new ImageLoader();
+                                       var loader:ImageURLLoader=new ImageURLLoader();
                                        loader.dataFormat=URLLoaderDataFormat.BINARY;
                                        loader.filename=filename;
                                        loader.addEventListener(Event.COMPLETE,                                         loadedImage,                    false, 0, true);
                                        loader.dataFormat=URLLoaderDataFormat.BINARY;
                                        loader.filename=filename;
                                        loader.addEventListener(Event.COMPLETE,                                         loadedImage,                    false, 0, true);
@@ -88,6 +88,7 @@ package net.systemeD.halcyon.styleparser {
                // data handler
 
                private function loadedImage(event:Event):void {
                // data handler
 
                private function loadedImage(event:Event):void {
+Globals.vars.root.addDebug("adding image at "+event.target.filename);
                        images[event.target.filename]=event.target.data;
                        iconsToLoad--;
                        if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }
                        images[event.target.filename]=event.target.data;
                        iconsToLoad--;
                        if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }