UIGifAnimEx.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "StdAfx.h"
  2. #ifdef USE_XIMAGE_EFFECT
  3. #include "UIGifAnimEx.h"
  4. #include "../../3rd/CxImage/ximage.h"
  5. //
  6. namespace DuiLib
  7. {
  8. #define GIFANIMUIEX_EVENT_TIEM_ID 100
  9. IMPLEMENT_DUICONTROL(CGifAnimExUI)
  10. struct CGifAnimExUI::Imp
  11. {
  12. bool m_bRealStop ;//外部停止了
  13. bool m_bLoadImg ;//是否加载过图片
  14. bool m_bTimer ;//是否启动定时器
  15. bool m_bAutoStart ;//是否自动开始
  16. int m_nDelay ;//循环毫秒数
  17. UINT m_nFrameCount ;//gif图片总帧数
  18. UINT m_nFramePosition ;//当前放到第几帧
  19. CxImage* m_pGifImage ;//gif对象
  20. CPaintManagerUI*& m_pManager ;
  21. CGifAnimExUI* m_pOwer ;//拥有者
  22. Imp(CPaintManagerUI* & pManager):m_pManager(pManager),
  23. m_bLoadImg(false),m_bTimer(false),
  24. m_nDelay(100),m_pGifImage(NULL),m_nFrameCount(0U),
  25. m_nFramePosition(0U),
  26. m_pOwer(NULL),m_bRealStop(false),m_bAutoStart(true)
  27. {
  28. }
  29. void SetOwer(CGifAnimExUI *pOwer)
  30. {
  31. m_pOwer = pOwer;
  32. }
  33. ~Imp()
  34. {
  35. if ( m_pGifImage != NULL )
  36. {
  37. delete m_pGifImage;
  38. m_pGifImage = NULL;
  39. }
  40. }
  41. inline void CheckLoadImage()
  42. {
  43. if(!m_bLoadImg)
  44. LoadGifImage();
  45. }
  46. inline bool IsLoadImage(){return m_bLoadImg;}
  47. virtual void LoadGifImage()
  48. {
  49. CDuiString sImag = m_pOwer->GetBkImage();
  50. m_bLoadImg = true;
  51. m_pGifImage = CRenderEngine::LoadGifImageX(STRINGorID(sImag),0, 0);
  52. if ( NULL == m_pGifImage ) return;
  53. m_nFrameCount = m_pGifImage->GetNumFrames();
  54. m_nFramePosition = 0;
  55. m_nDelay = m_pGifImage->GetFrameDelay();
  56. if (m_nDelay <= 0 )
  57. m_nDelay = 100;
  58. if(!m_bAutoStart)
  59. m_bRealStop = true;
  60. if(m_bAutoStart && m_pOwer->IsVisible())
  61. StartAnim();
  62. }
  63. void SetAutoStart(bool bAuto)
  64. {
  65. m_bAutoStart = bAuto;
  66. }
  67. void StartAnim()
  68. {
  69. if(!m_bTimer)
  70. {
  71. if(!IsLoadImage())
  72. {
  73. LoadGifImage();
  74. m_pOwer->Invalidate();
  75. }
  76. if(m_pGifImage)
  77. m_pManager->SetTimer( m_pOwer, GIFANIMUIEX_EVENT_TIEM_ID, m_nDelay );
  78. m_bTimer = true;
  79. }
  80. }
  81. void StopAnim(bool bGoFirstFrame)//bGoFirstFrame 是否跑到第一帧
  82. {
  83. if(m_bTimer)
  84. {
  85. if(bGoFirstFrame)
  86. {
  87. m_nFramePosition = 0U;
  88. m_pOwer->Invalidate();
  89. }
  90. m_pManager->KillTimer( m_pOwer, GIFANIMUIEX_EVENT_TIEM_ID );
  91. m_bTimer = false;
  92. }
  93. }
  94. void EventOnTimer(const WPARAM idEvent )
  95. {
  96. if ( idEvent != GIFANIMUIEX_EVENT_TIEM_ID )
  97. return;
  98. ++m_nFramePosition;
  99. if(m_nFramePosition >= m_nFrameCount)
  100. m_nFramePosition = 0;
  101. if(!m_pOwer->IsVisible())return;
  102. m_pOwer->Invalidate();
  103. }
  104. void DrawFrame( HDC hDC,const RECT& rcPaint,const RECT &rcItem)
  105. {
  106. if ( NULL == hDC || NULL == m_pGifImage ) return;
  107. if(m_pGifImage)
  108. {
  109. if (CxImage* pImage = m_pGifImage->GetFrame(m_nFramePosition))
  110. pImage->Draw2(hDC,rcItem);
  111. }
  112. }
  113. void EventSetVisible(bool bVisible)
  114. {
  115. if(bVisible)
  116. {
  117. if(!m_bRealStop)
  118. StartAnim();
  119. }
  120. else
  121. StopAnim(true);
  122. }
  123. };
  124. CGifAnimExUI::CGifAnimExUI(void):m_pImp(new CGifAnimExUI::Imp(m_pManager))
  125. {
  126. this;
  127. m_pImp->SetOwer(this);
  128. }
  129. CGifAnimExUI::~CGifAnimExUI(void)
  130. {
  131. m_pImp->StopAnim(false);
  132. delete m_pImp;
  133. m_pImp = nullptr;
  134. }
  135. LPCTSTR CGifAnimExUI::GetClass() const
  136. {
  137. return _T("GifAnimUI");
  138. }
  139. LPVOID CGifAnimExUI::GetInterface( LPCTSTR pstrName )
  140. {
  141. if( _tcscmp(pstrName, _T("GifAnim")) == 0 )
  142. return static_cast<CGifAnimExUI*>(this);
  143. return CLabelUI::GetInterface(pstrName);
  144. }
  145. void CGifAnimExUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  146. {
  147. if( _tcscmp(pstrName, _T("auto")) == 0 )
  148. m_pImp->SetAutoStart(_tcscmp(pstrValue, _T("true")) == 0);
  149. else
  150. __super::SetAttribute(pstrName, pstrValue);
  151. }
  152. void CGifAnimExUI::Init()
  153. {
  154. __super::Init();
  155. m_pImp->CheckLoadImage();
  156. }
  157. void CGifAnimExUI::SetVisible(bool bVisible /*= true*/)
  158. {
  159. __super::SetVisible(bVisible);
  160. m_pImp->EventSetVisible(bVisible);
  161. }
  162. void CGifAnimExUI::SetInternVisible(bool bVisible/* = true*/)
  163. {
  164. __super::SetInternVisible(bVisible);
  165. m_pImp->EventSetVisible(bVisible);
  166. }
  167. bool CGifAnimExUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  168. {
  169. if( !::IntersectRect( &m_rcPaint, &rcPaint, &m_rcItem ) ) return true;
  170. m_pImp->DrawFrame( hDC,rcPaint,m_rcItem);
  171. return true;
  172. }
  173. void CGifAnimExUI::DoEvent( TEventUI& event )
  174. {
  175. this;
  176. WPARAM nID = event.wParam;
  177. if( event.Type == UIEVENT_TIMER )
  178. m_pImp->EventOnTimer(nID);
  179. }
  180. void CGifAnimExUI::StartAnim()
  181. {
  182. m_pImp->m_bRealStop = false;
  183. m_pImp->StartAnim();
  184. }
  185. void CGifAnimExUI::StopAnim()
  186. {
  187. m_pImp->m_bRealStop = true;
  188. m_pImp->StopAnim(true);
  189. }
  190. }
  191. #endif//USE_XIMAGE_EFFECT