BsComponent.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /**
  21. * Called once per frame on all components.
  22. *
  23. * @note Internal method.
  24. */
  25. virtual void update() { }
  26. /**
  27. * Calculates bounds of the visible contents represented by this component (e.g. a mesh for Renderable).
  28. *
  29. * @param[in] bounds Bounds of the contents in world space coordinates.
  30. * @return True if the component has bounds with non-zero volume, otherwise false.
  31. */
  32. virtual bool calculateBounds(Bounds& bounds);
  33. /**
  34. * Checks if this and the provided component represent the same type.
  35. *
  36. * @note
  37. * RTTI type cannot be checked directly since components can be further specialized internally for scripting
  38. * purposes.
  39. */
  40. virtual bool typeEquals(const Component& other);
  41. /**
  42. * Removes the component from parent SceneObject and deletes it. All the references to this component will be
  43. * marked as destroyed and you will get an exception if you try to use them.
  44. *
  45. * @param[in] immediate If true the destruction will be performed immediately, otherwise it will be delayed
  46. * until the end of the current frame (preferred option).
  47. */
  48. void destroy(bool immediate = false);
  49. protected:
  50. friend class SceneObject;
  51. friend class SceneObjectRTTI;
  52. Component(const HSceneObject& parent);
  53. virtual ~Component();
  54. /**
  55. * Construct any resources the component needs before use. Called when the parent scene object is instantiated.
  56. * A non-instantiated component shouldn't be used for any other purpose than serialization.
  57. */
  58. virtual void instantiate() {}
  59. /** Called when the component is ready to be initialized. */
  60. virtual void onInitialized() {}
  61. /** Called just before the component is destroyed. */
  62. virtual void onDestroyed() {}
  63. /** Called just before the component is deactivated or destroyed. */
  64. virtual void onDisabled() {}
  65. /** Called when the component is activated or created. */
  66. virtual void onEnabled() {}
  67. /**
  68. * Destroys this component.
  69. *
  70. * @param[in] handle Game object handle this this object.
  71. * @param[in] immediate If true, the object will be deallocated and become unusable right away. Otherwise the
  72. * deallocation will be delayed to the end of frame (preferred method).
  73. *
  74. * @note Unlike destroy(), does not remove the component from its parent.
  75. */
  76. void destroyInternal(GameObjectHandleBase& handle, bool immediate = false) override;
  77. private:
  78. Component(const Component& other) { }
  79. protected:
  80. HSceneObject mParent;
  81. /************************************************************************/
  82. /* RTTI */
  83. /************************************************************************/
  84. public:
  85. friend class ComponentRTTI;
  86. static RTTITypeBase* getRTTIStatic();
  87. RTTITypeBase* getRTTI() const override;
  88. protected:
  89. Component() {} // Serialization only
  90. };
  91. /** @} */
  92. }