New RuleChain object to replace previous nested array within StyleChooser
[potlatch2.git] / net / systemeD / halcyon / styleparser / RuleChain.as
1 package net.systemeD.halcyon.styleparser {
2
3     import net.systemeD.halcyon.connection.*;
4
5         /**     A descendant list of MapCSS selectors (Rules).
6         
7                 For example,
8                         relation[type=route] way[highway=primary]
9                         ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
10                             first Rule           second Rule
11                                |------------|---------|
12                                             |
13                                      one RuleChain
14
15         */
16
17         public class RuleChain {
18                 public var rules:Array=[];              // should eventually become a Vector of Rules
19                 public var subpart:String='';   // subpart name, as in way[highway=primary]::centreline
20
21                 // Test a ruleChain
22                 // - run a set of tests in the chain
23                 //              works backwards from at position "pos" in array, or -1  for the last
24                 //              separate tags object is required in case they've been dynamically retagged
25                 // - if they fail, return false
26                 // - if they succeed, and it's the last in the chain, return happily
27                 // - if they succeed, and there's more in the chain, rerun this for each parent until success
28                 
29                 public function test(pos:int, obj:Entity, tags:Object, zoom:uint):Boolean {
30                         if (pos==-1) { pos=rules.length-1; }
31
32                         var r:Rule=rules[pos];
33                         if (!r.test(obj, tags, zoom)) { return false; }
34                         if (pos==0) { return true; }
35                         
36                         var o:Array=obj.parentObjects;
37                         for each (var p:Entity in o) {
38                                 if (test(pos-1, p, p.getTagsHash(), zoom)) { return true; }
39                         }
40                         return false;
41                 }
42                 
43                 public function get length():int {
44                         return rules.length;
45                 }
46                 
47                 // ---------------------------------------------------------------------------------------------
48                 // Methods to add properties (used by parsers such as MapCSS)
49
50                 public function addRule(e:String=''):void {
51                         rules.push(new Rule(e));
52                 }
53
54                 public function addConditionToLast(c:Condition):void {
55                         rules[rules.length-1].conditions.push(c);
56                 }
57
58                 public function addZoomToLast(z1:uint,z2:uint):void {
59                         rules[rules.length-1].minZoom=z1;
60                         rules[rules.length-1].maxZoom=z2;
61                 }
62
63
64         }
65 }