/modules/core/source/temple/core/display/CoreShape.as
ActionScript | 161 lines | 96 code | 28 blank | 37 comment | 12 complexity | 25e8af72ce8998dfdd7a481f4728dfe6 MD5 | raw file
- /*
- include "../includes/License.as.inc";
- */
-
- package temple.core.display
- {
- import flash.display.Loader;
- import flash.display.Shape;
- import flash.display.Stage;
- import flash.events.Event;
- import flash.geom.Point;
- import temple.core.debug.Registry;
- import temple.core.debug.log.Log;
- import temple.core.debug.log.LogLevel;
- import temple.core.debug.objectToString;
- import temple.core.destruction.DestructEvent;
- import temple.core.events.EventListenerManager;
- import temple.core.templelibrary;
-
-
- /**
- * @eventType temple.core.destruction.DestructEvent.DESTRUCT
- */
- [Event(name = "DestructEvent.destruct", type = "temple.core.destruction.DestructEvent")]
-
- /**
- * Base class for all Shapes in the Temple. The CoreShape handles some core features of the Temple:
- * <ul>
- * <li>Registration to the Registry class.</li>
- * <li>Global reference to the stage trough the StageProvider.</li>
- * <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>
- * <li>Event dispatch optimization.</li>
- * <li>Easy remove of all EventListeners.</li>
- * <li>Wrapper for Log class for easy logging.</li>
- * <li>Completely destructible.</li>
- * <li>Tracked in Memory (of this feature is enabled).</li>
- * <li>Some useful extra properties like autoAlpha, position and scale.</li>
- * </ul>
- *
- * <p>You should always use and/or extend the CoreShape instead of Shape if you want to make use of the Temple features.</p>
- *
- * @see temple.core.Temple#registerObjectsInMemory
- *
- * @includeExample CoreDisplayObjectsExample.as
- *
- * @author Thijs Broerse
- */
- public class CoreShape extends Shape implements ICoreDisplayObject
- {
- include "../includes/Version.as.inc";
-
- include "../includes/ConstructNamespace.as.inc";
-
- private const _toStringProps:Vector.<String> = Vector.<String>(['name']);
- private var _eventListenerManager:EventListenerManager;
- private var _isDestructed:Boolean;
- private var _onStage:Boolean;
- private var _onParent:Boolean;
- private var _registryId:uint;
- private var _destructOnUnload:Boolean = true;
- private var _emptyPropsInToString:Boolean = true;
-
- public function CoreShape(name:String = null)
- {
- if (name) this.name = name;
- super();
-
- construct::coreShape(name);
- }
-
- /**
- * @private
- */
- construct function coreShape(name:String):void
- {
- if (this.loaderInfo) this.loaderInfo.addEventListener(Event.UNLOAD, this.handleUnload, false, 0, true);
-
- this._registryId = Registry.add(this);
-
- // Set listeners to keep track of object is on stage, since we can't trust the .parent property
- super.addEventListener(Event.ADDED, this.handleAdded);
- super.addEventListener(Event.ADDED_TO_STAGE, this.handleAddedToStage);
- super.addEventListener(Event.REMOVED, this.handleRemoved);
- super.addEventListener(Event.REMOVED_FROM_STAGE, this.handleRemovedFromStage);
-
- name;
- }
-
- include "../includes/CoreObjectMethods.as.inc";
-
- include "../includes/CoreDisplayObjectMethods.as.inc";
-
- include "../includes/CoreEventDispatcherMethods.as.inc";
-
- include "../includes/LogMethods.as.inc";
-
- include "../includes/CoreDisplayObjectHandlers.as.inc";
-
- include "../includes/ToStringPropsMethods.as.inc";
-
- include "../includes/ToStringMethods.as.inc";
-
- include "../includes/IsDestructed.as.inc";
-
- /**
- * @inheritDoc
- */
- public function destruct():void
- {
- if (this._isDestructed) return;
-
- this.dispatchEvent(new DestructEvent(DestructEvent.DESTRUCT));
-
- // clear mask, so it won't keep a reference to an other object
- this.mask = null;
-
- if (this.loaderInfo) this.loaderInfo.removeEventListener(Event.UNLOAD, this.handleUnload);
-
- this.removeEventListener(Event.ENTER_FRAME, this.handleDestructedFrameDelay);
-
- if (this._eventListenerManager)
- {
- this._eventListenerManager.destruct();
- this._eventListenerManager = null;
- }
-
- super.removeEventListener(Event.ENTER_FRAME, this.handleDestructedFrameDelay);
- super.removeEventListener(Event.ADDED, this.handleAdded);
- super.removeEventListener(Event.ADDED_TO_STAGE, this.handleAddedToStage);
- super.removeEventListener(Event.REMOVED, this.handleRemoved);
- super.removeEventListener(Event.REMOVED_FROM_STAGE, this.handleRemovedFromStage);
-
- if (this.parent)
- {
- if (this.parent is Loader)
- {
- Loader(this.parent).unload();
- }
- else
- {
- if (this._onParent)
- {
- if (this.name && this.parent.hasOwnProperty(this.name)) this.parent[this.name] = null;
- this.parent.removeChild(this);
- }
- else
- {
- // something weird happened, since we have a parent but didn't receive an ADDED event. So do the try-catch thing
- try
- {
- if (this.name && this.parent.hasOwnProperty(this.name)) this.parent[this.name] = null;
- this.parent.removeChild(this);
- }
- catch (e:Error){}
- }
- }
- }
- this._isDestructed = true;
- }
- }
- }