better still...
[potlatch2.git] / net / systemeD / halcyon / styleparser / StyleChooser.as
1 package net.systemeD.halcyon.styleparser {
2
3         import net.systemeD.halcyon.connection.Entity;
4         import net.systemeD.halcyon.Globals;
5
6         public class StyleChooser {
7
8                 /*
9                         A StyleChooser object is equivalent to one CSS selector+declaration.
10
11                         Its ruleChains property is an array of all the selectors, which would
12                         traditionally be comma-separated. For example:
13                                 h1, h2, h3 em
14                         is three ruleChains.
15                         
16                         Each ruleChain is itself an array of nested selectors. So the above 
17                         example would roughly be encoded as:
18                                 [[h1],[h2],[h3,em]]
19                                   ^^   ^^   ^^ ^^   each of these is a Rule
20
21                         The styles property is an array of all the style objects to be drawn
22                         if any of the ruleChains evaluate to true.
23
24                 */
25
26                 public var ruleChains:Array=[[]];               // array of array of Rules
27                 public var styles:Array=[];                             // array of ShapeStyle/ShieldStyle/TextStyle/PointStyle
28
29                 private var rcpos:uint=0;
30                 private var stylepos:uint=0;
31
32                 // Update the current StyleList from this StyleChooser
33
34                 public function updateStyles(obj:Entity, tags:Object, sl:StyleList, imageWidths:Object, zoom:uint):void {
35                         // Are any of the ruleChains fulfilled?
36                         // ** needs to cope with min/max zoom
37                         var w:Number;
38                         var fulfilled:Boolean=false;
39                         for each (var c:Array in ruleChains) {
40                                 if (testChain(c,-1,obj,tags,zoom)) {
41                                         fulfilled=true; break;
42                                 }
43                         }
44                         if (!fulfilled) { return; }
45
46                         // Update StyleList
47                         for each (var r:Style in styles) {
48                                 var a:*;
49                                 if (r is ShapeStyle) {
50                                         a=sl.shapeStyles;
51                                         if (ShapeStyle(r).width>sl.maxwidth && !r.evals['width']) { sl.maxwidth=ShapeStyle(r).width; }
52                                 } else if (r is ShieldStyle) {
53                                         a=sl.shieldStyles;
54                                 } else if (r is TextStyle) { 
55                                         a=sl.textStyles;
56                                 } else if (r is PointStyle) { 
57                                         a=sl.pointStyles;
58                                         w=0;
59                                         if (PointStyle(r).icon_width && !PointStyle(r).evals['icon_width']) {
60                                                 w=PointStyle(r).icon_width;
61                                         } else if (PointStyle(r).icon_image && imageWidths[PointStyle(r).icon_image]) {
62                                                 w=imageWidths[PointStyle(r).icon_image];
63                                         }
64                                         if (w>sl.maxwidth) { sl.maxwidth=w; }
65                                 } else if (r is InstructionStyle) {
66                                         if (InstructionStyle(r).breaker) { return; }
67                                         if (InstructionStyle(r).set_tags) {
68                                                 for (var k:String in InstructionStyle(r).set_tags) { tags[k]=InstructionStyle(r).set_tags[k]; }
69                                         }
70                                         continue;
71                                 }
72                                 if (r.drawn) { tags[':drawn']='yes'; }
73                                 tags['_width']=sl.maxwidth;
74                                 
75                                 r.runEvals(tags);
76                                 sl.addSublayer(r.sublayer);
77                                 if (a[r.sublayer]) {
78                                         // If there's already a style on this sublayer, then merge them
79                                         // (making a deep copy if necessary to avoid altering the root style)
80                                         if (!a[r.sublayer].merged) { a[r.sublayer]=a[r.sublayer].deepCopy(); }
81                                         a[r.sublayer].mergeWith(r);
82                                 } else {
83                                         // Otherwise, just assign it
84                                         a[r.sublayer]=r;
85                                 }
86                         }
87                 }
88
89
90                 // Test a ruleChain
91                 // - run a set of tests in the chain
92                 //              works backwards from at position "pos" in array, or -1  for the last
93                 //              separate tags object is required in case they've been dynamically retagged
94                 // - if they fail, return false
95                 // - if they succeed, and it's the last in the chain, return happily
96                 // - if they succeed, and there's more in the chain, rerun this for each parent until success
97                 
98                 private function testChain(chain:Array,pos:int,obj:Entity,tags:Object,zoom:uint):Boolean {
99                         if (pos==-1) { pos=chain.length-1; }
100
101                         var r:Rule=chain[pos];
102                         if (!r.test(obj, tags, zoom)) { return false; }
103                         if (pos==0) { return true; }
104                         
105                         var o:Array=obj.parentObjects;
106                         for each (var p:Entity in o) {
107                                 if (testChain(chain, pos-1, p, p.getTagsHash(), zoom )) { return true; }
108                         }
109                         return false;
110                 }
111                 
112                 
113                 // ---------------------------------------------------------------------------------------------
114                 // Methods to add properties (used by parsers such as MapCSS)
115                 
116                 // newGroup             <- starts a new ruleChain in this.ruleChains
117                 public function newGroup():void {
118                         if (ruleChains[rcpos].length>0) {
119                                 ruleChains[++rcpos]=[];
120                         }
121                 }
122
123                 // newObject    <- adds into the current ruleChain (starting a new Rule)
124                 public function newObject(e:String=''):void {
125                         ruleChains[rcpos].push(new Rule(e));
126                 }
127
128                 // addZoom              <- adds into the current ruleChain (existing Rule)
129                 public function addZoom(z1:uint,z2:uint):void {
130                         ruleChains[rcpos][ruleChains[rcpos].length-1].minZoom=z1;
131                         ruleChains[rcpos][ruleChains[rcpos].length-1].maxZoom=z2;
132                 }
133                 
134                 // addCondition <- adds into the current ruleChain (existing Rule)
135                 public function addCondition(c:Condition):void {
136                         ruleChains[rcpos][ruleChains[rcpos].length-1].conditions.push(c);
137                 }
138
139                 // addStyles    <- adds to this.styles
140                 public function addStyles(a:Array):void {
141                         styles=styles.concat(a);
142                 }
143                 
144         }
145 }