fix text offset for POIs, and casing for node shapes
[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.connection.Node;
12     import net.systemeD.halcyon.connection.Connection;
13         import net.systemeD.halcyon.styleparser.*;
14         import net.systemeD.halcyon.Globals;
15         
16         public class NodeUI extends EntityUI {
17                 
18         private var node:Node;
19                 public var loaded:Boolean=false;
20                 private var iconname:String='';                         // name of icon
21                 private var heading:Number=0;                           // heading within way
22                 private var rotation:Number=0;                          // rotation applied to this POI
23
24                 public function NodeUI(node:Node, map:Map, heading:Number=0) {
25                         super();
26                         this.map = map;
27                         this.node = node;
28                         this.heading = heading;
29                         node.addEventListener(Connection.NODE_MOVED, nodeMoved);
30                 }
31                 
32                 public function nodeMoved(event:Event):void {
33                     updatePosition();
34                 }
35                 
36                 override public function redraw(sl:StyleList=null):Boolean {
37                         var tags:Object = node.getTagsCopy();
38                         tags=applyStateClasses(tags);
39                         if (!node.hasParentWays) { tags[':poi']='yes'; }
40                         if (!sl) { sl=map.ruleset.getStyles(this.node,tags); }
41
42                         var inWay:Boolean=node.hasParentWays;
43                         var hasStyles:Boolean=sl.hasStyles();
44                         
45                         removeSprites(); iconname='';
46                         return renderFromStyle(sl,tags);
47                 }
48
49                 private function renderFromStyle(sl:StyleList,tags:Object):Boolean {
50                         var r:Boolean=false;    // ** rendered
51                         var w:Number;
52                         var icon:Sprite;
53                         layer=10;
54                         for (var sublayer:int=10; sublayer>=0; sublayer--) {
55
56                                 if (sl.pointStyles[sublayer]) {
57                                         var s:PointStyle=sl.pointStyles[sublayer];
58                                         r=true;
59                                         if (s.rotation) { rotation=s.rotation; }
60
61                                         if (s.icon_image!=iconname) {
62                                                 if (s.icon_image=='square') {
63                                                         // draw square
64                                                         icon=new Sprite();
65                                                         addToLayer(icon,STROKESPRITE,sublayer);
66                                                         w=styleIcon(icon,sl,sublayer);
67                                                         icon.graphics.drawRect(0,0,w,w);
68                                                         addHitSprite(w);
69                                                         updatePosition();
70                                                         iconname='_square';
71
72                                                 } else if (s.icon_image=='circle') {
73                                                         // draw circle
74                                                         icon=new Sprite();
75                                                         addToLayer(icon,STROKESPRITE,sublayer);
76                                                         w=styleIcon(icon,sl,sublayer);
77                                                         icon.graphics.drawCircle(w,w,w);
78                                                         addHitSprite(w);
79                                                         updatePosition();
80                                                         iconname='_circle';
81
82                                                 } else if (map.ruleset.images[s.icon_image]) {
83                                                         // 'load' icon (actually just from library)
84                                                         var loader:Loader = new Loader();
85                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void { loadedIcon(e,sublayer); } );
86                                                         loader.loadBytes(map.ruleset.images[s.icon_image]);
87                                                         iconname=s.icon_image;
88                                                 }
89                                         } else {
90                                                 // already loaded, so just reposition
91                                                 updatePosition();
92                                         }
93                                 }
94
95                                 // name sprite
96                                 var a:String, t:TextStyle;
97                                 if (sl.textStyles[sublayer]) {
98                                         t=sl.textStyles[sublayer];
99                                         a=tags[t.text];
100                                 }
101
102                                 if (a) { 
103                                         var name:Sprite=new Sprite();
104                                         addToLayer(name,NAMESPRITE);
105                                         t.writeNameLabel(name,a,map.lon2coord(node.lon),map.latp2coord(node.latp));
106                                 }
107                         }
108                         return r;
109                 }
110
111
112                 private function styleIcon(icon:Sprite, sl:StyleList, sublayer:uint):Number {
113                         loaded=true;
114
115                         // get colours
116                         if (sl.shapeStyles[sublayer]) {
117                                 var s:ShapeStyle=sl.shapeStyles[sublayer];
118                                 if (s.color) { icon.graphics.beginFill(s.color); }
119                                 if (s.casing_width || !isNaN(s.casing_color)) {
120                                         icon.graphics.lineStyle(s.casing_width ? s.casing_width : 1,
121                                                                                         s.casing_color ? s.casing_color : 0,
122                                                                                         s.casing_opacity ? s.casing_opacity : 1);
123                                 }
124                         }
125
126                         // return width
127                         return sl.pointStyles[sublayer].icon_width ? sl.pointStyles[sublayer].icon_width : 4;
128                 }
129
130                 private function addHitSprite(w:uint):void {
131             var hitzone:Sprite = new Sprite();
132             hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
133                         hitzone.graphics.beginFill(0);
134                         hitzone.graphics.drawRect(0,0,w,w);
135             addToLayer(hitzone, CLICKSPRITE);
136             hitzone.visible = false;
137                         createListenSprite(hitzone);
138                 }
139
140                 private function loadedIcon(event:Event,sublayer:uint):void {
141                         var icon:Sprite=new Sprite();
142                         addToLayer(icon,STROKESPRITE,sublayer);
143                         icon.addChild(Bitmap(event.target.content));
144                         addHitSprite(icon.width);
145                         loaded=true;
146                         updatePosition();
147                 }
148
149         override protected function mouseEvent(event:MouseEvent):void {
150                         map.entityMouseEvent(event, node);
151         }
152
153                 private function updatePosition():void {
154                         if (!loaded) { return; }
155
156                         for (var i:uint=0; i<sprites.length; i++) {
157                                 var d:DisplayObject=sprites[i];
158                                 d.x=0; d.y=0; d.rotation=0;
159
160                                 var m:Matrix=new Matrix();
161                                 m.translate(-d.width/2,-d.height/2);
162                                 m.rotate(rotation);
163                                 m.translate(map.lon2coord(node.lon),map.latp2coord(node.latp));
164                                 d.transform.matrix=m;
165                         }
166                 }
167         }
168 }