92f230316bd1250364962db33dee07ae0414638e
[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                         for (var sublayer:uint=0; sublayer<10; sublayer++) {
40
41                                 if (sl.pointStyles[sublayer]) {
42                                         var s:PointStyle=sl.pointStyles[sublayer];
43 //                                      if ((s is PointStyle) && s.icon && s.icon!="") 
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 //                                      if ((s is TextStyle) && s.tag && tags[s.tag])
63                                         // create name sprite
64                                         if (!name) {
65                                                 name=new Sprite();
66                                                 var c:DisplayObject=map.getChildAt(12);
67                                                 Sprite(c).addChild(name);
68                                         }
69                                         t.writeNameLabel(name,tags[t.text],map.lon2coord(node.lon),map.latp2coord(node.latp));
70                                 }
71                         }
72                         if (!r && iconname!='') {
73                                 // not rendered any more, so remove
74                                 var l:DisplayObject=map.getChildAt(11);
75                                 Sprite(l).removeChild(icon);
76                                 iconname='';
77                         }
78                 }
79
80                 private function loadedIcon(event:Event):void {
81                         icon = Bitmap(event.target.content);
82                         var l:DisplayObject=map.getChildAt(11);
83                         Sprite(l).addChild(icon);
84                         updatePosition();
85                 }
86
87                 private function updatePosition():void {
88                         icon.x=map.lon2coord(node.lon)-icon.width/2;
89                         icon.y=map.latp2coord(node.latp)-icon.height/2;
90                 }
91
92         }
93 }