multipolygon fix
[potlatch2.git] / net / systemeD / halcyon / EntityUI.as
1 package net.systemeD.halcyon {
2
3         import flash.display.*;
4         import flash.events.MouseEvent;
5         import flash.text.AntiAliasType;
6         import flash.text.GridFitType;
7         import net.systemeD.halcyon.Globals;
8         import net.systemeD.halcyon.styleparser.StyleList;
9         import net.systemeD.halcyon.styleparser.RuleSet;
10     import net.systemeD.halcyon.connection.*;
11
12         public class EntityUI {
13
14                 protected var entity:Entity;
15                 protected var sprites:Array=new Array();                // instances in display list
16         protected var listenSprite:Sprite=new Sprite(); // clickable sprite to receive events
17                 protected var stateClasses:Object=new Object(); // special context-sensitive classes, e.g. :hover
18                 protected var layer:int=0;                                              // map layer
19                 protected var interactive:Boolean=true;                 // does it respond to connection events?
20                 protected var suspended:Boolean=false;                  // suspend redrawing?
21                 protected var redrawDue:Boolean=false;                  // redraw called while suspended?
22                 protected var redrawStyleList:StyleList;                // stylelist to be used when redrawing?
23                 public var paint:MapPaint;                                              // reference to parent MapPaint
24                 public var ruleset:RuleSet;                                             // reference to ruleset in operation
25
26                 protected const FILLSPRITE:uint=0;
27                 protected const CASINGSPRITE:uint=1;
28                 protected const STROKESPRITE:uint=2;
29                 protected const NAMESPRITE:uint=3;
30                 protected const NODESPRITE:uint=4;
31                 protected const CLICKSPRITE:uint=5;
32
33                 public static const DEFAULT_TEXTFIELD_PARAMS:Object = {
34                         embedFonts: true,
35                         antiAliasType: AntiAliasType.ADVANCED,
36                         gridFitType: GridFitType.NONE
37                 };
38
39                 public function EntityUI(entity:Entity, paint:MapPaint, interactive:Boolean) {
40                         this.entity=entity;
41                         this.paint=paint;
42                         this.interactive=interactive;
43             entity.addEventListener(Connection.TAG_CHANGED, tagChanged);
44                         entity.addEventListener(Connection.ADDED_TO_RELATION, relationAdded);
45                         entity.addEventListener(Connection.REMOVED_FROM_RELATION, relationRemoved);
46                         entity.addEventListener(Connection.SUSPEND_REDRAW, suspendRedraw);
47                         entity.addEventListener(Connection.RESUME_REDRAW, resumeRedraw);
48                         listenSprite.addEventListener(MouseEvent.CLICK, mouseEvent);
49                         listenSprite.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEvent);
50                         listenSprite.addEventListener(MouseEvent.ROLL_OVER, mouseEvent);
51                         listenSprite.addEventListener(MouseEvent.MOUSE_OUT, mouseEvent);
52                         listenSprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseEvent);
53                         listenSprite.addEventListener(MouseEvent.MOUSE_UP, mouseEvent);
54                         listenSprite.addEventListener(MouseEvent.MOUSE_MOVE, mouseEvent);
55                 }
56
57
58                 // -----------------------------------------------------------------
59                 // Event listeners
60                 
61                 protected function attachRelationListeners():void {
62                     var relations:Array = entity.parentRelations;
63             for each(var relation:Relation in relations ) {
64                 relation.addEventListener(Connection.TAG_CHANGED, relationTagChanged);
65             }
66                 }
67
68                 protected function relationAdded(event:RelationMemberEvent):void {
69                     event.relation.addEventListener(Connection.TAG_CHANGED, relationTagChanged);
70                     redraw();
71                 }
72                 
73                 protected function relationRemoved(event:RelationMemberEvent):void {
74                     event.relation.removeEventListener(Connection.TAG_CHANGED, relationTagChanged);
75                     redraw();
76                 }
77                 
78         protected function tagChanged(event:TagEvent):void {
79             redraw();
80         }
81
82         protected function relationTagChanged(event:TagEvent):void {
83             redraw();
84         }
85                 
86         protected function mouseEvent(event:MouseEvent):void {
87                         paint.map.entityMouseEvent(event, entity);
88         }
89
90
91                 // -----------------------------------------------------------------
92
93                 // Add object (stroke/fill/roadname) to layer sprite
94                 
95                 protected function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void {
96                         var l:DisplayObject, o:Sprite;
97                         if (sublayer!=-1) {
98                                 o=paint.sublayer(layer,sublayer);
99                         } else {
100                                 l=paint.getChildAt(layer-paint.minlayer);
101                                 o=(l as Sprite).getChildAt(t) as Sprite;
102                         }
103                         o.addChild(s);
104                         sprites.push(s);
105             if ( s is Sprite ) {
106                 Sprite(s).mouseChildren = false;
107                 Sprite(s).mouseEnabled = false;
108             }
109                 }
110
111
112                 public function removeSprites():void {
113                         while (sprites.length>0) {
114                                 var d:DisplayObject=sprites.pop();
115                                 if (d.parent) { d.parent.removeChild(d); }
116                         }
117                         listenSprite.hitArea=null;
118                 }
119
120                 protected function offsetSprites(x:Number, y:Number):void {
121                         for each (var d:DisplayObject in sprites) {
122                                 d.x=x; d.y=y;
123                         }
124                 }
125
126                 protected function setListenSprite(hitzone:Sprite):void {
127                         if (!listenSprite.parent) { addToLayer(listenSprite, CLICKSPRITE); }
128             listenSprite.hitArea = hitzone;
129             listenSprite.buttonMode = true;
130             listenSprite.mouseChildren = true;
131             listenSprite.mouseEnabled = true;
132                 }
133
134         public function setHighlight(stateType:String, isOn:*):void {
135             if ( isOn && stateClasses[stateType] == null ) {
136                 stateClasses[stateType] = isOn;
137             } else if ( !isOn && stateClasses[stateType] != null ) {
138                 delete stateClasses[stateType];
139             }
140         }
141
142                 protected function applyStateClasses(tags:Object):Object {
143             for (var stateKey:String in stateClasses) {
144                 tags[":"+stateKey] = 'yes';
145             }
146                         return tags;
147                 }
148                 
149                 public function toString():String {
150                         return "[EntityUI "+entity+"]";
151                 }
152
153                 // Redraw control
154                 
155                 public function redraw(sl:StyleList=null):Boolean {
156                         if (suspended) { redrawStyleList=sl; redrawDue=true; return false; }
157                         return doRedraw(sl);
158                 }
159                 
160                 public function doRedraw(sl:StyleList):Boolean {
161                         // to be overwritten
162                         return false;
163                 }
164                 
165                 public function suspendRedraw(event:EntityEvent):void {
166                         suspended=true;
167                         redrawDue=false;
168                 }
169                 
170                 public function resumeRedraw(event:EntityEvent):void {
171                         suspended=false;
172                         if (redrawDue) { 
173                                 doRedraw(redrawStyleList);
174                                 redrawDue=false;
175                                 redrawStyleList=null; 
176                         }
177                 }
178
179         }
180
181 }