/src/sg/camolite/display/GBaseDisplay.as

https://github.com/stewymac07/SG-Camo-Collections · ActionScript · 167 lines · 82 code · 24 blank · 61 comment · 7 complexity · b15c2df81fac76de2e19005d64ab7415 MD5 · raw file

  1. package sg.camolite.display {
  2. import camo.core.display.AbstractDisplay;
  3. import flash.geom.Rectangle;
  4. import sg.camo.interfaces.IBehaviour;
  5. import sg.camo.interfaces.IBehaviouralBase;
  6. import sg.camo.interfaces.IResizable;
  7. // Non-wildcard
  8. //import flash.display.DisplayObject;
  9. //import flash.display.Sprite;
  10. // Wildcard import necessary in certain situations instead of the above (for Flash in particular)
  11. import flash.display.*;
  12. import sg.camo.interfaces.IReflectClass;
  13. /**
  14. * Base class extending from AbstractDisplay that supports a base to add and remove behaviours
  15. * on itself, including GBase implementation.
  16. *
  17. * @see sg.camo.interfaces.IBehaviour
  18. * @see sg.camolite.display.GBase
  19. *
  20. * @author Glenn Ko
  21. */
  22. public class GBaseDisplay extends AbstractDisplay implements IBehaviouralBase, IResizable, IReflectClass {
  23. protected var _behaviours:Object = { };
  24. /**
  25. * Constructor.
  26. * @param customDisp (Optional) A custom sprite you might want to inject to
  27. * to be set as the <code>display</code> sprite reference.
  28. *
  29. */
  30. public function GBaseDisplay(customDisp:Sprite = null) {
  31. display = customDisp != null ? $addChild(customDisp) as Sprite : display;
  32. super(this);
  33. }
  34. // -- IReflectClass
  35. public function get reflectClass():Class {
  36. return GBaseDisplay;
  37. }
  38. /**
  39. * <b>[Stage instance]</b> to hook up <code>display</code> sprite reference.
  40. */
  41. public function set displaySprite(spr:Sprite):void {
  42. display = spr;
  43. }
  44. /**
  45. * Sets mask display object of nested child display and adds mask to local displaylist
  46. */
  47. public function set displayMask(disp:DisplayObject):void {
  48. getDisplaySprite().mask = disp;
  49. $addChild(disp);
  50. }
  51. /**
  52. * Attempt to create a new <code>display</code> sprite instance if no instance of it is currrently found.
  53. * @return A new Sprite instance or the current <code>display</code> Sprite instance
  54. */
  55. protected function getDisplaySprite():Sprite {
  56. var chk:Sprite = display!=null ? display : $getChildByName("displaySprite") as Sprite;
  57. return chk != null ? chk : createNewDisplaySprite();
  58. }
  59. /**
  60. * Force the creation of a new <code>display</code> sprite reference.
  61. * @return A fresh new Sprite() instance, which is added to the local display list immediately.
  62. */
  63. protected function createNewDisplaySprite():Sprite {
  64. display = new Sprite();
  65. $addChild(display);
  66. return display;
  67. }
  68. // Public interface IBehaviouralBase
  69. public function addBehaviour(beh:IBehaviour):void {
  70. _behaviours[beh.behaviourName] = beh;
  71. beh.activate(this);
  72. }
  73. public function removeBehaviour(beh:IBehaviour):void {
  74. if (beh == null) return;
  75. beh.destroy();
  76. delete _behaviours[beh.behaviourName]
  77. }
  78. public function getBehaviour(behName:String):IBehaviour {
  79. return _behaviours[behName];
  80. }
  81. /**
  82. * Protected method to allow extended classes to immediately add behaviours to specific
  83. * display objects and activate those behaviours immediately. Also registers display behaviour
  84. * into it's own internal registry of behaviours (with a unique id based off the display object's name)
  85. * to facilitate garbage collection. <br/>
  86. * <i>NOTE: It is assumed every registered target display object instance has a unique
  87. * name to facilitate this process.<i/>
  88. * @param beh The behaviour to use and activate over the target.
  89. * @param targ (DisplayObject) A DisplayObject instance to use as the target.
  90. */
  91. protected function addDisplayBehaviourTo(beh:IBehaviour, targ:DisplayObject):void {
  92. _behaviours[beh.behaviourName + "_" + targ.name] = beh;
  93. beh.activate(targ);
  94. }
  95. /**
  96. * Protected method to allow extended classes to remove an assosiated behaviour of a specific
  97. * display object.<br/>
  98. * <i>NOTE: It is assumed the target has <b>not</b> changed it's name prior to when it was activated,
  99. * as this same name is used to retrieve back the assosiated behaviour for the particular display object target.
  100. * @param behaviourName The assosiated behaviour to remove and dispose off.
  101. * @param targ (DisplayObject) The targetted DisplayObject instance
  102. */
  103. protected function removeDisplayBehaviourOf(behaviourName:String, targ:DisplayObject):void { // we assume targ name doesn't change.
  104. var key:String = behaviourName + "_" + targ.name;
  105. var beh:IBehaviour = _behaviours[key];
  106. if (beh == null) {
  107. return;
  108. }
  109. beh.destroy();
  110. delete _behaviours[key];
  111. }
  112. public function set spacer(spr:Sprite):void {
  113. resize(spr.width, spr.height);
  114. spr.visible = false;
  115. }
  116. override public function get width():Number {
  117. return _width > 0 ? _width : display.width > _width ? display.width : _width;
  118. }
  119. override public function get height():Number {
  120. return _height > 0 ? _height : display.height > _height ? display.height : _height;
  121. }
  122. /**
  123. * Define specific width/height measurements through the resize method
  124. * @param w A valid width higher than 0 to take effect.
  125. * @param h A valid height higher than 0 to take effect.
  126. */
  127. override public function resize(w:Number, h:Number):void {
  128. _width = w > 0 ? w : _width;
  129. _height = h > 0 ? h : _height;
  130. }
  131. /**
  132. * Clears all behaviours as well.
  133. */
  134. override public function destroy():void {
  135. var hash:Object = _behaviours;
  136. for (var i:String in hash) {
  137. (hash[i] as IBehaviour).destroy();
  138. }
  139. _behaviours = null;
  140. super.destroy();
  141. }
  142. }
  143. }