Event.h 2.7 KB

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