Fix =no MapCSS check. This must be the longest-standing bug in P2...
[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                                  ^^^^ ^^^^ ^^^^^^^  each of these is a RuleChain
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 RuleChains (each one an array of Rules)
27                 public var styles:Array=[];                                     // array of ShapeStyle/ShieldStyle/TextStyle/PointStyle
28                 public var zoomSpecific:Boolean=false;          // are any of the rules zoom-specific?
29
30                 private var rcpos:uint;
31                 private var stylepos:uint=0;
32
33                 public function StyleChooser():void {
34                         ruleChains=[new RuleChain()];
35                         rcpos=0;
36                 }
37
38                 public function get currentChain():RuleChain {
39                         return ruleChains[rcpos];
40                 }
41                 
42                 // Update the current StyleList from this StyleChooser
43
44                 public function updateStyles(obj:Entity, tags:Object, sl:StyleList, imageWidths:Object, zoom:uint):void {
45                         if (zoomSpecific) { sl.validAt=zoom; }
46
47                         // Are any of the ruleChains fulfilled?
48                         var w:Number;
49                         for each (var c:RuleChain in ruleChains) {
50                                 if (c.test(-1,obj,tags,zoom)) {
51                                         sl.addSubpart(c.subpart);
52
53                                         // Update StyleList
54                                         for each (var r:Style in styles) {
55                                                 var a:Object;
56                                                 if (r is ShapeStyle) {
57                                                         a=sl.shapeStyles;
58                                                         if (ShapeStyle(r).width>sl.maxwidth && !r.evals['width']) { sl.maxwidth=ShapeStyle(r).width; }
59                                                 } else if (r is ShieldStyle) {
60                                                         a=sl.shieldStyles;
61                                                 } else if (r is TextStyle) { 
62                                                         a=sl.textStyles;
63                                                 } else if (r is PointStyle) { 
64                                                         a=sl.pointStyles;
65                                                         w=0;
66                                                         if (PointStyle(r).icon_width && !PointStyle(r).evals['icon_width']) {
67                                                                 w=PointStyle(r).icon_width;
68                                                         } else if (PointStyle(r).icon_image && imageWidths[PointStyle(r).icon_image]) {
69                                                                 w=imageWidths[PointStyle(r).icon_image];
70                                                         }
71                                                         if (w>sl.maxwidth) { sl.maxwidth=w; }
72                                                 } else if (r is InstructionStyle) {
73                                                         if (InstructionStyle(r).breaker) { return; }
74                                                         if (InstructionStyle(r).set_tags) {
75                                                                 for (var k:String in InstructionStyle(r).set_tags) { tags[k]=InstructionStyle(r).set_tags[k]; }
76                                                         }
77                                                         continue;
78                                                 }
79                                                 if (r.drawn) { tags[':drawn']='yes'; }
80                                                 tags['_width']=sl.maxwidth;
81                                 
82                                                 r.runEvals(tags);
83                                                 if (a[c.subpart]) {
84                                                         // If there's already a style on this sublayer, then merge them
85                                                         // (making a deep copy if necessary to avoid altering the root style)
86                                                         if (!a[c.subpart].merged) { a[c.subpart]=a[c.subpart].deepCopy(); }
87                                                         a[c.subpart].mergeWith(r);
88                                                 } else {
89                                                         // Otherwise, just assign it
90                                                         a[c.subpart]=r;
91                                                 }
92                                         }
93                                 }
94                         }
95                 }
96                 
97                 
98                 // ---------------------------------------------------------------------------------------------
99                 // Methods to add properties (used by parsers such as MapCSS)
100                 
101                 // newRuleChain         <- starts a new ruleChain in this.ruleChains
102                 public function newRuleChain():void {
103                         if (ruleChains[rcpos].length>0) {
104                                 ruleChains[++rcpos]=new RuleChain();
105                         }
106                 }
107
108                 public function addStyles(a:Array):void {
109                         styles=styles.concat(a);
110                 }
111
112         }
113 }