Event.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef ANKI_EVENT_EVENT_H
  2. #define ANKI_EVENT_EVENT_H
  3. #include "anki/scene/Common.h"
  4. #include "anki/util/Bitset.h"
  5. #include "anki/Math.h"
  6. namespace anki {
  7. // Forward
  8. class EventManager;
  9. class SceneNode;
  10. /// @addtogroup Events
  11. /// @{
  12. /// The base class for all events
  13. class Event: public Bitset<U8>
  14. {
  15. friend class EventManager;
  16. public:
  17. /// Event flags
  18. enum EventFlags
  19. {
  20. EF_NONE = 0,
  21. EF_REANIMATE = 1 << 0,
  22. EF_MARKED_FOR_DELETION = 1 << 1
  23. };
  24. /// @name Constructors/Destructor
  25. /// @{
  26. /// Constructor
  27. Event(EventManager* manager, F32 startTime, F32 duration,
  28. SceneNode* snode = nullptr, U8 flags = EF_NONE);
  29. virtual ~Event();
  30. /// @}
  31. /// @name Accessors
  32. /// @{
  33. F32 getStartTime() const
  34. {
  35. return startTime;
  36. }
  37. F32 getDuration() const
  38. {
  39. return duration;
  40. }
  41. Bool isDead(F32 crntTime) const
  42. {
  43. return crntTime >= startTime + duration;
  44. }
  45. EventManager& getEventManager()
  46. {
  47. return *manager;
  48. }
  49. const EventManager& getEventManager() const
  50. {
  51. return *manager;
  52. }
  53. SceneNode* getSceneNode()
  54. {
  55. return node;
  56. }
  57. const SceneNode* getSceneNode() const
  58. {
  59. return node;
  60. }
  61. SceneAllocator<U8> getSceneAllocator() const;
  62. SceneAllocator<U8> getSceneFrameAllocator() const;
  63. /// @}
  64. /// This method should be implemented by the derived classes
  65. /// @param prevUpdateTime The time of the previous update (sec)
  66. /// @param crntTime The current time (sec)
  67. virtual void update(F32 prevUpdateTime, F32 crntTime) = 0;
  68. /// This is called when the event is killed
  69. /// @param prevUpdateTime The time of the previous update (sec)
  70. /// @param crntTime The current time (sec)
  71. /// @return Return false if you don't want to be killed
  72. virtual Bool onKilled(F32 prevUpdateTime, F32 crntTime)
  73. {
  74. (void)prevUpdateTime;
  75. (void)crntTime;
  76. return true;
  77. }
  78. /// Mark event for deletion
  79. void markForDeletion()
  80. {
  81. enableBits(EF_MARKED_FOR_DELETION);
  82. node = nullptr;
  83. }
  84. /// Ask if event is marked for deletion
  85. Bool isMarkedForDeletion() const
  86. {
  87. return bitsEnabled(EF_MARKED_FOR_DELETION);
  88. }
  89. protected:
  90. /// The time the event will start. Eg 23:00. If it's < 0 then start the
  91. /// event now
  92. F32 startTime;
  93. F32 duration; ///< The duration of the event
  94. /// Linear interpolation between values
  95. /// @param[in] from Starting value
  96. /// @param[in] to Ending value
  97. /// @param[in] u The percentage from the from "from" value. Values
  98. /// from [0.0, 1.0]
  99. template<typename Type>
  100. static Type interpolate(const Type& from, const Type& to, F32 u)
  101. {
  102. //ANKI_ASSERT(u >= 0 && u <= 1.0);
  103. return from * (1.0 - u) + to * u;
  104. }
  105. template<typename Type>
  106. static Type cosInterpolate(const Type& from, const Type& to, F32 u)
  107. {
  108. F32 u2 = (1.0 - cos(u * getPi<F32>())) / 2.0;
  109. return from * (1.0 - u2) + to * u2;
  110. }
  111. template<typename Type>
  112. static Type cubicInterpolate(
  113. const Type& a, const Type& b, const Type& c,
  114. const Type& d, F32 u)
  115. {
  116. F32 u2 = u * u;
  117. Type a0 = d - c - a + b;
  118. Type a1 = a - b - a0;
  119. Type a2 = c - a;
  120. Type a3 = b;
  121. return(a0 * u * u2 + a1 * u2 + a2 * u + a3);
  122. }
  123. /// Return the u between current time and when the event started
  124. /// @return A number [0.0, 1.0]
  125. F32 getDelta(F32 crntTime) const;
  126. private:
  127. EventManager* manager = nullptr; ///< Keep it here to access allocators etc
  128. SceneNode* node = nullptr; ///< Optional scene node
  129. };
  130. /// @}
  131. } // end namespace anki
  132. #endif