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