Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial import of Away3D Broomstick integration classes

  • Loading branch information...
commit 6e3f3eaa19a73e67fba1bd67f367995b90fc1bfe 0 parents
@PaulTondeur authored
57 src/com/paultondeur/away3d/robotlegs/base/Mediator3D.as
@@ -0,0 +1,57 @@
+package com.paultondeur.away3d.robotlegs.base {
+ import com.paultondeur.away3d.robotlegs.core.IMediator3DMap;
+
+ import org.robotlegs.base.EventMap;
+ import org.robotlegs.core.IEventMap;
+
+ import flash.events.Event;
+ import flash.events.IEventDispatcher;
+
+ /**
+ * @author Paul Tondeur
+ */
+ public class Mediator3D extends Mediator3DBase {
+ [Inject]
+ public var mediatorMap : IMediator3DMap;
+ protected var _eventDispatcher : IEventDispatcher;
+ protected var _eventMap : IEventMap;
+
+ public function Mediator3D() {
+ }
+
+ override public function preRemove() : void {
+ if (_eventMap)
+ _eventMap.unmapListeners();
+ super.preRemove();
+ }
+
+ public function get eventDispatcher() : IEventDispatcher {
+ return _eventDispatcher;
+ }
+
+ [Inject]
+ public function set eventDispatcher(value : IEventDispatcher) : void {
+ _eventDispatcher = value;
+ }
+
+ /**
+ * Local EventMap
+ *
+ * @return The EventMap for this Actor
+ */
+ protected function get eventMap() : IEventMap {
+ return _eventMap || (_eventMap = new EventMap(eventDispatcher));
+ }
+
+ /**
+ * Dispatch helper method
+ *
+ * @param event The Event to dispatch on the <code>IContext</code>'s <code>IEventDispatcher</code>
+ */
+ protected function dispatch(event : Event) : Boolean {
+ if (eventDispatcher.hasEventListener(event.type))
+ return eventDispatcher.dispatchEvent(event);
+ return false;
+ }
+ }
+}
50 src/com/paultondeur/away3d/robotlegs/base/Mediator3DBase.as
@@ -0,0 +1,50 @@
+package com.paultondeur.away3d.robotlegs.base {
+ import away3d.containers.ObjectContainer3D;
+
+ import com.paultondeur.away3d.robotlegs.core.IMediator3D;
+
+ /**
+ * @author Paul Tondeur
+ */
+ public class Mediator3DBase implements IMediator3D {
+ /**
+ * Internal
+ *
+ * <p>In the case of deffered instantiation, onRemove might get called before
+ * onCreationComplete has fired. This here Bool helps us track that scenario.</p>
+ */
+ protected var removed : Boolean;
+ protected var _object3D : ObjectContainer3D;
+
+ public function Mediator3DBase() {
+ super();
+ }
+
+ // ---------------------------------------------------------------------
+ // API
+ // ---------------------------------------------------------------------
+ public function preRegister() : void {
+ removed = false;
+ onRegister();
+ }
+
+ public function onRegister() : void {
+ }
+
+ public function preRemove() : void {
+ removed = true;
+ onRemove();
+ }
+
+ public function onRemove() : void {
+ }
+
+ public function get object3D() : ObjectContainer3D {
+ return _object3D;
+ }
+
+ public function set object3D(object3D : ObjectContainer3D) : void {
+ _object3D = object3D;
+ }
+ }
+}
214 src/com/paultondeur/away3d/robotlegs/base/Mediator3DMap.as
@@ -0,0 +1,214 @@
+package com.paultondeur.away3d.robotlegs.base {
+ import away3d.containers.ObjectContainer3D;
+ import away3d.containers.View3D;
+ import away3d.events.Scene3DEvent;
+
+ import com.paultondeur.away3d.robotlegs.core.IMediator3D;
+ import com.paultondeur.away3d.robotlegs.core.IMediator3DMap;
+
+ import org.robotlegs.base.ContextError;
+ import org.robotlegs.core.IInjector;
+ import org.robotlegs.core.IReflector;
+
+ import flash.utils.Dictionary;
+ import flash.utils.getQualifiedClassName;
+
+ /**
+ * @author Paul Tondeur
+ */
+ public class Mediator3DMap implements IMediator3DMap {
+ protected var _view3D : View3D;
+ protected var _injector : IInjector;
+ protected var _reflector : IReflector;
+ protected var _mediatorsByObject3D : Dictionary;
+ protected var _mappingConfigByObject3D : Dictionary;
+ protected var _mappingConfigByObject3DClassName : Dictionary;
+
+ public function get view3D() : View3D {
+ return _view3D;
+ }
+
+ public function set view3D(value : View3D) : void {
+ if (value != _view3D) {
+ removeSceneListeners();
+ _view3D = value;
+ addSceneListeners();
+ }
+ }
+
+ public function Mediator3DMap(view3D : View3D, injector : IInjector, reflector : IReflector) {
+ _injector = injector;
+ _reflector = reflector;
+ this.view3D = view3D;
+
+ _mediatorsByObject3D = new Dictionary(true);
+ _mappingConfigByObject3D = new Dictionary(true);
+ _mappingConfigByObject3DClassName = new Dictionary(false);
+ }
+
+ public function mapObject3D(object3DClassOrName : *, mediatorClassOrArray : *, injectObject3DAs : * = null, autoCreate : Boolean = true, autoRemove : Boolean = true) : void {
+ var object3DClassName : String = _reflector.getFQCN(object3DClassOrName);
+
+ var config : MappingConfig = new MappingConfig();
+ if (mediatorClassOrArray is Array) {
+ for each (var mediator:Class in mediatorClassOrArray) {
+ checkMediatorClass(mediator, object3DClassName);
+ }
+
+ config.mediatorClasses = mediatorClassOrArray;
+ } else {
+ checkMediatorClass(mediatorClassOrArray, object3DClassName);
+ config.mediatorClasses = [mediatorClassOrArray];
+ }
+
+ config.autoCreate = autoCreate;
+ config.autoRemove = autoRemove;
+ if (injectObject3DAs) {
+ if (injectObject3DAs is Array) {
+ config.typedObject3DClasses = (injectObject3DAs as Array).concat();
+ } else if (injectObject3DAs is Class) {
+ config.typedObject3DClasses = [injectObject3DAs];
+ }
+ } else if (object3DClassOrName is Class) {
+ config.typedObject3DClasses = [object3DClassOrName];
+ }
+
+ _mappingConfigByObject3DClassName[object3DClassName] = config;
+ }
+
+ private function checkMediatorClass(mediator : Class, object3DClassName : String) : void {
+ if (_mappingConfigByObject3DClassName[object3DClassName] != null)
+ throw new ContextError(ContextError.E_MEDIATORMAP_OVR + ' - ' + mediator);
+
+ if (_reflector.classExtendsOrImplements(mediator, IMediator3D) == false)
+ throw new ContextError(ContextError.E_MEDIATORMAP_NOIMPL + ' - ' + mediator);
+ }
+
+ public function unmapObject3D(object3DClassOrName : *) : void {
+ var object3DClassName : String = _reflector.getFQCN(object3DClassOrName);
+
+ delete _mappingConfigByObject3DClassName[object3DClassName];
+ }
+
+ public function createMediators(object3D : ObjectContainer3D) : Vector.<IMediator3D> {
+ return createMediatorsUsing(object3D);
+ }
+
+ public function registerMediators(object3D : ObjectContainer3D, mediators : Vector.<IMediator3D>) : void {
+ _mappingConfigByObject3D[object3D] = _mappingConfigByObject3DClassName[getQualifiedClassName(object3D)];
+ for each (var mediator:IMediator3D in mediators) {
+ _injector.mapValue(_reflector.getClass(mediator), mediator);
+ _mediatorsByObject3D[object3D] = mediator;
+ mediator.object3D = object3D;
+ mediator.preRegister();
+ }
+ }
+
+ public function removeMediator(mediator : IMediator3D) : IMediator3D {
+ if (mediator) {
+ var objectMediators : Vector.<IMediator3D> = _mediatorsByObject3D[mediator.object3D];
+ var mediatorIndex : int = objectMediators.indexOf(mediator);
+ if (mediatorIndex > -1) {
+ objectMediators.splice(mediatorIndex, 1);
+ }
+
+ if (objectMediators.length == 0) {
+ delete _mediatorsByObject3D[objectMediators];
+ delete _mappingConfigByObject3D[mediator.object3D];
+ }
+ mediator.preRemove();
+ mediator.object3D = null;
+ _injector.unmap(_reflector.getClass(mediator));
+ }
+ return mediator;
+ }
+
+ public function removeMediatorsByObject3D(object3D : ObjectContainer3D) : Vector.<IMediator3D> {
+ var mediators : Vector.<IMediator3D> = retrieveMediatorList(object3D);
+ for each (var mediator:IMediator3D in mediators) {
+ removeMediator(mediator);
+ }
+ return mediators;
+ }
+
+ public function retrieveMediatorList(object3D : ObjectContainer3D) : Vector.<IMediator3D> {
+ return _mediatorsByObject3D[object3D];
+ }
+
+ public function hasMapping(object3DClassOrName : *) : Boolean {
+ var object3DClassName : String = _reflector.getFQCN(object3DClassOrName);
+ return (_mappingConfigByObject3DClassName[object3DClassName] != null);
+ }
+
+ public function hasMediator(mediator : IMediator3D) : Boolean {
+ for each (var objectMediators : Vector.<IMediator3D> in _mediatorsByObject3D) {
+ for each (var med:IMediator3D in objectMediators) {
+ if (med == mediator)
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public function hasMediatorsForObject3D(object3D : ObjectContainer3D) : Boolean {
+ return _mediatorsByObject3D[object3D] != null;
+ }
+
+ protected function removeSceneListeners() : void {
+ if (view3D) {
+ view3D.scene.removeEventListener(Scene3DEvent.ADDED_TO_SCENE, onViewAdded);
+ view3D.scene.removeEventListener(Scene3DEvent.REMOVED_FROM_SCENE, onViewRemoved);
+ }
+ }
+
+ protected function addSceneListeners() : void {
+ if (view3D) {
+ view3D.scene.addEventListener(Scene3DEvent.ADDED_TO_SCENE, onViewAdded);
+ view3D.scene.addEventListener(Scene3DEvent.REMOVED_FROM_SCENE, onViewRemoved);
+ }
+ }
+
+ protected function createMediatorsUsing(object3D : ObjectContainer3D, object3DClassName : String = '', config : MappingConfig = null) : Vector.<IMediator3D> {
+ var mediators : Vector.<IMediator3D> = _mediatorsByObject3D[object3D];
+ if (mediators == null) {
+ mediators = new Vector.<IMediator3D>();
+ object3DClassName ||= getQualifiedClassName(object3D);
+ config ||= _mappingConfigByObject3DClassName[object3DClassName];
+ if (config) {
+ for each (var claxx:Class in config.typedObject3DClasses) {
+ _injector.mapValue(claxx, object3D);
+ }
+ for each (var mediatorClass:Class in config.mediatorClasses) {
+ mediators.push(_injector.instantiate(mediatorClass));
+ }
+ for each (var clazz:Class in config.typedObject3DClasses) {
+ _injector.unmap(clazz);
+ }
+ registerMediators(object3D, mediators);
+ }
+ }
+ return mediators;
+ }
+
+ protected function onViewAdded(event : Scene3DEvent) : void {
+ var object3DClassName : String = getQualifiedClassName(event.objectContainer3D);
+ var config : MappingConfig = _mappingConfigByObject3DClassName[object3DClassName];
+ if (config && config.autoCreate)
+ createMediatorsUsing(event.objectContainer3D, object3DClassName, config);
+ }
+
+ protected function onViewRemoved(event : Scene3DEvent) : void {
+ var config : MappingConfig = _mappingConfigByObject3D[event.objectContainer3D];
+ if (config && config.autoRemove) {
+ removeMediatorsByObject3D(event.objectContainer3D);
+ }
+ }
+ }
+}
+class MappingConfig {
+ public var mediatorClasses : Array;
+ public var typedObject3DClasses : Array;
+ public var autoCreate : Boolean;
+ public var autoRemove : Boolean;
+}
22 src/com/paultondeur/away3d/robotlegs/base/Render3DEvent.as
@@ -0,0 +1,22 @@
+package com.paultondeur.away3d.robotlegs.base {
+ import flash.events.Event;
+
+ /**
+ * @author Paul Tondeur
+ */
+ public class Render3DEvent extends Event {
+ public static const PRE_RENDER : String = "preRender";
+ public static const POST_RENDER : String = "postRender";
+ public static const REQUEST_START_RENDERING : String = "requestStartRendering";
+ public static const REQUEST_STOP_RENDERING : String = "requestStopRendering";
+
+ public function Render3DEvent(type : String, bubbles : Boolean = false, cancelable : Boolean = false) {
+ super(type, bubbles, cancelable);
+ }
+
+ public override function clone() : Event
+ {
+ return new Render3DEvent(type, bubbles, cancelable);
+ }
+ }
+}
16 src/com/paultondeur/away3d/robotlegs/core/IContext3D.as
@@ -0,0 +1,16 @@
+package com.paultondeur.away3d.robotlegs.core {
+ import away3d.containers.View3D;
+
+ /**
+ * @author Paul Tondeur
+ */
+ public interface IContext3D {
+ function get view3D() : View3D;
+
+ function set view3D(value : View3D) : void
+
+ function get threeDeeMap() : IMediator3DMap;
+
+ function set threeDeeMap(value : IMediator3DMap) : void
+ }
+}
46 src/com/paultondeur/away3d/robotlegs/core/IMediator3D.as
@@ -0,0 +1,46 @@
+package com.paultondeur.away3d.robotlegs.core {
+ import away3d.containers.ObjectContainer3D;
+
+ /**
+ * @author Paul Tondeur
+ */
+ public interface IMediator3D {
+ /**
+ * Should be invoked by the <code>IMediatorMap</code> during <code>IMediator</code> registration
+ */
+ function preRegister() : void;
+
+ /**
+ * Should be invoked by the <code>IMediator</code> itself when it is ready to be interacted with
+ *
+ * <p>Override and place your initialization code here</p>
+ */
+ function onRegister() : void;
+
+ /**
+ * Invoked when the <code>IMediator</code> has been removed by the <code>IMediatorMap</code>
+ */
+ function preRemove() : void;
+
+ /**
+ * Should be invoked by the <code>IMediator</code> itself when it is ready to for cleanup
+ *
+ * <p>Override and place your cleanup code here</p>
+ */
+ function onRemove() : void;
+
+ /**
+ * The <code>IMediator</code>'s view component
+ *
+ * @return The view component
+ */
+ function get object3D() : ObjectContainer3D;
+
+ /**
+ * The <code>IMediator</code>'s view component
+ *
+ * @param The view component
+ */
+ function set object3D(object3D : ObjectContainer3D) : void;
+ }
+}
102 src/com/paultondeur/away3d/robotlegs/core/IMediator3DMap.as
@@ -0,0 +1,102 @@
+package com.paultondeur.away3d.robotlegs.core {
+ import away3d.containers.ObjectContainer3D;
+ import away3d.containers.View3D;
+
+ /**
+ * @author Paul Tondeur
+ */
+ public interface IMediator3DMap {
+ /**
+ * Map an <code>IThreeDeeMediator</code> to a object3D Class
+ *
+ * @param object3DClassOrName The concrete object3D Class or Fully Qualified Class Name
+ * @param mediatorClass The <code>IThreeDeeMediator</code> Class
+ * @param injectObject3DAs The explicit object3D Interface or Class that the mediator depends on OR an Array of such Interfaces/Classes.
+ * @param autoCreate Automatically construct and register an instance of Class <code>mediatorClass</code> when an instance of Class <code>object3DClass</code> is detected
+ * @param autoRemove Automatically remove an instance of Class <code>mediatorClass</code> when its <code>object3DClass</code> leaves the ancestory of the context object3D
+ */
+ function mapObject3D(object3DClassOrName : *, mediatorClassOrArray : *, injectObject3DAs : * = null, autoCreate : Boolean = true, autoRemove : Boolean = true) : void;
+
+ /**
+ * Unmap a object3D Class
+ *
+ * @param object3DClassOrName The concrete object3D Class or Fully Qualified Class Name
+ */
+ function unmapObject3D(object3DClassOrName : *) : void;
+
+ /**
+ * Create an instance of a mapped <code>IThreeDeeMediator</code>
+ *
+ * <p>This will instantiate and register a Mediator for a given Object3D Component.
+ * Mediator dependencies will be automatically resolved.</p>
+ *
+ * @param object3D An instance of the object3D Class previously mapped to an <code>IThreeDeeMediator</code> Class
+ * @return The <code>IThreeDeeMediator</code>
+ */
+ function createMediators(object3D : ObjectContainer3D) : Vector.<IMediator3D>;
+
+ /**
+ * Manually register an <code>IThreeDeeMediator</code> instance
+ *
+ * <p>NOTE: Registering a Mediator will NOT inject its dependencies.
+ * It is assumed that dependencies are already satisfied.</p>
+ *
+ * @param object3D The object3D component for the <code>IThreeDeeMediator</code>
+ * @param mediator The <code>IThreeDeeMediator</code> to register
+ */
+ function registerMediators(object3D : ObjectContainer3D, mediators : Vector.<IMediator3D>) : void;
+
+ /**
+ * Remove a registered <code>IThreeDeeMediator</code> instance
+ *
+ * @param mediator The <code>IThreeDeeMediator</code> to remove
+ * @return The <code>IThreeDeeMediator</code> that was removed
+ */
+ function removeMediator(mediator : IMediator3D) : IMediator3D;
+
+ /**
+ * Remove a registered <code>IThreeDeeMediator</code> instance
+ *
+ * @param object3DComponent The object3D that the <code>IThreeDeeMediator</code> was registered with
+ * @return The <code>IThreeDeeMediator</code> that was removed
+ */
+ function removeMediatorsByObject3D(object3D : ObjectContainer3D) : Vector.<IMediator3D>;
+
+ /**
+ * Retrieve a registered <code>IThreeDeeMediator</code> instance
+ *
+ * @param object3DComponent The object3D that the <code>IThreeDeeMediator</code> was registered with
+ * @return The <code>IThreeDeeMediator</code>
+ */
+ function retrieveMediatorList(object3D : ObjectContainer3D) : Vector.<IMediator3D>;
+
+ /**
+ * Check if the object3D Class has been mapped or not
+ *
+ * @param object3DClassOrName The concrete object3D Class or Fully Qualified Class Name
+ *
+ * @return Whether this object3D class has been mapped
+ */
+ function hasMapping(object3DClassOrName : *) : Boolean;
+
+ /**
+ * Check if the <code>IThreeDeeMediator</code> has been registered
+ *
+ * @param mediator The <code>IThreeDeeMediator</code> instance
+ * @return Whether this <code>IThreeDeeMediator</code> has been registered
+ */
+ function hasMediator(mediator : IMediator3D) : Boolean;
+
+ /**
+ * Check if an <code>IThreeDeeMediator</code> has been registered for a object3D instance
+ *
+ * @param object3DComponent The object3D that the <code>IThreeDeeMediator</code> was registered with
+ * @return Whether an <code>IThreeDeeMediator</code> has been registered for this object3D instance
+ */
+ function hasMediatorsForObject3D(object3D : ObjectContainer3D) : Boolean;
+
+ function get view3D() : View3D;
+
+ function set view3D(value : View3D) : void
+ }
+}
53 src/com/paultondeur/away3d/robotlegs/mvcs/Context3D.as
@@ -0,0 +1,53 @@
+package com.paultondeur.away3d.robotlegs.mvcs {
+ import com.paultondeur.away3d.robotlegs.mvcs.view.View3DMediator;
+ import away3d.containers.View3D;
+
+ import com.paultondeur.away3d.robotlegs.base.Mediator3DMap;
+ import com.paultondeur.away3d.robotlegs.core.IContext3D;
+ import com.paultondeur.away3d.robotlegs.core.IMediator3DMap;
+
+ import org.robotlegs.mvcs.Context;
+
+ import flash.display.DisplayObjectContainer;
+
+ /**
+ * @author Paul Tondeur
+ */
+ public class Context3D extends Context implements IContext3D {
+ private var _view3D : View3D;
+ private var _threeDeeMap : IMediator3DMap;
+
+ public function Context3D(contextView : DisplayObjectContainer = null, autoStartup : Boolean = true) {
+ super(contextView, autoStartup);
+ }
+
+ public function get view3D() : View3D {
+ return _view3D || (_view3D = new View3D());
+ }
+
+ public function set view3D(value : View3D) : void {
+ _view3D = value;
+ }
+
+ public function get threeDeeMap() : IMediator3DMap {
+ return _threeDeeMap ||= new Mediator3DMap(view3D, createChildInjector(), reflector);
+ }
+
+ public function set threeDeeMap(value : IMediator3DMap) : void {
+ _threeDeeMap = value;
+ }
+
+ override protected function mapInjections() : void {
+ super.mapInjections();
+ injector.mapValue(View3D, view3D);
+ injector.mapValue(IMediator3DMap, threeDeeMap);
+ }
+
+ override public function startup() : void {
+ mediatorMap.mapView(View3D, View3DMediator);
+ contextView.addChild(view3D);
+
+ super.startup();
+ }
+ }
+}
38 src/com/paultondeur/away3d/robotlegs/mvcs/view/View3DMediator.as
@@ -0,0 +1,38 @@
+package com.paultondeur.away3d.robotlegs.mvcs.view {
+ import away3d.containers.View3D;
+
+ import com.paultondeur.away3d.robotlegs.base.Render3DEvent;
+
+ import org.robotlegs.mvcs.Mediator;
+
+ import flash.events.Event;
+
+ /**
+ * @author Paul Tondeur
+ */
+ public class View3DMediator extends Mediator {
+ [Inject]
+ public var view3D : View3D;
+ protected var preRender : Render3DEvent = new Render3DEvent(Render3DEvent.PRE_RENDER);
+ protected var postRender : Render3DEvent = new Render3DEvent(Render3DEvent.POST_RENDER);
+
+ override public function onRegister() : void {
+ eventMap.mapListener(eventDispatcher, Render3DEvent.REQUEST_START_RENDERING, onStart);
+ eventMap.mapListener(eventDispatcher, Render3DEvent.REQUEST_STOP_RENDERING, onStop);
+ }
+
+ private function onStart(event : Render3DEvent) : void {
+ eventMap.mapListener(view3D, Event.ENTER_FRAME, onRender);
+ }
+
+ private function onStop(event : Render3DEvent) : void {
+ eventMap.mapListener(view3D, Event.ENTER_FRAME, onRender);
+ }
+
+ protected function onRender(event : Event) : void {
+ dispatch(preRender);
+ view3D.render();
+ dispatch(postRender);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.