Event.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2009-present, 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. /// @param startTime The time the event will start. If it's < 0 then start the event now.
  18. /// @param duration The duration of the event.
  19. Event(Second startTime, Second duration)
  20. {
  21. init(startTime, duration);
  22. }
  23. virtual ~Event() = default;
  24. Second getStartTime() const
  25. {
  26. return m_startTime;
  27. }
  28. Second getDuration() const
  29. {
  30. return m_duration;
  31. }
  32. Bool isDead(Second crntTime) const
  33. {
  34. return crntTime >= m_startTime + m_duration;
  35. }
  36. void markForDeletion()
  37. {
  38. m_markedForDeletion = true;
  39. }
  40. Bool isMarkedForDeletion() const
  41. {
  42. return m_markedForDeletion;
  43. }
  44. void setReanimate(Bool reanimate)
  45. {
  46. m_reanimate = reanimate;
  47. }
  48. Bool getReanimate() const
  49. {
  50. return m_reanimate;
  51. }
  52. WeakArray<SceneNode*> getAssociatedSceneNodes()
  53. {
  54. return (m_associatedNodes.getSize() == 0) ? WeakArray<SceneNode*>()
  55. : WeakArray<SceneNode*>(&m_associatedNodes[0], m_associatedNodes.getSize());
  56. }
  57. ConstWeakArray<SceneNode*> getAssociatedSceneNodes() const
  58. {
  59. return (m_associatedNodes.getSize() == 0) ? ConstWeakArray<SceneNode*>()
  60. : ConstWeakArray<SceneNode*>(&m_associatedNodes[0], m_associatedNodes.getSize());
  61. }
  62. void addAssociatedSceneNode(SceneNode* node)
  63. {
  64. ANKI_ASSERT(node);
  65. m_associatedNodes.emplaceBack(node);
  66. }
  67. /// This method should be implemented by the derived classes
  68. /// @param prevUpdateTime The time of the previous update (sec)
  69. /// @param crntTime The current time (sec)
  70. virtual void update(Second prevUpdateTime, Second crntTime) = 0;
  71. /// This is called when the event is killed
  72. /// @param prevUpdateTime The time of the previous update (sec)
  73. /// @param crntTime The current time (sec)
  74. virtual void onKilled([[maybe_unused]] Second prevUpdateTime, [[maybe_unused]] Second crntTime)
  75. {
  76. }
  77. protected:
  78. Second m_startTime = 0.0;
  79. Second m_duration = 0.0;
  80. Bool m_markedForDeletion = false;
  81. Bool m_reanimate = false;
  82. SceneDynamicArray<SceneNode*> m_associatedNodes;
  83. /// Return the u between current time and when the event started
  84. /// @return A number [0.0, 1.0]
  85. Second getDelta(Second crntTime) const;
  86. void init(Second startTime, Second duration);
  87. };
  88. /// @}
  89. } // end namespace anki