2
0

BsComponent.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /** Called when the component's parent scene object has changed. */
  68. virtual void onTransformChanged(TransformChangedFlags flags) { }
  69. /** Checks whether the component wants to received the specified transform changed message. */
  70. bool supportsNotify(TransformChangedFlags flags) const { return (mNotifyFlags & flags) != 0; }
  71. /**
  72. * Destroys this component.
  73. *
  74. * @param[in] handle Game object handle this this object.
  75. * @param[in] immediate If true, the object will be deallocated and become unusable right away. Otherwise the
  76. * deallocation will be delayed to the end of frame (preferred method).
  77. *
  78. * @note Unlike destroy(), does not remove the component from its parent.
  79. */
  80. void destroyInternal(GameObjectHandleBase& handle, bool immediate = false) override;
  81. private:
  82. Component(const Component& other) { }
  83. protected:
  84. HSceneObject mParent;
  85. HComponent mThisHandle;
  86. TransformChangedFlags mNotifyFlags;
  87. /************************************************************************/
  88. /* RTTI */
  89. /************************************************************************/
  90. public:
  91. friend class ComponentRTTI;
  92. static RTTITypeBase* getRTTIStatic();
  93. RTTITypeBase* getRTTI() const override;
  94. protected:
  95. Component() {} // Serialization only
  96. };
  97. /** @} */
  98. }