Download on resize
[potlatch2.git] / net / systemeD / halcyon / Map.as
index 5054ac1..29aded4 100644 (file)
@@ -64,6 +64,7 @@ package net.systemeD.halcyon {
                public const NOT_DRAGGING:uint=0;                               //  |
                public const NOT_MOVED:uint=1;                                  //  |
                public const DRAGGING:uint=2;                                   //  |
+               public const SWALLOW_MOUSEUP:uint=3;                    //  |
                /** How far the map can be dragged without actually triggering a pan. */
                public const TOLERANCE:uint=7;                                  //  |
                
@@ -74,8 +75,6 @@ package net.systemeD.halcyon {
                public var backdrop:Object; 
                /** background tile object */
                public var tileset:TileSet; 
-               /** background tile URL, name and scheme */
-               public var tileparams:Object={ url:'' }; 
                /** show all objects, even if unstyled? */
                public var showall:Boolean=true; 
                
@@ -122,7 +121,7 @@ package net.systemeD.halcyon {
                // ------------------------------------------------------------------------------------------
                /** Recalculate co-ordinates from new Flash origin */
 
-               public function updateCoords(tx:Number,ty:Number):void {
+               private function updateCoords(tx:Number,ty:Number):void {
                        setScrollRectXY(tx,ty);
 
                        edge_t=coord2lat(-ty          );
@@ -135,7 +134,7 @@ package net.systemeD.halcyon {
                }
                
                /** Move the map to centre on a given latitude/longitude. */
-               public function updateCoordsFromLatLon(lat:Number,lon:Number):void {
+               private function updateCoordsFromLatLon(lat:Number,lon:Number):void {
                        var cy:Number=-(lat2coord(lat)-mapheight/2);
                        var cx:Number=-(lon2coord(lon)-mapwidth/2);
                        updateCoords(cx,cy);
@@ -207,6 +206,7 @@ package net.systemeD.halcyon {
                        mapwidth = w; centre_lon=coord2lon(-getX()+w/2);
                        mapheight= h; centre_lat=coord2lat(-getY()+h/2);
                        setScrollRectSize(w,h);
+                       updateCoords(getX(),getY());
 
                        this.dispatchEvent(new MapEvent(MapEvent.RESIZE, {width:w, height:h}));
                        
@@ -220,13 +220,14 @@ package net.systemeD.halcyon {
             }
                }
 
-        /** Download map data. Data is downloaded for the connection and the vector layers, where supported.
+        /** Download map data. Data is downloaded for the currently visible layers
         * 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} ));
                        for (var i:uint=0; i<paintContainer.numChildren; i++)
-                               getLayerAt(i).connection.loadBbox(edge_l,edge_r,edge_t,edge_b);
+                               if(getLayerAt(i).visible == true) {
+                    getLayerAt(i).connection.loadBbox(edge_l,edge_r,edge_t,edge_b);
+                }
                }
 
         // Handle mouse events on ways/nodes
@@ -247,7 +248,7 @@ package net.systemeD.halcyon {
                
                public function addLayer(connection:Connection, styleurl:String, backgroundlayer:Boolean=true, interactive:Boolean=false):MapPaint {
                        var paint:MapPaint=new MapPaint(this, connection, styleurl, -5, 5);
-                       paintContainer.addChild(paint);
+                       paintContainer.addChildAt(paint,0);
                        paint.isBackground=backgroundlayer;
                        paint.interactive=interactive;
                        return paint;
@@ -293,6 +294,14 @@ package net.systemeD.halcyon {
                        return editableLayer;
                }
 
+               /** Find which paint object an entity will be displayed on. */
+               public function getLayerForEntity(entity:Entity):MapPaint {
+                       for (var i:uint=0; i<paintContainer.numChildren; i++) {
+                               if (getLayerAt(i).sameConnection(entity)) return getLayerAt(i);
+                       }
+                       return null;
+               }
+
                // ------------------------------------------------------------------------------------------
                // Redraw all items, zoom in and out
                
@@ -327,12 +336,6 @@ package net.systemeD.halcyon {
                        download();
                }
 
-               /** Select a new background imagery. */
-               public function setBackground(bg:Object):void {
-                       tileparams=bg;
-                       if (tileset) { tileset.init(bg, bg.url!=''); }
-               }
-
                /** Set background dimming on/off. */
                public function setDimming(dim:Boolean):void {
                        if (tileset) { tileset.setDimming(dim); }
@@ -388,7 +391,8 @@ package net.systemeD.halcyon {
                /** Prepare for being dragged by recording start time and location of mouse. */
                public function mouseDownHandler(event:MouseEvent):void {
                        if (!_draggable) { return; }
-                       dragstate=NOT_MOVED;
+                       if (dragstate==DRAGGING) { moveMap(x,y); dragstate=SWALLOW_MOUSEUP; }   // cancel drag if mouse-up occurred outside the window (thanks, Safari)
+                       else { dragstate=NOT_MOVED; }
                        lastxmouse=stage.mouseX; downX=stage.mouseX;
                        lastymouse=stage.mouseY; downY=stage.mouseY;
                        downTime=new Date().getTime();
@@ -427,6 +431,7 @@ package net.systemeD.halcyon {
 
                private function everyFrame(event:Event):void {
                        if (tileset) { tileset.serviceQueue(); }
+                       if (stage.focus && !stage.contains(stage.focus)) { stage.focus=stage; }
                }
 
                // ------------------------------------------------------------------------------------------
@@ -445,21 +450,5 @@ package net.systemeD.halcyon {
                        }
                }
 
-               // ------------------------------------------------------------------------------------------
-               // Debugging
-               
-               public function clearDebug():void {
-                       if (!Globals.vars.hasOwnProperty('debug')) return;
-                       Globals.vars.debug.text='';
-               }
-                       
-               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");
-                       Globals.vars.debug.scrollV=Globals.vars.debug.maxScrollV;
-               }
-
        }
 }