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