some more footling around with POI nodes in ways
[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:Sprite;                                         // instance in display list
17                 public var name:Sprite;                                         //  |
18                 private var iconname:String='';                         // name of icon
19                 public var loaded:Boolean=false;
20
21                 public static const DEFAULT_TEXTFIELD_PARAMS:Object = {
22 //                      embedFonts: true,
23                         antiAliasType: AntiAliasType.ADVANCED,
24                         gridFitType: GridFitType.NONE
25                 };
26 //              [Embed(source="fonts/DejaVuSans.ttf", fontFamily="DejaVu", fontWeight="normal", mimeType="application/x-font-truetype")]
27 //              public static var DejaVu:Class;
28
29                 public function POI(node:Node, map:Map, sl:StyleList=null) {
30                         this.map = map;
31                         this.node = node;
32                         redraw(sl);
33                 }
34                 
35                 public function redraw(sl:StyleList=null):Boolean {
36                         var tags:Object = node.getTagsCopy();
37                         // ** apply :hover etc.
38                         if (!sl) { sl=map.ruleset.getStyles(this.node,tags); }
39                         if (!sl.hasStyles()) { return false; }
40                         
41                         var r:Boolean=false;    // ** rendered
42                         var l:DisplayObject;
43                         for (var sublayer:uint=0; sublayer<10; sublayer++) {
44
45                                 if (sl.pointStyles[sublayer]) {
46                                         var s:PointStyle=sl.pointStyles[sublayer];
47                                         r=true;
48                                         if (s.icon_image!=iconname) {
49                                                 // 'load' icon (actually just from library)
50                                                 if (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                                         }
60                                 }
61
62                                 if (sl.textStyles[sublayer]) {
63                                         var t:TextStyle=sl.textStyles[sublayer];
64                                         // create name sprite
65                                         if (!name) {
66                                                 name=new Sprite();
67                                                 var c:DisplayObject=map.getChildAt(map.NAMESPRITE);
68                                                 Sprite(c).addChild(name);
69                                         }
70                                         t.writeNameLabel(name,tags[t.text],map.lon2coord(node.lon),map.latp2coord(node.latp));
71                                 }
72                         }
73                         if (!r && iconname!='') {
74                                 // not rendered any more, so remove
75                                 l=map.getChildAt(map.POISPRITE);
76                                 Sprite(l).removeChild(icon);
77                                 iconname='';
78                         }
79                         return true;
80                 }
81
82                 private function loadedIcon(event:Event):void {
83                         icon = new Sprite();
84                         icon.addChild(Bitmap(event.target.content));
85                         var l:DisplayObject=map.getChildAt(map.POISPRITE);
86                         Sprite(l).addChild(icon);
87                         updatePosition();
88
89             icon.addEventListener(MouseEvent.CLICK, mouseEvent);
90             icon.buttonMode = true;
91             icon.mouseEnabled = true;
92
93                         loaded=true;
94                 }
95
96         private function mouseEvent(event:MouseEvent):void {
97                         map.entityMouseEvent(event, node);
98         }
99
100                 private function updatePosition():void {
101                         icon.x=map.lon2coord(node.lon)-icon.width/2;
102                         icon.y=map.latp2coord(node.latp)-icon.height/2;
103                 }
104
105         }
106 }