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