better still...
[potlatch2.git] / net / systemeD / halcyon / styleparser / Style.as
1 package net.systemeD.halcyon.styleparser {
2
3         import flash.utils.ByteArray;
4         import flash.net.*;
5
6         public class Style {
7
8                 public var merged:Boolean=false;
9                 public var edited:Boolean=false;                // true once a property has been set from a string
10                 public var sublayer:Number=5;
11                 public var interactive:Boolean=true;    
12                 public var evals:Object={};                             // compiled SWFs for each eval. We keep it here, not in the property 
13                                                                                                 //  | itself, so that we can retain typing for each property
14
15                 // Return an exact copy of this object
16                 // ** this needs some benchmarking - may be quicker to iterate over .properties, copying each one
17
18                 public function deepCopy():* {
19                         registerClassAlias("net.systemeD.halcyon.styleparser.ShapeStyle",ShapeStyle);
20                         registerClassAlias("net.systemeD.halcyon.styleparser.TextStyle",TextStyle);
21                         registerClassAlias("net.systemeD.halcyon.styleparser.PointStyle",PointStyle);
22                         registerClassAlias("net.systemeD.halcyon.styleparser.ShieldStyle",ShieldStyle);
23                         registerClassAlias("net.systemeD.halcyon.styleparser.InstructionStyle",InstructionStyle);
24                         var a:ByteArray=new ByteArray();
25                         a.writeObject(this);
26                         a.position=0;
27                         return (a.readObject());
28                 }
29
30                 // Add properties from another object
31
32                 public function mergeWith(additional:Style):void {
33                         for each (var prop:String in properties) {
34                                 if (additional[prop]) {
35                                         this[prop]=additional[prop];
36                                 }
37                         }
38                         this.merged=true;
39                 }
40
41                 // Getters (to be overridden)
42
43                 public function get properties():Array {
44                         return [];
45                 }
46                 
47                 public function get drawn():Boolean {
48                         return false;
49                 }
50                 
51                 // Eval handling
52                 
53                 public function hasEvals():Boolean {
54                         for (var k:String in evals) { return true; }
55                         return false;
56                 }
57                 
58                 public function runEvals(tags:Object):void {
59                         for (var k:String in evals) {
60                                 // ** Do we need to do typing here?
61                                 this[k]=evals[k].exec(tags);
62                                 
63                                 // ** If the stylesheet has width=eval('_width+2'), then this will set Style.width to 7 (say).
64                                 //    
65                         }
66                 }
67
68                 // Set property and cast as correct type (used in stylesheet imports)
69                 
70                 public function setPropertyFromString(k:String,v:*):Boolean {
71                         if (!this.hasOwnProperty(k)) { return false; }
72                         if (v is Eval) { evals[k]=v; v=1; }
73
74                         // Arrays don't return a proper typeof, so check manually
75                         // Note that undefined class variables always have typeof=object,
76                         // so we need to declare them as empty arrays (cf ShapeStyle)
77                         if (this[k] is Array) {
78                                 // Split comma-separated array and coerce as numbers
79                                 this[k]=v.split(',').map(function(el:Object,index:int,array:Array):Number { return Number(el); });
80                                 edited=true; return true;
81                         }
82
83                         // Check for other object types
84                         switch (typeof(this[k])) {
85                                 case "number":  this[k]=Number(v) ; edited=true; return true;
86                                 case "object":  // **for now, just assume objects are undefined strings
87                                                                 // We'll probably need to fix this in future if we have more complex
88                                                                 // properties
89                                 case "string":  this[k]=String(v) ; edited=true; return true;
90                                 case "boolean": this[k]=Boolean(v); edited=true; return true;
91                         }
92                         return false;
93                 }
94
95                 public function toString():String {
96                         var str:String='';
97             for each (var k:String in this.properties) {
98                                 if (this.hasOwnProperty(k)) { str+=k+"="+this[k]+"; "; }
99                         }
100                         return str;
101         }
102         }
103 }