Better support for MapCSS 0.2, including subparts
[potlatch2.git] / net / systemeD / halcyon / styleparser / Rule.as
1 package net.systemeD.halcyon.styleparser {
2
3     import net.systemeD.halcyon.connection.*;
4
5         /**     A MapCSS selector. Contains a list of Conditions; the entity type to which the selector applies; 
6                 and the zoom levels at which it is true. way[waterway=river][boat=yes] would be parsed into one Rule. <p>
7                 
8                 The selectors and declaration together form a StyleChooser.                                                                                       </p>
9                 
10                 @see net.systemeD.halcyon.styleparser.Condition
11                 @see net.systemeD.halcyon.styleparser.StyleChooser */
12
13         public class Rule {
14
15                 /** The Conditions to be evaluated for the Rule to be fulfilled. */
16                 public var conditions:Array = [];
17                 /** Do all Conditions need to be true for the Rule to be fulfilled? (Always =true for MapCSS.) */
18                 public var isAnd:Boolean = true;
19                 /** Minimum zoom level at which the Rule is fulfilled. */
20                 public var minZoom:uint = 0;
21                 /** Maximum zoom level at which the Rule is fulfilled. */
22                 public var maxZoom:uint = 255;
23                 /** Entity type to which the Rule applies. Can be 'way', 'node', 'relation', 'area' (closed way) or 'line' (unclosed way). */
24                 public var subject:String='';
25                 
26                 public function Rule(subject:String=''):void {
27                         this.subject=subject;
28                 }
29                 
30                 /** Evaluate the Rule on the given entity, tags and zoom level.
31                         @return True if the Rule passes, false if the conditions aren't fulfilled. */
32
33                 public function test(entity:Entity,tags:Object,zoom:uint):Boolean {
34                         if (subject!='' && !entity.isType(subject)) { return false; }
35                         if (zoom<minZoom || zoom>maxZoom) { return false; }
36                         
37                         var v:Boolean=true; var i:uint=0;
38                         for each (var condition:Condition in conditions) {
39                                 var r:Boolean=condition.test(tags);
40                                 if (i==0) { v=r; }
41                                 else if (isAnd) { v=v && r; }
42                                 else { v = v || r;}
43                                 i++;
44                         }
45                         return v;
46                 }
47
48                 public function toString():String {
49                         return subject+" z"+minZoom+"-"+maxZoom+": "+conditions;
50                 }
51         }
52 }