32dfef0b1eb59aecccedf94140fd189ed8c62b2b
[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.connection.Connection;
11         import net.systemeD.halcyon.styleparser.*;
12         
13         public class POI extends Object {
14                 
15         private var node:Node;
16                 public var map:Map;                                                     // reference to parent map
17                 public var icon:Sprite;                                         // instance in display list
18                 public var name:Sprite;                                         //  |
19                 private var iconname:String='';                         // name of icon
20                 public var loaded:Boolean=false;
21
22                 public static const DEFAULT_TEXTFIELD_PARAMS:Object = {
23 //                      embedFonts: true,
24                         antiAliasType: AntiAliasType.ADVANCED,
25                         gridFitType: GridFitType.NONE
26                 };
27 //              [Embed(source="fonts/DejaVuSans.ttf", fontFamily="DejaVu", fontWeight="normal", mimeType="application/x-font-truetype")]
28 //              public static var DejaVu:Class;
29
30                 public function POI(node:Node, map:Map, sl:StyleList=null) {
31                         this.map = map;
32                         this.node = node;
33                         redraw(sl);
34                         node.addEventListener(Connection.NODE_MOVED, nodeMoved);
35                 }
36                 
37                 public function nodeMoved(event:Event):void {
38                     updatePosition();
39                 }
40                 
41                 public function redraw(sl:StyleList=null):Boolean {
42                         var tags:Object = node.getTagsCopy();
43                         // ** apply :hover etc.
44                         if (!sl) { sl=map.ruleset.getStyles(this.node,tags); }
45                         if (!sl.hasStyles() && iconname=='') { return false; }
46                         
47                         var r:Boolean=false;    // ** rendered
48                         var l:DisplayObject;
49                         for (var sublayer:uint=0; sublayer<10; sublayer++) {
50
51                                 if (sl.pointStyles[sublayer]) {
52                                         var s:PointStyle=sl.pointStyles[sublayer];
53                                         r=true;
54                                         if (s.icon_image!=iconname) {
55                                                 // 'load' icon (actually just from library)
56                                                 if (map.ruleset.images[s.icon_image]) {
57                                                         var loader:Loader = new Loader();
58                                                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedIcon);
59                                                         loader.loadBytes(map.ruleset.images[s.icon_image]);
60                                                         iconname=s.icon_image;
61                                                 }
62                                         } else {
63                                                 // already loaded, so just reposition
64                                                 updatePosition();
65                                         }
66                                 }
67
68                                 // name sprite
69                                 var a:String, t:TextStyle;
70                                 if (sl.textStyles[sublayer]) {
71                                         t=sl.textStyles[sublayer];
72                                         a=tags[t.text];
73                                 }
74
75                                 var c:DisplayObject=map.getChildAt(map.NAMESPRITE);
76                                 if (a) { 
77                                         if (!name) { name=new Sprite(); Sprite(c).addChild(name); }
78                                         t.writeNameLabel(name,a,map.lon2coord(node.lon),map.latp2coord(node.latp));
79                                 } else if (name) {
80                                         Sprite(c).removeChild(name);
81                                         name=null;
82                                 }
83                         }
84                         if (!r && iconname!='') {
85                                 // not rendered any more, so remove
86                                 l=map.getChildAt(map.POISPRITE);
87                                 Sprite(l).removeChild(icon);
88                                 iconname='';
89                         }
90                         return true;
91                 }
92
93                 private function loadedIcon(event:Event):void {
94                         icon = new Sprite();
95                         icon.addChild(Bitmap(event.target.content));
96                         var l:DisplayObject=map.getChildAt(map.POISPRITE);
97                         Sprite(l).addChild(icon);
98                         loaded=true;
99                         updatePosition();
100
101             icon.addEventListener(MouseEvent.CLICK, mouseEvent);
102             icon.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEvent);
103             icon.addEventListener(MouseEvent.MOUSE_OVER, mouseEvent);
104             icon.addEventListener(MouseEvent.MOUSE_OUT, mouseEvent);
105             icon.addEventListener(MouseEvent.MOUSE_DOWN, mouseEvent);
106             icon.addEventListener(MouseEvent.MOUSE_UP, mouseEvent);
107             icon.addEventListener(MouseEvent.MOUSE_MOVE, mouseEvent);
108             icon.buttonMode = true;
109             icon.mouseEnabled = true;
110
111                 }
112
113         private function mouseEvent(event:MouseEvent):void {
114                         map.entityMouseEvent(event, node);
115         }
116
117                 private function updatePosition():void {
118                         if (!loaded) { return; }
119                         icon.x=map.lon2coord(node.lon)-icon.width/2;
120                         icon.y=map.latp2coord(node.latp)-icon.height/2;
121                 }
122
123         }
124 }