BsComponent.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "Scene/BsGameObject.h"
  6. #include "Math/BsBounds.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Scene
  10. * @{
  11. */
  12. /** Flags that control behavior of a Component. */
  13. enum class ComponentFlag
  14. {
  15. /**
  16. * Ensures that scene manager cannot pause or stop component callbacks from executing. Off by default.
  17. * Note that this flag must be specified on component creation, in its constructor and any later changes
  18. * to the flag will be ignored.
  19. */
  20. AlwaysRun = 1
  21. };
  22. typedef Flags<ComponentFlag> ComponentFlags;
  23. BS_FLAGS_OPERATORS(ComponentFlag)
  24. /**
  25. * Components represent primary logic elements in the scene. They are attached to scene objects.
  26. *
  27. * You should implement some or all of update/onCreated/onInitialized/onEnabled/onDisabled/onTransformChanged/
  28. * onDestroyed methods to implement the relevant component logic. Avoid putting logic in constructors or destructors.
  29. *
  30. * Components can be in different states. These states control which of the events listed above trigger:
  31. * - Running - Scene manager is sending out events.
  32. * - Paused - Scene manager is sending out all events except per-frame update().
  33. * - Stopped - Scene manager is not sending out events except for onCreated/onDestroyed.
  34. *
  35. * These states can be changed globally though SceneManager and affect all components. Individual components can
  36. * override these states in two ways:
  37. * - Set the ComponentFlag::AlwaysRun to true and the component will always stay in Running state, regardless of
  38. * state set in SceneManager. This flag should be set in constructor and not change during component lifetime.
  39. * - If the component's parent SceneObject is inactive (SceneObject::setActive(false)), or any of his parents are
  40. * inactive, then the component is considered to be in Stopped state, regardless whether the ComponentFlag::AlwaysRun
  41. * flag is set or not.
  42. **/
  43. class BS_CORE_EXPORT Component : public GameObject
  44. {
  45. public:
  46. /** Returns the SceneObject this Component is assigned to. */
  47. HSceneObject sceneObject() const { return mParent; }
  48. /** @copydoc sceneObject */
  49. HSceneObject SO() const { return sceneObject(); }
  50. /** Returns a handle to this object. */
  51. HComponent getHandle() const { return mThisHandle; }
  52. /** Called once per frame. Only called if the component is in Running state. */
  53. virtual void update() { }
  54. /**
  55. * Calculates bounds of the visible contents represented by this component (for example a mesh for Renderable).
  56. *
  57. * @param[in] bounds Bounds of the contents in world space coordinates.
  58. * @return True if the component has bounds with non-zero volume, otherwise false.
  59. */
  60. virtual bool calculateBounds(Bounds& bounds);
  61. /**
  62. * Checks if this and the provided component represent the same type.
  63. *
  64. * @note
  65. * RTTI type cannot be checked directly since components can be further specialized internally for scripting
  66. * purposes.
  67. */
  68. virtual bool typeEquals(const Component& other);
  69. /**
  70. * Removes the component from parent SceneObject and deletes it. All the references to this component will be
  71. * marked as destroyed and you will get an exception if you try to use them.
  72. *
  73. * @param[in] immediate If true the destruction will be performed immediately, otherwise it will be delayed
  74. * until the end of the current frame (preferred option).
  75. */
  76. void destroy(bool immediate = false);
  77. /** @name Internal
  78. * @{
  79. */
  80. /**
  81. * Construct any resources the component needs before use. Called when the parent scene object is instantiated.
  82. * A non-instantiated component shouldn't be used for any other purpose than serialization.
  83. */
  84. virtual void _instantiate() {}
  85. /** Sets new flags that determine when is onTransformChanged called. */
  86. void setNotifyFlags(TransformChangedFlags flags) { mNotifyFlags = flags; }
  87. /** Gets the currently assigned notify flags. See _setNotifyFlags(). */
  88. TransformChangedFlags _getNotifyFlags() const { return mNotifyFlags; }
  89. /** @} */
  90. protected:
  91. friend class SceneManager;
  92. friend class SceneObject;
  93. friend class SceneObjectRTTI;
  94. Component(const HSceneObject& parent);
  95. virtual ~Component();
  96. /** Called once when the component has been created. Called regardless of the state the component is in. */
  97. virtual void onCreated() {}
  98. /**
  99. * Called once when the component first leaves the Stopped state. This includes component creation if requirements
  100. * for leaving Stopped state are met, in which case it is called after onCreated.
  101. */
  102. virtual void onInitialized() {}
  103. /** Called once just before the component is destroyed. Called regardless of the state the component is in. */
  104. virtual void onDestroyed() {}
  105. /**
  106. * Called every time a component is placed into the Stopped state. This includes component destruction if component
  107. * wasn't already in Stopped state during destruction. When called during destruction it is called before
  108. * onDestroyed.
  109. */
  110. virtual void onDisabled() {}
  111. /**
  112. * Called every time a component leaves the Stopped state. This includes component creation if requirements
  113. * for leaving the Stopped state are met. When called during creation it is called after onInitialized.
  114. */
  115. virtual void onEnabled() {}
  116. /**
  117. * Called when the component's parent scene object has changed. Not called if the component is in Stopped state.
  118. * Also only called if necessary notify flags are set via _setNotifyFlags().
  119. */
  120. virtual void onTransformChanged(TransformChangedFlags flags) { }
  121. /** Checks whether the component wants to received the specified transform changed message. */
  122. bool supportsNotify(TransformChangedFlags flags) const { return (mNotifyFlags & flags) != 0; }
  123. /** Enables or disabled a flag controlling component's behaviour. */
  124. void setFlag(ComponentFlag flag, bool enabled) { if (enabled) mFlags.set(flag); else mFlags.unset(flag); }
  125. /** Checks if the component has a certain flag enabled. */
  126. bool hasFlag(ComponentFlag flag) const { return mFlags.isSet(flag); }
  127. /** Sets an index that uniquely identifies a component with the SceneManager. */
  128. void setSceneManagerId(UINT32 id) { mSceneManagerId = id; }
  129. /** Returns an index that unique identifies a component with the SceneManager. */
  130. UINT32 getSceneManagerId() const { return mSceneManagerId; }
  131. /**
  132. * Destroys this component.
  133. *
  134. * @param[in] handle Game object handle this this object.
  135. * @param[in] immediate If true, the object will be deallocated and become unusable right away. Otherwise the
  136. * deallocation will be delayed to the end of frame (preferred method).
  137. *
  138. * @note Unlike destroy(), does not remove the component from its parent.
  139. */
  140. void destroyInternal(GameObjectHandleBase& handle, bool immediate = false) override;
  141. private:
  142. Component(const Component& other) { }
  143. protected:
  144. HComponent mThisHandle;
  145. TransformChangedFlags mNotifyFlags;
  146. ComponentFlags mFlags;
  147. UINT32 mSceneManagerId;
  148. private:
  149. HSceneObject mParent;
  150. /************************************************************************/
  151. /* RTTI */
  152. /************************************************************************/
  153. public:
  154. friend class ComponentRTTI;
  155. static RTTITypeBase* getRTTIStatic();
  156. RTTITypeBase* getRTTI() const override;
  157. protected:
  158. Component(); // Serialization only
  159. };
  160. /** @} */
  161. }