Support inline status, where the status field is part of e.g. map calls
[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.styleparser.*;
12     import net.systemeD.halcyon.connection.*;
13         
14         /** The graphical representation of a Node (including POIs and nodes that are part of Ways). */
15         public class NodeUI extends EntityUI {
16                 
17                 public var loaded:Boolean=false;
18                 private var iconnames:Object={};                        // name of icon on each subpart
19                 private var heading:Number=0;                           // heading within way
20                 private var rotation:Number=0;                          // rotation applied to this POI
21                 private static const NO_LAYER:int=-99999;
22
23                 /**
24                  * @param node The corresponding Node.
25                  * @param paint MapPaint object to attach this NodeUI to.
26                  * @param heading Optional angle.
27                  * @param layer Which layer on the MapPaint object it sits on. @default Top layer
28                  * @param stateClasses A settings object definining the initial state of the node (eg, highlighted, hover...) */
29                 public function NodeUI(node:Node, paint:MapPaint, heading:Number=0, layer:int=NO_LAYER, stateClasses:Object=null) {
30                         super(node,paint);
31                         if (layer==NO_LAYER) { this.layer=paint.maxlayer; } else { this.layer=layer; }
32                         this.heading = heading;
33                         if (stateClasses) {
34                                 for (var state:String in stateClasses) {
35                                         if (stateClasses[state]) { this.stateClasses[state]=stateClasses[state]; }
36                                 }
37                         }
38                         entity.addEventListener(Connection.NODE_MOVED, nodeMoved, false, 0, true);
39             entity.addEventListener(Connection.NODE_ALTERED, nodeAltered, false, 0, true);
40             entity.addEventListener(Connection.ENTITY_DRAGGED, nodeDragged, false, 0, true);
41             attachRelationListeners();
42                         redraw();
43                 }
44                 
45                 public function removeEventListeners():void {
46                         removeGenericEventListeners();
47                         entity.removeEventListener(Connection.NODE_MOVED, nodeMoved);
48             entity.removeEventListener(Connection.NODE_ALTERED, nodeAltered);
49             entity.removeEventListener(Connection.ENTITY_DRAGGED, nodeDragged);
50                         removeRelationListeners();
51                 }
52
53                 /** Respond to movement event. */
54                 public function nodeMoved(event:Event):void {
55                     updatePosition();
56                 }
57
58         private function nodeAltered(event:Event):void {
59             redraw();
60         }
61
62                 private function nodeDragged(event:EntityDraggedEvent):void {
63                         updatePosition(event.xDelta,event.yDelta);
64                 }
65
66                 /** Update settings then draw node. */
67                 override public function doRedraw():Boolean {
68                         if (!paint.ready) { return false; }
69                         if (entity.deleted) { return false; }
70
71                         var tags:Object = entity.getTagsCopy();
72                         setStateClass('poi', !entity.hasParentWays);
73                         setStateClass('junction', entity.numParentWays>1);
74             setStateClass('hasTags', entity.hasInterestingTags());
75             setStateClass('dupe', Node(entity).isDupe());
76                         tags=applyStateClasses(tags);
77                         if (entity.status) { tags['_status']=entity.status; }
78                         if (!styleList || !styleList.isValidAt(paint.map.scale)) {
79                                 styleList=paint.ruleset.getStyles(entity,tags,paint.map.scale); 
80                         }
81
82                         var suggestedLayer:Number=styleList.layerOverride();
83                         if (!isNaN(suggestedLayer)) { layer=suggestedLayer; }
84
85                         var inWay:Boolean=entity.hasParentWays;
86                         var hasStyles:Boolean=styleList.hasStyles();
87                         
88                         removeSprites(); iconnames={};
89                         return renderFromStyle(tags);
90                 }
91
92                 /** Assemble the layers of icons to draw the node, with hit zone if interactive. */
93                 private function renderFromStyle(tags:Object):Boolean {
94                         var r:Boolean=false;                    // ** rendered
95                         var maxwidth:Number=4;                  // biggest width
96                         var w:Number;
97                         var icon:Sprite;
98                         interactive=false;
99                         for each (var subpart:String in styleList.subparts) {
100
101                                 if (styleList.pointStyles[subpart]) {
102                                         var s:PointStyle=styleList.pointStyles[subpart];
103                                         interactive||=s.interactive;
104                                         r=true;
105                                         if (s.rotation) { rotation=s.rotation; }
106                                         if (s.icon_image!=iconnames[subpart]) {
107                                                 if (s.icon_image=='square') {
108                                                         // draw square
109                                                         icon=new Sprite();
110                                                         addToLayer(icon,STROKESPRITE,s.sublayer);
111                                                         w=styleIcon(icon,subpart);
112                                                         icon.graphics.drawRect(0,0,w,w);
113                                                         if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
114                                                         iconnames[subpart]='_square';
115
116                                                 } else if (s.icon_image=='circle') {
117                                                         // draw circle
118                                                         icon=new Sprite();
119                                                         addToLayer(icon,STROKESPRITE,s.sublayer);
120                                                         w=styleIcon(icon,subpart);
121                                                         icon.graphics.drawCircle(w,w,w);
122                                                         if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
123                                                         iconnames[subpart]='_circle';
124
125                                                 } else if (paint.ruleset.images[s.icon_image]) {
126                                                         // 'load' icon (actually just from library)
127                                                         var loader:ExtendedLoader = new ExtendedLoader();
128                                                         loader.info['sublayer']=s.sublayer;
129                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon, false, 0, true);
130                                                         loader.loadBytes(paint.ruleset.images[s.icon_image]);
131                                                         iconnames[subpart]=s.icon_image;
132                                                 }
133                                         }
134                                 }
135
136                                 // name sprite
137                                 var a:String='', t:TextStyle;
138                                 if (styleList.textStyles[subpart]) {
139                                         t=styleList.textStyles[subpart];
140                                         interactive||=t.interactive;
141                                         a=tags[t.text];
142                                 }
143
144                                 if (a) { 
145                                         var name:Sprite=new Sprite();
146                                         addToLayer(name,NAMESPRITE);
147                                         t.writeNameLabel(name,a,0,0);
148                     loaded=true;
149                                 }
150                         }
151                         if (!r) { return false; }
152                         if (interactive) { addHitSprite(maxwidth); }
153                         updatePosition();
154                         return true;
155                 }
156
157
158                 private function styleIcon(icon:Sprite, subpart:String):Number {
159                         loaded=true;
160
161                         // get colours
162                         if (styleList.shapeStyles[subpart]) {
163                                 var s:ShapeStyle=styleList.shapeStyles[subpart];
164                                 if (!isNaN(s.color)) { icon.graphics.beginFill(s.color, s.opacity ? s.opacity : 1);
165                                         }
166                                 if (s.casing_width || !isNaN(s.casing_color)) {
167                                         icon.graphics.lineStyle(s.casing_width ? s.casing_width : 1,
168                                                                                         s.casing_color ? s.casing_color : 0,
169                                                                                         s.casing_opacity ? s.casing_opacity : 1);
170                                 }
171                         }
172
173                         // return width
174                         return styleList.pointStyles[subpart].icon_width;
175                 }
176
177                 private function addHitSprite(w:uint):void {
178             hitzone = new Sprite();
179             hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
180                         hitzone.graphics.beginFill(0);
181                         hitzone.graphics.drawRect(0,0,w,w);
182                         hitzone.visible = false;
183                         setListenSprite();
184                 }
185
186                 private function loadedIcon(event:Event):void {
187                         var icon:Sprite=new Sprite();
188                         var sublayer:Number=event.target.loader.info['sublayer'];
189                         addToLayer(icon,STROKESPRITE,sublayer);
190                         icon.addChild(Bitmap(event.target.content));
191                         addHitSprite(icon.width);
192                         loaded=true;
193                         updatePosition();
194                 }
195
196                 private function updatePosition(xDelta:Number=0,yDelta:Number=0):void {
197                         if (!loaded) { return; }
198
199                         var baseX:Number=paint.map.lon2coord(Node(entity).lon);
200                         var baseY:Number=paint.map.latp2coord(Node(entity).latp);
201                         for (var i:uint=0; i<sprites.length; i++) {
202                                 var d:DisplayObject=sprites[i];
203                                 d.x=0; d.y=0; d.rotation=0;
204
205                                 var m:Matrix=new Matrix();
206                                 m.translate(-d.width/2,-d.height/2);
207                                 m.rotate(rotation);
208                                 m.translate(baseX+xDelta,baseY+yDelta);
209                                 d.transform.matrix=m;
210                         }
211                 }
212         public function hitTest(x:Number, y:Number):Node {
213             if (hitzone && hitzone.hitTestPoint(x,y,true)) { return entity as Node; }
214             return null;
215         }
216                 
217         }
218 }