fix some bugs in feature selector, and awesome stuff
[potlatch2.git] / net / systemeD / halcyon / mapfeatures / MapFeatures.as
index 82b114cbb7ea56b3b6c8c257a6cf9d54b646ddc4..3d1c8f1c16e92d71a90ff2e959108bedd572e569 100644 (file)
@@ -1,5 +1,6 @@
 package net.systemeD.halcyon.mapfeatures {
 
+    import flash.events.EventDispatcher;
     import flash.events.Event;
     import flash.net.URLLoader;
     import flash.net.URLRequest;
@@ -10,7 +11,7 @@ package net.systemeD.halcyon.mapfeatures {
     import net.systemeD.halcyon.connection.*;
 
 
-       public class MapFeatures {
+       public class MapFeatures extends EventDispatcher {
         private static var instance:MapFeatures;
 
         public static function getInstance():MapFeatures {
@@ -24,6 +25,8 @@ package net.systemeD.halcyon.mapfeatures {
 
 
         private var xml:XML = null;
+        private var _features:Array = null;
+        private var _categories:Array = null;
 
         protected function loadFeatures():void {
             var request:URLRequest = new URLRequest("map_features.xml");
@@ -34,22 +37,33 @@ package net.systemeD.halcyon.mapfeatures {
 
         private function onFeatureLoad(event:Event):void {
             xml = new XML(URLLoader(event.target).data);
+            
+            _features = new Array();
+            for each(var feature:XML in xml.feature) {
+                _features.push(new Feature(this, feature));
+            }            
+            _categories = new Array();
+            for each(var catXML:XML in xml.category) {
+                if ( catXML.child("category").length() == 0 )
+                  _categories.push(new Category(this, catXML.@name, catXML.@id, _categories.length));
+            }
+            dispatchEvent(new Event("featuresLoaded"));
         }
 
         public function hasLoaded():Boolean {
             return xml != null;
         }
 
-        public function findMatchingFeature(entity:Entity):XML {
+        public function findMatchingFeature(entity:Entity):Feature {
             if ( xml == null )
                 return null;
 
-            for each(var feature:XML in xml.feature) {
+            for each(var feature:Feature in features) {
                 // check for matching tags
                 var match:Boolean = true;
-                for each(var tag:XML in feature.tag) {
-                    var entityTag:String = entity.getTag(tag.@k);
-                    match = entityTag == tag.@v || (entityTag != null && tag.@v == "*");
+                for each(var tag:Object in feature.tags) {
+                    var entityTag:String = entity.getTag(tag.k);
+                    match = entityTag == tag.v || (entityTag != null && tag.v == "*");
                     if ( !match ) break;
                 }
                 if ( match )
@@ -57,6 +71,21 @@ package net.systemeD.halcyon.mapfeatures {
             }
             return null;
         }
+        
+        [Bindable(event="featuresLoaded")]
+        public function get categories():Array {
+            if ( xml == null )
+                return null;            
+            return _categories;
+        }
+
+        [Bindable(event="featuresLoaded")]
+        public function get features():Array {
+            if ( xml == null )
+                return null;            
+            return _features;
+        }
+
     }
 
 }