Reinstate embedded fonts (we need them for the rotated text, d'oh) but move to a...
[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 styles:Array=map.ruleset.getStyles(true,tags,map.scale);
37                         var r:Boolean=false;    // ** rendered
38                         for each (var s:* in styles) {
39                                 if ((s is PointStyle) && s.icon && s.icon!="") {
40                                         r=true;
41                                         if (s.icon!=iconname) {
42                                                 // 'load' icon (actually just from library)
43                                                 if (map.ruleset.images[s.icon]) {
44                                                         var loader:Loader = new Loader();
45                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
46                                                         loader.loadBytes(map.ruleset.images[s.icon]);
47                                                         iconname=s.icon;
48                                                 }
49                                         } else {
50                                                 // already loaded, so just reposition
51                                                 updatePosition();
52                                                 iconname=s.icon;
53                                         }
54                                 } else if ((s is TextStyle) && s.tag && tags[s.tag]) {
55                                         // create name sprite
56                                         if (!name) {
57                                                 name=new Sprite();
58                                                 var c:DisplayObject=map.getChildAt(12);
59                                                 Sprite(c).addChild(name);
60                                         }
61                                         s.writeNameLabel(name,tags[s.tag],map.lon2coord(node.lon),map.latp2coord(node.latp));
62                                 }
63                         }
64                         if (!r && iconname!='') {
65                                 // not rendered any more, so remove
66                                 var l:DisplayObject=map.getChildAt(11);
67                                 Sprite(l).removeChild(icon);
68                                 iconname='';
69                         }
70                 }
71
72                 private function loadedIcon(event:Event):void {
73                         icon = Bitmap(event.target.content);
74                         var l:DisplayObject=map.getChildAt(11);
75                         Sprite(l).addChild(icon);
76                         updatePosition();
77                 }
78
79                 private function updatePosition():void {
80                         icon.x=map.lon2coord(node.lon)-icon.width/2;
81                         icon.y=map.latp2coord(node.latp)-icon.height/2;
82                 }
83
84         }
85 }