Event.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Common.h>
  7. #include <AnKi/Util/List.h>
  8. #include <AnKi/Util/WeakArray.h>
  9. namespace anki {
  10. /// @addtogroup scene
  11. /// @{
  12. /// The base class for all events
  13. class Event : public IntrusiveListEnabled<Event>
  14. {
  15. friend class EventManager;
  16. public:
  17. /// Constructor
  18. Event(EventManager* manager);
  19. virtual ~Event();
  20. SceneAllocator<U8> getAllocator() const;
  21. EventManager& getEventManager()
  22. {
  23. return *m_manager;
  24. }
  25. const EventManager& getEventManager() const
  26. {
  27. return *m_manager;
  28. }
  29. SceneGraph& getSceneGraph();
  30. const SceneGraph& getSceneGraph() const;
  31. Second getStartTime() const
  32. {
  33. return m_startTime;
  34. }
  35. Second getDuration() const
  36. {
  37. return m_duration;
  38. }
  39. Bool isDead(Second crntTime) const
  40. {
  41. return crntTime >= m_startTime + m_duration;
  42. }
  43. /// @note It's thread safe.
  44. void setMarkedForDeletion();
  45. Bool getMarkedForDeletion() const
  46. {
  47. return m_markedForDeletion;
  48. }
  49. void setReanimate(Bool reanimate)
  50. {
  51. m_reanimate = reanimate;
  52. }
  53. Bool getReanimate() const
  54. {
  55. return m_reanimate;
  56. }
  57. WeakArray<SceneNode*> getAssociatedSceneNodes()
  58. {
  59. return (m_associatedNodes.getSize() == 0)
  60. ? WeakArray<SceneNode*>()
  61. : WeakArray<SceneNode*>(&m_associatedNodes[0], m_associatedNodes.getSize());
  62. }
  63. void addAssociatedSceneNode(SceneNode* node)
  64. {
  65. ANKI_ASSERT(node);
  66. m_associatedNodes.emplaceBack(getAllocator(), node);
  67. }
  68. /// This method should be implemented by the derived classes
  69. /// @param prevUpdateTime The time of the previous update (sec)
  70. /// @param crntTime The current time (sec)
  71. virtual Error update(Second prevUpdateTime, Second crntTime) = 0;
  72. /// This is called when the event is killed
  73. /// @param prevUpdateTime The time of the previous update (sec)
  74. /// @param crntTime The current time (sec)
  75. virtual Error onKilled([[maybe_unused]] Second prevUpdateTime, [[maybe_unused]] Second crntTime)
  76. {
  77. return Error::NONE;
  78. }
  79. protected:
  80. EventManager* m_manager = nullptr;
  81. Second m_startTime = 0.0;
  82. Second m_duration = 0.0;
  83. Bool m_markedForDeletion = false;
  84. Bool m_reanimate = false;
  85. DynamicArray<SceneNode*> m_associatedNodes;
  86. /// @param startTime The time the event will start. If it's < 0 then start the event now.
  87. /// @param duration The duration of the event.
  88. void init(Second startTime, Second duration);
  89. /// Return the u between current time and when the event started
  90. /// @return A number [0.0, 1.0]
  91. Second getDelta(Second crntTime) const;
  92. };
  93. /// @}
  94. } // end namespace anki