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