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 e248dde..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();
                        
         }
@@ -180,6 +179,12 @@ package net.systemeD.halcyon {
                        this.dispatchEvent(new MapEvent(MapEvent.NUDGE_BACKGROUND, { x: x, y: y }));
                }
 
+               private function moveMap(dx:Number,dy:Number):void {
+                       updateCoords(x+dx,y+dy);
+                       updateEntityUIs(false, false);
+                       download();
+               }
+
                // Co-ordinate conversion functions
 
                public function latp2coord(a:Number):Number     { return -(a-basey)*scalefactor; }
@@ -201,10 +206,11 @@ package net.systemeD.halcyon {
                // Resize map size based on current stage and height
 
                public function updateSize(w:uint, h:uint):void {
+                       mapwidth = w; centre_lon=coord2lon(-x+w/2);
+                       mapheight= h; centre_lat=coord2lat(-y+h/2);
+
                        this.dispatchEvent(new MapEvent(MapEvent.RESIZE, {width:w, height:h}));
                        
-                       mapwidth = w;
-                       mapheight= h;
             if ( backdrop != null ) {
                 backdrop.width=mapwidth;
                 backdrop.height=mapheight;
@@ -215,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} ));
                        
@@ -231,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 {
@@ -256,25 +266,27 @@ package net.systemeD.halcyon {
                }
 
         public function setHighlight(entity:Entity, settings:Object):void {
-                       if      ( entity is Way  ) { paint.wayuis[entity.id].setHighlight(settings); }
-                       else if ( entity is Node ) { paint.nodeuis[entity.id].setHighlight(settings); }
+                       if      ( entity is Way  && paint.wayuis[entity.id] ) { paint.wayuis[entity.id].setHighlight(settings);  }
+                       else if ( entity is Node && paint.nodeuis[entity.id]) { paint.nodeuis[entity.id].setHighlight(settings); }
         }
 
         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;
                        }
                }
 
@@ -330,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);
@@ -349,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 {
@@ -397,11 +410,7 @@ package net.systemeD.halcyon {
                }
         
                public function mouseUpHandler(event:MouseEvent=null):void {
-                       if (dragstate==DRAGGING) {
-                               updateCoords(x,y);
-                               updateEntityUIs(false, false);
-                               download();
-                       }
+                       if (dragstate==DRAGGING) { moveMap(0,0); }
                        dragstate=NOT_DRAGGING;
                }
         
@@ -435,10 +444,16 @@ package net.systemeD.halcyon {
                // Miscellaneous events
                
                public function keyUpHandler(event:KeyboardEvent):void {
-                       addDebug("pressed "+event.keyCode);
-                       if (event.keyCode==33) { zoomIn(); }                    // Page Up - zoom in
-                       if (event.keyCode==34) { zoomOut(); }                   // Page Down - zoom out
-//                     if (event.keyCode==76) { reportPosition(); }    // L - report lat/long
+                       if (event.target is TextField) return;                          // not meant for us
+                       switch (event.keyCode) {
+                               case 33:        zoomIn(); break;                                        // Page Up - zoom in
+                               case 34:        zoomOut(); break;                                       // Page Down - zoom out
+                               case 37:        moveMap(mapwidth/2,0); break;           // left cursor
+                               case 38:        moveMap(0,mapheight/2); break;          // up cursor
+                               case 39:        moveMap(-mapwidth/2,0); break;          // right cursor
+                               case 40:        moveMap(0,-mapheight/2); break;         // down cursor
+//                             case 76:        reportPosition(); break;                        // L - report lat/long
+                       }
                }
 
                public function connectionError(err:Object=null): void {
@@ -454,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");