]> git.openstreetmap.org Git - potlatch2.git/blob - net/systemeD/halcyon/NodeUI.as
allow layers to be overridden in MapCSS; this means we can bring selected nodes to...
[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.Globals;
14         
15         public class NodeUI extends EntityUI {
16                 
17                 public var loaded:Boolean=false;
18                 private var iconnames:Object={};                        // name of icon on each sublayer
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                 public function NodeUI(node:Node, paint:MapPaint, heading:Number=0, layer:int=NO_LAYER, stateClasses:Object=null) {
24                         super(node,paint);
25                         if (layer==NO_LAYER) { this.layer=paint.maxlayer; } else { this.layer=layer; }
26                         this.heading = heading;
27                         if (stateClasses) {
28                                 for (var state:String in stateClasses) {
29                                         if (stateClasses[state]) { this.stateClasses[state]=stateClasses[state]; }
30                                 }
31                         }
32                         entity.addEventListener(Connection.NODE_MOVED, nodeMoved);
33             attachRelationListeners();
34                         redraw();
35                 }
36                 
37                 public function removeEventListeners():void {
38                         removeGenericEventListeners();
39                         entity.removeEventListener(Connection.NODE_MOVED, nodeMoved);
40                 }
41
42                 public function nodeMoved(event:Event):void {
43                     updatePosition();
44                 }
45
46                 override public function doRedraw():Boolean {
47                         if (!paint.ready) { return false; }
48                         if (entity.deleted) { return false; }
49
50                         var tags:Object = entity.getTagsCopy();
51                         tags=applyStateClasses(tags);
52                         if (!entity.hasParentWays) { tags[':poi']='yes'; }
53             if (entity.hasInterestingTags()) { tags[':hasTags']='yes'; }
54             if (Node(entity).isDupe()) { tags[':dupe']='yes'; }
55                         if (!styleList || !styleList.isValidAt(paint.map.scale)) {
56                                 styleList=paint.ruleset.getStyles(entity,tags,paint.map.scale); 
57                         }
58
59                         var suggestedLayer:Number=styleList.layerOverride();
60                         if (!isNaN(suggestedLayer)) { layer=suggestedLayer; }
61
62                         var inWay:Boolean=entity.hasParentWays;
63                         var hasStyles:Boolean=styleList.hasStyles();
64                         
65                         removeSprites(); iconnames={};
66                         return renderFromStyle(tags);
67                 }
68
69                 private function renderFromStyle(tags:Object):Boolean {
70                         var r:Boolean=false;                    // ** rendered
71                         var maxwidth:Number=4;                  // biggest width
72                         var w:Number;
73                         var icon:Sprite;
74                         interactive=false;
75                         for each (var sublayer:Number in styleList.sublayers) {
76
77                                 if (styleList.pointStyles[sublayer]) {
78                                         var s:PointStyle=styleList.pointStyles[sublayer];
79                                         interactive||=s.interactive;
80                                         r=true;
81                                         if (s.rotation) { rotation=s.rotation; }
82                                         if (s.icon_image!=iconnames[sublayer]) {
83                                                 if (s.icon_image=='square') {
84                                                         // draw square
85                                                         icon=new Sprite();
86                                                         addToLayer(icon,STROKESPRITE,sublayer);
87                                                         w=styleIcon(icon,sublayer);
88                                                         icon.graphics.drawRect(0,0,w,w);
89                                                         if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
90                                                         iconnames[sublayer]='_square';
91
92                                                 } else if (s.icon_image=='circle') {
93                                                         // draw circle
94                                                         icon=new Sprite();
95                                                         addToLayer(icon,STROKESPRITE,sublayer);
96                                                         w=styleIcon(icon,sublayer);
97                                                         icon.graphics.drawCircle(w,w,w);
98                                                         if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
99                                                         iconnames[sublayer]='_circle';
100
101                                                 } else if (paint.ruleset.images[s.icon_image]) {
102                                                         // 'load' icon (actually just from library)
103                                                         var loader:ExtendedLoader = new ExtendedLoader();
104                                                         loader.info['sublayer']=sublayer;
105                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
106                                                         loader.loadBytes(paint.ruleset.images[s.icon_image]);
107                                                         iconnames[sublayer]=s.icon_image;
108                                                 }
109                                         }
110                                 }
111
112                                 // name sprite
113                                 var a:String='', t:TextStyle;
114                                 if (styleList.textStyles[sublayer]) {
115                                         t=styleList.textStyles[sublayer];
116                                         interactive||=t.interactive;
117                                         a=tags[t.text];
118                                 }
119
120                                 if (a) { 
121                                         var name:Sprite=new Sprite();
122                                         addToLayer(name,NAMESPRITE);
123                                         t.writeNameLabel(name,a,0,0);
124                                 }
125                         }
126                         if (!r) { return false; }
127                         if (interactive) { addHitSprite(maxwidth); }
128                         updatePosition();
129                         return true;
130                 }
131
132
133                 private function styleIcon(icon:Sprite, sublayer:Number):Number {
134                         loaded=true;
135
136                         // get colours
137                         if (styleList.shapeStyles[sublayer]) {
138                                 var s:ShapeStyle=styleList.shapeStyles[sublayer];
139                                 if (!isNaN(s.color)) { icon.graphics.beginFill(s.color);
140                                         }
141                                 if (s.casing_width || !isNaN(s.casing_color)) {
142                                         icon.graphics.lineStyle(s.casing_width ? s.casing_width : 1,
143                                                                                         s.casing_color ? s.casing_color : 0,
144                                                                                         s.casing_opacity ? s.casing_opacity : 1);
145                                 }
146                         }
147
148                         // return width
149                         return styleList.pointStyles[sublayer].icon_width;
150                 }
151
152                 private function addHitSprite(w:uint):void {
153             hitzone = new Sprite();
154             hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
155                         hitzone.graphics.beginFill(0);
156                         hitzone.graphics.drawRect(0,0,w,w);
157                         hitzone.visible = false;
158                         setListenSprite();
159                 }
160
161                 private function loadedIcon(event:Event):void {
162                         var icon:Sprite=new Sprite();
163                         var sublayer:Number=event.target.loader.info['sublayer'];
164                         addToLayer(icon,STROKESPRITE,sublayer);
165                         icon.addChild(Bitmap(event.target.content));
166                         addHitSprite(icon.width);
167                         loaded=true;
168                         updatePosition();
169                 }
170
171                 private function updatePosition():void {
172                         if (!loaded) { return; }
173
174                         for (var i:uint=0; i<sprites.length; i++) {
175                                 var d:DisplayObject=sprites[i];
176                                 d.x=0; d.y=0; d.rotation=0;
177
178                                 var m:Matrix=new Matrix();
179                                 m.translate(-d.width/2,-d.height/2);
180                                 m.rotate(rotation);
181                                 m.translate(paint.map.lon2coord(Node(entity).lon),paint.map.latp2coord(Node(entity).latp));
182                                 d.transform.matrix=m;
183                         }
184                 }
185         }
186 }