Replace SuperTabNavigator (not compatible with Flex 4) with standard TabNavigator...
[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.ImageBank;
14
15     public class MarkerUI 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         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, false, 0, true);
33             entity.addEventListener(Connection.NODE_ALTERED, markerAltered, false, 0, true);
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 subpart:String in styleList.subparts) {
81
82                 if (styleList.pointStyles[subpart]) {
83                     var s:PointStyle=styleList.pointStyles[subpart];
84                     interactive||=s.interactive;
85                     r=true;
86                     if (s.rotation) { rotation=s.rotation; }
87                     if (s.icon_image!=iconnames[subpart]) {
88                                                 icon=new Sprite();
89                                                 iconnames[subpart]=s.icon_image;
90                                                 addToLayer(icon,STROKESPRITE,s.sublayer);
91                         if (s.icon_image=='square') {
92                             // draw square
93                             w=styleIcon(icon,subpart);
94                             icon.graphics.drawRect(0,0,w,w);
95                             if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
96
97                         } else if (s.icon_image=='circle') {
98                             // draw circle
99                             w=styleIcon(icon,subpart);
100                             icon.graphics.drawCircle(w,w,w);
101                             if (s.interactive) { maxwidth=Math.max(w,maxwidth); }
102
103                                                 } else if (ImageBank.getInstance().hasImage(s.icon_image)) {
104                                                         // load icon from library
105                                                         icon.addChild(ImageBank.getInstance().getAsDisplayObject(s.icon_image));
106 //                                                      addHitSprite(icon.width);                       // ** check this - we're doing it below too
107 //                                                      loaded=true; updatePosition();          // ** check this
108                                                         if (s.interactive) { maxwidth=Math.max(icon.width,maxwidth); }
109                                                 }
110                     }
111                 }
112
113                 // name sprite
114                 var a:String='', t:TextStyle;
115                 if (styleList.textStyles[subpart]) {
116                     t=styleList.textStyles[subpart];
117                     interactive||=t.interactive;
118                     a=tags[t.text];
119                 }
120
121                 if (a) {
122                     var name:Sprite=new Sprite();
123                     addToLayer(name,NAMESPRITE);
124                     t.writeNameLabel(name,a,0,0);
125                     loaded=true;
126                 }
127             }
128             if (!r) { return false; }
129             if (interactive) { addHitSprite(maxwidth); }
130             updatePosition();
131             return true;
132         }
133
134
135         private function styleIcon(icon:Sprite, subpart:String):Number {
136             loaded=true;
137
138             // get colours
139             if (styleList.shapeStyles[subpart]) {
140                 var s:ShapeStyle=styleList.shapeStyles[subpart];
141                 if (!isNaN(s.color)) { icon.graphics.beginFill(s.color);
142                     }
143                 if (s.casing_width || !isNaN(s.casing_color)) {
144                     icon.graphics.lineStyle(s.casing_width ? s.casing_width : 1,
145                                             s.casing_color ? s.casing_color : 0,
146                                             s.casing_opacity ? s.casing_opacity : 1);
147                 }
148             }
149
150             // return width
151             return styleList.pointStyles[subpart].icon_width;
152         }
153
154         private function addHitSprite(w:uint):void {
155             hitzone = new Sprite();
156             hitzone.graphics.lineStyle(4, 0x000000, 1, false, "normal", CapsStyle.ROUND, JointStyle.ROUND);
157             hitzone.graphics.beginFill(0);
158             hitzone.graphics.drawRect(0,0,w,w);
159             hitzone.visible = false;
160             setListenSprite();
161         }
162
163         private function updatePosition():void {
164             if (!loaded) { return; }
165
166             for (var i:uint=0; i<sprites.length; i++) {
167                 var d:DisplayObject=sprites[i];
168                 d.x=0; d.y=0; d.rotation=0;
169
170                 var m:Matrix=new Matrix();
171                 m.translate(-d.width/2,-d.height/2);
172                 m.rotate(rotation);
173                 m.translate(paint.map.lon2coord(Marker(entity).lon),paint.map.latp2coord(Marker(entity).latp));
174                 d.transform.matrix=m;
175             }
176         }
177     }
178 }