fix coord transport for the bitmap sprites
[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                                 if (sl.textStyles[sublayer]) {
69                                         var t:TextStyle=sl.textStyles[sublayer];
70                                         // create name sprite
71                                         if (!name) {
72                                                 name=new Sprite();
73                                                 var c:DisplayObject=map.getChildAt(map.NAMESPRITE);
74                                                 Sprite(c).addChild(name);
75                                         }
76                                         t.writeNameLabel(name,tags[t.text],map.lon2coord(node.lon),map.latp2coord(node.latp));
77                                 }
78                         }
79                         if (!r && iconname!='') {
80                                 // not rendered any more, so remove
81                                 l=map.getChildAt(map.POISPRITE);
82                                 Sprite(l).removeChild(icon);
83                                 iconname='';
84                         }
85                         return true;
86                 }
87
88                 private function loadedIcon(event:Event):void {
89                         icon = new Sprite();
90                         icon.addChild(Bitmap(event.target.content));
91                         var l:DisplayObject=map.getChildAt(map.POISPRITE);
92                         Sprite(l).addChild(icon);
93                         loaded=true;
94                         updatePosition();
95
96             icon.addEventListener(MouseEvent.CLICK, mouseEvent);
97             icon.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEvent);
98             icon.addEventListener(MouseEvent.MOUSE_OVER, mouseEvent);
99             icon.addEventListener(MouseEvent.MOUSE_OUT, mouseEvent);
100             icon.addEventListener(MouseEvent.MOUSE_DOWN, mouseEvent);
101             icon.addEventListener(MouseEvent.MOUSE_UP, mouseEvent);
102             icon.addEventListener(MouseEvent.MOUSE_MOVE, mouseEvent);
103             icon.buttonMode = true;
104             icon.mouseEnabled = true;
105
106                 }
107
108         private function mouseEvent(event:MouseEvent):void {
109                         map.entityMouseEvent(event, node);
110         }
111
112                 private function updatePosition():void {
113                         if (!loaded) { return; }
114                         icon.x=map.lon2coord(node.lon)-icon.width/2;
115                         icon.y=map.latp2coord(node.latp)-icon.height/2;
116                 }
117
118         }
119 }