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