Dispatch an event when the map sprites etc have been initialized
[potlatch2.git] / net / systemeD / halcyon / Map.as
old mode 100755 (executable)
new mode 100644 (file)
index 7b0d2fa..836c0e1
@@ -66,18 +66,13 @@ package net.systemeD.halcyon {
                public var initparams:Object;                                   // object containing HTML page parameters
 
                public var backdrop:Object;                                             // reference to backdrop sprite
-               public var tileset:TileSet;                                             // 900913 tile background
-               private var tileurl:String='';                                  // internal tile URL
+               public var tileset:TileSet;                                             // background tile object
+               private var tileparams:Object={ url:'' };               // background tile URL, name and scheme
                private var styleurl:String='';                                 // internal style URL
                public var showall:Boolean=true;                                // show all objects, even if unstyled?
                
                public var connection:Connection;                               // server connection
                public var vectorlayers:Object={};                              // VectorLayer objects 
-
-               public const TILESPRITE:uint=0;
-               public const VECTORSPRITE:uint=1;
-               public const WAYSPRITE:uint=2;
-               public const NAMESPRITE:uint=13;
                
                // ------------------------------------------------------------------------------------------
                // Map constructor function
@@ -125,7 +120,7 @@ package net.systemeD.halcyon {
 
                        tileset=new TileSet(this);                                      // 0 - 900913 background
                        addChild(tileset);                                                      //   |
-                       tileset.init(tileurl);                                          //   |
+                       tileset.init(tileparams);                                       //   |
 
                        vectorbg = new Sprite();                                        // 1 - vector background layers
                        addChild(vectorbg);                                                     //   |
@@ -138,13 +133,17 @@ package net.systemeD.halcyon {
                                paint.ruleset=new RuleSet(MINSCALE,MAXSCALE,redraw,redrawPOIs);
                                paint.ruleset.loadFromCSS(styleurl);
                        }
-                       if (startscale>0) { scale=startscale; }
+                       if (startscale>0) {
+                               scale=startscale;
+                               this.dispatchEvent(new MapEvent(MapEvent.SCALE, {scale:scale}));
+                       }
 
                        scalefactor=MASTERSCALE/Math.pow(2,13-scale);
                        baselon    =startlon          -(mapwidth /2)/scalefactor;
                        basey      =lat2latp(startlat)+(mapheight/2)/scalefactor;
                        addDebug("Baselon "+baselon+", basey "+basey);
                        updateCoords(0,0);
+            this.dispatchEvent(new Event(MapEvent.INITIALISED));
                        download();
                        
         }
@@ -222,10 +221,9 @@ package net.systemeD.halcyon {
             }
                }
 
-               // ------------------------------------------------------------------------------------------
-               // Download map data
-               // (typically from whichways, but will want to add more connections)
-
+        /** Download map data. Data is downloaded for the connection and the vector layers, where supported.
+        * The bounding box for the download is taken from the current map edges.
+        */
                public function download():void {
                        this.dispatchEvent(new MapEvent(MapEvent.DOWNLOAD, {minlon:edge_l, maxlon:edge_r, maxlat:edge_t, minlat:edge_b} ));
                        
@@ -238,6 +236,11 @@ package net.systemeD.halcyon {
                        }
                        addDebug("Calling download with "+edge_l+"-"+edge_r+", "+edge_t+"-"+edge_b);
                        connection.loadBbox(edge_l,edge_r,edge_t,edge_b);
+
+            // Do the same for vector layers
+            for each (var layer:VectorLayer in vectorlayers) {
+              layer.loadBbox(edge_l,edge_r,edge_t,edge_b);
+            }
                }
 
         private function newWayCreated(event:EntityEvent):void {
@@ -268,20 +271,22 @@ package net.systemeD.halcyon {
         }
 
         public function setHighlightOnNodes(way:Way, settings:Object):void {
-                       paint.wayuis[way.id].setHighlightOnNodes(settings);
+                       if (paint.wayuis[way.id]) paint.wayuis[way.id].setHighlightOnNodes(settings);
         }
 
-               public function setPurgable(entity:Entity, purgable:Boolean):void {
-                       if ( entity is Way  ) {
-                               var way:Way=entity as Way;
-                               paint.wayuis[way.id].purgable=purgable;
-                               for (var i:uint=0; i<way.length; i++) {
-                                       if (paint.nodeuis[way.getNode(i).id]) {
-                                               paint.nodeuis[way.getNode(i).id].purgable=purgable;
+               public function setPurgable(entities:Array, purgable:Boolean):void {
+                       for each (var entity:Entity in entities) {
+                               if ( entity is Way  ) {
+                                       var way:Way=entity as Way;
+                                       if (paint.wayuis[way.id]) { paint.wayuis[way.id].purgable=purgable; }
+                                       for (var i:uint=0; i<way.length; i++) {
+                                               if (paint.nodeuis[way.getNode(i).id]) {
+                                                       paint.nodeuis[way.getNode(i).id].purgable=purgable;
+                                               }
                                        }
+                               } else if ( entity is Node && paint.nodeuis[entity.id]) { 
+                                       paint.nodeuis[entity.id].purgable=purgable;
                                }
-                       } else if ( entity is Node ) { 
-                               paint.nodeuis[entity.id].purgable=purgable;
                        }
                }
 
@@ -337,6 +342,7 @@ package net.systemeD.halcyon {
                private function changeScale(newscale:uint):void {
                        addDebug("new scale "+newscale);
                        scale=newscale;
+                       this.dispatchEvent(new MapEvent(MapEvent.SCALE, {scale:scale}));
                        scalefactor=MASTERSCALE/Math.pow(2,13-scale);
                        updateCoordsFromLatLon((edge_t+edge_b)/2,(edge_l+edge_r)/2);    // recentre
                        tileset.changeScale(scale);
@@ -356,9 +362,9 @@ package net.systemeD.halcyon {
                        }
         }
 
-               public function setBackground(url:String):void {
-                       tileurl=url;
-                       if (tileset) { tileset.init(url, url!=''); }
+               public function setBackground(bg:Object):void {
+                       tileparams=bg;
+                       if (tileset) { tileset.init(bg, bg.url!=''); }
                }
 
                public function setDimming(dim:Boolean):void {
@@ -463,6 +469,7 @@ package net.systemeD.halcyon {
                }
                        
                public function addDebug(text:String):void {
+                       trace(text);
                        if (!Globals.vars.hasOwnProperty('debug')) return;
                        if (!Globals.vars.debug.visible) return;
                        Globals.vars.debug.appendText(text+"\n");