1 package net.systemeD.halcyon.styleparser {
3 import flash.utils.ByteArray;
6 /** A Style is a set of graphic properties (e.g. stroke colour and width, casing colour and width,
7 font and text size), typically derived from a MapCSS descriptor. This is the base class
8 for particular style groupings such as ShapeStyle and PointStyle.
10 @see net.systemeD.halcyon.styleparser.StyleList
11 @see net.systemeD.halcyon.styleparser.StyleChooser
16 /** Has this style had another style merged into it?
17 (When styles cascade, then we need to merge the first style with any subsequent styles that apply.) */
18 public var merged:Boolean=false;
20 /** Is the style active (properties have been set)? */
21 public var edited:Boolean=false;
23 /** The sublayer is the z-index property _within_ an OSM layer.
24 It enables (for example) trunk roads to be rendered above primary roads within that OSM layer,
25 and so on. "OSM layer 1 / sublayer 5" will render above "OSM layer 1 / sublayer 4", but below
26 "OSM layer 2 / sublayer 4". */
27 public var sublayer:Number=5;
29 /** Does this style permit mouse interaction?
30 (Some styling, such as P2's back-level yellow highlight for selected ways, should not respond
32 public var interactive:Boolean=true;
34 /** Compiled SWFs for each eval. We keep it here, not in the property itself, so that we can retain typing for each property. */
35 public var evals:Object={};
37 /** Make an exact copy of an object.
38 Used when merging cascading styles. (FIXME: this needs some benchmarking - it may be quicker to simply iterate over .properties,
40 public function deepCopy():* {
41 registerClassAlias("net.systemeD.halcyon.styleparser.ShapeStyle",ShapeStyle);
42 registerClassAlias("net.systemeD.halcyon.styleparser.TextStyle",TextStyle);
43 registerClassAlias("net.systemeD.halcyon.styleparser.PointStyle",PointStyle);
44 registerClassAlias("net.systemeD.halcyon.styleparser.ShieldStyle",ShieldStyle);
45 registerClassAlias("net.systemeD.halcyon.styleparser.InstructionStyle",InstructionStyle);
46 var a:ByteArray=new ByteArray();
49 return (a.readObject());
52 /** Merge two Style objects. */
53 public function mergeWith(additional:Style):void {
54 for each (var prop:String in properties) {
55 if (additional[prop]) {
56 this[prop]=additional[prop];
62 /** Properties getter, to be overridden. */
63 public function get properties():Array {
67 /** Does this style require anything to be drawn? (To be overridden.) */
68 public function get drawn():Boolean {
72 /** Are there any eval functions defined? */
73 public function hasEvals():Boolean {
74 for (var k:String in evals) { return true; }
78 /** Run all evals for this Style over the supplied tags.
79 If, for example, the stylesheet contains width=eval('_width+2'), then this will set Style.width to 7. */
80 public function runEvals(tags:Object):void {
81 for (var k:String in evals) {
82 // ** Do we need to do typing here?
83 this[k]=evals[k].exec(tags);
87 /** Set a property, casting as correct type. */
88 public function setPropertyFromString(k:String,v:*):Boolean {
89 if (!this.hasOwnProperty(k)) { return false; }
90 if (v is Eval) { evals[k]=v; v=1; }
92 // Arrays don't return a proper typeof, so check manually
93 // Note that undefined class variables always have typeof=object,
94 // so we need to declare them as empty arrays (cf ShapeStyle)
95 if (this[k] is Array) {
96 // Split comma-separated array and coerce as numbers
97 this[k]=v.split(',').map(function(el:Object,index:int,array:Array):Number { return Number(el); });
98 edited=true; return true;
101 // Check for other object types
102 switch (typeof(this[k])) {
103 case "number": this[k]=Number(v) ; edited=true; return true;
104 case "object": // **for now, just assume objects are undefined strings
105 // We'll probably need to fix this in future if we have more complex
107 case "string": this[k]=String(v) ; edited=true; return true;
108 case "boolean": this[k]=Boolean(v); edited=true; return true;
113 /** Summarise Style as String - for debugging. */
114 public function toString():String {
116 for each (var k:String in this.properties) {
117 if (this.hasOwnProperty(k)) { str+=k+"="+this[k]+"; "; }