Properties.as 2.0 KB

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