MapCSS in progress. Vast amounts still to do, but the meat of it is here. POIs are...
[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                         var sl:StyleList=map.ruleset.getStyles(this.node,map.scale);
37                         var r:Boolean=false;    // ** rendered
38                         for (var sublayer:uint=0; sublayer<10; sublayer++) {
39
40                                 if (sl.pointStyles[sublayer]) {
41                                         var s:PointStyle=sl.pointStyles[sublayer];
42 //                                      if ((s is PointStyle) && s.icon && s.icon!="") 
43                                         r=true;
44                                         if (s.icon_image!=iconname) {
45                                                 // 'load' icon (actually just from library)
46                                                 if (map.ruleset.images[s.icon_image]) {
47                                                         var loader:Loader = new Loader();
48                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
49                                                         loader.loadBytes(map.ruleset.images[s.icon_image]);
50                                                         iconname=s.icon_image;
51                                                 }
52                                         } else {
53                                                 // already loaded, so just reposition
54                                                 updatePosition();
55                                                 iconname=s.icon_image;
56                                         }
57                                 }
58
59                                 if (sl.textStyles[sublayer]) {
60                                         var t:TextStyle=sl.textStyles[sublayer];
61 //                                      if ((s is TextStyle) && s.tag && tags[s.tag])
62                                         // create name sprite
63                                         if (!name) {
64                                                 name=new Sprite();
65                                                 var c:DisplayObject=map.getChildAt(12);
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                                 var l:DisplayObject=map.getChildAt(11);
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(11);
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 }