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 ed136c3..836c0e1
@@ -2,29 +2,35 @@ package net.systemeD.halcyon {
 
        import flash.text.TextField;
        import flash.display.DisplayObjectContainer;
+       import flash.display.Loader;
        import flash.display.Sprite;
        import flash.display.Shape;
        import flash.display.Stage;
+       import flash.display.BitmapData;
+       import flash.display.LoaderInfo;
+       import flash.text.Font;
+       import flash.utils.ByteArray;
        import flash.events.*;
        import flash.net.*;
 
     import net.systemeD.halcyon.connection.*;
     import net.systemeD.halcyon.connection.EntityEvent;
        import net.systemeD.halcyon.styleparser.*;
+       import net.systemeD.halcyon.Globals;
+
+//     for experimental export function:
+//     import flash.net.FileReference;
+//     import com.adobe.images.JPGEncoder;
 
     public class Map extends Sprite {
 
                public const MASTERSCALE:Number=5825.4222222222;// master map scale - how many Flash pixels in 1 degree longitude
                                                                                                                // (for Landsat, 5120)
                public const MINSCALE:uint=13;                                  // don't zoom out past this
-               public const MAXSCALE:uint=19;                                  // don't zoom in past this
+               public const MAXSCALE:uint=23;                                  // don't zoom in past this
 
-               public var ruleset:RuleSet=new RuleSet(redrawPOIs);     // rules
-               
-               public var ways:Object=new Object();                    // geodata
-               public var nodes:Object=new Object();                   //  |
-               public var pois:Object=new Object();                    //  |
-               public var relations:Object=new Object();               //  |
+               public var paint:MapPaint;                                              // sprite for ways and (POI/tagged) nodes in core layer
+               public var vectorbg:Sprite;                                             // sprite for vector background layers
 
                public var scale:uint=14;                                               // map scale
                public var scalefactor:Number=MASTERSCALE;              // current scaling factor for lon/latp
@@ -33,82 +39,111 @@ package net.systemeD.halcyon {
                public var bigedge_b:Number= 999999;                    //  |
                public var bigedge_t:Number=-999999;                    //  |
 
-               public var waycount:uint=0;                                             // ways:                number currently loaded
-               public var waysrequested:uint=0;                                //                              total number requested
-               public var waysreceived:uint=0;                                 //                              total number received
-               public var relcount:uint=0;                                             // relations:   number currently loaded
-               public var relsrequested:uint=0;                                //                              total number requested
-               public var relsreceived:uint=0;                                 //                              total number received
-               public var poicount:uint=0;                                             // POIs:                number currently loaded
-               public var whichrequested:uint=0;                               // whichways:   total number requested
-               public var whichreceived:uint=0;                                //                              total number received
-
                public var edge_l:Number;                                               // current bounding box
                public var edge_r:Number;                                               //  |
                public var edge_t:Number;                                               //  |
                public var edge_b:Number;                                               //  |
+               public var centre_lat:Number;                                   // centre lat/lon
+               public var centre_lon:Number;                                   //  |
 
                public var baselon:Number;                                              // urllon-xradius/masterscale;
                public var basey:Number;                                                // lat2lat2p(urllat)+yradius/masterscale;
                public var mapwidth:uint;                                               // width (Flash pixels)
                public var mapheight:uint;                                              // height (Flash pixels)
 
-               private var dragging:Boolean=false;                             // dragging map?
+               public var dragstate:uint=NOT_DRAGGING;                 // dragging map
+               private var _draggable:Boolean=true;                    //  |
                private var lastxmouse:Number;                                  //  |
                private var lastymouse:Number;                                  //  |
+               private var downX:Number;                                               //  |
+               private var downY:Number;                                               //  |
+               private var downTime:Number;                                    //  |
+               public const NOT_DRAGGING:uint=0;                               //  |
+               public const NOT_MOVED:uint=1;                                  //  |
+               public const DRAGGING:uint=2;                                   //  |
+               public const TOLERANCE:uint=7;                                  //  |
                
+               public var initparams:Object;                                   // object containing HTML page parameters
+
                public var backdrop:Object;                                             // reference to backdrop sprite
+               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 var connection:Connection;                       // server connection
-
                // ------------------------------------------------------------------------------------------
                // Map constructor function
 
-        public function Map() {
-
-                       // Set up layering
-                       // [layer][2]                   - names
-                       // [layer][1][sublayer] - stroke
-                       // [layer][0]                   - fill
-
-                       for (var l:int=0; l<13; l++) {                          // 11 layers (10 is +5, 0 is -5)
-                               var s:Sprite=new Sprite();                              //  |
-                               s.addChild(new Sprite());                               //      | 0 fill
-                               var t:Sprite=new Sprite();                              //  | 1 stroke
-                               for (var j:int=0; j<11; j++) {                  //      |  | ten sublayers
-                                       t.addChild(new Sprite());                       //  |  |  |
-                               }                                                                               //  |  |  |
-                               s.addChild(t);                                                  //  |  |
-                               s.addChild(new Sprite());                               //      | 2 names
-                               addChild(s);                                                    //  |
-                       }
-                       s=new Sprite(); addChild(s);                            // 11 - POIs
-                       s=new Sprite(); addChild(s);                            // 12 - shields
+        public function Map(initparams:Object) {
 
-                       connection= Connection.getConnection();
+                       this.initparams=initparams;
+                       connection = Connection.getConnection(initparams);
             connection.addEventListener(Connection.NEW_WAY, newWayCreated);
             connection.addEventListener(Connection.NEW_POI, newPOICreated);
-                       connection.getEnvironment(new Responder(gotEnvironment,connectionError));
+            connection.addEventListener(Connection.WAY_RENUMBERED, wayRenumbered);
+            connection.addEventListener(Connection.NODE_RENUMBERED, nodeRenumbered);
+                       gotEnvironment(null);
 
+                       addEventListener(Event.ENTER_FRAME, everyFrame);
         }
 
                public function gotEnvironment(r:Object):void {
-                       init(52.022,-1.2745);
+                       var loader:Loader = new Loader();
+                       loader.contentLoaderInfo.addEventListener(Event.COMPLETE, gotFont);
+                       loader.load(new URLRequest("FontLibrary.swf"));
+               }
+               
+               public function gotFont(r:Event):void {
+                       var FontLibrary:Class = r.target.applicationDomain.getDefinition("FontLibrary") as Class;
+                       Font.registerFont(FontLibrary.DejaVu);
+
+                       if (initparams['lat'] != null) {
+                               // parameters sent from HTML
+                               init(initparams['lat'],
+                                        initparams['lon'],
+                                        initparams['zoom']);
+
+                       } else {
+                               // somewhere innocuous
+                               init(53.09465,-2.56495,17);
+                       }
                }
 
                // ------------------------------------------------------------------------------------------
                // Initialise map at a given lat/lon
 
-        public function init(startlat:Number,startlon:Number):void {
+        public function init(startlat:Number,startlon:Number,startscale:uint=0):void {
+                       while (numChildren) { removeChildAt(0); }
+
+                       tileset=new TileSet(this);                                      // 0 - 900913 background
+                       addChild(tileset);                                                      //   |
+                       tileset.init(tileparams);                                       //   |
 
-                       ruleset.load("test.yaml?d="+Math.random());
-//                     rules.initExample();            // initialise dummy rules
+                       vectorbg = new Sprite();                                        // 1 - vector background layers
+                       addChild(vectorbg);                                                     //   |
 
-                       updateSize();
-                       baselon  =startlon                      -(mapwidth /2)/MASTERSCALE;
-                       basey    =lat2latp(startlat)+(mapheight/2)/MASTERSCALE;
+                       paint = new MapPaint(this,-5,5);                        // 2 - core paint object
+                       addChild(paint);                                                        //   |
+                       paint.isBackground=false;                                       //   |
+
+                       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);
+                       }
+                       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();
                        
         }
@@ -119,13 +154,13 @@ package net.systemeD.halcyon {
                public function updateCoords(tx:Number,ty:Number):void {
                        x=tx; y=ty;
 
-                       // ** calculate tile_l etc.
                        edge_t=coord2lat(-y          );
                        edge_b=coord2lat(-y+mapheight);
                        edge_l=coord2lon(-x          );
                        edge_r=coord2lon(-x+mapwidth );
-                       addDebug("Lon "+edge_l+"-"+edge_r);
-                       addDebug("Lat "+edge_b+"-"+edge_t);
+                       setCentre();
+
+                       tileset.update();
                }
                
                public function updateCoordsFromLatLon(lat:Number,lon:Number):void {
@@ -133,7 +168,22 @@ package net.systemeD.halcyon {
                        var cx:Number=-(lon2coord(lon)-mapwidth/2);
                        updateCoords(cx,cy);
                }
+               
+               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}));
+               }
+               
+               public function nudgeBackground(x:Number,y:Number):void {
+                       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
 
@@ -155,73 +205,130 @@ package net.systemeD.halcyon {
                // ------------------------------------------------------------------------------------------
                // Resize map size based on current stage and height
 
-               public function updateSize():void {
-                       mapwidth =stage.stageWidth; backdrop.width=mapwidth; mask.width=mapwidth;
-                       mapheight=stage.stageHeight; backdrop.height=mapheight; mask.height=mapheight;
-               }
+               public function updateSize(w:uint, h:uint):void {
+                       mapwidth = w; centre_lon=coord2lon(-x+w/2);
+                       mapheight= h; centre_lat=coord2lat(-y+h/2);
 
-               // ------------------------------------------------------------------------------------------
-               // Download map data
-               // (typically from whichways, but will want to add more connections)
+                       this.dispatchEvent(new MapEvent(MapEvent.RESIZE, {width:w, height:h}));
+                       
+            if ( backdrop != null ) {
+                backdrop.width=mapwidth;
+                backdrop.height=mapheight;
+            }
+            if ( mask != null ) {
+                mask.width=mapwidth;
+                mask.height=mapheight;
+            }
+               }
 
+        /** 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} ));
+                       
                        if (edge_l>=bigedge_l && edge_r<=bigedge_r &&
                                edge_b>=bigedge_b && edge_t<=bigedge_t) { return; }     // we have already loaded this area, so ignore
                        bigedge_l=edge_l; bigedge_r=edge_r;
                        bigedge_b=edge_b; bigedge_t=edge_t;
-                       addDebug("Calling with "+edge_l+"-"+edge_r+", "+edge_t+"-"+edge_b);
-                       connection.loadBbox(edge_l,edge_r,edge_t,edge_b);
-               }
-
-/*
-               public function gotBbox(r:Object):void {
-                       addDebug("got whichways");
-                       var code:uint         =r.shift(); if (code) { connectionError(); return; }
-                       var message:String    =r.shift();
-                       var waylist:Array     =r[0];
-                       var pointlist:Array   =r[1];
-                       var relationlist:Array=r[2];
-                       var i:uint, v:uint;
-
-                       for each (var w:Array in waylist) {
-                               i=w[0]; v=w[1];
-                               if (ways[i] && ways[i].version==v) { continue; }
-                               ways[i]=new Way(i,v,this);
-                               ways[i].load(connection);
-                       }
-
-                       for each (var p:Array in pointlist) {
-                               i=p[0]; v=p[4];
-                               if (pois[i] && pois[i].version==v) { continue; }
-                               pois[i]=new POI(i,v,p[1],p[2],p[3],this);
+                       if (connection.waycount>1000) {
+                               connection.purgeOutside(edge_l,edge_r,edge_t,edge_b);
                        }
+                       addDebug("Calling download with "+edge_l+"-"+edge_r+", "+edge_t+"-"+edge_b);
+                       connection.loadBbox(edge_l,edge_r,edge_t,edge_b);
 
-                       addDebug("waylist is "+waylist);
+            // 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 {
             var way:Way = event.entity as Way;
-            ways[way.id] = new WayUI(way, this);
+                       if (!way.loaded || !way.within(edge_l,edge_r,edge_t,edge_b)) { return; }
+                       paint.createWayUI(way);
         }
 
         private function newPOICreated(event:EntityEvent):void {
             var node:Node = event.entity as Node;
-            pois[node.id] = new POI(node, this);
+                       if (!node.within(edge_l,edge_r,edge_t,edge_b)) { return; }
+                       paint.createNodeUI(node);
+        }
+
+               private function wayRenumbered(event:EntityRenumberedEvent):void {
+            var way:Way = event.entity as Way;
+                       paint.renumberWayUI(way,event.oldID);
+               }
+
+               private function nodeRenumbered(event:EntityRenumberedEvent):void {
+            var node:Node = event.entity as Node;
+                       paint.renumberNodeUI(node,event.oldID);
+               }
+
+        public function setHighlight(entity:Entity, settings:Object):void {
+                       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 {
+                       if (paint.wayuis[way.id]) paint.wayuis[way.id].setHighlightOnNodes(settings);
         }
 
+               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;
+                               }
+                       }
+               }
+
+        // Handle mouse events on ways/nodes
+        private var mapController:MapController = null;
+
+        public function setController(controller:MapController):void {
+            this.mapController = controller;
+        }
+
+        public function entityMouseEvent(event:MouseEvent, entity:Entity):void {
+            if ( mapController != null )
+                mapController.entityMouseEvent(event, entity);
+                               
+        }
+
+               // ------------------------------------------------------------------------------------------
+               // Add vector layer
+               
+               public function addVectorLayer(layer:VectorLayer):void {
+                       vectorlayers[layer.name]=layer;
+                       vectorbg.addChild(layer.paint);
+               }
+
                // ------------------------------------------------------------------------------------------
                // Redraw all items, zoom in and out
                
+               public function updateEntityUIs(redraw:Boolean,remove:Boolean):void {
+                       paint.updateEntityUIs(connection.getObjectsByBbox(edge_l, edge_r, edge_t, edge_b), redraw, remove);
+                       for each (var v:VectorLayer in vectorlayers) {
+                               v.paint.updateEntityUIs(v.getObjectsByBbox(edge_l, edge_r, edge_t, edge_b), redraw, remove);
+                       }
+               }
                public function redraw():void {
-                       for each (var w:WayUI in ways) { w.redraw(); }
-                       for each (var p:POI in pois) { p.redraw(); }
+                       paint.redraw();
+                       for each (var v:VectorLayer in vectorlayers) { v.paint.redraw(); }
                }
-
-               public function redrawPOIs():void {
-                       for each (var p:POI in pois) { p.redraw(); }
+               public function redrawPOIs():void { 
+                       paint.redrawPOIs();
+                       for each (var v:VectorLayer in vectorlayers) { v.paint.redrawPOIs(); }
                }
-
+               
                public function zoomIn():void {
                        if (scale==MAXSCALE) { return; }
                        changeScale(scale+1);
@@ -235,16 +342,53 @@ package net.systemeD.halcyon {
                private function changeScale(newscale:uint):void {
                        addDebug("new scale "+newscale);
                        scale=newscale;
-                       scalefactor=MASTERSCALE/Math.pow(2,14-scale);
+                       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);
+                       updateEntityUIs(true,true);
                        download();
-                       redraw();
                }
 
                private function reportPosition():void {
                        addDebug("lon "+coord2lon(mouseX)+", lat "+coord2lat(mouseY));
                }
+               
+               public function setStyle(url:String):void {
+                       styleurl=url;
+                       if (paint) { 
+                               paint.ruleset=new RuleSet(MINSCALE,MAXSCALE,redraw,redrawPOIs);
+                               paint.ruleset.loadFromCSS(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); }
+               }
+
+               // ------------------------------------------------------------------------------------------
+               // Export (experimental)
+               // ** just a bit of fun for now!
+               // really needs to take a bbox, and make sure that the image is correctly cropped/resized 
+               // to that area (will probably require creating a new DisplayObject with a different origin
+               // and mask)
+/*             
+               public function export():void {
+                       addDebug("size is "+this.width+","+this.height);
+                       var jpgSource:BitmapData = new BitmapData(800,800); // (this.width, this.height);
+                       jpgSource.draw(this);
+                       var jpgEncoder:JPGEncoder = new JPGEncoder(85);
+                       var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
+                       var fileRef:FileReference = new FileReference();
+//                     fileRef.save(jpgStream,'map.jpeg');
+               }
+
+*/
 
                // ==========================================================================================
                // Events
@@ -252,34 +396,64 @@ package net.systemeD.halcyon {
                // ------------------------------------------------------------------------------------------
                // Mouse events
                
+               public function set draggable(draggable:Boolean):void {
+                       _draggable=draggable;
+                       dragstate=NOT_DRAGGING;
+               }
+
                public function mouseDownHandler(event:MouseEvent):void {
-                       dragging=true;
-                       lastxmouse=mouseX; lastymouse=mouseY;
+                       if (!_draggable) { return; }
+                       dragstate=NOT_MOVED;
+                       lastxmouse=mouseX; downX=stage.mouseX;
+                       lastymouse=mouseY; downY=stage.mouseY;
+                       downTime=new Date().getTime();
                }
         
-               public function mouseUpHandler(event:MouseEvent):void {
-                       if (!dragging) { return; }
-                       dragging=false;
-                       updateCoords(x,y);
-                       download();
+               public function mouseUpHandler(event:MouseEvent=null):void {
+                       if (dragstate==DRAGGING) { moveMap(0,0); }
+                       dragstate=NOT_DRAGGING;
                }
         
                public function mouseMoveHandler(event:MouseEvent):void {
-                       if (!dragging) { return; }
+                       if (!_draggable) { return; }
+                       if (dragstate==NOT_DRAGGING) { return; }
+                       
+                       if (dragstate==NOT_MOVED) {
+                               if (new Date().getTime()-downTime<300) {
+                                       if (Math.abs(downX-stage.mouseX)<=TOLERANCE   && Math.abs(downY-stage.mouseY)<=TOLERANCE  ) return;
+                               } else {
+                                       if (Math.abs(downX-stage.mouseX)<=TOLERANCE/2 && Math.abs(downY-stage.mouseY)<=TOLERANCE/2) return;
+                               }
+                               dragstate=DRAGGING;
+                       }
+                       
                        x+=mouseX-lastxmouse;
                        y+=mouseY-lastymouse;
                        lastxmouse=mouseX; lastymouse=mouseY;
+                       setCentre();
                }
         
+               // ------------------------------------------------------------------------------------------
+               // Do every frame
+
+               private function everyFrame(event:Event):void {
+                       if (tileset) { tileset.serviceQueue(); }
+               }
+
                // ------------------------------------------------------------------------------------------
                // Miscellaneous events
                
                public function keyUpHandler(event:KeyboardEvent):void {
-addDebug("pressed "+event.keyCode);
-                       if (event.keyCode==82) { this.redraw(); }                       // R - redraw
-                       if (event.keyCode==73) { this.zoomIn(); }                       // I - zoom in
-                       if (event.keyCode==79) { this.zoomOut(); }                      // O - zoom out
-                       if (event.keyCode==76) { this.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 {
@@ -289,10 +463,18 @@ addDebug("pressed "+event.keyCode);
                // ------------------------------------------------------------------------------------------
                // 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;
                }
-               
+
        }
 }