working (though incomplete) 900913;
[potlatch2.git] / net / systemeD / halcyon / POI.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 net.systemeD.halcyon.connection.Node;
10         import net.systemeD.halcyon.styleparser.*;
11         
12         public class POI extends Object {
13                 
14         private var node:Node;
15                 public var map:Map;                                                     // reference to parent map
16                 public var icon:Bitmap;                                         // instance in display list
17                 public var name:Sprite;                                         //  |
18                 private var iconname:String='';                         // name of icon
19
20                 public static const DEFAULT_TEXTFIELD_PARAMS:Object = {
21 //                      embedFonts: true,
22                         antiAliasType: AntiAliasType.ADVANCED,
23                         gridFitType: GridFitType.NONE
24                 };
25 //              [Embed(source="fonts/DejaVuSans.ttf", fontFamily="DejaVu", fontWeight="normal", mimeType="application/x-font-truetype")]
26 //              public static var DejaVu:Class;
27
28                 public function POI(node:Node, map:Map) {
29                         this.map = map;
30                         this.node = node;
31                         redraw();
32                 }
33                 
34                 public function redraw():void {
35                         var tags:Object = node.getTagsCopy();
36                         // ** apply :hover etc.
37                         var sl:StyleList=map.ruleset.getStyles(this.node,tags);
38                         var r:Boolean=false;    // ** rendered
39                         var l:DisplayObject;
40                         for (var sublayer:uint=0; sublayer<10; sublayer++) {
41
42                                 if (sl.pointStyles[sublayer]) {
43                                         var s:PointStyle=sl.pointStyles[sublayer];
44 //                                      if ((s is PointStyle) && s.icon && s.icon!="") 
45                                         r=true;
46                                         if (s.icon_image!=iconname) {
47                                                 // 'load' icon (actually just from library)
48                                                 if (map.ruleset.images[s.icon_image]) {
49 //                                                      l=map.getChildAt(map.POISPRITE);
50 //                                                      Sprite(l).addChild(map.ruleset.images[s.icon_image]);
51                                                         var loader:Loader = new Loader();
52                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
53                                                         loader.loadBytes(map.ruleset.images[s.icon_image]);
54                                                         iconname=s.icon_image;
55                                                 }
56                                         } else {
57                                                 // already loaded, so just reposition
58                                                 updatePosition();
59                                                 iconname=s.icon_image;
60                                         }
61                                 }
62
63                                 if (sl.textStyles[sublayer]) {
64                                         var t:TextStyle=sl.textStyles[sublayer];
65 //                                      if ((s is TextStyle) && s.tag && tags[s.tag])
66                                         // create name sprite
67                                         if (!name) {
68                                                 name=new Sprite();
69                                                 var c:DisplayObject=map.getChildAt(map.NAMESPRITE);
70                                                 Sprite(c).addChild(name);
71                                         }
72                                         t.writeNameLabel(name,tags[t.text],map.lon2coord(node.lon),map.latp2coord(node.latp));
73                                 }
74                         }
75                         if (!r && iconname!='') {
76                                 // not rendered any more, so remove
77                                 l=map.getChildAt(map.POISPRITE);
78                                 Sprite(l).removeChild(icon);
79                                 iconname='';
80                         }
81                 }
82
83                 private function loadedIcon(event:Event):void {
84                         icon = Bitmap(event.target.content);
85                         var l:DisplayObject=map.getChildAt(map.POISPRITE);
86                         Sprite(l).addChild(icon);
87                         updatePosition();
88                 }
89
90                 private function updatePosition():void {
91                         icon.x=map.lon2coord(node.lon)-icon.width/2;
92                         icon.y=map.latp2coord(node.latp)-icon.height/2;
93                 }
94
95         }
96 }