be01436da347c94906441268cba396f6229fd256
[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                                         r=true;
45                                         if (s.icon_image!=iconname) {
46                                                 // 'load' icon (actually just from library)
47                                                 if (map.ruleset.images[s.icon_image]) {
48                                                         var loader:Loader = new Loader();
49                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
50                                                         loader.loadBytes(map.ruleset.images[s.icon_image]);
51                                                         iconname=s.icon_image;
52                                                 }
53                                         } else {
54                                                 // already loaded, so just reposition
55                                                 updatePosition();
56                                                 iconname=s.icon_image;
57                                         }
58                                 }
59
60                                 if (sl.textStyles[sublayer]) {
61                                         var t:TextStyle=sl.textStyles[sublayer];
62                                         // create name sprite
63                                         if (!name) {
64                                                 name=new Sprite();
65                                                 var c:DisplayObject=map.getChildAt(map.NAMESPRITE);
66                                                 Sprite(c).addChild(name);
67                                         }
68                                         t.writeNameLabel(name,tags[t.text],map.lon2coord(node.lon),map.latp2coord(node.latp));
69                                 }
70                         }
71                         if (!r && iconname!='') {
72                                 // not rendered any more, so remove
73                                 l=map.getChildAt(map.POISPRITE);
74                                 Sprite(l).removeChild(icon);
75                                 iconname='';
76                         }
77                 }
78
79                 private function loadedIcon(event:Event):void {
80                         icon = Bitmap(event.target.content);
81                         var l:DisplayObject=map.getChildAt(map.POISPRITE);
82                         Sprite(l).addChild(icon);
83                         updatePosition();
84                 }
85
86                 private function updatePosition():void {
87                         icon.x=map.lon2coord(node.lon)-icon.width/2;
88                         icon.y=map.latp2coord(node.latp)-icon.height/2;
89                 }
90
91         }
92 }