First stab at getting nodes with interesting tags to show up. Few kinks need to be...
[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.TAG_CHANGED, tagChanged);
33                         entity.addEventListener(Connection.NODE_MOVED, nodeMoved);
34                         entity.addEventListener(Connection.NODE_DELETED, nodeDeleted);
35             attachRelationListeners();
36                         redraw();
37                 }
38                 
39                 public function nodeMoved(event:Event):void {
40                     updatePosition();
41                 }
42
43                 public function nodeDeleted(event:Event):void {
44                         removeSprites();
45                 }
46
47                 override public function doRedraw():Boolean {
48                         if (!paint.ready) { return false; }
49                         if (entity.deleted) { return false; }
50
51                         var tags:Object = entity.getTagsCopy();
52                         tags=applyStateClasses(tags);
53                         if (!entity.hasParentWays) { tags[':poi']='yes'; }
54             if (entity.hasInterestingTags()) { tags[':hasTags']='yes'; }
55                         if (!styleList || !styleList.isValidAt(paint.map.scale)) {
56                                 styleList=paint.ruleset.getStyles(entity,tags,paint.map.scale); 
57                         }
58
59                         var inWay:Boolean=entity.hasParentWays;
60                         var hasStyles:Boolean=styleList.hasStyles();
61                         
62                         removeSprites(); iconnames={};
63                         return renderFromStyle(tags);
64                 }
65
66                 private function renderFromStyle(tags:Object):Boolean {
67                         var r:Boolean=false;                    // ** rendered
68                         var maxwidth:Number=4;                  // biggest width
69                         var w:Number;
70                         var icon:Sprite;
71                         interactive=false;
72                         for each (var sublayer:Number in styleList.sublayers) {
73
74                                 if (styleList.pointStyles[sublayer]) {
75                                         var s:PointStyle=styleList.pointStyles[sublayer];
76                                         interactive||=s.interactive;
77                                         r=true;
78                                         if (s.rotation) { rotation=s.rotation; }
79                                         if (s.icon_image!=iconnames[sublayer]) {
80                                                 if (s.icon_image=='square') {
81                                                         // draw square
82                                                         icon=new Sprite();
83                                                         addToLayer(icon,STROKESPRITE,sublayer);
84                                                         w=styleIcon(icon,sublayer);
85                                                         icon.graphics.drawRect(0,0,w,w);
86                                                         if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
87                                                         iconnames[sublayer]='_square';
88
89                                                 } else if (s.icon_image=='circle') {
90                                                         // draw circle
91                                                         icon=new Sprite();
92                                                         addToLayer(icon,STROKESPRITE,sublayer);
93                                                         w=styleIcon(icon,sublayer);
94                                                         icon.graphics.drawCircle(w,w,w);
95                                                         if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
96                                                         iconnames[sublayer]='_circle';
97
98                                                 } else if (paint.ruleset.images[s.icon_image]) {
99                                                         // 'load' icon (actually just from library)
100                                                         var loader:ExtendedLoader = new ExtendedLoader();
101                                                         loader.info['sublayer']=sublayer;
102                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
103                                                         loader.loadBytes(paint.ruleset.images[s.icon_image]);
104                                                         iconnames[sublayer]=s.icon_image;
105                                                 }
106                                         }
107                                 }
108
109                                 // name sprite
110                                 var a:String='', t:TextStyle;
111                                 if (styleList.textStyles[sublayer]) {
112                                         t=styleList.textStyles[sublayer];
113                                         interactive||=t.interactive;
114                                         a=tags[t.text];
115                                 }
116
117                                 if (a) { 
118                                         var name:Sprite=new Sprite();
119                                         addToLayer(name,NAMESPRITE);
120                                         t.writeNameLabel(name,a,0,0);
121                                 }
122                         }
123                         if (!r) { return false; }
124                         if (interactive) { addHitSprite(maxwidth); }
125                         updatePosition();
126                         return true;
127                 }
128
129
130                 private function styleIcon(icon:Sprite, sublayer:Number):Number {
131                         loaded=true;
132
133                         // get colours
134                         if (styleList.shapeStyles[sublayer]) {
135                                 var s:ShapeStyle=styleList.shapeStyles[sublayer];
136                                 if (s.color) { icon.graphics.beginFill(s.color); 
137                                         }
138                                 if (s.casing_width || !isNaN(s.casing_color)) {
139                                         icon.graphics.lineStyle(s.casing_width ? s.casing_width : 1,
140                                                                                         s.casing_color ? s.casing_color : 0,
141                                                                                         s.casing_opacity ? s.casing_opacity : 1);
142                                 }
143                         }
144
145                         // return width
146                         return styleList.pointStyles[sublayer].icon_width;
147                 }
148
149                 private function addHitSprite(w:uint):void {
150             var hitzone:Sprite = new Sprite();
151             hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
152                         hitzone.graphics.beginFill(0);
153                         hitzone.graphics.drawRect(0,0,w,w);
154             addToLayer(hitzone, NODECLICKSPRITE);
155             hitzone.visible = false;
156                         setListenSprite(NODECLICKSPRITE, hitzone);
157                 }
158
159                 private function loadedIcon(event:Event):void {
160                         var icon:Sprite=new Sprite();
161                         var sublayer:Number=event.target.loader.info['sublayer'];
162                         addToLayer(icon,STROKESPRITE,sublayer);
163                         icon.addChild(Bitmap(event.target.content));
164                         addHitSprite(icon.width);
165                         loaded=true;
166                         updatePosition();
167                 }
168
169                 private function updatePosition():void {
170                         if (!loaded) { return; }
171
172                         for (var i:uint=0; i<sprites.length; i++) {
173                                 var d:DisplayObject=sprites[i];
174                                 d.x=0; d.y=0; d.rotation=0;
175
176                                 var m:Matrix=new Matrix();
177                                 m.translate(-d.width/2,-d.height/2);
178                                 m.rotate(rotation);
179                                 m.translate(paint.map.lon2coord(Node(entity).lon),paint.map.latp2coord(Node(entity).latp));
180                                 d.transform.matrix=m;
181                         }
182                 }
183         }
184 }