BsComponent.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGameObject.h"
  4. #include "BsBounds.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Components represent primary logic elements in the scene.
  9. * They are attached to scene objects.
  10. */
  11. class BS_CORE_EXPORT Component : public GameObject
  12. {
  13. public:
  14. /**
  15. * @brief Returns the SceneObject this Component is assigned to.
  16. */
  17. HSceneObject sceneObject() const { return mParent; }
  18. /**
  19. * @copydoc sceneObject
  20. */
  21. HSceneObject SO() const { return sceneObject(); }
  22. /**
  23. * @brief Called once per frame on all components.
  24. *
  25. * @note Internal method.
  26. */
  27. virtual void update() { }
  28. /**
  29. * @brief Calculates bounds of the visible contents represented by this component (e.g. a mesh for Renderable).
  30. *
  31. * @param bounds Bounds of the contents in world space coordinates.
  32. *
  33. * @returns True if the component has bounds with non-zero volume, otherwise false.
  34. */
  35. virtual bool calculateBounds(Bounds& bounds);
  36. /**
  37. * @brief Checks if this and the provided component represent the same type.
  38. *
  39. * @note RTTI type cannot be checked directly since components can be further specialized internally
  40. * for scripting purposes.
  41. */
  42. virtual bool typeEquals(const Component& other);
  43. /**
  44. * @brief Removes the component from parent SceneObject and deletes it. All
  45. * the references to this component will be marked as destroyed and you
  46. * will get an exception if you try to use them.
  47. *
  48. * @param [in] immediate If true the destruction will be performed immediately, otherwise
  49. * it will be delayed until the end of the current frame (preferred option).
  50. */
  51. void destroy(bool immediate = false);
  52. protected:
  53. friend class SceneObject;
  54. friend class SceneObjectRTTI;
  55. Component(const HSceneObject& parent);
  56. virtual ~Component();
  57. /**
  58. * @brief Construct any resources the component needs before use. Called when the parent
  59. * scene object is instantiated. A non-instantiated component shouldn't be used for
  60. * any other purpose than serialization.
  61. */
  62. virtual void instantiate() {}
  63. /**
  64. * @brief Called when the component is ready to be initialized.
  65. */
  66. virtual void onInitialized() {}
  67. /**
  68. * @brief Called just before the component is destroyed.
  69. */
  70. virtual void onDestroyed() {}
  71. /**
  72. * @brief Called just before the component is deactivated or destroyed.
  73. */
  74. virtual void onDisabled() {}
  75. /**
  76. * @brief Called when the component is activated or created.
  77. */
  78. virtual void onEnabled() {}
  79. /**
  80. * @brief Destroys this component.
  81. *
  82. * @param [in] handle Game object handle this this object.
  83. * @param [in] immediate If true, the object will be deallocated and become unusable
  84. * right away. Otherwise the deallocation will be delayed to the end of
  85. * frame (preferred method).
  86. *
  87. * @note Unlike "destroy", does not remove the component from its parent.
  88. */
  89. void destroyInternal(GameObjectHandleBase& handle, bool immediate = false) override;
  90. private:
  91. Component(const Component& other) { }
  92. protected:
  93. HSceneObject mParent;
  94. /************************************************************************/
  95. /* RTTI */
  96. /************************************************************************/
  97. public:
  98. friend class ComponentRTTI;
  99. static RTTITypeBase* getRTTIStatic();
  100. virtual RTTITypeBase* getRTTI() const;
  101. protected:
  102. Component() {} // Serialization only
  103. };
  104. }