fix horrid merge bug (yay!). Implement suspend/resume events for redrawing, to speed...
[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                         node.addEventListener(Connection.NODE_DELETED, nodeDeleted);
31                         node.addEventListener(Connection.SUSPEND_REDRAW, suspendRedraw);
32                         node.addEventListener(Connection.RESUME_REDRAW, resumeRedraw);
33                 }
34                 
35                 public function nodeMoved(event:Event):void {
36                     updatePosition();
37                 }
38
39                 public function nodeDeleted(event:Event):void {
40                         removeSprites();
41                 }
42                 
43                 override public function doRedraw(sl:StyleList):Boolean {
44                         var tags:Object = node.getTagsCopy();
45                         tags=applyStateClasses(tags);
46                         if (!node.hasParentWays) { tags[':poi']='yes'; }
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 || !isNaN(s.casing_color)) {
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                                 }
131                         }
132
133                         // return width
134                         return sl.pointStyles[sublayer].icon_width ? sl.pointStyles[sublayer].icon_width : 4;
135                 }
136
137                 private function addHitSprite(w:uint):void {
138             var hitzone:Sprite = new Sprite();
139             hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
140                         hitzone.graphics.beginFill(0);
141                         hitzone.graphics.drawRect(0,0,w,w);
142             addToLayer(hitzone, CLICKSPRITE);
143             hitzone.visible = false;
144                         createListenSprite(hitzone);
145                 }
146
147                 private function loadedIcon(event:Event,sublayer:uint):void {
148                         var icon:Sprite=new Sprite();
149                         addToLayer(icon,STROKESPRITE,sublayer);
150                         icon.addChild(Bitmap(event.target.content));
151                         addHitSprite(icon.width);
152                         loaded=true;
153                         updatePosition();
154                 }
155
156         override protected function mouseEvent(event:MouseEvent):void {
157                         map.entityMouseEvent(event, node);
158         }
159
160                 private function updatePosition():void {
161                         if (!loaded) { return; }
162
163                         for (var i:uint=0; i<sprites.length; i++) {
164                                 var d:DisplayObject=sprites[i];
165                                 d.x=0; d.y=0; d.rotation=0;
166
167                                 var m:Matrix=new Matrix();
168                                 m.translate(-d.width/2,-d.height/2);
169                                 m.rotate(rotation);
170                                 m.translate(map.lon2coord(node.lon),map.latp2coord(node.latp));
171                                 d.transform.matrix=m;
172                         }
173                 }
174         }
175 }