59238ea4f9eb6482ddebf0c8fbf078997d9fa5c7
[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                 public function redraw(sl:StyleList=null,forceDraw:Boolean=false):Boolean {
37                         // *** forcedraw can be removed
38
39                         var tags:Object = node.getTagsCopy();
40
41                         // special tags
42                         if (!node.hasParentWays) { tags[':poi']='yes'; }
43             for (var stateKey:String in stateClasses) {
44                 tags[":"+stateKey] = stateKey;
45             }
46
47                         if (!sl) { sl=map.ruleset.getStyles(this.node,tags); }
48
49                         var inWay:Boolean=node.hasParentWays;
50                         var hasStyles:Boolean=sl.hasStyles();
51                         
52                         removeSprites(); iconname='';
53                         return renderFromStyle(sl,tags);
54                 }
55
56                 private function renderFromStyle(sl:StyleList,tags:Object):Boolean {
57                         var r:Boolean=false;    // ** rendered
58                         var w:Number;
59                         var icon:Sprite;
60                         layer=10;
61                         for (var sublayer:int=10; sublayer>=0; sublayer--) {
62
63                                 if (sl.pointStyles[sublayer]) {
64                                         var s:PointStyle=sl.pointStyles[sublayer];
65                                         r=true;
66                                         if (s.rotation) { rotation=s.rotation; }
67
68                                         if (s.icon_image!=iconname) {
69                                                 if (s.icon_image=='square') {
70                                                         // draw square
71                                                         icon=new Sprite();
72                                                         addToLayer(icon,STROKESPRITE,sublayer);
73                                                         w=styleIcon(icon,sl,sublayer);
74                                                         icon.graphics.drawRect(0,0,w,w);
75                                                         addHitSprite(w);
76                                                         updatePosition();
77                                                         iconname='_square';
78
79                                                 } else if (s.icon_image=='circle') {
80                                                         // draw circle
81                                                         icon=new Sprite();
82                                                         addToLayer(icon,STROKESPRITE,sublayer);
83                                                         w=styleIcon(icon,sl,sublayer);
84                                                         icon.graphics.drawCircle(w,w,w);
85                                                         addHitSprite(w);
86                                                         updatePosition();
87                                                         iconname='_circle';
88
89                                                 } else if (map.ruleset.images[s.icon_image]) {
90                                                         // 'load' icon (actually just from library)
91                                                         var loader:Loader = new Loader();
92                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void { loadedIcon(e,sublayer); } );
93                                                         loader.loadBytes(map.ruleset.images[s.icon_image]);
94                                                         iconname=s.icon_image;
95                                                 }
96                                         } else {
97                                                 // already loaded, so just reposition
98                                                 updatePosition();
99                                         }
100                                 }
101
102                                 // name sprite
103                                 var a:String, t:TextStyle;
104                                 if (sl.textStyles[sublayer]) {
105                                         t=sl.textStyles[sublayer];
106                                         a=tags[t.text];
107                                 }
108
109                                 if (a) { 
110                                         var name:Sprite=new Sprite();
111                                         addToLayer(name,NAMESPRITE);
112                                         t.writeNameLabel(name,a,map.lon2coord(node.lon),map.latp2coord(node.latp));
113                                 }
114                         }
115                         return r;
116                 }
117
118
119                 private function styleIcon(icon:Sprite, sl:StyleList, sublayer:uint):Number {
120                         loaded=true;
121
122                         // get colours
123                         if (sl.shapeStyles[sublayer]) {
124                                 var s:ShapeStyle=sl.shapeStyles[sublayer];
125                                 if (s.color) { icon.graphics.beginFill(s.color); }
126                                 if (s.casing_width || s.casing_color!=false) {
127                                         icon.graphics.lineStyle(s.casing_width ? s.casing_width : 1,
128                                                                                         s.casing_color ? s.casing_color : 0,
129                                                                                         s.casing_opacity ? s.casing_opacity : 1);
130                                         // ** this appears to give casing to things that shouldn't have it
131                                         // Globals.vars.root.addDebug("casing: "+(s.casing_width ? s.casing_width : 1)+","+(s.casing_color ? s.casing_color : 0)+","+(s.casing_opacity ? s.casing_opacity : 1)); 
132                                 }
133                         }
134
135                         // return width
136                         return sl.pointStyles[sublayer].icon_width ? sl.pointStyles[sublayer].icon_width : 4;
137                 }
138
139                 private function addHitSprite(w:uint):void {
140             var hitzone:Sprite = new Sprite();
141             hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
142                         hitzone.graphics.beginFill(0);
143                         hitzone.graphics.drawRect(0,0,w,w);
144             addToLayer(hitzone, CLICKSPRITE);
145             hitzone.visible = false;
146                         createListenSprite(hitzone);
147                 }
148
149                 private function loadedIcon(event:Event,sublayer:uint):void {
150                         var icon:Sprite=new Sprite();
151                         addToLayer(icon,STROKESPRITE,sublayer);
152                         icon.addChild(Bitmap(event.target.content));
153                         addHitSprite(icon.width);
154                         loaded=true;
155                         updatePosition();
156                 }
157
158         override protected function mouseEvent(event:MouseEvent):void {
159                         map.entityMouseEvent(event, node);
160         }
161
162                 private function updatePosition():void {
163                         if (!loaded) { return; }
164
165                         // ** this won't work with text objects. They have a different .x and .y
166                         //    and (obviously) don't need to be rotated. Needs fixing
167                         for (var i:uint=0; i<sprites.length; i++) {
168                                 var d:DisplayObject=sprites[i];
169                                 d.x=0; d.y=0; d.rotation=0;
170
171                                 var m:Matrix=new Matrix();
172                                 m.translate(-d.width/2,-d.height/2);
173                                 m.rotate(rotation);
174                                 m.translate(map.lon2coord(node.lon),map.latp2coord(node.latp));
175                                 d.transform.matrix=m;
176                         }
177                 }
178         }
179 }