drag ways (still needs undo though)
[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 offsetSprites(x:Number, y:Number):void {
72                         for each (var d:DisplayObject in sprites) {
73                                 d.x=x; d.y=y;
74                         }
75                 }
76
77                 protected function setListenSprite(hitzone:Sprite):void {
78                         if (!listenSprite.parent) { addToLayer(listenSprite, CLICKSPRITE); }
79             listenSprite.hitArea = hitzone;
80             listenSprite.buttonMode = true;
81             listenSprite.mouseChildren = true;
82             listenSprite.mouseEnabled = true;
83                 }
84
85         protected function mouseEvent(event:MouseEvent):void {
86         }
87
88         public function setHighlight(stateType:String, isOn:*):void {
89             if ( isOn && stateClasses[stateType] == null ) {
90                 stateClasses[stateType] = isOn;
91             } else if ( !isOn && stateClasses[stateType] != null ) {
92                 delete stateClasses[stateType];
93             }
94         }
95
96                 protected function applyStateClasses(tags:Object):Object {
97             for (var stateKey:String in stateClasses) {
98                 tags[":"+stateKey] = 'yes';
99             }
100                         return tags;
101                 }
102                 
103                 // Redraw control
104                 
105                 public function redraw(sl:StyleList=null):Boolean {
106                         if (suspended) { redrawStyleList=sl; redrawDue=true; return false; }
107                         return doRedraw(sl);
108                 }
109                 
110                 public function doRedraw(sl:StyleList):Boolean {
111                         // to be overwritten
112                         return false;
113                 }
114                 
115                 public function suspendRedraw(event:EntityEvent):void {
116                         suspended=true;
117                         redrawDue=false;
118                 }
119                 
120                 public function resumeRedraw(event:EntityEvent):void {
121                         suspended=false;
122                         if (redrawDue) { 
123                                 doRedraw(redrawStyleList);
124                                 redrawDue=false;
125                                 redrawStyleList=null; 
126                         }
127                 }
128
129         }
130
131 }