start of turn restriction editor
[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?"+Math.random());
33             var loader:URLLoader = new URLLoader();
34             loader.addEventListener(Event.COMPLETE, onFeatureLoad);
35             loader.load(request);
36         }
37
38         internal function get definition():XML {
39             return xml;
40         }
41         
42         private function onFeatureLoad(event:Event):void {
43             xml = new XML(URLLoader(event.target).data);
44             
45             _features = new Array();
46             for each(var feature:XML in xml.feature) {
47                 _features.push(new Feature(this, feature));
48             }            
49             _categories = new Array();
50             for each(var catXML:XML in xml.category) {
51                 if ( catXML.child("category").length() == 0 )
52                   _categories.push(new Category(this, catXML.@name, catXML.@id, _categories.length));
53             }
54             dispatchEvent(new Event("featuresLoaded"));
55         }
56
57         public function hasLoaded():Boolean {
58             return xml != null;
59         }
60
61         public function findMatchingFeature(entity:Entity):Feature {
62             if ( xml == null )
63                 return null;
64
65             for each(var feature:Feature in features) {
66                 var match:Boolean = true;
67
68                 // check for matching tags
69                 for each(var tag:Object in feature.tags) {
70                     var entityTag:String = entity.getTag(tag.k);
71                     match = entityTag == tag.v || (entityTag != null && tag.v == "*");
72                     if ( !match ) break;
73                 }
74
75                                 // check for matching withins
76                                 if (match) {
77                                         for each (var within:Object in feature.withins) {
78                                                 match = entity.countParentObjects(within) >= (within.minimum ? within.minimum : 1);
79                                                 if (!match) { break; }
80                                         }
81                                 }
82
83                 if (match) {
84                     return feature;
85                                 }
86             }
87             return null;
88         }
89         
90         [Bindable(event="featuresLoaded")]
91         public function get categories():Array {
92             if ( xml == null )
93                 return null;        
94             return _categories;
95         }
96
97         [Bindable(event="featuresLoaded")]
98         public function getCategoriesForType(type:String):Array {
99             if ( xml == null )
100                 return null;
101             if ( type == null || type == "" )  
102                 return []; //_categories;
103                 
104             var filteredCategories:Array = new Array();
105             for each( var cat:Category in _categories ) {
106                 if ( cat.getFeaturesForType(type).length > 0 )
107                     filteredCategories.push(cat);
108             }
109             return filteredCategories;
110         }
111
112         [Bindable(event="featuresLoaded")]
113         public function get features():Array {
114             if ( xml == null )
115                 return null;            
116             return _features;
117         }
118
119         [Bindable(event="featuresLoaded")]
120         public function get pois():Array {
121             if (xml == null )
122                 return null;
123             var pois:Array = [];
124             var counter:int = 0;
125             for each ( var feature:Feature in _features ) {
126               if (feature.isType("point")) {
127                 pois.push(feature);
128               }
129             }
130             return pois;
131         }
132     }
133
134 }
135
136