PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/core/source/temple/core/display/CoreShape.as

http://templelibrary.googlecode.com/
ActionScript | 161 lines | 96 code | 28 blank | 37 comment | 12 complexity | 25e8af72ce8998dfdd7a481f4728dfe6 MD5 | raw file
  1. /*
  2. include "../includes/License.as.inc";
  3. */
  4. package temple.core.display
  5. {
  6. import flash.display.Loader;
  7. import flash.display.Shape;
  8. import flash.display.Stage;
  9. import flash.events.Event;
  10. import flash.geom.Point;
  11. import temple.core.debug.Registry;
  12. import temple.core.debug.log.Log;
  13. import temple.core.debug.log.LogLevel;
  14. import temple.core.debug.objectToString;
  15. import temple.core.destruction.DestructEvent;
  16. import temple.core.events.EventListenerManager;
  17. import temple.core.templelibrary;
  18. /**
  19. * @eventType temple.core.destruction.DestructEvent.DESTRUCT
  20. */
  21. [Event(name = "DestructEvent.destruct", type = "temple.core.destruction.DestructEvent")]
  22. /**
  23. * Base class for all Shapes in the Temple. The CoreShape handles some core features of the Temple:
  24. * <ul>
  25. * <li>Registration to the Registry class.</li>
  26. * <li>Global reference to the stage trough the StageProvider.</li>
  27. * <li>Corrects a timeline bug in Flash (see <a href="http://www.tyz.nl/2009/06/23/weird-parent-thing-bug-in-flash/" target="_blank">http://www.tyz.nl/2009/06/23/weird-parent-thing-bug-in-flash/</a>).</li>
  28. * <li>Event dispatch optimization.</li>
  29. * <li>Easy remove of all EventListeners.</li>
  30. * <li>Wrapper for Log class for easy logging.</li>
  31. * <li>Completely destructible.</li>
  32. * <li>Tracked in Memory (of this feature is enabled).</li>
  33. * <li>Some useful extra properties like autoAlpha, position and scale.</li>
  34. * </ul>
  35. *
  36. * <p>You should always use and/or extend the CoreShape instead of Shape if you want to make use of the Temple features.</p>
  37. *
  38. * @see temple.core.Temple#registerObjectsInMemory
  39. *
  40. * @includeExample CoreDisplayObjectsExample.as
  41. *
  42. * @author Thijs Broerse
  43. */
  44. public class CoreShape extends Shape implements ICoreDisplayObject
  45. {
  46. include "../includes/Version.as.inc";
  47. include "../includes/ConstructNamespace.as.inc";
  48. private const _toStringProps:Vector.<String> = Vector.<String>(['name']);
  49. private var _eventListenerManager:EventListenerManager;
  50. private var _isDestructed:Boolean;
  51. private var _onStage:Boolean;
  52. private var _onParent:Boolean;
  53. private var _registryId:uint;
  54. private var _destructOnUnload:Boolean = true;
  55. private var _emptyPropsInToString:Boolean = true;
  56. public function CoreShape(name:String = null)
  57. {
  58. if (name) this.name = name;
  59. super();
  60. construct::coreShape(name);
  61. }
  62. /**
  63. * @private
  64. */
  65. construct function coreShape(name:String):void
  66. {
  67. if (this.loaderInfo) this.loaderInfo.addEventListener(Event.UNLOAD, this.handleUnload, false, 0, true);
  68. this._registryId = Registry.add(this);
  69. // Set listeners to keep track of object is on stage, since we can't trust the .parent property
  70. super.addEventListener(Event.ADDED, this.handleAdded);
  71. super.addEventListener(Event.ADDED_TO_STAGE, this.handleAddedToStage);
  72. super.addEventListener(Event.REMOVED, this.handleRemoved);
  73. super.addEventListener(Event.REMOVED_FROM_STAGE, this.handleRemovedFromStage);
  74. name;
  75. }
  76. include "../includes/CoreObjectMethods.as.inc";
  77. include "../includes/CoreDisplayObjectMethods.as.inc";
  78. include "../includes/CoreEventDispatcherMethods.as.inc";
  79. include "../includes/LogMethods.as.inc";
  80. include "../includes/CoreDisplayObjectHandlers.as.inc";
  81. include "../includes/ToStringPropsMethods.as.inc";
  82. include "../includes/ToStringMethods.as.inc";
  83. include "../includes/IsDestructed.as.inc";
  84. /**
  85. * @inheritDoc
  86. */
  87. public function destruct():void
  88. {
  89. if (this._isDestructed) return;
  90. this.dispatchEvent(new DestructEvent(DestructEvent.DESTRUCT));
  91. // clear mask, so it won't keep a reference to an other object
  92. this.mask = null;
  93. if (this.loaderInfo) this.loaderInfo.removeEventListener(Event.UNLOAD, this.handleUnload);
  94. this.removeEventListener(Event.ENTER_FRAME, this.handleDestructedFrameDelay);
  95. if (this._eventListenerManager)
  96. {
  97. this._eventListenerManager.destruct();
  98. this._eventListenerManager = null;
  99. }
  100. super.removeEventListener(Event.ENTER_FRAME, this.handleDestructedFrameDelay);
  101. super.removeEventListener(Event.ADDED, this.handleAdded);
  102. super.removeEventListener(Event.ADDED_TO_STAGE, this.handleAddedToStage);
  103. super.removeEventListener(Event.REMOVED, this.handleRemoved);
  104. super.removeEventListener(Event.REMOVED_FROM_STAGE, this.handleRemovedFromStage);
  105. if (this.parent)
  106. {
  107. if (this.parent is Loader)
  108. {
  109. Loader(this.parent).unload();
  110. }
  111. else
  112. {
  113. if (this._onParent)
  114. {
  115. if (this.name && this.parent.hasOwnProperty(this.name)) this.parent[this.name] = null;
  116. this.parent.removeChild(this);
  117. }
  118. else
  119. {
  120. // something weird happened, since we have a parent but didn't receive an ADDED event. So do the try-catch thing
  121. try
  122. {
  123. if (this.name && this.parent.hasOwnProperty(this.name)) this.parent[this.name] = null;
  124. this.parent.removeChild(this);
  125. }
  126. catch (e:Error){}
  127. }
  128. }
  129. }
  130. this._isDestructed = true;
  131. }
  132. }
  133. }