BsComponent.h 7.6 KB

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