Remember dimming/sharpening preferences
[potlatch2.git] / net / systemeD / halcyon / Map.as
index 2df61ef..59f93e6 100644 (file)
@@ -12,6 +12,7 @@ package net.systemeD.halcyon {
        import flash.utils.ByteArray;
        import flash.events.*;
        import flash.net.*;
+    import flash.external.ExternalInterface;
 
     import net.systemeD.halcyon.connection.*;
     import net.systemeD.halcyon.connection.EntityEvent;
@@ -66,8 +67,8 @@ 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?
                
@@ -115,12 +116,17 @@ package net.systemeD.halcyon {
                // ------------------------------------------------------------------------------------------
                // Initialise map at a given lat/lon
 
-        public function init(startlat:Number,startlon:Number,startscale:uint=0):void {
+        public function init(startlat:Number, startlon:Number, startscale:uint=0):void {
                        while (numChildren) { removeChildAt(0); }
 
                        tileset=new TileSet(this);                                      // 0 - 900913 background
+                       if (initparams['tileblocks']) {                         //   | option to block dodgy tile sources
+                               tileset.blocks=initparams['tileblocks'];//   |
+                       }                                                                                       //   |
                        addChild(tileset);                                                      //   |
-                       tileset.init(tileurl);                                          //   |
+                       tileset.init(tileparams, false, 
+                                    initparams['background_dim']    ==null ? true  : initparams['background_dim'],
+                                    initparams['background_sharpen']==null ? false : initparams['background_sharpen']);
 
                        vectorbg = new Sprite();                                        // 1 - vector background layers
                        addChild(vectorbg);                                                     //   |
@@ -143,8 +149,15 @@ package net.systemeD.halcyon {
                        basey      =lat2latp(startlat)+(mapheight/2)/scalefactor;
                        addDebug("Baselon "+baselon+", basey "+basey);
                        updateCoords(0,0);
+            this.dispatchEvent(new Event(MapEvent.INITIALISED));
                        download();
-                       
+
+            if (ExternalInterface.available) {
+              ExternalInterface.addCallback("setPosition", function (lat:Number,lon:Number,zoom:uint):void {
+                  updateCoordsFromLatLon(lat, lon);
+                  changeScale(zoom);
+              });
+            }
         }
 
                // ------------------------------------------------------------------------------------------
@@ -171,7 +184,7 @@ package net.systemeD.halcyon {
                private function setCentre():void {
                        centre_lat=coord2lat(-y+mapheight/2);
                        centre_lon=coord2lon(-x+mapwidth/2);
-                       this.dispatchEvent(new MapEvent(MapEvent.MOVE, {lat:centre_lat, lon:centre_lon, scale:scale}));
+                       this.dispatchEvent(new MapEvent(MapEvent.MOVE, {lat:centre_lat, lon:centre_lon, scale:scale, minlon:edge_l, maxlon:edge_r, minlat:edge_b, maxlat:edge_t}));
                }
                
                public function nudgeBackground(x:Number,y:Number):void {
@@ -220,10 +233,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} ));
                        
@@ -236,6 +248,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 {
@@ -269,15 +286,19 @@ package net.systemeD.halcyon {
                        if (paint.wayuis[way.id]) paint.wayuis[way.id].setHighlightOnNodes(settings);
         }
 
+               /* Protect Entities and EntityUIs against purging. This prevents the currently selected items
+                  from being purged even though they're off-screen. */
+
                public function setPurgable(entities:Array, purgable:Boolean):void {
                        for each (var entity:Entity in entities) {
+                               entity.locked=!purgable;
                                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;
-                                               }
+                                               var node:Node=way.getNode(i)
+                                               node.locked=!purgable;
+                                               if (paint.nodeuis[node.id]) { paint.nodeuis[node.id].purgable=purgable; }
                                        }
                                } else if ( entity is Node && paint.nodeuis[entity.id]) { 
                                        paint.nodeuis[entity.id].purgable=purgable;
@@ -357,14 +378,26 @@ 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 {
                        if (tileset) { tileset.setDimming(dim); }
                }
+               public function getDimming():Boolean {
+                       if (tileset) { return tileset.getDimming(); }
+                       return true;
+               }
+
+               public function setSharpen(sharpen:Boolean):void {
+                       if (tileset) { tileset.setSharpen(sharpen); }
+               }
+               public function getSharpen():Boolean {
+                       if (tileset) { return tileset.getSharpen(); }
+                       return false;
+               }
 
                // ------------------------------------------------------------------------------------------
                // Export (experimental)