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