select and move POI nodes, and a handful of smaller fixes too
[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 || s.casing_color!=false) {
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                                         // ** this appears to give casing to things that shouldn't have it
124                                         // 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)); 
125                                 }
126                         }
127
128                         // return width
129                         return sl.pointStyles[sublayer].icon_width ? sl.pointStyles[sublayer].icon_width : 4;
130                 }
131
132                 private function addHitSprite(w:uint):void {
133             var hitzone:Sprite = new Sprite();
134             hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
135                         hitzone.graphics.beginFill(0);
136                         hitzone.graphics.drawRect(0,0,w,w);
137             addToLayer(hitzone, CLICKSPRITE);
138             hitzone.visible = false;
139                         createListenSprite(hitzone);
140                 }
141
142                 private function loadedIcon(event:Event,sublayer:uint):void {
143                         var icon:Sprite=new Sprite();
144                         addToLayer(icon,STROKESPRITE,sublayer);
145                         icon.addChild(Bitmap(event.target.content));
146                         addHitSprite(icon.width);
147                         loaded=true;
148                         updatePosition();
149                 }
150
151         override protected function mouseEvent(event:MouseEvent):void {
152                         map.entityMouseEvent(event, node);
153         }
154
155                 private function updatePosition():void {
156                         if (!loaded) { return; }
157
158                         // ** this won't work with text objects. They have a different .x and .y
159                         //    and (obviously) don't need to be rotated. Needs fixing
160                         for (var i:uint=0; i<sprites.length; i++) {
161                                 var d:DisplayObject=sprites[i];
162                                 d.x=0; d.y=0; d.rotation=0;
163
164                                 var m:Matrix=new Matrix();
165                                 m.translate(-d.width/2,-d.height/2);
166                                 m.rotate(rotation);
167                                 m.translate(map.lon2coord(node.lon),map.latp2coord(node.latp));
168                                 d.transform.matrix=m;
169                         }
170                 }
171         }
172 }