UIFadeButton.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "StdAfx.h"
  2. #include "UIFadeButton.h"
  3. namespace DuiLib {
  4. IMPLEMENT_DUICONTROL(CFadeButtonUI)
  5. CFadeButtonUI::CFadeButtonUI(): m_bMouseHove( FALSE ), m_bMouseLeave( FALSE )
  6. {
  7. Attach(this);
  8. }
  9. CFadeButtonUI::~CFadeButtonUI()
  10. {
  11. StopAnimation();
  12. }
  13. LPCTSTR CFadeButtonUI::GetClass() const
  14. {
  15. return _T("FadeButtonUI");
  16. }
  17. LPVOID CFadeButtonUI::GetInterface(LPCTSTR pstrName)
  18. {
  19. if( _tcscmp(pstrName, _T("FadeButton")) == 0 )
  20. return static_cast<CFadeButtonUI*>(this);
  21. return CButtonUI::GetInterface(pstrName);
  22. }
  23. void CFadeButtonUI::SetNormalImage(LPCTSTR pStrImage)
  24. {
  25. m_sNormalImage = pStrImage;
  26. m_sLastImage = m_sNormalImage;
  27. }
  28. void CFadeButtonUI::DoEvent(TEventUI& event)
  29. {
  30. if( event.Type == UIEVENT_TIMER ) {
  31. OnTimer( event.wParam );
  32. }
  33. else if(IsEnabled()) {
  34. if( event.Type == UIEVENT_MOUSEENTER && !IsAnimationRunning( FADE_IN_ID ) )
  35. {
  36. m_bFadeAlpha = 0;
  37. m_bMouseHove = TRUE;
  38. StopAnimation(FADE_OUT_ID);
  39. StartAnimation(FADE_ELLAPSE, FADE_FRAME_COUNT, FADE_IN_ID);
  40. Invalidate();
  41. return;
  42. }
  43. else if( event.Type == UIEVENT_MOUSELEAVE && !IsAnimationRunning( FADE_OUT_ID ) )
  44. {
  45. m_bFadeAlpha = 0;
  46. m_bMouseLeave = TRUE;
  47. StopAnimation(FADE_IN_ID);
  48. StartAnimation(FADE_ELLAPSE, FADE_FRAME_COUNT, FADE_OUT_ID);
  49. Invalidate();
  50. return;
  51. }
  52. }
  53. CButtonUI::DoEvent( event );
  54. }
  55. void CFadeButtonUI::OnTimer( int nTimerID )
  56. {
  57. OnAnimationElapse( nTimerID );
  58. }
  59. void CFadeButtonUI::PaintStatusImage(HDC hDC)
  60. {
  61. if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
  62. else m_uButtonState &= ~ UISTATE_FOCUSED;
  63. if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
  64. else m_uButtonState &= ~ UISTATE_DISABLED;
  65. if( (m_uButtonState & UISTATE_DISABLED) != 0 ) {
  66. if( !m_sDisabledImage.IsEmpty() ) {
  67. if( !DrawImage(hDC, (LPCTSTR)m_sDisabledImage) ) {}
  68. else return;
  69. }
  70. }
  71. else if( (m_uButtonState & UISTATE_PUSHED) != 0 ) {
  72. if( !m_sPushedImage.IsEmpty() ) {
  73. if( !DrawImage(hDC, (LPCTSTR)m_sPushedImage) ) {}
  74. else return;
  75. }
  76. }
  77. else if( (m_uButtonState & UISTATE_FOCUSED) != 0 ) {
  78. if( !m_sFocusedImage.IsEmpty() ) {
  79. if( !DrawImage(hDC, (LPCTSTR)m_sFocusedImage) ) {}
  80. else return;
  81. }
  82. }
  83. if( !m_sNormalImage.IsEmpty() ) {
  84. if( m_bMouseHove ) {
  85. m_bMouseHove = FALSE;
  86. m_sLastImage = m_sHotImage;
  87. }
  88. if( m_bMouseLeave ) {
  89. m_bMouseLeave = FALSE;
  90. m_sLastImage = m_sNormalImage;
  91. }
  92. if( IsAnimationRunning(FADE_IN_ID) || IsAnimationRunning(FADE_OUT_ID)) {
  93. m_sOldImage = m_sNormalImage;
  94. m_sNewImage = m_sHotImage;
  95. if( IsAnimationRunning(FADE_OUT_ID) ) {
  96. m_sOldImage = m_sHotImage;
  97. m_sNewImage = m_sNormalImage;
  98. }
  99. CDuiString sFadeOut, sFadeIn;
  100. sFadeOut.Format(_T("fade='%d'"), 255 - m_bFadeAlpha);
  101. sFadeIn.Format(_T("fade='%d'"), m_bFadeAlpha);
  102. if( !DrawImage(hDC, (LPCTSTR)m_sOldImage, sFadeOut) ) {}
  103. if( !DrawImage(hDC, (LPCTSTR)m_sNewImage, sFadeIn) ) {}
  104. return;
  105. }
  106. else {
  107. if(m_sLastImage.IsEmpty()) m_sLastImage = m_sNormalImage;
  108. if( !DrawImage(hDC, (LPCTSTR)m_sLastImage) ) {}
  109. return;
  110. }
  111. }
  112. }
  113. void CFadeButtonUI::OnAnimationStep(INT nTotalFrame, INT nCurFrame, INT nAnimationID)
  114. {
  115. m_bFadeAlpha = (BYTE)((nCurFrame / (double)nTotalFrame) * 255);
  116. m_bFadeAlpha = m_bFadeAlpha == 0 ? 10 : m_bFadeAlpha;
  117. Invalidate();
  118. }
  119. } // namespace DuiLib