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