Better support for MapCSS 0.2, including subparts
[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='default';    // 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 (length==0) { return false; }
31                         if (pos==-1) { pos=rules.length-1; }
32                         
33                         var r:Rule=rules[pos];
34                         if (!r.test(obj, tags, zoom)) { return false; }
35                         if (pos==0) { return true; }
36                         
37                         var o:Array=obj.parentObjects;
38                         for each (var p:Entity in o) {
39                                 if (test(pos-1, p, p.getTagsHash(), zoom)) { return true; }
40                         }
41                         return false;
42                 }
43                 
44                 public function get length():int {
45                         return rules.length;
46                 }
47                 
48                 public function setSubpart(s:String):void {
49                         subpart = s=='' ? 'default' : s;
50                 }
51
52                 // ---------------------------------------------------------------------------------------------
53                 // Methods to add properties (used by parsers such as MapCSS)
54
55                 public function addRule(e:String=''):void {
56                         rules.push(new Rule(e));
57                 }
58
59                 public function addConditionToLast(c:Condition):void {
60                         rules[rules.length-1].conditions.push(c);
61                 }
62
63                 public function addZoomToLast(z1:uint,z2:uint):void {
64                         rules[rules.length-1].minZoom=z1;
65                         rules[rules.length-1].maxZoom=z2;
66                 }
67
68         }
69 }