beginning of vector background layers. And a whole load of other stuff too
[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=paint.getChildAt(layer-paint.minlayer);
52                         var o:DisplayObject=Sprite(l).getChildAt(t);
53                         if (sublayer!=-1) { o=Sprite(o).getChildAt(sublayer); }
54                         Sprite(o).addChild(s);
55                         sprites.push(s);
56             if ( s is Sprite ) {
57                 Sprite(s).mouseChildren = false;
58                 Sprite(s).mouseEnabled = false;
59             }
60                 }
61
62
63                 public function removeSprites():void {
64                         while (sprites.length>0) {
65                                 var d:DisplayObject=sprites.pop();
66                                 if (d.parent) { d.parent.removeChild(d); }
67                         }
68                         listenSprite.hitArea=null;
69                 }
70
71                 protected function setListenSprite(hitzone:Sprite):void {
72                         if (!listenSprite.parent) { addToLayer(listenSprite, CLICKSPRITE); }
73             listenSprite.hitArea = hitzone;
74             listenSprite.buttonMode = true;
75             listenSprite.mouseChildren = true;
76             listenSprite.mouseEnabled = true;
77                 }
78
79         protected function mouseEvent(event:MouseEvent):void {
80         }
81
82         public function setHighlight(stateType:String, isOn:*):void {
83             if ( isOn && stateClasses[stateType] == null ) {
84                 stateClasses[stateType] = isOn;
85             } else if ( !isOn && stateClasses[stateType] != null ) {
86                 delete stateClasses[stateType];
87             }
88         }
89
90                 protected function applyStateClasses(tags:Object):Object {
91             for (var stateKey:String in stateClasses) {
92                 tags[":"+stateKey] = 'yes';
93             }
94                         return tags;
95                 }
96                 
97                 // Redraw control
98                 
99                 public function redraw(sl:StyleList=null):Boolean {
100                         if (suspended) { redrawStyleList=sl; redrawDue=true; return false; }
101                         return doRedraw(sl);
102                 }
103                 
104                 public function doRedraw(sl:StyleList):Boolean {
105                         // to be overwritten
106                         return false;
107                 }
108                 
109                 public function suspendRedraw(event:EntityEvent):void {
110                         suspended=true;
111                         redrawDue=false;
112                 }
113                 
114                 public function resumeRedraw(event:EntityEvent):void {
115                         suspended=false;
116                         if (redrawDue) { 
117                                 doRedraw(redrawStyleList);
118                                 redrawDue=false;
119                                 redrawStyleList=null; 
120                         }
121                 }
122
123         }
124
125 }