fix some bugs in feature selector, and awesome stuff
[potlatch2.git] / net / systemeD / halcyon / WayUI.as
index 15490bdf3c81c4687287e6992bb758b36981702f..fe0527aedd96befe2b276d074cf7ba5609c53f2a 100755 (executable)
@@ -8,6 +8,7 @@ package net.systemeD.halcyon {
        import flash.text.GridFitType;
        import flash.text.TextField;
        import flash.text.TextFormat;
+       import flash.events.*;
        import net.systemeD.halcyon.styleparser.*;
     import net.systemeD.halcyon.connection.*;
 
@@ -21,14 +22,13 @@ package net.systemeD.halcyon {
                public var layer:int=0;                                         // map layer
                public var map:Map;                                                     // reference to parent map
                public var sprites:Array=new Array();           // instances in display list
+        private var hitzone:Sprite;
 
                public static const DEFAULT_TEXTFIELD_PARAMS:Object = {
                        embedFonts: true,
                        antiAliasType: AntiAliasType.ADVANCED,
                        gridFitType: GridFitType.NONE
                };
-               [Embed(source="fonts/DejaVuSans.ttf", fontFamily="DejaVu", fontWeight="normal", mimeType="application/x-font-truetype")]
-               public static var DejaVu:Class;
                public var nameformat:TextFormat;
 
 
@@ -36,8 +36,13 @@ package net.systemeD.halcyon {
                        this.way = way;
                        this.map = map;
             init();
+            way.addEventListener(Connection.TAG_CHANGE, wayTagChanged);
                }
                
+        private function wayTagChanged(event:TagEvent):void {
+            redraw();
+        }
+
                private function init():void {
                        recalculate();
                        redraw();
@@ -55,23 +60,23 @@ package net.systemeD.halcyon {
                        pathlength=0;
                        patharea=0;
                        
+                       lx = way.getNode(way.length-1).lon;
+                       ly = way.getNode(way.length-1).latp;
                        for ( var i:uint = 0; i < way.length; i++ ) {
                 var node:Node = way.getNode(i);
                 var latp:Number = node.latp;
                 var lon:Number  = node.lon;
-                               if ( !isNaN(lx) ) {
-                    pathlength += Math.sqrt( Math.pow(lon-lx,2)+Math.pow(latp-ly,2) );
-                                       patharea += lx*latp-lon*ly;
-                                       sc = (lx*latp-lon*ly); 
-                                       cx += (lx+lon)*sc;
-                                       cy += (ly+latp)*sc;
-                }
+                               if ( i>0 ) { pathlength += Math.sqrt( Math.pow(lon-lx,2)+Math.pow(latp-ly,2) ); }
+                               sc = (lx*latp-lon*ly)*map.scalefactor;
+                               cx += (lx+lon)*sc;
+                               cy += (ly+latp)*sc;
+                               patharea += sc;
                                lx=lon; ly=latp;
                        }
 
                        pathlength*=map.scalefactor;
-                       patharea*=map.scalefactor/2;
-                       if (patharea>0 && way.isArea()) {
+                       patharea/=2;
+                       if (patharea!=0 && way.isArea()) {
                                centroid_x=map.lon2coord(cx/patharea/6);
                                centroid_y=map.latp2coord(cy/patharea/6);
                        } else if (pathlength>0) {
@@ -89,7 +94,7 @@ package net.systemeD.halcyon {
 
                        // remove all currently existing sprites
                        while (sprites.length>0) {
-                               var d:Sprite=sprites.pop(); d.parent.removeChild(d);
+                               var d:DisplayObject=sprites.pop(); d.parent.removeChild(d);
                        }
 
                        // which layer?
@@ -102,14 +107,14 @@ package net.systemeD.halcyon {
                        for each (var s:* in styles) {
 
                                if (s is ShapeStyle) {
-                                       var stroke:Sprite, fill:Sprite, roadname:Sprite, f:Graphics, g:Graphics;
+                                       var stroke:Shape, fill:Shape, roadname:Sprite, f:Graphics, g:Graphics;
                                        var doStroke:Boolean=false, doDashed:Boolean=false;
                                        var doFill:Boolean=false, fill_colour:uint, fill_opacity:Number;
                                        var doCasing:Boolean=false, doDashedCasing:Boolean=false;
 
                                        // Set stroke style
                                        if (s.isStroked)  {
-                                               stroke=new Sprite(); addToLayer(stroke,1,s.sublayer); g=stroke.graphics;
+                                               stroke=new Shape(); addToLayer(stroke,1,s.sublayer); g=stroke.graphics;
                                g.moveTo(map.lon2coord(way.getNode(0).lon), map.latp2coord(way.getNode(0).latp));
                                                g.lineStyle(s.stroke_width, s.stroke_colour, s.stroke_opacity/100,
                                                                        false, "normal", s.stroke_linecap, s.stroke_linejoin);
@@ -117,7 +122,7 @@ package net.systemeD.halcyon {
 
                                        // Set fill and casing style
                                        if (s.isFilled || s.isCased) {
-                                               fill=new Sprite(); addToLayer(fill,0); f=fill.graphics;
+                                               fill=new Shape(); addToLayer(fill,0); f=fill.graphics;
                                f.moveTo(map.lon2coord(way.getNode(0).lon), map.latp2coord(way.getNode(0).latp));
                                                if (s.isCased)  { f.lineStyle(s.casing_width, s.casing_colour, s.casing_opacity/100,
                                                                                  false, "normal", s.stroke_linecap, s.stroke_linejoin); }
@@ -151,6 +156,32 @@ package net.systemeD.halcyon {
                                        // ** to do
                                }
                        }
+
+            if ( styles.length == 0 ) {
+                // there's no styles... so add a thin trace
+                var def:Sprite = new Sprite();
+                def.graphics.lineStyle(0.5, 0x808080, 1, false, "normal");
+                solidLine(def.graphics);
+                addToLayer(def, 1);
+            }
+
+            // create a generic "way" hitzone sprite
+            hitzone = new Sprite();
+            hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
+            solidLine(hitzone.graphics);
+            addToLayer(hitzone, 2);
+            hitzone.visible = false;
+
+            var listenSprite:Sprite = new Sprite();
+            listenSprite.hitArea = hitzone;
+            addToLayer(listenSprite, 2);
+            listenSprite.buttonMode = true;
+            listenSprite.mouseEnabled = true;
+            listenSprite.addEventListener(MouseEvent.CLICK, mouseEvent);
+            listenSprite.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEvent);
+            listenSprite.addEventListener(MouseEvent.MOUSE_OVER, mouseEvent);
+            listenSprite.addEventListener(MouseEvent.MOUSE_OUT, mouseEvent);
+
                }
                
                // ------------------------------------------------------------------------------------------
@@ -283,8 +314,10 @@ package net.systemeD.halcyon {
 
                private function rotatedLetter(char:String, t:Number, w:Number, h:Number, a:Number, o:Number):TextField {
                        var tf:TextField = new TextField();
-                       tf.embedFonts = true;
+            tf.mouseEnabled = false;
+            tf.mouseWheelEnabled = false;
                        tf.defaultTextFormat = nameformat;
+                       tf.embedFonts = true;
                        tf.text = char;
                        tf.width = tf.textWidth+4;
                        tf.height = tf.textHeight+4;
@@ -301,12 +334,21 @@ package net.systemeD.halcyon {
                
                // Add object (stroke/fill/roadname) to layer sprite
                
-               private function addToLayer(s:Sprite,t:uint,sublayer:int=-1):void {
+               private function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void {
                        var l:DisplayObject=Map(map).getChildAt(layer);
                        var o:DisplayObject=Sprite(l).getChildAt(t);
                        if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); }
                        Sprite(o).addChild(s);
                        sprites.push(s);
+            if ( s is Sprite ) Sprite(s).mouseEnabled = false;
                }
+
+        private function mouseEvent(event:MouseEvent):void {
+            map.wayMouseEvent(event, way);
+        }
+
+        public function setHighlight(highlight:Boolean):void {
+            hitzone.visible = highlight;
+        }
        }
 }