tb_animation.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include "animation/tb_animation.h"
  6. #include "tb_system.h"
  7. namespace tb {
  8. // == Helpers =======================================================
  9. #define SMOOTHSTEP(x) ((x) * (x) * (3.0f - 2.0f * (x)))
  10. static float sc(float x)
  11. {
  12. float s = x < 0 ? -1.f : 1.f;
  13. x = ABS(x);
  14. if (x >= 1)
  15. return s;
  16. return s * (x < 0 ? x / 0.5f : (x / (1 + x * x)) / 0.5f);
  17. }
  18. static float SmoothCurve(float x, float a)
  19. {
  20. float r = a * x / (2 * a * x - a - x + 1);
  21. r = (r - 0.5f) * 2;
  22. return sc(r) * 0.5f + 0.5f;
  23. }
  24. // == TBAnimationObject ===============================================
  25. void TBAnimationObject::InvokeOnAnimationStart()
  26. {
  27. TBLinkListOf<TBAnimationListener>::Iterator li = m_listeners.IterateForward();
  28. OnAnimationStart();
  29. while (TBAnimationListener *listener = li.GetAndStep())
  30. listener->OnAnimationStart(this);
  31. }
  32. void TBAnimationObject::InvokeOnAnimationUpdate(float progress)
  33. {
  34. TBLinkListOf<TBAnimationListener>::Iterator li = m_listeners.IterateForward();
  35. OnAnimationUpdate(progress);
  36. while (TBAnimationListener *listener = li.GetAndStep())
  37. listener->OnAnimationUpdate(this, progress);
  38. }
  39. void TBAnimationObject::InvokeOnAnimationStop(bool aborted)
  40. {
  41. TBLinkListOf<TBAnimationListener>::Iterator li = m_listeners.IterateForward();
  42. OnAnimationStop(aborted);
  43. while (TBAnimationListener *listener = li.GetAndStep())
  44. listener->OnAnimationStop(this, aborted);
  45. }
  46. // == TBAnimationManager ==============================================
  47. TBLinkListOf<TBAnimationObject> TBAnimationManager::animating_objects;
  48. static int block_animations_counter = 0;
  49. //static
  50. void TBAnimationManager::AbortAllAnimations()
  51. {
  52. while (TBAnimationObject *obj = animating_objects.GetFirst())
  53. AbortAnimation(obj, true);
  54. }
  55. //static
  56. void TBAnimationManager::Update()
  57. {
  58. double time_now = TBSystem::GetTimeMS();
  59. TBLinkListOf<TBAnimationObject>::Iterator iter = animating_objects.IterateForward();
  60. while (TBAnimationObject *obj = iter.GetAndStep())
  61. {
  62. // Adjust the start time if it's the first update time for this object.
  63. if (obj->adjust_start_time)
  64. {
  65. obj->animation_start_time = time_now;
  66. obj->adjust_start_time = false;
  67. }
  68. // Calculate current progress
  69. // If animation_duration is 0, it should just complete immediately.
  70. float progress = 1.0f;
  71. if (obj->animation_duration != 0)
  72. {
  73. progress = (float)(time_now - obj->animation_start_time) / (float)obj->animation_duration;
  74. progress = MIN(progress, 1.0f);
  75. }
  76. // Apply animation curve
  77. float tmp;
  78. switch (obj->animation_curve)
  79. {
  80. case ANIMATION_CURVE_SLOW_DOWN:
  81. tmp = 1 - progress;
  82. progress = 1 - tmp * tmp * tmp;
  83. break;
  84. case ANIMATION_CURVE_SPEED_UP:
  85. progress = progress * progress * progress;
  86. break;
  87. case ANIMATION_CURVE_BEZIER:
  88. progress = SMOOTHSTEP(progress);
  89. break;
  90. case ANIMATION_CURVE_SMOOTH:
  91. progress = SmoothCurve(progress, 0.6f);
  92. break;
  93. default: // linear (progress is already linear)
  94. break;
  95. };
  96. // Update animation
  97. obj->InvokeOnAnimationUpdate(progress);
  98. // Remove completed animations
  99. if (progress == 1.0f)
  100. {
  101. animating_objects.Remove(obj);
  102. obj->InvokeOnAnimationStop(false);
  103. delete obj;
  104. }
  105. }
  106. }
  107. //static
  108. bool TBAnimationManager::HasAnimationsRunning()
  109. {
  110. return animating_objects.HasLinks();
  111. }
  112. //static
  113. void TBAnimationManager::StartAnimation(TBAnimationObject *obj, ANIMATION_CURVE animation_curve, double animation_duration, ANIMATION_TIME animation_time)
  114. {
  115. if (obj->IsAnimating())
  116. AbortAnimation(obj, false);
  117. if (IsAnimationsBlocked())
  118. animation_duration = 0;
  119. obj->adjust_start_time = (animation_time == ANIMATION_TIME_FIRST_UPDATE ? true : false);
  120. obj->animation_start_time = TBSystem::GetTimeMS();
  121. obj->animation_duration = MAX(animation_duration, 0.0);
  122. obj->animation_curve = animation_curve;
  123. animating_objects.AddLast(obj);
  124. obj->InvokeOnAnimationStart();
  125. }
  126. //static
  127. void TBAnimationManager::AbortAnimation(TBAnimationObject *obj, bool delete_animation)
  128. {
  129. if (obj->IsAnimating())
  130. {
  131. animating_objects.Remove(obj);
  132. obj->InvokeOnAnimationStop(true);
  133. if (delete_animation)
  134. delete obj;
  135. }
  136. }
  137. //static
  138. bool TBAnimationManager::IsAnimationsBlocked()
  139. {
  140. return block_animations_counter > 0;
  141. }
  142. //static
  143. void TBAnimationManager::BeginBlockAnimations()
  144. {
  145. block_animations_counter++;
  146. }
  147. //static
  148. void TBAnimationManager::EndBlockAnimations()
  149. {
  150. assert(block_animations_counter > 0);
  151. block_animations_counter--;
  152. }
  153. }; // namespace tb