fix some bugs in feature selector, and awesome stuff
authorDave Stubbs <osm@randomjunk.co.uk>
Wed, 12 Aug 2009 20:22:56 +0000 (20:22 +0000)
committerDave Stubbs <osm@randomjunk.co.uk>
Wed, 12 Aug 2009 20:22:56 +0000 (20:22 +0000)
net/systemeD/halcyon/CategorySelector.mxml
net/systemeD/halcyon/Map.as
net/systemeD/halcyon/TagViewer.mxml
net/systemeD/halcyon/connection/Connection.as
net/systemeD/halcyon/mapfeatures/Category.as
net/systemeD/halcyon/mapfeatures/Feature.as
net/systemeD/halcyon/mapfeatures/MapFeatures.as
resources/map_features.xml

index 4957b35..9bd712b 100644 (file)
@@ -5,11 +5,13 @@
 
   <mx:HBox horizontalGap="0"> 
   <mx:ToggleButtonBar height="100%" dataProvider="{categoryStack}" direction="vertical"/>
-  <mx:ViewStack id="categoryStack" width="100%" height="100%">
+  <mx:ViewStack id="categoryStack" width="100%" height="100%"
+      creationComplete="setSelectedFeature(_selectedType);">
       <mx:Repeater id="catRep" dataProvider="{MapFeatures.getInstance().categories}">
           <mx:VBox label="{catRep.currentItem.name}">
           <mx:TileList dataProvider="{catRep.currentItem.features}"
-                       width="100%" height="100%" change="itemSelected(event);">
+                       width="100%" height="100%" change="itemSelected(event);"
+                       creationComplete="ensureSelection();">
               <mx:itemRenderer>
                   <mx:Component>
                       <mx:VBox width="100" height="75"
@@ -33,7 +35,9 @@
       import net.systemeD.halcyon.mapfeatures.*;
 
       import mx.controls.*;
+      import mx.containers.*;
       
+      private var settingSelection:Boolean = false;
       private var _selectedType:Feature;
       
       [Bindable(event="selectedType")]
       
       private function itemSelected(event:Event):void {
           _selectedType = Feature(TileList(event.currentTarget).selectedItem);
-          dispatchEvent(new Event("selectedType"));
+          if ( !settingSelection )
+              dispatchEvent(new Event("selectedType"));
       }
+      
+      /**
+       * Set the selected feature displayed in the selector.
+       *
+       * The tab is switched to the first category the feature
+       * is part of.
+       *
+       */
+      public function setSelectedFeature(feature:Feature):void {
+          _selectedType = feature;
+          
+          // check whether stack built yet, if not we get called again when it's made
+          if ( categoryStack == null )
+              return;
+
+          if ( feature != null ) {
+              var primaryCategory:Category = feature.findFirstCategory();
+          
+              // set the tab to the selected item's category
+              categoryStack.selectedIndex = primaryCategory.index;
+          }
+          
+          // finalise the item selection
+          ensureSelection();
+      }
+      
+      /**
+       * Sets the selected feature on each category page to the current
+       * value of _selectedType. If there is no selected feature then all
+       * panels selection will be blanked.
+       *
+       * This function does not change the selected category tab.
+       *
+       * Called both from setSelectedFeature, and category tab completion
+       * (so that newly created tabs work as expected)
+       */   
+      private function ensureSelection():void {
+          settingSelection = true;
+          for (var i:Number = 0; i < categoryStack.numChildren; i++) {
+              var box:Box = Box(categoryStack.getChildAt(i));
+              if ( box == null || box.numChildren == 0 )
+                  continue;
+              var tileList:TileList = TileList(box.getChildAt(0));
+              var category:Category = Category(tileList.getRepeaterItem());
+              var selected:Boolean = false;
+              for each( var catFeature:Feature in category.features ) {
+                  if ( catFeature == _selectedType ) {
+                      tileList.selectedItem = _selectedType;
+                      selected = true;
+                  }
+              }
+              if ( !selected )
+                  tileList.selectedItem = null;
+          }
+          settingSelection = false;
+      }
+      
   ]]></mx:Script>
 </mx:VBox>
 
index 09346c0..239da0c 100755 (executable)
@@ -119,7 +119,7 @@ package net.systemeD.halcyon {
                        var FontLibrary:Class = r.target.applicationDomain.getDefinition("FontLibrary") as Class;
                        Font.registerFont(FontLibrary.DejaVu);
 
-                       if (initparams.hasOwnProperty('lat')) {
+                       if (initparams['lat'] != null) {
                                // parameters sent from HTML
                                init(initparams['lat'],
                                         initparams['lon'],
index e185ae3..8b805da 100644 (file)
@@ -86,6 +86,7 @@
           var txt:String = feature.htmlDetails(entity);
           iconText.htmlText = txt;
           popupChange.label = feature.name;
+          tw.setSelectedFeature(feature);
       }
 
       private function blankFeatureIcon(entity:Entity):void {
@@ -94,6 +95,7 @@
                "<i>Nothing selected</i>" :
                "<b>Not recognised</b><br/>Try looking at the tags under the advanced properties";
           popupChange.label = "unknown";
+          tw.setSelectedFeature(null);
       }
 
       private function checkAdvanced():void {
           if ( selectedEntity == null )
               return;
 
+          var newFeature:Feature = tw.selectedType;
+          
           // remove tags from the current feature
           if ( feature != null ) {
-              for each( var tag:Object in feature.tags ) {
-                  selectedEntity.setTag(tag["k"], null);
+              for each( var oldtag:Object in feature.tags ) {
+                  selectedEntity.setTag(oldtag["k"], null);
               }
           }
           
           // set tags for new feature
-          for each( var tag:Object in tw.selectedType.tags ) {
-              selectedEntity.setTag(tag["k"], tag["v"]);
+          if ( newFeature != null ) {
+              for each( var newtag:Object in newFeature.tags ) {
+                  selectedEntity.setTag(newtag["k"], newtag["v"]);
+              }
           }
           
-          trace("w000t "+tw.selectedType);
           popupChange.close();
       }
   ]]></mx:Script>
index d4d13c1..9cfa9ec 100755 (executable)
@@ -7,7 +7,7 @@ package net.systemeD.halcyon.connection {
 
        public class Connection extends EventDispatcher {
 
-        private static var CONNECTION_TYPE:String = "AMF";
+        private static var CONNECTION_TYPE:String = "XML";
         private static var connectionInstance:Connection = null;
 
         protected static var policyURL:String = "http://127.0.0.1:3000/api/crossdomain.xml";
@@ -15,9 +15,12 @@ package net.systemeD.halcyon.connection {
 
         public static function getConnection(api:String,policy:String,conn:String):Connection {
                        
-                       policyURL=policy;
-                       apiBaseURL=api;
-                       CONNECTION_TYPE=conn;
+                       if ( policy != null )
+                           policyURL=policy;
+                       if ( api != null )
+                           apiBaseURL=api;
+                       if ( conn != null )
+                           CONNECTION_TYPE=conn;
                        
             if ( connectionInstance == null ) {
                 if ( CONNECTION_TYPE == "XML" )
index 14d0d28..b716b4c 100644 (file)
@@ -8,11 +8,13 @@ package net.systemeD.halcyon.mapfeatures {
         private var _name:String;
         private var _id:String;
         private var _features:Array;
+        private var _index:uint;
 
-        public function Category(mapFeatures:MapFeatures, name:String, id:String) {
+        public function Category(mapFeatures:MapFeatures, name:String, id:String, globalIndex:uint) {
             this.mapFeatures = mapFeatures;
             this._name = name;
             this._id = id;
+            this._index = globalIndex;
             
             _features = new Array();
             for each( var feature:Feature in mapFeatures.features ) {
@@ -24,6 +26,10 @@ package net.systemeD.halcyon.mapfeatures {
         public function get id():String {
             return _id;
         }
+        
+        public function get index():uint {
+            return _index;
+        }
 
         [Bindable(event="categoryChange")]
         public function get name():String {
index abc3bde..88dc187 100644 (file)
@@ -10,7 +10,8 @@ package net.systemeD.halcyon.mapfeatures {
         private static var variablesPattern:RegExp = /[$][{]([^}]+)[}]/g;
         private var _tags:Array;
 
-        public function Feature(_xml:XML) {
+        public function Feature(mapFeatures:MapFeatures, _xml:XML) {
+            this.mapFeatures = mapFeatures;
             this._xml = _xml;
             _tags = new Array();
             
@@ -70,6 +71,14 @@ package net.systemeD.halcyon.mapfeatures {
         public function get tags():Array {
             return _tags;
         }
+        
+        public function findFirstCategory():Category {
+            for each( var cat:Category in mapFeatures.categories ) {
+                if ( isInCategory(cat.id) )
+                    return cat;
+            }
+            return null;
+        }
     }
 }
 
index 42859cc..3d1c8f1 100644 (file)
@@ -40,12 +40,12 @@ package net.systemeD.halcyon.mapfeatures {
             
             _features = new Array();
             for each(var feature:XML in xml.feature) {
-                _features.push(new Feature(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.push(new Category(this, catXML.@name, catXML.@id, _categories.length));
             }
             dispatchEvent(new Event("featuresLoaded"));
         }
index 55dee80..f4d515c 100644 (file)
     </icon>
 
     <line/>
-    <tag k="highway" v="tertiary"/>
+    <tag k="highway" v="road"/>
   </feature>
 
   <feature name="Living Street">