disable caching when using Flash Debug Player
[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
7         import flash.system.Security;
8         import flash.net.*;
9
10     import net.systemeD.halcyon.connection.*;
11         import net.systemeD.halcyon.DebugURLRequest;
12
13         public class MapFeatures extends EventDispatcher {
14         private static var instance:MapFeatures;
15
16         public static function getInstance():MapFeatures {
17             if ( instance == null ) {
18                 instance = new MapFeatures();
19                 instance.loadFeatures();
20             }
21             return instance;
22         }
23
24
25
26         private var xml:XML = null;
27         private var _features:Array = null;
28         private var _categories:Array = null;
29
30         protected function loadFeatures():void {
31             var request:DebugURLRequest = new DebugURLRequest("map_features.xml");
32             var loader:URLLoader = new URLLoader();
33             loader.addEventListener(Event.COMPLETE, onFeatureLoad);
34             loader.load(request.request);
35         }
36
37         internal function get definition():XML {
38             return xml;
39         }
40         
41         private function onFeatureLoad(event:Event):void {
42             xml = new XML(URLLoader(event.target).data);
43             
44             _features = new Array();
45             for each(var feature:XML in xml.feature) {
46                 _features.push(new Feature(this, feature));
47             }            
48             _categories = new Array();
49             for each(var catXML:XML in xml.category) {
50                 if ( catXML.child("category").length() == 0 )
51                   _categories.push(new Category(this, catXML.@name, catXML.@id, _categories.length));
52             }
53             dispatchEvent(new Event("featuresLoaded"));
54         }
55
56         public function hasLoaded():Boolean {
57             return xml != null;
58         }
59
60         public function findMatchingFeature(entity:Entity):Feature {
61             if ( xml == null )
62                 return null;
63
64             for each(var feature:Feature in features) {
65                 var match:Boolean = true;
66
67                 // check for matching tags
68                 for each(var tag:Object in feature.tags) {
69                     var entityTag:String = entity.getTag(tag.k);
70                     match = entityTag == tag.v || (entityTag != null && tag.v == "*");
71                     if ( !match ) break;
72                 }
73
74                                 // check for matching withins
75                                 if (match) {
76                                         for each (var within:Object in feature.withins) {
77                                                 match = entity.countParentObjects(within) >= (within.minimum ? within.minimum : 1);
78                                                 if (!match) { break; }
79                                         }
80                                 }
81
82                 if (match) {
83                     return feature;
84                                 }
85             }
86             return null;
87         }
88         
89         [Bindable(event="featuresLoaded")]
90         public function get categories():Array {
91             if ( xml == null )
92                 return null;        
93             return _categories;
94         }
95
96         [Bindable(event="featuresLoaded")]
97         public function getCategoriesForType(type:String):Array {
98             if ( xml == null )
99                 return null;
100             if ( type == null || type == "" )  
101                 return []; //_categories;
102                 
103             var filteredCategories:Array = new Array();
104             for each( var cat:Category in _categories ) {
105                 if ( cat.getFeaturesForType(type).length > 0 )
106                     filteredCategories.push(cat);
107             }
108             return filteredCategories;
109         }
110
111         [Bindable(event="featuresLoaded")]
112         public function get features():Array {
113             if ( xml == null )
114                 return null;            
115             return _features;
116         }
117
118         [Bindable(event="featuresLoaded")]
119         public function get pois():Array {
120             if (xml == null )
121                 return null;
122             var pois:Array = [];
123             var counter:int = 0;
124             for each ( var feature:Feature in _features ) {
125               if (feature.isType("point")) {
126                 pois.push(feature);
127               }
128             }
129             return pois;
130         }
131     }
132
133 }
134
135