PageRenderTime 404ms CodeModel.GetById 201ms app.highlight 6ms RepoModel.GetById 195ms app.codeStats 0ms

/src/away3d/core/managers/Stage3DManager.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 142 lines | 74 code | 18 blank | 50 comment | 5 complexity | 3e1772f47528d261bc01a40afd0ab418 MD5 | raw file
  1package away3d.core.managers
  2{
  3	import away3d.arcane;
  4	
  5	import flash.display.Stage;
  6	import flash.utils.Dictionary;
  7	
  8	use namespace arcane;
  9	
 10	/**
 11	 * The Stage3DManager class provides a multiton object that handles management for Stage3D objects. Stage3D objects
 12	 * should not be requested directly, but are exposed by a Stage3DProxy.
 13	 *
 14	 * @see away3d.core.managers.Stage3DProxy
 15	 */
 16	public class Stage3DManager
 17	{
 18		private static var _instances:Dictionary;
 19		private static var _stageProxies:Vector.<Stage3DProxy>;
 20		private static var _numStageProxies:uint = 0;
 21		
 22		private var _stage:Stage;
 23		
 24		/**
 25		 * Creates a new Stage3DManager class.
 26		 * @param stage The Stage object that contains the Stage3D objects to be managed.
 27		 * @private
 28		 */
 29		public function Stage3DManager(stage:Stage, Stage3DManagerSingletonEnforcer:Stage3DManagerSingletonEnforcer)
 30		{
 31			if (!Stage3DManagerSingletonEnforcer)
 32				throw new Error("This class is a multiton and cannot be instantiated manually. Use Stage3DManager.getInstance instead.");
 33			_stage = stage;
 34			
 35			if (!_stageProxies)
 36				_stageProxies = new Vector.<Stage3DProxy>(_stage.stage3Ds.length, true);
 37		}
 38		
 39		/**
 40		 * Gets a Stage3DManager instance for the given Stage object.
 41		 * @param stage The Stage object that contains the Stage3D objects to be managed.
 42		 * @return The Stage3DManager instance for the given Stage object.
 43		 */
 44		public static function getInstance(stage:Stage):Stage3DManager
 45		{
 46			return (_instances ||= new Dictionary())[stage] ||= new Stage3DManager(stage, new Stage3DManagerSingletonEnforcer());
 47		}
 48		
 49		/**
 50		 * Requests the Stage3DProxy for the given index.
 51		 * @param index The index of the requested Stage3D.
 52		 * @param forceSoftware Whether to force software mode even if hardware acceleration is available.
 53		 * @param profile The compatibility profile, an enumeration of Context3DProfile
 54		 * @return The Stage3DProxy for the given index.
 55		 */
 56		public function getStage3DProxy(index:uint, forceSoftware:Boolean = false, profile:String = "baseline"):Stage3DProxy
 57		{
 58			if (!_stageProxies[index]) {
 59				_numStageProxies++;
 60				_stageProxies[index] = new Stage3DProxy(index, _stage.stage3Ds[index], this, forceSoftware, profile);
 61			}
 62			
 63			return _stageProxies[index];
 64		}
 65		
 66		/**
 67		 * Removes a Stage3DProxy from the manager.
 68		 * @param stage3DProxy
 69		 * @private
 70		 */
 71		arcane function removeStage3DProxy(stage3DProxy:Stage3DProxy):void
 72		{
 73			_numStageProxies--;
 74			_stageProxies[stage3DProxy.stage3DIndex] = null;
 75		}
 76		
 77		/**
 78		 * Get the next available stage3DProxy. An error is thrown if there are no Stage3DProxies available
 79		 * @param forceSoftware Whether to force software mode even if hardware acceleration is available.
 80		 * @param profile The compatibility profile, an enumeration of Context3DProfile
 81		 * @return The allocated stage3DProxy
 82		 */
 83		public function getFreeStage3DProxy(forceSoftware:Boolean = false, profile:String = "baseline"):Stage3DProxy
 84		{
 85			var i:uint;
 86			var len:uint = _stageProxies.length;
 87			
 88			while (i < len) {
 89				if (!_stageProxies[i]) {
 90					getStage3DProxy(i, forceSoftware, profile);
 91					_stageProxies[i].width = _stage.stageWidth;
 92					_stageProxies[i].height = _stage.stageHeight;
 93					return _stageProxies[i];
 94				}
 95				++i;
 96			}
 97			
 98			throw new Error("Too many Stage3D instances used!");
 99			return null;
100		}
101		
102		/**
103		 * Checks if a new stage3DProxy can be created and managed by the class.
104		 * @return true if there is one slot free for a new stage3DProxy
105		 */
106		public function get hasFreeStage3DProxy():Boolean
107		{
108			return _numStageProxies < _stageProxies.length? true : false;
109		}
110		
111		/**
112		 * Returns the amount of stage3DProxy objects that can be created and managed by the class
113		 * @return the amount of free slots
114		 */
115		public function get numProxySlotsFree():uint
116		{
117			return _stageProxies.length - _numStageProxies;
118		}
119		
120		/**
121		 * Returns the amount of Stage3DProxy objects currently managed by the class.
122		 * @return the amount of slots used
123		 */
124		public function get numProxySlotsUsed():uint
125		{
126			return _numStageProxies;
127		}
128		
129		/**
130		 * Returns the maximum amount of Stage3DProxy objects that can be managed by the class
131		 * @return the maximum amount of Stage3DProxy objects that can be managed by the class
132		 */
133		public function get numProxySlotsTotal():uint
134		{
135			return _stageProxies.length;
136		}
137	}
138}
139
140class Stage3DManagerSingletonEnforcer
141{
142}