PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/reflex/display/Display.as

http://github.com/reflex/reflex-framework
ActionScript | 176 lines | 101 code | 28 blank | 47 comment | 6 complexity | 8f1127d9b50c543b4da4804029c4c7d6 MD5 | raw file
Possible License(s): Unlicense
  1. package reflex.display
  2. {
  3. import flash.display.Sprite;
  4. import mx.events.StyleEvent;
  5. import reflex.binding.DataChange;
  6. import reflex.events.DataChangeEvent;
  7. import reflex.measurement.IMeasurable;
  8. import reflex.measurement.IMeasurablePercent;
  9. import reflex.measurement.IMeasurements;
  10. import reflex.measurement.Measurements;
  11. import reflex.styles.IStyleable;
  12. import reflex.styles.Style;
  13. /**
  14. * Provides explicit, implicit, and percent based measurement properties as well as a light-weight styling system.
  15. * Although most display classes in Reflex extend Display, it's not referenced directly by the framework itself.
  16. * In other words, you are not required to extend this class to use Reflex!
  17. *
  18. * @alpha
  19. */
  20. public class Display extends Sprite implements IStyleable, IMeasurable, IMeasurablePercent
  21. {
  22. private var _id:String;
  23. private var _styleName:String;
  24. private var _style:Style;
  25. private var _explicit:IMeasurements;
  26. private var _measured:IMeasurements;
  27. private var _percentWidth:Number;
  28. private var _percentHeight:Number;
  29. protected var unscaledWidth:Number = 160;
  30. protected var unscaledHeight:Number = 22;
  31. public function Display() {
  32. _style = new Style(); // need to make object props bindable - something like ObjectProxy but lighter?
  33. _explicit = new Measurements(this, NaN, NaN);
  34. _measured = new Measurements(this, 160, 22);
  35. }
  36. // IStyleable implementation
  37. [Bindable(event="idChange", noEvent)]
  38. public function get id():String { return _id; }
  39. public function set id(value:String):void {
  40. DataChange.change(this, "id", _id, _id = value);
  41. }
  42. [Bindable(event="styleNameChange", noEvent)]
  43. public function get styleName():String { return _styleName;}
  44. public function set styleName(value:String):void {
  45. DataChange.change(this, "styleName", _styleName, _styleName= value);
  46. }
  47. [Bindable(event="styleChange", noEvent)]
  48. public function get style():Style { return _style; }
  49. public function set style(value:*):void { // this needs expanding in the future
  50. if (value is String) {
  51. var token:String = value as String;
  52. reflex.styles.parseStyles(_style, token);
  53. } else {
  54. throw new Error("BitmapDisplay.set style() does not currently accept a parameter of type: " + value);
  55. }
  56. }
  57. public function getStyle(property:String):* {
  58. return style[property];
  59. }
  60. public function setStyle(property:String, value:*):void {
  61. //style[property] = value;
  62. //DataChange.change(this, property, style[property], style[property] = value);
  63. // hasEventListener is stopping dispatch for styles ???
  64. var eventType:String = property + "Change";
  65. var event:DataChangeEvent = new DataChangeEvent(eventType, style[property], style[property] = value);
  66. this.dispatchEvent(event);
  67. }
  68. [Bindable(event="xChange", noEvent)]
  69. override public function get x():Number { return super.x; }
  70. override public function set x(value:Number):void {
  71. DataChange.change(this, "x", super.x, super.x = value);
  72. }
  73. [Bindable(event="yChange", noEvent)]
  74. override public function get y():Number { return super.y; }
  75. override public function set y(value:Number):void {
  76. DataChange.change(this, "y", super.y, super.y = value);
  77. }
  78. // IMeasurable implementation
  79. // these width/height setters need review in regards to scaling.
  80. // I think I would perfer following Flex's lead here.
  81. /**
  82. * @inheritDoc
  83. */
  84. [PercentProxy("percentWidth")]
  85. [Bindable(event="widthChange", noEvent)]
  86. override public function get width():Number { return unscaledWidth; }
  87. override public function set width(value:Number):void {
  88. unscaledWidth = _explicit.width = value; // this will dispatch for us if needed
  89. }
  90. /**
  91. * @inheritDoc
  92. */
  93. [PercentProxy("percentHeight")]
  94. [Bindable(event="heightChange", noEvent)]
  95. override public function get height():Number { return unscaledHeight; }
  96. override public function set height(value:Number):void {
  97. unscaledHeight = _explicit.height = value; // this will dispatch for us if needed (order is important)
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. //[Bindable(event="explicitChange", noEvent)]
  103. public function get explicit():IMeasurements { return _explicit; }
  104. /*public function set explicit(value:IMeasurements):void {
  105. if (value != null) { // must not be null
  106. DataChange.change(this, "explicit", _explicit, _explicit = value);
  107. }
  108. }*/
  109. /**
  110. * @inheritDoc
  111. */
  112. //[Bindable(event="measuredChange", noEvent)]
  113. public function get measured():IMeasurements { return _measured; }
  114. /*public function set measured(value:IMeasurements):void {
  115. if (value != null) { // must not be null
  116. DataChange.change(this, "measured", _measured, _measured = value);
  117. }
  118. }*/
  119. /**
  120. * @inheritDoc
  121. */
  122. [Bindable(event="percentWidthChange", noEvent)]
  123. public function get percentWidth():Number { return _percentWidth; }
  124. public function set percentWidth(value:Number):void {
  125. DataChange.change(this, "percentWidth", _percentWidth, _percentWidth = value);
  126. }
  127. /**
  128. * @inheritDoc
  129. */
  130. [Bindable(event="percentHeightChange", noEvent, noEvent)]
  131. public function get percentHeight():Number { return _percentHeight; }
  132. public function set percentHeight(value:Number):void {
  133. DataChange.change(this, "percentHeight", _percentHeight, _percentHeight = value);
  134. }
  135. [Bindable(event="visibleChange")]
  136. override public function get visible():Boolean { return super.visible; }
  137. override public function set visible(value:Boolean):void {
  138. DataChange.change(this, "visible", super.visible, super.visible = value);
  139. }
  140. /**
  141. * @inheritDoc
  142. */
  143. public function setSize(width:Number, height:Number):void {
  144. if (unscaledWidth != width) { DataChange.change(this, "width", unscaledWidth, unscaledWidth = width); }
  145. if (unscaledHeight != height) { DataChange.change(this, "height", unscaledHeight, unscaledHeight = height); }
  146. }
  147. }
  148. }