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