Support inline status, where the status field is part of e.g. map calls
[potlatch2.git] / net / systemeD / halcyon / EntityUI.as
index b4ba411..bca513b 100644 (file)
@@ -4,27 +4,38 @@ package net.systemeD.halcyon {
        import flash.events.MouseEvent;
        import flash.text.AntiAliasType;
        import flash.text.GridFitType;
-       import net.systemeD.halcyon.Globals;
        import net.systemeD.halcyon.styleparser.StyleList;
-       import net.systemeD.halcyon.styleparser.RuleSet;
     import net.systemeD.halcyon.connection.*;
 
+       /** Parent class of representations of map Entities, with properties about how they should be drawn. */ 
        public class EntityUI {
 
+               /** The entity represented by this class. */
                protected var entity:Entity;
-               protected var styleList:StyleList;                              // current StyleList for this entity
-               protected var sprites:Array=new Array();                // instances in display list
-               protected var listenSprite:Sprite=new Sprite(); // clickable sprite to receive events
-               protected var hitzone:Sprite;                                   // hitzone for above
-               protected var stateClasses:Object=new Object(); // special context-sensitive classes, e.g. :hover
-               protected var layer:Number=0;                                   // map layer
-               protected var suspended:Boolean=false;                  // suspend redrawing?
-               protected var redrawDue:Boolean=false;                  // redraw called while suspended?
-               protected var clearLimit:uint=0;                                // sprite to clear back to
-               public var paint:MapPaint;                                              // reference to parent MapPaint
-               public var ruleset:RuleSet;                                             // reference to ruleset in operation
-               public var interactive:Boolean=true;                    // does object respond to clicks?
-               public var purgable:Boolean=true;                               // can it be deleted when offscreen?
+               /** Current StyleList for this entity. */
+               protected var styleList:StyleList;
+               /** Instances in display list */
+               protected var sprites:Array=new Array();
+               /** The clickable sprite that will receive events. */
+               protected var listenSprite:Sprite=new Sprite();
+               /** Hitzone for the sprite - must be set by subclass-specific code. */
+               protected var hitzone:Sprite;
+               /** Special context-sensitive classes such as :hover. */
+               protected var stateClasses:Object=new Object();
+               /** Map layer */
+               protected var layer:Number=0;
+               /** Is drawing suspended? */
+               protected var suspended:Boolean=false;  
+               /** Redraw called while suspended? */
+               protected var redrawDue:Boolean=false;
+               /** Sprite to clear back to */
+               protected var clearLimit:uint=0;
+               /** Reference to parent MapPaint */
+               public var paint:MapPaint;      
+               /** Does object respond to clicks? */
+               public var interactive:Boolean=true;
+               /** Can it be deleted when offscreen? */
+               public var purgable:Boolean=true;
 
                protected const FILLSPRITE:uint=0;
                protected const CASINGSPRITE:uint=1;
@@ -39,25 +50,29 @@ package net.systemeD.halcyon {
                        gridFitType: GridFitType.NONE
                };
 
+               /** Constructor function, adds a bunch of event listeners. */
                public function EntityUI(entity:Entity, paint:MapPaint) {
                        this.entity=entity;
                        this.paint=paint;
-            entity.addEventListener(Connection.TAG_CHANGED, tagChanged);
-                       entity.addEventListener(Connection.ADDED_TO_RELATION, relationAdded);
-                       entity.addEventListener(Connection.REMOVED_FROM_RELATION, relationRemoved);
-                       entity.addEventListener(Connection.SUSPEND_REDRAW, suspendRedraw);
-                       entity.addEventListener(Connection.RESUME_REDRAW, resumeRedraw);
-                       listenSprite.addEventListener(MouseEvent.CLICK, mouseEvent);
-                       listenSprite.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEvent);
-                       listenSprite.addEventListener(MouseEvent.ROLL_OVER, mouseEvent);
-                       listenSprite.addEventListener(MouseEvent.MOUSE_OUT, mouseEvent);
-                       listenSprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseEvent);
-                       listenSprite.addEventListener(MouseEvent.MOUSE_UP, mouseEvent);
-                       listenSprite.addEventListener(MouseEvent.MOUSE_MOVE, mouseEvent);
+            entity.addEventListener(Connection.TAG_CHANGED, tagChanged, false, 0, true);
+            entity.addEventListener(Connection.STATUS_CHANGED, statusChanged, false, 0, true);
+                       entity.addEventListener(Connection.ADDED_TO_RELATION, relationAdded, false, 0, true);
+                       entity.addEventListener(Connection.REMOVED_FROM_RELATION, relationRemoved, false, 0, true);
+                       entity.addEventListener(Connection.SUSPEND_REDRAW, suspendRedraw, false, 0, true);
+                       entity.addEventListener(Connection.RESUME_REDRAW, resumeRedraw, false, 0, true);
+                       listenSprite.addEventListener(MouseEvent.CLICK, mouseEvent, false, 0, true);
+                       listenSprite.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEvent, false, 0, true);
+                       listenSprite.addEventListener(MouseEvent.ROLL_OVER, mouseEvent, false, 0, true);
+                       listenSprite.addEventListener(MouseEvent.MOUSE_OUT, mouseEvent, false, 0, true);
+                       listenSprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseEvent, false, 0, true);
+                       listenSprite.addEventListener(MouseEvent.MOUSE_UP, mouseEvent, false, 0, true);
+                       listenSprite.addEventListener(MouseEvent.MOUSE_MOVE, mouseEvent, false, 0, true);
                }
 
+               /** Remove the default event listeners. */
                protected function removeGenericEventListeners():void {
             entity.removeEventListener(Connection.TAG_CHANGED, tagChanged);
+            entity.removeEventListener(Connection.STATUS_CHANGED, statusChanged);
                        entity.removeEventListener(Connection.ADDED_TO_RELATION, relationAdded);
                        entity.removeEventListener(Connection.REMOVED_FROM_RELATION, relationRemoved);
                        entity.removeEventListener(Connection.SUSPEND_REDRAW, suspendRedraw);
@@ -77,12 +92,19 @@ package net.systemeD.halcyon {
                protected function attachRelationListeners():void {
                    var relations:Array = entity.parentRelations;
             for each(var relation:Relation in relations ) {
-                relation.addEventListener(Connection.TAG_CHANGED, relationTagChanged);
+                relation.addEventListener(Connection.TAG_CHANGED, relationTagChanged, false, 0, true);
+            }
+               }
+
+               protected function removeRelationListeners():void {
+                       var relations:Array = entity.parentRelations;
+            for each(var relation:Relation in relations) {
+                relation.removeEventListener(Connection.TAG_CHANGED, relationTagChanged);
             }
                }
 
                protected function relationAdded(event:RelationMemberEvent):void {
-                   event.relation.addEventListener(Connection.TAG_CHANGED, relationTagChanged);
+                   event.relation.addEventListener(Connection.TAG_CHANGED, relationTagChanged, false, 0, true);
                        invalidateStyleList();
                    redraw();
                }
@@ -103,6 +125,11 @@ package net.systemeD.halcyon {
             redraw();
         }
                
+        protected function statusChanged(event:EntityEvent):void {
+                       invalidateStyleList();
+            redraw();
+        }
+
         protected function mouseEvent(event:MouseEvent):void {
                        paint.map.entityMouseEvent(event, entity);
         }
@@ -110,15 +137,15 @@ package net.systemeD.halcyon {
 
                // -----------------------------------------------------------------
 
-               // Add object (stroke/fill/roadname) to layer sprite
+               /** Add object (stroke/fill/roadname) to layer sprite*/
                
-               protected function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void {
+               protected function addToLayer(s:DisplayObject,spritetype:uint,sublayer:int=-1):void {
                        var l:Sprite, o:Sprite;
                        if (sublayer!=-1) {
-                               o=paint.sublayer(layer,sublayer);
+                               o=paint.sublayer(layer,spritetype,sublayer);
                        } else {
                                l=paint.getPaintSpriteAt(layer);
-                               o=l.getChildAt(t) as Sprite;
+                               o=l.getChildAt(spritetype) as Sprite;
                        }
                        o.addChild(s);
                        if (sprites.indexOf(s)==-1) { sprites.push(s); }
@@ -146,6 +173,7 @@ package net.systemeD.halcyon {
                        }
                }
 
+               /** Remove all sprites associated with this entity, and clear hitzone. */
                public function removeSprites():void {
                        while (sprites.length>clearLimit) {
                                var d:DisplayObject=sprites.pop();
@@ -157,6 +185,13 @@ package net.systemeD.halcyon {
                        }
                }
                
+               public function removeListenSprite():void {
+                       if (listenSprite && listenSprite.parent) listenSprite.parent.removeChild(listenSprite);
+                       if (hitzone && hitzone.parent) hitzone.parent.removeChild(hitzone);
+                       listenSprite=null;
+                       hitzone=null;
+               }
+               
                public function protectSprites():void { clearLimit=sprites.length; }
                public function unprotectSprites():void { clearLimit=0; }
 
@@ -207,23 +242,25 @@ package net.systemeD.halcyon {
                        return "[EntityUI "+entity+"]";
                }
 
-               // Redraw control
+               /** Request redraw */
                
                public function redraw():Boolean {
                        if (suspended) { redrawDue=true; return false; }
                        return doRedraw();
                }
                
+               /** Actually do the redraw. To be overwritten. */
                public function doRedraw():Boolean {
-                       // to be overwritten
                        return false;
                }
                
+               /** Temporarily suspend redrawing of object. */
                public function suspendRedraw(event:EntityEvent):void {
                        suspended=true;
                        redrawDue=false;
                }
                
+               /** Resume redrawing. */
                public function resumeRedraw(event:EntityEvent):void {
                        suspended=false;
                        if (redrawDue) {