[7849] | 1 | package {
|
---|
| 2 |
|
---|
| 3 | import flash.utils.Dictionary;
|
---|
| 4 | import string.Utils;
|
---|
| 5 |
|
---|
| 6 | public class Properties extends Object
|
---|
| 7 | {
|
---|
| 8 | private var _props:Dictionary;
|
---|
| 9 | private var _parent:Properties;
|
---|
| 10 |
|
---|
| 11 | public function Properties( json:Object, parent:Properties=null ) {
|
---|
| 12 |
|
---|
| 13 | // Dictionary can use an object as a key
|
---|
| 14 | this._props = new Dictionary();
|
---|
| 15 | this._parent = parent;
|
---|
| 16 |
|
---|
| 17 | // tr.ace(json);
|
---|
| 18 |
|
---|
| 19 | for (var prop:String in json ) {
|
---|
| 20 |
|
---|
| 21 | // tr.ace( prop +' = ' + json[prop]);
|
---|
| 22 | this._props[prop] = json[prop];
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public function get(name:String):* {
|
---|
| 27 |
|
---|
| 28 | // is this key in the dictionary?
|
---|
| 29 | if ( name in this._props )
|
---|
| 30 | return this._props[name];
|
---|
| 31 |
|
---|
| 32 | // test the parent
|
---|
| 33 | if ( this._parent != null )
|
---|
| 34 | return this._parent.get( name );
|
---|
| 35 |
|
---|
| 36 | //
|
---|
| 37 | // key/property not found, report and dump the stack trace
|
---|
| 38 | //
|
---|
| 39 | var e:Error = new Error();
|
---|
| 40 | var str:String = e.getStackTrace();
|
---|
| 41 |
|
---|
| 42 | tr.aces( 'ERROR: property not found', name, str);
|
---|
| 43 | return Number.NEGATIVE_INFINITY;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | //
|
---|
| 47 | // this is a bit dirty, I wish I could do something like:
|
---|
| 48 | // props.get('colour').as_colour()
|
---|
| 49 | //
|
---|
| 50 | public function get_colour(name:String):Number {
|
---|
| 51 | return Utils.get_colour(this.get(name));
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | // set does not recurse down, we don't want to set
|
---|
| 55 | // our parents property
|
---|
| 56 | public function set(name:String, value:Object):void {
|
---|
| 57 | this._props[name] = value;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public function has(name:String):Boolean {
|
---|
| 61 | if ( this._props[name] == null ) {
|
---|
| 62 | if ( this._parent != null )
|
---|
| 63 | return this._parent.has(name);
|
---|
| 64 | else
|
---|
| 65 | return false;
|
---|
| 66 | }
|
---|
| 67 | else
|
---|
| 68 | return true;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public function set_parent(p:Properties):void {
|
---|
| 72 | if ( this._parent != null )
|
---|
| 73 | p.set_parent( this._parent );
|
---|
| 74 |
|
---|
| 75 | this._parent = p;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | //
|
---|
| 79 | // recurse and kill everything
|
---|
| 80 | //
|
---|
| 81 | public function die(): void {
|
---|
| 82 | if ( this._parent )
|
---|
| 83 | this._parent.die();
|
---|
| 84 |
|
---|
| 85 | for (var key:Object in this._props) {
|
---|
| 86 | // iterates through each object key
|
---|
| 87 | this._props[key] = null;
|
---|
| 88 | }
|
---|
| 89 | this._parent = null;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|