Anim.cpp 2.8 KB

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