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