fix horrid merge bug (yay!). Implement suspend/resume events for redrawing, to speed...
[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.connection.EntityEvent;
10
11         public class EntityUI {
12
13                 protected var sprites:Array=new Array();                // instances in display list
14         protected var listenSprite:Sprite;                              // clickable sprite to receive events
15                 protected var stateClasses:Object=new Object(); // special context-sensitive classes, e.g. :hover
16                 protected var layer:int=0;                                              // map layer
17                 protected var suspended:Boolean=false;                  // suspend redrawing?
18                 protected var redrawDue:Boolean=false;                  // redraw called while suspended?
19                 protected var redrawStyleList:StyleList;                // stylelist to be used when redrawing?
20                 public var map:Map;                                                             // reference to parent map
21
22                 protected const FILLSPRITE:uint=0;
23                 protected const CASINGSPRITE:uint=1;
24                 protected const STROKESPRITE:uint=2;
25                 protected const NAMESPRITE:uint=3;
26                 protected const NODESPRITE:uint=4;
27                 protected const CLICKSPRITE:uint=5;
28
29                 public static const DEFAULT_TEXTFIELD_PARAMS:Object = {
30                         embedFonts: true,
31                         antiAliasType: AntiAliasType.ADVANCED,
32                         gridFitType: GridFitType.NONE
33                 };
34
35                 public function EntityUI() {
36                 }
37
38                 // Add object (stroke/fill/roadname) to layer sprite
39                 
40                 protected function addToLayer(s:DisplayObject,t:uint,sublayer:int=-1):void {
41                         var l:DisplayObject=Map(map).getChildAt(map.WAYSPRITE+layer);
42                         var o:DisplayObject=Sprite(l).getChildAt(t);
43                         if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); }
44                         Sprite(o).addChild(s);
45                         sprites.push(s);
46             if ( s is Sprite ) {
47                 Sprite(s).mouseEnabled = false;
48                 Sprite(s).mouseChildren = false;
49             }
50                 }
51                 
52                 public function removeSprites():void {
53                         while (sprites.length>0) {
54                                 var d:DisplayObject=sprites.pop();
55                                 if (d.parent) { d.parent.removeChild(d); }
56                         }
57                         listenSprite=null;
58                 }
59
60                 protected function createListenSprite(hitzone:Sprite):void {
61             if ( listenSprite == null ) {
62                 listenSprite = new Sprite();
63                 listenSprite.addEventListener(MouseEvent.CLICK, mouseEvent);
64                 listenSprite.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEvent);
65                 listenSprite.addEventListener(MouseEvent.MOUSE_OVER, mouseEvent);
66                 listenSprite.addEventListener(MouseEvent.MOUSE_OUT, mouseEvent);
67                 listenSprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseEvent);
68                 listenSprite.addEventListener(MouseEvent.MOUSE_UP, mouseEvent);
69                 listenSprite.addEventListener(MouseEvent.MOUSE_MOVE, mouseEvent);
70             }
71             listenSprite.hitArea = hitzone;
72             addToLayer(listenSprite, CLICKSPRITE);
73             listenSprite.buttonMode = true;
74             listenSprite.mouseEnabled = true;
75                 }
76
77         protected function mouseEvent(event:MouseEvent):void {
78         }
79
80         public function setHighlight(stateType:String, isOn:*):void {
81             if ( isOn && stateClasses[stateType] == null ) {
82                 stateClasses[stateType] = isOn;
83             } else if ( !isOn && stateClasses[stateType] != null ) {
84                 delete stateClasses[stateType];
85             }
86         }
87
88                 protected function applyStateClasses(tags:Object):Object {
89             for (var stateKey:String in stateClasses) {
90                 tags[":"+stateKey] = 'yes';
91             }
92                         return tags;
93                 }
94                 
95                 // Redraw control
96                 
97                 public function redraw(sl:StyleList=null):Boolean {
98                         if (suspended) { redrawStyleList=sl; redrawDue=true; return false; }
99                         return doRedraw(sl);
100                 }
101                 
102                 public function doRedraw(sl:StyleList):Boolean {
103                         // to be overwritten
104                         return false;
105                 }
106                 
107                 public function suspendRedraw(event:EntityEvent):void {
108                         suspended=true;
109                         redrawDue=false;
110                 }
111                 
112                 public function resumeRedraw(event:EntityEvent):void {
113                         suspended=false;
114                         if (redrawDue) { 
115                                 doRedraw(redrawStyleList);
116                                 redrawDue=false;
117                                 redrawStyleList=null; 
118                         }
119                 }
120
121         }
122
123 }