UIAnimation.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "StdAfx.h"
  2. #include "UIAnimation.h"
  3. #include <vector>
  4. #include <algorithm>
  5. namespace DuiLib {
  6. struct CUIAnimation::Imp
  7. {
  8. std::vector<CAnimationData*> m_arAnimations;
  9. };
  10. CUIAnimation::CUIAnimation():m_pImp(new CUIAnimation::Imp())
  11. {
  12. m_pControl = NULL;
  13. }
  14. CUIAnimation:: ~CUIAnimation()
  15. {
  16. if(m_pImp)
  17. {
  18. delete m_pImp;
  19. m_pImp = NULL;
  20. }
  21. }
  22. void CUIAnimation::Attach(CControlUI* pOwner)
  23. {
  24. m_pControl = pOwner;
  25. }
  26. BOOL CUIAnimation::StartAnimation(int nElapse, int nTotalFrame, int nAnimationID /*= 0*/, BOOL bLoop/* = FALSE*/)
  27. {
  28. CAnimationData* pData = GetAnimationDataByID(nAnimationID);
  29. if( NULL != pData
  30. || nElapse <= 0
  31. || nTotalFrame <= 0
  32. || NULL == m_pControl )
  33. {
  34. ASSERT(FALSE);
  35. return FALSE;
  36. }
  37. CAnimationData* pAnimation = new CAnimationData(nElapse, nTotalFrame, nAnimationID, bLoop);
  38. if( NULL == pAnimation ) return FALSE;
  39. if(m_pControl->GetManager()->SetTimer( m_pControl, nAnimationID, nElapse ))
  40. {
  41. m_pImp->m_arAnimations.push_back(pAnimation);
  42. return TRUE;
  43. }
  44. return FALSE;
  45. }
  46. void CUIAnimation::StopAnimation(int nAnimationID /*= 0*/)
  47. {
  48. if(m_pControl == NULL) return;
  49. if(nAnimationID != 0)
  50. {
  51. CAnimationData* pData = GetAnimationDataByID(nAnimationID);
  52. if( NULL != pData )
  53. {
  54. m_pControl->GetManager()->KillTimer( m_pControl, nAnimationID );
  55. m_pImp->m_arAnimations.erase(std::remove(m_pImp->m_arAnimations.begin(), m_pImp->m_arAnimations.end(), pData), m_pImp->m_arAnimations.end());
  56. if(pData != NULL){
  57. delete pData;
  58. pData = NULL;
  59. }
  60. return;
  61. }
  62. }
  63. else
  64. {
  65. int nCount = m_pImp->m_arAnimations.size();
  66. for(int i=0; i<nCount; ++i)
  67. {
  68. CAnimationData* pData = m_pImp->m_arAnimations[i];
  69. if(pData) {
  70. m_pControl->GetManager()->KillTimer(m_pControl, pData->m_nAnimationID);
  71. if(pData != NULL){
  72. delete pData;
  73. pData = NULL;
  74. }
  75. }
  76. }
  77. m_pImp->m_arAnimations.clear();
  78. }
  79. }
  80. BOOL CUIAnimation::IsAnimationRunning(int nAnimationID)
  81. {
  82. CAnimationData* pData = GetAnimationDataByID(nAnimationID);
  83. return NULL != pData;
  84. }
  85. int CUIAnimation::GetCurrentFrame(int nAnimationID/* = 0*/)
  86. {
  87. CAnimationData* pData = GetAnimationDataByID(nAnimationID);
  88. if( NULL == pData )
  89. {
  90. ASSERT(FALSE);
  91. return -1;
  92. }
  93. return pData->m_nCurFrame;
  94. }
  95. BOOL CUIAnimation::SetCurrentFrame(int nFrame, int nAnimationID/* = 0*/)
  96. {
  97. CAnimationData* pData = GetAnimationDataByID(nAnimationID);
  98. if( NULL == pData)
  99. {
  100. ASSERT(FALSE);
  101. return FALSE;
  102. }
  103. if(nFrame >= 0 && nFrame <= pData->m_nTotalFrame)
  104. {
  105. pData->m_nCurFrame = nFrame;
  106. return TRUE;
  107. }
  108. else
  109. {
  110. ASSERT(FALSE);
  111. }
  112. return FALSE;
  113. }
  114. void CUIAnimation::OnAnimationElapse(int nAnimationID)
  115. {
  116. if(m_pControl == NULL) return;
  117. CAnimationData* pData = GetAnimationDataByID(nAnimationID);
  118. if( NULL == pData ) return;
  119. int nCurFrame = pData->m_nCurFrame;
  120. if(nCurFrame == 0)
  121. {
  122. OnAnimationStart(nAnimationID, pData->m_bFirstLoop);
  123. pData->m_bFirstLoop = FALSE;
  124. }
  125. OnAnimationStep(pData->m_nTotalFrame, nCurFrame, nAnimationID);
  126. if(nCurFrame >= pData->m_nTotalFrame)
  127. {
  128. OnAnimationStop(nAnimationID);
  129. if(pData->m_bLoop)
  130. {
  131. pData->m_nCurFrame = 0;
  132. }
  133. else
  134. {
  135. m_pControl->GetManager()->KillTimer( m_pControl, nAnimationID );
  136. m_pImp->m_arAnimations.erase(std::remove(m_pImp->m_arAnimations.begin(), m_pImp->m_arAnimations.end(), pData), m_pImp->m_arAnimations.end());
  137. delete pData;
  138. pData = NULL;
  139. }
  140. }
  141. if( NULL != pData )
  142. {
  143. ++(pData->m_nCurFrame);
  144. }
  145. }
  146. CAnimationData* CUIAnimation::GetAnimationDataByID(int nAnimationID)
  147. {
  148. CAnimationData* pRet = NULL;
  149. int nCount = m_pImp->m_arAnimations.size();
  150. for(int i=0; i<nCount; ++i)
  151. {
  152. if(m_pImp->m_arAnimations[i]->m_nAnimationID == nAnimationID)
  153. {
  154. pRet = m_pImp->m_arAnimations[i];
  155. break;
  156. }
  157. }
  158. return pRet;
  159. }
  160. } // namespace DuiLib