Anim.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Anim.h"
  7. #include <math.h>
  8. #include <cmath>
  9. #include "Gwen/Utility.h"
  10. using namespace Gwen;
  11. #ifndef GWEN_NO_ANIMATION
  12. static Gwen::Anim::Animation::List g_Animations;
  13. static Gwen::Anim::Animation::ChildList g_AnimationsListed;
  14. void Gwen::Anim::Add(Gwen::Controls::Base* control, Animation* animation)
  15. {
  16. animation->m_Control = control;
  17. g_Animations[control].push_back(animation);
  18. }
  19. void Gwen::Anim::Cancel(Gwen::Controls::Base* control)
  20. {
  21. /* cannot use std::list iterator with algoryhtmns based on pointers
  22. struct AnimDeletePredicate
  23. {
  24. AnimDeletePredicate( Gwen::Controls::Base* control )
  25. {
  26. this->control = control;
  27. }
  28. bool operator() ( Gwen::Anim::Animation* anim )
  29. {
  30. return anim->m_Control == control;
  31. }
  32. Gwen::Controls::Base* control;
  33. };
  34. std::remove_if ( g_Animations.begin(), g_Animations.end(), AnimDeletePredicate( control ) );
  35. */
  36. Gwen::Anim::Animation::List::iterator iAnimations;
  37. if ((iAnimations = g_Animations.find(control)) != g_Animations.end())
  38. {
  39. Gwen::Anim::Animation::ChildList& ChildAnimationsForControl = iAnimations->second;
  40. Gwen::Anim::Animation::ChildList::iterator iAnimationChild = ChildAnimationsForControl.begin();
  41. if (iAnimationChild != ChildAnimationsForControl.end())
  42. {
  43. do
  44. {
  45. delete (*iAnimationChild);
  46. } while (++iAnimationChild != ChildAnimationsForControl.end());
  47. }
  48. g_Animations.erase(iAnimations);
  49. }
  50. }
  51. void Gwen::Anim::Think()
  52. {
  53. Gwen::Anim::Animation::List::iterator it = g_Animations.begin();
  54. if (it != g_Animations.end())
  55. {
  56. Gwen::Anim::Animation::ChildList::iterator itChild;
  57. Gwen::Anim::Animation* anim;
  58. do
  59. {
  60. if ((itChild = it->second.begin()) != it->second.end())
  61. {
  62. do
  63. {
  64. anim = *itChild;
  65. anim->Think();
  66. if (anim->Finished())
  67. {
  68. itChild = it->second.erase(itChild);
  69. delete anim;
  70. }
  71. else
  72. {
  73. ++itChild;
  74. }
  75. } while (itChild != it->second.end());
  76. }
  77. } while (++it != g_Animations.end());
  78. }
  79. }
  80. Gwen::Anim::TimedAnimation::TimedAnimation(float fLength, float fDelay, float fEase)
  81. {
  82. m_fStart = Platform::GetTimeInSeconds() + fDelay;
  83. m_fEnd = m_fStart + fLength;
  84. m_fEase = fEase;
  85. m_bStarted = false;
  86. m_bFinished = false;
  87. }
  88. void Gwen::Anim::TimedAnimation::Think()
  89. {
  90. if (m_bFinished) return;
  91. float fCurrent = Platform::GetTimeInSeconds();
  92. float fSecondsIn = fCurrent - m_fStart;
  93. if (fSecondsIn < 0.0f) return;
  94. if (!m_bStarted)
  95. {
  96. m_bStarted = true;
  97. OnStart();
  98. }
  99. float fDelta = fSecondsIn / (m_fEnd - m_fStart);
  100. if (fDelta < 0.0f) fDelta = 0.0f;
  101. if (fDelta > 1.0f) fDelta = 1.0f;
  102. Run(std::pow(fDelta, m_fEase));
  103. if (fDelta == 1.0f) {
  104. m_bFinished = true;
  105. OnFinish();
  106. }
  107. }
  108. bool Gwen::Anim::TimedAnimation::Finished()
  109. {
  110. return m_bFinished;
  111. }
  112. #endif