tb_animation.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_ANIMATION_H
  6. #define TB_ANIMATION_H
  7. #include "../tb_linklist.h"
  8. #include "../tb_object.h"
  9. namespace tb {
  10. class TBAnimationObject;
  11. /** Defines how the animation progress value is interpolated. */
  12. enum ANIMATION_CURVE {
  13. ANIMATION_CURVE_LINEAR, ///< Linear
  14. ANIMATION_CURVE_SLOW_DOWN, ///< Fast start, slow end
  15. ANIMATION_CURVE_SPEED_UP, ///< Slow start, fast end
  16. ANIMATION_CURVE_BEZIER, ///< Slow start, slow end. Almost linear.
  17. ANIMATION_CURVE_SMOOTH ///< Slow start, slow end. Stronger than ANIMATION_CURVE_BEZIER.
  18. };
  19. /** Defines what the animation duration time is relative to. */
  20. enum ANIMATION_TIME {
  21. /** The start time begins when the animation start in TBAnimationManager::StartAnimation. */
  22. ANIMATION_TIME_IMMEDIATELY,
  23. /** The animation start in StartAnimation just as with ANIMATION_TIME_IMMEDIATELY,
  24. but the start time is adjusted to when the animations Update is about to be called
  25. the first time since it was started.
  26. Using this is most often preferable since starting a animation is often accompanied
  27. with some extra work that might eat up a considerable time of the total duration (and
  28. chop of the beginning of it).
  29. F.ex: Creating a window and starting its appearance animation. During initialization
  30. of the window, you might initiate loading of additional resources. When that is done
  31. and you finally end up updating animations, most of the animation time might already
  32. have passed. If the animation start time is adjusted to the first update, the whole
  33. animation will run from 0.0 - 1.0 smoothly when the initialization is done. */
  34. ANIMATION_TIME_FIRST_UPDATE
  35. };
  36. #define ANIMATION_DEFAULT_CURVE ANIMATION_CURVE_SLOW_DOWN
  37. #define ANIMATION_DEFAULT_DURATION 200
  38. /** TBAnimationListener - Listens to the progress of TBAnimationObject. */
  39. class TBAnimationListener : public TBLinkOf<TBAnimationListener>
  40. {
  41. public:
  42. virtual ~TBAnimationListener() {};
  43. /** Called after the animation object handled its own OnAnimationStart.
  44. See TBAnimationObject::OnAnimationStart for details. */
  45. virtual void OnAnimationStart(TBAnimationObject *obj) = 0;
  46. /** Called after the animation object handled its own OnAnimationStart.
  47. See TBAnimationObject::OnAnimationUpdate for details. */
  48. virtual void OnAnimationUpdate(TBAnimationObject *obj, float progress) = 0;
  49. /** Called after the animation object handled its own OnAnimationStart.
  50. See TBAnimationObject::OnAnimationStop for details. */
  51. virtual void OnAnimationStop(TBAnimationObject *obj, bool aborted) = 0;
  52. };
  53. /** TBAnimationObject - Base class for all animated object */
  54. class TBAnimationObject : public TBTypedObject, public TBLinkOf<TBAnimationObject>
  55. {
  56. public:
  57. ANIMATION_CURVE animation_curve;
  58. double animation_start_time;
  59. double animation_duration;
  60. bool adjust_start_time;
  61. public:
  62. // For safe typecasting
  63. TBOBJECT_SUBCLASS(TBAnimationObject, TBTypedObject);
  64. virtual ~TBAnimationObject() {}
  65. /** Return true if the object is currently animating. */
  66. bool IsAnimating() const { return linklist ? true : false; }
  67. /** Called on animation start */
  68. virtual void OnAnimationStart() = 0;
  69. /** Called on animation update. progress is current progress from 0 to 1.
  70. Note that it isn't called on start, so progress 0 might not happen.
  71. It will be called with progress 1 before the animation is completed normally (not aborted) */
  72. virtual void OnAnimationUpdate(float progress) = 0;
  73. /** Called on animation stop. aborted is true if it was aborted before completion.
  74. Note that if a animation is started when it's already running, it will first
  75. be aborted and then started again. */
  76. virtual void OnAnimationStop(bool aborted) = 0;
  77. /** Add an listener to this animation object. */
  78. void AddListener(TBAnimationListener *listener) { m_listeners.AddLast(listener); }
  79. /** Remove an listener from this animation object. */
  80. void RemoveListener(TBAnimationListener *listener) { m_listeners.Remove(listener); }
  81. private:
  82. friend class TBAnimationManager;
  83. TBLinkListOf<TBAnimationListener> m_listeners;
  84. void InvokeOnAnimationStart();
  85. void InvokeOnAnimationUpdate(float progress);
  86. void InvokeOnAnimationStop(bool aborted);
  87. };
  88. /** TBAnimationManager - System class that manages all animated object */
  89. class TBAnimationManager
  90. {
  91. private:
  92. static TBLinkListOf<TBAnimationObject> animating_objects;
  93. public:
  94. /** Update all running animations. */
  95. static void Update();
  96. /** Return true if there is running animations. */
  97. static bool HasAnimationsRunning();
  98. static void StartAnimation(TBAnimationObject *obj,
  99. ANIMATION_CURVE animation_curve = ANIMATION_DEFAULT_CURVE,
  100. double animation_duration = ANIMATION_DEFAULT_DURATION,
  101. ANIMATION_TIME animation_time = ANIMATION_TIME_FIRST_UPDATE);
  102. /** Abort the animation. If delete_animation is true, the animation will be deleted in
  103. this call after running callbacks and listeners callbacks. In rare situations,
  104. you might want to keep the animation around and delete it later (or start it
  105. again). */
  106. static void AbortAnimation(TBAnimationObject *obj, bool delete_animation);
  107. /** Abort and delete all animations. */
  108. static void AbortAllAnimations();
  109. /** Return true if new animations are blocked. */
  110. static bool IsAnimationsBlocked();
  111. /** Begin a period of blocking new animations. End the period with EndBlockAnimations.
  112. If StartAnimation is called during the blocked period, the animation object will
  113. finish the next animation update as it completed normally. */
  114. static void BeginBlockAnimations();
  115. /** End a period of blocking new animations that was started with BeginBlockAnimations. */
  116. static void EndBlockAnimations();
  117. };
  118. /** TBAnimationBlocker blocks new animations during its lifetime.
  119. It's convenient to put on the stack to block new animations
  120. within a scope of code. */
  121. class TBAnimationBlocker
  122. {
  123. public:
  124. TBAnimationBlocker() { TBAnimationManager::BeginBlockAnimations(); }
  125. ~TBAnimationBlocker() { TBAnimationManager::EndBlockAnimations(); }
  126. };
  127. }; // namespace tb
  128. #endif // TB_ANIMATION_H