Download on resize
[potlatch2.git] / net / systemeD / halcyon / Map.as
index 5024b23..29aded4 100644 (file)
@@ -29,7 +29,7 @@ package net.systemeD.halcyon {
                public const MAXSCALE:uint=23; 
 
                // Container for MapPaint objects
-               public var paintContainer:Sprite;
+               private var paintContainer:Sprite;
 
                /** map scale */
                public var scale:uint=14;                                                
@@ -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,10 +75,6 @@ package net.systemeD.halcyon {
                public var backdrop:Object; 
                /** background tile object */
                public var tileset:TileSet; 
-               /** background tile URL, name and scheme */
-               private var tileparams:Object={ url:'' }; 
-               /** internal style URL */
-               private var styleurl:String=''; 
                /** show all objects, even if unstyled? */
                public var showall:Boolean=true; 
                
@@ -124,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          );
@@ -137,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);
@@ -186,7 +183,6 @@ package net.systemeD.halcyon {
             if (lat> edge_t || lat < edge_b || lon < edge_l || lon > edge_r) {
                 moveMapFromLatLon(lat, lon);
             }
-
                }
 
                // Co-ordinate conversion functions
@@ -210,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}));
                        
@@ -223,25 +220,16 @@ 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++) {
-                               paintContainer.getChildAt(i).connection.loadBbox(edge_l,edge_r,edge_t,edge_b);
-                       }
+                       for (var i:uint=0; i<paintContainer.numChildren; i++)
+                               if(getLayerAt(i).visible == true) {
+                    getLayerAt(i).connection.loadBbox(edge_l,edge_r,edge_t,edge_b);
+                }
                }
 
-               // >>>> REFACTOR: moved to MapPaint:
-        // public function setHighlight(entity:Entity, settings:Object):void {
-        // public function setHighlightOnNodes(way:Way, settings:Object):void {
-               // public function protectWay(way:Way):void {
-               // public function unprotectWay(way:Way):void {
-               // public function limitWayDrawing(way:Way,except:Number=NaN,only:Number=NaN):void {
-               // public function setPurgable(entities:Array, purgable:Boolean):void {
-
-
         // Handle mouse events on ways/nodes
         private var mapController:MapController = null;
 
@@ -258,29 +246,59 @@ package net.systemeD.halcyon {
                // ------------------------------------------------------------------------------------------
                // Add layers
                
-               public function addLayer(connection:Connection, styleurl:String, backgroundlayer:Boolean=true):void {
-                       var paint:MapPaint=new MapPaint(this,connection,-5,5);
-                       paintContainer.addChild(paint);
+               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.addChildAt(paint,0);
                        paint.isBackground=backgroundlayer;
-                       if (styleurl) {
-                               // if we've only just set up paint, then setStyle won't have created the RuleSet
-                               paint.ruleset=new RuleSet(MINSCALE,MAXSCALE,redraw,redrawPOIs);
-                               paint.ruleset.loadFromCSS(styleurl);
-                       }
+                       paint.interactive=interactive;
+                       return paint;
                }
 
                public function removeLayerByName(name:String):void {
-                       for (var i:uint=0; i<paintContainer.numChildren; i++)
-                               if (paintContainer.getChildAt(i).connection.name==name) {
+                       for (var i:uint=0; i<paintContainer.numChildren; i++) {
+                               if (getLayerAt(i).connection.name==name)
                                        paintContainer.removeChildAt(i);
                                        // >>>> REFACTOR: needs to do the equivalent of VectorLayer.blank()
-                               }
                        }
                }
                
                public function findLayer(name:String):MapPaint {
                        for (var i:uint=0; i<paintContainer.numChildren; i++)
-                               if (paintContainer.getChildAt(i).connection.name==name) return paintContainer.getChildAt(i);
+                               if (getLayerAt(i).connection.name==name) return getLayerAt(i);
+                       return null;
+               }
+
+               private function getLayerAt(i:uint):MapPaint {
+                       return MapPaint(paintContainer.getChildAt(i));
+               }
+
+        /** Get all the layers available for this Map object
+        *   @return An array of MapPaint objects */
+        public function getLayers():Array {
+            var a:Array = [];
+            for (var i:uint=0; i<paintContainer.numChildren; i++) {
+                a.push(getLayerAt(i));
+            }
+            return a;
+        }
+               
+               /* Find which layer is editable */
+               public function get editableLayer():MapPaint {
+                       var editableLayer:MapPaint;
+                       for (var i:uint=0; i<paintContainer.numChildren; i++) {
+                               if (!getLayerAt(i).isBackground) {
+                                       if (editableLayer) trace("Multiple editable layers found");
+                                       editableLayer=getLayerAt(i);
+                               }
+                       }
+                       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;
                }
 
@@ -289,15 +307,15 @@ package net.systemeD.halcyon {
                
                private function updateAllEntityUIs(redraw:Boolean,remove:Boolean):void {
                        for (var i:uint=0; i<paintContainer.numChildren; i++)
-                               paintContainer.getChildAt(i).updateEntityUIs(redraw, remove);
+                               getLayerAt(i).updateEntityUIs(redraw, remove);
                }
                public function redraw():void {
                        for (var i:uint=0; i<paintContainer.numChildren; i++)
-                               paintContainer.getChildAt(i).redraw();
+                               getLayerAt(i).redraw();
                }
                public function redrawPOIs():void { 
                        for (var i:uint=0; i<paintContainer.numChildren; i++)
-                               paintContainer.getChildAt(i).redrawPOIs();
+                               getLayerAt(i).redrawPOIs();
                }
                
                public function zoomIn():void {
@@ -318,21 +336,6 @@ package net.systemeD.halcyon {
                        download();
                }
 
-               /** Switch to new MapCSS. */
-               public function setStyle(url:String):void {
-                       styleurl=url;
-                       if (paint) { 
-                               paint.ruleset=new RuleSet(MINSCALE,MAXSCALE,redraw,redrawPOIs);
-                               paint.ruleset.loadFromCSS(url);
-                       }
-        }
-
-               /** 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;
-               }
-
        }
 }