BsComponent.h 4.4 KB

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