/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

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