New RuleChain object to replace previous nested array within StyleChooser
[potlatch2.git] / net / systemeD / halcyon / styleparser / StyleChooser.as
index 05b71b8823d4bda32fd611831c18b873680fa8e7..1a3f86fa83a64c2e2ce317f70fe504e1ba872114 100644 (file)
@@ -10,25 +10,35 @@ package net.systemeD.halcyon.styleparser {
                        Its ruleChains property is an array of all the selectors, which would
                        traditionally be comma-separated. For example:
                                h1, h2, h3 em
-                       is three ruleChains.
+                       is three RuleChains.
                        
-                       Each ruleChain is itself an array of nested selectors. So the above 
+                       Each RuleChain is itself an array of nested selectors. So the above 
                        example would roughly be encoded as:
                                [[h1],[h2],[h3,em]]
                                  ^^   ^^   ^^ ^^   each of these is a Rule
-
+                                ^^^^ ^^^^ ^^^^^^^  each of these is a RuleChain
+                               
                        The styles property is an array of all the style objects to be drawn
                        if any of the ruleChains evaluate to true.
 
                */
 
-               public var ruleChains:Array=[[]];                       // array of array of Rules
+               public var ruleChains:Array;                            // array of RuleChains (each one an array of Rules)
                public var styles:Array=[];                                     // array of ShapeStyle/ShieldStyle/TextStyle/PointStyle
                public var zoomSpecific:Boolean=false;          // are any of the rules zoom-specific?
 
-               private var rcpos:uint=0;
+               private var rcpos:uint;
                private var stylepos:uint=0;
 
+               public function StyleChooser():void {
+                       ruleChains=[new RuleChain()];
+                       rcpos=0;
+               }
+
+               public function get currentChain():RuleChain {
+                       return ruleChains[rcpos];
+               }
+
                // Update the current StyleList from this StyleChooser
 
                public function updateStyles(obj:Entity, tags:Object, sl:StyleList, imageWidths:Object, zoom:uint):void {
@@ -37,8 +47,8 @@ package net.systemeD.halcyon.styleparser {
                        // Are any of the ruleChains fulfilled?
                        var w:Number;
                        var fulfilled:Boolean=false;
-                       for each (var c:Array in ruleChains) {
-                               if (testChain(c,-1,obj,tags,zoom)) {
+                       for each (var c:RuleChain in ruleChains) {
+                               if (c.test(-1,obj,tags,zoom)) {
                                        fulfilled=true; break;
                                }
                        }
@@ -86,58 +96,18 @@ package net.systemeD.halcyon.styleparser {
                                }
                        }
                }
-
-
-               // Test a ruleChain
-               // - run a set of tests in the chain
-               //              works backwards from at position "pos" in array, or -1  for the last
-               //              separate tags object is required in case they've been dynamically retagged
-               // - if they fail, return false
-               // - if they succeed, and it's the last in the chain, return happily
-               // - if they succeed, and there's more in the chain, rerun this for each parent until success
-               
-               private function testChain(chain:Array,pos:int,obj:Entity,tags:Object,zoom:uint):Boolean {
-                       if (pos==-1) { pos=chain.length-1; }
-
-                       var r:Rule=chain[pos];
-                       if (!r.test(obj, tags, zoom)) { return false; }
-                       if (pos==0) { return true; }
-                       
-                       var o:Array=obj.parentObjects;
-                       for each (var p:Entity in o) {
-                               if (testChain(chain, pos-1, p, p.getTagsHash(), zoom )) { return true; }
-                       }
-                       return false;
-               }
                
                
                // ---------------------------------------------------------------------------------------------
                // Methods to add properties (used by parsers such as MapCSS)
                
-               // newGroup             <- starts a new ruleChain in this.ruleChains
-               public function newGroup():void {
+               // newRuleChain         <- starts a new ruleChain in this.ruleChains
+               public function newRuleChain():void {
                        if (ruleChains[rcpos].length>0) {
-                               ruleChains[++rcpos]=[];
+                               ruleChains[++rcpos]=new RuleChain();
                        }
                }
 
-               // newObject    <- adds into the current ruleChain (starting a new Rule)
-               public function newObject(e:String=''):void {
-                       ruleChains[rcpos].push(new Rule(e));
-               }
-
-               // addZoom              <- adds into the current ruleChain (existing Rule)
-               public function addZoom(z1:uint,z2:uint):void {
-                       ruleChains[rcpos][ruleChains[rcpos].length-1].minZoom=z1;
-                       ruleChains[rcpos][ruleChains[rcpos].length-1].maxZoom=z2;
-                       zoomSpecific=true;
-               }
-               
-               // addCondition <- adds into the current ruleChain (existing Rule)
-               public function addCondition(c:Condition):void {
-                       ruleChains[rcpos][ruleChains[rcpos].length-1].conditions.push(c);
-               }
-
                // addStyles    <- adds to this.styles
                public function addStyles(a:Array):void {
                        styles=styles.concat(a);