40dfc067f07b75cc72278feb7ab1387040790139
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / MapFeatures.as
1 package net.systemeD.potlatch2.mapfeatures {
2
3     import flash.events.EventDispatcher;
4     import flash.events.Event;
5     import flash.net.URLLoader;
6     import flash.net.URLRequest;
7
8         import flash.system.Security;
9         import flash.net.*;
10
11     import net.systemeD.halcyon.connection.*;
12
13
14         public class MapFeatures extends EventDispatcher {
15         private static var instance:MapFeatures;
16
17         public static function getInstance():MapFeatures {
18             if ( instance == null ) {
19                 instance = new MapFeatures();
20                 instance.loadFeatures();
21             }
22             return instance;
23         }
24
25
26
27         private var xml:XML = null;
28         private var _features:Array = null;
29         private var _categories:Array = null;
30
31         protected function loadFeatures():void {
32             var request:URLRequest = new URLRequest("map_features.xml");
33             var loader:URLLoader = new URLLoader();
34             loader.addEventListener(Event.COMPLETE, onFeatureLoad);
35             loader.load(request);
36         }
37
38         private function onFeatureLoad(event:Event):void {
39             xml = new XML(URLLoader(event.target).data);
40             
41             _features = new Array();
42             for each(var feature:XML in xml.feature) {
43                 _features.push(new Feature(this, feature));
44             }            
45             _categories = new Array();
46             for each(var catXML:XML in xml.category) {
47                 if ( catXML.child("category").length() == 0 )
48                   _categories.push(new Category(this, catXML.@name, catXML.@id, _categories.length));
49             }
50             dispatchEvent(new Event("featuresLoaded"));
51         }
52
53         public function hasLoaded():Boolean {
54             return xml != null;
55         }
56
57         public function findMatchingFeature(entity:Entity):Feature {
58             if ( xml == null )
59                 return null;
60
61             for each(var feature:Feature in features) {
62                 // check for matching tags
63                 var match:Boolean = true;
64                 for each(var tag:Object in feature.tags) {
65                     var entityTag:String = entity.getTag(tag.k);
66                     match = entityTag == tag.v || (entityTag != null && tag.v == "*");
67                     if ( !match ) break;
68                 }
69                 if ( match )
70                     return feature;
71             }
72             return null;
73         }
74         
75         [Bindable(event="featuresLoaded")]
76         public function get categories():Array {
77             if ( xml == null )
78                 return null;            
79             return _categories;
80         }
81
82         [Bindable(event="featuresLoaded")]
83         public function get features():Array {
84             if ( xml == null )
85                 return null;            
86             return _features;
87         }
88
89     }
90
91 }
92
93