Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.7 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: mtkanim.cxx
  3. *
  4. * Copyright (c) 1997 Microsoft Corporation
  5. *
  6. \**************************************************************************/
  7. #include "mtk.hxx"
  8. #include "mtkanim.hxx"
  9. /**************************************************************************\
  10. * MTKANIMATOR constructors
  11. *
  12. * The object needs to be attached to an hwnd, since it's required for timer
  13. * creation, and msg posting.
  14. \**************************************************************************/
  15. MTKANIMATOR::MTKANIMATOR()
  16. {
  17. hwnd = NULL;
  18. Init();
  19. }
  20. MTKANIMATOR::MTKANIMATOR( HWND hwndAttach )
  21. : hwnd( hwndAttach )
  22. {
  23. Init();
  24. }
  25. /**************************************************************************\
  26. *
  27. \**************************************************************************/
  28. void
  29. MTKANIMATOR::Init()
  30. {
  31. idTimer = 0;
  32. nFrames = 0;
  33. mode = MTK_ANIMATE_CONTINUOUS;
  34. msUpdateInterval = 0;
  35. AnimateFunc = NULL;
  36. }
  37. /**************************************************************************\
  38. * MTKANIMATOR destructor
  39. *
  40. \**************************************************************************/
  41. MTKANIMATOR::~MTKANIMATOR()
  42. {
  43. Stop();
  44. }
  45. /**************************************************************************\
  46. * SetFunc
  47. *
  48. * Set the animation function callback. This immediately replaces the previous
  49. * callback, and if a timer is currently running, then the new function gets
  50. * called. If the new callback is NULL, then the timer is stopped.
  51. *
  52. \**************************************************************************/
  53. void
  54. MTKANIMATOR::SetFunc( MTK_ANIMATEPROC Func )
  55. {
  56. AnimateFunc = Func;
  57. if( ! AnimateFunc ) {
  58. Stop();
  59. }
  60. }
  61. /**************************************************************************\
  62. * SetMode
  63. *
  64. \**************************************************************************/
  65. void
  66. MTKANIMATOR::SetMode( UINT newMode, float *fParam )
  67. {
  68. switch( newMode ) {
  69. case MTK_ANIMATE_CONTINUOUS :
  70. // First param is animation period ( 1/fps )
  71. {
  72. float fUpdateInterval = fParam[0];
  73. if( fUpdateInterval < 0.0f )
  74. SS_WARNING( "MTKANIMATOR::SetMode: bad update interval parameter\n" );
  75. fUpdateInterval = 0.0f;
  76. msUpdateInterval = (UINT) (fUpdateInterval * 1000.0f);
  77. }
  78. break;
  79. case MTK_ANIMATE_INTERVAL :
  80. // First param is number of frames,
  81. // Second param is total desired duration of the animation
  82. {
  83. int iFrameCount = (int) fParam[0];
  84. float fDuration = fParam[1];
  85. // Check parameters
  86. // fDuration = 0.0f is valid - it means go as fast as possible...
  87. if( (fDuration < 0.0f) || (iFrameCount <= 0) )
  88. SS_ERROR( "MTKANIMATOR::SetMode: bad parameter\n" );
  89. return;
  90. nFrames = iFrameCount;
  91. msUpdateInterval = (UINT) ( (fDuration / (float) nFrames) * 1000.0f);
  92. }
  93. break;
  94. default :
  95. return;
  96. }
  97. mode = newMode;
  98. }
  99. /**************************************************************************\
  100. * Start
  101. *
  102. * Start the animation by creating a new timer, if there is a valid animation
  103. * callback function.
  104. *
  105. \**************************************************************************/
  106. void
  107. MTKANIMATOR::Start( )
  108. {
  109. // Create new timer
  110. if( idTimer || !AnimateFunc )
  111. return; // Timer already running, or no animate func
  112. idTimer = SetTimer( hwnd, MTK_ANIMATE_TIMER_ID, msUpdateInterval, NULL );
  113. if( ! idTimer )
  114. SS_WARNING( "MTKANIMATOR::Start: SetTimer failure\n" );
  115. }
  116. /**************************************************************************\
  117. * Stop
  118. *
  119. * Stop the animation by killing the timer. Remove any WM_TIMER msgs
  120. *
  121. \**************************************************************************/
  122. void
  123. MTKANIMATOR::Stop( )
  124. {
  125. if( !idTimer )
  126. return;
  127. if( ! KillTimer( hwnd, MTK_ANIMATE_TIMER_ID ) )
  128. SS_WARNING( "MTKANIMATOR::Stop: KillTimer failure\n" );
  129. // remove any timer messages from queue
  130. MSG Msg;
  131. PeekMessage( &Msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE );
  132. idTimer = 0;
  133. }
  134. //mf: need better name for this
  135. /**************************************************************************\
  136. *
  137. * Call the animate function
  138. *
  139. * A return value of FALSE indicates that an interval based animation has
  140. * finished - TRUE otherwise.
  141. *
  142. \**************************************************************************/
  143. BOOL
  144. MTKANIMATOR::Draw( )
  145. {
  146. if( !AnimateFunc )
  147. return TRUE;
  148. (*AnimateFunc)();
  149. if( mode == MTK_ANIMATE_INTERVAL ) {
  150. if( --nFrames <= 0 )
  151. return FALSE;
  152. }
  153. return TRUE;
  154. }