BsSceneManager.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Utility/BsModule.h"
  6. #include "Scene/BsGameObject.h"
  7. namespace bs
  8. {
  9. class LightProbeVolume;
  10. /** @addtogroup Scene-Internal
  11. * @{
  12. */
  13. /** Information about a scene actor and the scene object it has been bound to. */
  14. struct BoundActorData
  15. {
  16. BoundActorData() { }
  17. BoundActorData(const SPtr<SceneActor>& actor, const HSceneObject& so)
  18. :actor(actor), so(so)
  19. { }
  20. SPtr<SceneActor> actor;
  21. HSceneObject so;
  22. };
  23. /** Possible states components can be in. Controls which component callbacks are triggered. */
  24. enum class ComponentState
  25. {
  26. Running, /**< All components callbacks are being triggered normally. */
  27. Paused, /**< All component callbacks except update are being triggered normally. */
  28. Stopped /**< No component callbacks are being triggered. */
  29. };
  30. /**
  31. * Keeps track of all active SceneObject%s and their components. Keeps track of component state and triggers their
  32. * events. Updates the transforms of objects as SceneObject%s move.
  33. */
  34. class BS_CORE_EXPORT SceneManager : public Module<SceneManager>
  35. {
  36. public:
  37. SceneManager();
  38. ~SceneManager();
  39. /** Returns the root scene object. */
  40. HSceneObject getRootNode() const { return mRootNode; }
  41. /**
  42. * Destroys all scene objects in the scene.
  43. *
  44. * @param[in] forceAll If true, then even the persistent objects will be unloaded.
  45. */
  46. void clearScene(bool forceAll = false);
  47. /**
  48. * Changes the component state that globally determines which component callbacks are activated. Only affects
  49. * components that don't have the ComponentFlag::AlwaysRun flag set.
  50. */
  51. void setComponentState(ComponentState state);
  52. /** Checks are the components currently in the Running state. */
  53. bool isRunning() const { return mComponentState == ComponentState::Running; }
  54. /**
  55. * Returns a list of all components of the specified type currently in the scene.
  56. *
  57. * @tparam T Type of the component to search for.
  58. *
  59. * @param[in] activeOnly If true only active components are returned, otherwise all components are returned.
  60. * @return A list of all matching components in the scene.
  61. */
  62. template<class T>
  63. Vector<GameObjectHandle<T>> findComponents(bool activeOnly = true);
  64. /** Returns all cameras in the scene. */
  65. const UnorderedMap<Camera*, SPtr<Camera>>& getAllCameras() const { return mCameras; }
  66. /**
  67. * Returns the camera in the scene marked as main. Main camera controls the final render surface that is displayed
  68. * to the user. If there are multiple main cameras, the first one found returned.
  69. */
  70. SPtr<Camera> getMainCamera() const;
  71. /**
  72. * Sets the render target that the main camera in the scene (if any) will render its view to. This generally means
  73. * the main game window when running standalone, or the Game viewport when running in editor.
  74. */
  75. void setMainRenderTarget(const SPtr<RenderTarget>& rt);
  76. /**
  77. * Binds a scene actor with a scene object. Every frame the scene object's transform will be monitored for
  78. * changes and those changes will be automatically transfered to the actor.
  79. */
  80. void _bindActor(const SPtr<SceneActor>& actor, const HSceneObject& so);
  81. /** Unbinds an actor that was previously bound using bindActor(). */
  82. void _unbindActor(const SPtr<SceneActor>& actor);
  83. /** Returns a scene object bound to the provided actor, if any. */
  84. HSceneObject _getActorSO(const SPtr<SceneActor>& actor) const;
  85. /** Notifies the scene manager that a new camera was created. */
  86. void _registerCamera(const SPtr<Camera>& camera);
  87. /** Notifies the scene manager that a camera was removed. */
  88. void _unregisterCamera(const SPtr<Camera>& camera);
  89. /** Notifies the scene manager that a camera either became the main camera, or has stopped being main camera. */
  90. void _notifyMainCameraStateChanged(const SPtr<Camera>& camera);
  91. /** Changes the root scene object. Any persistent objects will remain in the scene, now parented to the new root. */
  92. void _setRootNode(const HSceneObject& root);
  93. /** Called every frame. Calls update methods on all scene objects and their components. */
  94. void _update();
  95. /** Updates dirty transforms on any core objects that may be tied with scene objects. */
  96. void _updateCoreObjectTransforms();
  97. /** Notifies the manager that a new component has just been created. The manager triggers necessary callbacks. */
  98. void _notifyComponentCreated(const HComponent& component, bool parentActive);
  99. /**
  100. * Notifies the manager that a scene object the component belongs to was activated. The manager triggers necessary
  101. * callbacks.
  102. */
  103. void _notifyComponentActivated(const HComponent& component, bool triggerEvent);
  104. /**
  105. * Notifies the manager that a scene object the component belongs to was deactivated. The manager triggers necessary
  106. * callbacks.
  107. */
  108. void _notifyComponentDeactivated(const HComponent& component, bool triggerEvent);
  109. /** Notifies the manager that a component is about to be destroyed. The manager triggers necessary callbacks. */
  110. void _notifyComponentDestroyed(const HComponent& component);
  111. protected:
  112. friend class SceneObject;
  113. /**
  114. * Register a new node in the scene manager, on the top-most level of the hierarchy.
  115. *
  116. * @param[in] node Node you wish to add. It's your responsibility not to add duplicate or null nodes. This
  117. * method won't check.
  118. *
  119. * @note
  120. * After you add a node in the scene manager, it takes ownership of its memory and is responsible for releasing it.
  121. * Do NOT add nodes that have already been added (if you just want to change their parent). Normally this
  122. * method will only be called by SceneObject.
  123. */
  124. void registerNewSO(const HSceneObject& node);
  125. /** Callback that is triggered when the main render target size is changed. */
  126. void onMainRenderTargetResized();
  127. /** Removes a component from the active component list. */
  128. void removeFromActiveList(const HComponent& component);
  129. /** Removes a component from the inactive component list. */
  130. void removeFromInactiveList(const HComponent& component);
  131. /** Removes a component from the uninitialized component list. */
  132. void removeFromUninitializedList(const HComponent& component);
  133. /**
  134. * Encodes an index and a type into a single 32-bit integer. Top 2 bits represent the type, while the rest represent
  135. * the index.
  136. */
  137. UINT32 encodeComponentId(UINT32 idx, UINT32 type);
  138. /** Decodes an id encoded with encodeComponentId(). */
  139. void decodeComponentId(UINT32 id, UINT32& idx, UINT32& type);
  140. /** Checks does the specified component type match the provided RTTI id. */
  141. static bool isComponentOfType(const HComponent& component, UINT32 rttiId);
  142. protected:
  143. HSceneObject mRootNode;
  144. UnorderedMap<SceneActor*, BoundActorData> mBoundActors;
  145. UnorderedMap<Camera*, SPtr<Camera>> mCameras;
  146. Vector<SPtr<Camera>> mMainCameras;
  147. Vector<HComponent> mActiveComponents;
  148. Vector<HComponent> mInactiveComponents;
  149. Vector<HComponent> mUninitializedComponents;
  150. SPtr<RenderTarget> mMainRT;
  151. HEvent mMainRTResizedConn;
  152. ComponentState mComponentState = ComponentState::Running;
  153. };
  154. /** Provides easy access to the SceneManager. */
  155. BS_CORE_EXPORT SceneManager& gSceneManager();
  156. template<class T>
  157. Vector<GameObjectHandle<T>> SceneManager::findComponents(bool activeOnly)
  158. {
  159. UINT32 rttiId = T::getRTTIStatic()->getRTTIId();
  160. Vector<GameObjectHandle<T>> output;
  161. for(auto& entry : mActiveComponents)
  162. {
  163. if (isComponentOfType(entry, rttiId))
  164. output.push_back(static_object_cast<T>(entry));
  165. }
  166. if(!activeOnly)
  167. {
  168. for(auto& entry : mInactiveComponents)
  169. {
  170. if (isComponentOfType(entry, rttiId))
  171. output.push_back(static_object_cast<T>(entry));
  172. }
  173. for(auto& entry : mUninitializedComponents)
  174. {
  175. if (isComponentOfType(entry, rttiId))
  176. output.push_back(static_object_cast<T>(entry));
  177. }
  178. }
  179. return output;
  180. }
  181. /** @} */
  182. }