3b05fbcd57e812651cbbe9ba8d45a0d0e9aa73ef
[potlatch2.git] / net / systemeD / halcyon / NodeUI.as
1 package net.systemeD.halcyon {
2
3         import flash.display.*;
4         import flash.events.*;
5         import flash.text.AntiAliasType;
6         import flash.text.GridFitType;
7         import flash.text.TextField;
8         import flash.text.TextFormat;
9         import flash.geom.Matrix;
10         import flash.geom.Point;
11         import net.systemeD.halcyon.styleparser.*;
12     import net.systemeD.halcyon.connection.*;
13         import net.systemeD.halcyon.Globals;
14         
15         public class NodeUI extends EntityUI {
16                 
17                 public var loaded:Boolean=false;
18                 private var iconnames:Object={};                        // name of icon on each sublayer
19                 private var heading:Number=0;                           // heading within way
20                 private var rotation:Number=0;                          // rotation applied to this POI
21                 private static const NO_LAYER:int=-99999;
22
23                 public function NodeUI(node:Node, paint:MapPaint, heading:Number=0, layer:int=NO_LAYER, stateClasses:Object=null) {
24                         super(node,paint);
25                         if (layer==NO_LAYER) { this.layer=paint.maxlayer; } else { this.layer=layer; }
26                         this.heading = heading;
27                         if (stateClasses) {
28                                 for (var state:String in stateClasses) {
29                                         if (stateClasses[state]) { this.stateClasses[state]=stateClasses[state]; }
30                                 }
31                         }
32                         entity.addEventListener(Connection.NODE_MOVED, nodeMoved);
33             attachRelationListeners();
34                         redraw();
35                 }
36                 
37                 public function removeEventListeners():void {
38                         removeGenericEventListeners();
39                         entity.removeEventListener(Connection.NODE_MOVED, nodeMoved);
40                 }
41
42                 public function nodeMoved(event:Event):void {
43                     updatePosition();
44                 }
45
46                 override public function doRedraw():Boolean {
47                         if (!paint.ready) { return false; }
48                         if (entity.deleted) { return false; }
49
50                         var tags:Object = entity.getTagsCopy();
51                         tags=applyStateClasses(tags);
52                         if (!entity.hasParentWays) { tags[':poi']='yes'; }
53             if (entity.hasInterestingTags()) { tags[':hasTags']='yes'; }
54                         if (!styleList || !styleList.isValidAt(paint.map.scale)) {
55                                 styleList=paint.ruleset.getStyles(entity,tags,paint.map.scale); 
56                         }
57
58                         var inWay:Boolean=entity.hasParentWays;
59                         var hasStyles:Boolean=styleList.hasStyles();
60                         
61                         removeSprites(); iconnames={};
62                         return renderFromStyle(tags);
63                 }
64
65                 private function renderFromStyle(tags:Object):Boolean {
66                         var r:Boolean=false;                    // ** rendered
67                         var maxwidth:Number=4;                  // biggest width
68                         var w:Number;
69                         var icon:Sprite;
70                         interactive=false;
71                         for each (var sublayer:Number in styleList.sublayers) {
72
73                                 if (styleList.pointStyles[sublayer]) {
74                                         var s:PointStyle=styleList.pointStyles[sublayer];
75                                         interactive||=s.interactive;
76                                         r=true;
77                                         if (s.rotation) { rotation=s.rotation; }
78                                         if (s.icon_image!=iconnames[sublayer]) {
79                                                 if (s.icon_image=='square') {
80                                                         // draw square
81                                                         icon=new Sprite();
82                                                         addToLayer(icon,STROKESPRITE,sublayer);
83                                                         w=styleIcon(icon,sublayer);
84                                                         icon.graphics.drawRect(0,0,w,w);
85                                                         if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
86                                                         iconnames[sublayer]='_square';
87
88                                                 } else if (s.icon_image=='circle') {
89                                                         // draw circle
90                                                         icon=new Sprite();
91                                                         addToLayer(icon,STROKESPRITE,sublayer);
92                                                         w=styleIcon(icon,sublayer);
93                                                         icon.graphics.drawCircle(w,w,w);
94                                                         if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
95                                                         iconnames[sublayer]='_circle';
96
97                                                 } else if (paint.ruleset.images[s.icon_image]) {
98                                                         // 'load' icon (actually just from library)
99                                                         var loader:ExtendedLoader = new ExtendedLoader();
100                                                         loader.info['sublayer']=sublayer;
101                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
102                                                         loader.loadBytes(paint.ruleset.images[s.icon_image]);
103                                                         iconnames[sublayer]=s.icon_image;
104                                                 }
105                                         }
106                                 }
107
108                                 // name sprite
109                                 var a:String='', t:TextStyle;
110                                 if (styleList.textStyles[sublayer]) {
111                                         t=styleList.textStyles[sublayer];
112                                         interactive||=t.interactive;
113                                         a=tags[t.text];
114                                 }
115
116                                 if (a) { 
117                                         var name:Sprite=new Sprite();
118                                         addToLayer(name,NAMESPRITE);
119                                         t.writeNameLabel(name,a,0,0);
120                                 }
121                         }
122                         if (!r) { return false; }
123                         if (interactive) { addHitSprite(maxwidth); }
124                         updatePosition();
125                         return true;
126                 }
127
128
129                 private function styleIcon(icon:Sprite, sublayer:Number):Number {
130                         loaded=true;
131
132                         // get colours
133                         if (styleList.shapeStyles[sublayer]) {
134                                 var s:ShapeStyle=styleList.shapeStyles[sublayer];
135                                 if (!isNaN(s.color)) { icon.graphics.beginFill(s.color);
136                                         }
137                                 if (s.casing_width || !isNaN(s.casing_color)) {
138                                         icon.graphics.lineStyle(s.casing_width ? s.casing_width : 1,
139                                                                                         s.casing_color ? s.casing_color : 0,
140                                                                                         s.casing_opacity ? s.casing_opacity : 1);
141                                 }
142                         }
143
144                         // return width
145                         return styleList.pointStyles[sublayer].icon_width;
146                 }
147
148                 private function addHitSprite(w:uint):void {
149             hitzone = new Sprite();
150             hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
151                         hitzone.graphics.beginFill(0);
152                         hitzone.graphics.drawRect(0,0,w,w);
153                         hitzone.visible = false;
154                         setListenSprite();
155                 }
156
157                 private function loadedIcon(event:Event):void {
158                         var icon:Sprite=new Sprite();
159                         var sublayer:Number=event.target.loader.info['sublayer'];
160                         addToLayer(icon,STROKESPRITE,sublayer);
161                         icon.addChild(Bitmap(event.target.content));
162                         addHitSprite(icon.width);
163                         loaded=true;
164                         updatePosition();
165                 }
166
167                 private function updatePosition():void {
168                         if (!loaded) { return; }
169
170                         for (var i:uint=0; i<sprites.length; i++) {
171                                 var d:DisplayObject=sprites[i];
172                                 d.x=0; d.y=0; d.rotation=0;
173
174                                 var m:Matrix=new Matrix();
175                                 m.translate(-d.width/2,-d.height/2);
176                                 m.rotate(rotation);
177                                 m.translate(paint.map.lon2coord(Node(entity).lon),paint.map.latp2coord(Node(entity).latp));
178                                 d.transform.matrix=m;
179                         }
180                 }
181         }
182 }