ModelAnimationPlayerBase.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ModelAnimationPlayerBase.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using Microsoft.Xna.Framework;
  12. namespace CustomModelAnimation
  13. {
  14. /// <summary>
  15. /// This class serves as a base class for various animation players. It contains
  16. /// common functionality to deal with a clip, playing it back at a speed,
  17. /// notifying clients of completion, etc.
  18. /// </summary>
  19. public abstract class ModelAnimationPlayerBase
  20. {
  21. // Clip currently being played
  22. ModelAnimationClip currentClipValue;
  23. // Current timeindex and keyframe in the clip
  24. TimeSpan currentTimeValue;
  25. int currentKeyframe;
  26. // Speed of playback
  27. float playbackRate = 1.0f;
  28. // The amount of time for which the animation will play.
  29. // TimeSpan.MaxValue will loop forever. TimeSpan.Zero will play once.
  30. TimeSpan duration = TimeSpan.MaxValue;
  31. // Amount of time elapsed while playing
  32. TimeSpan elapsedPlaybackTime = TimeSpan.Zero;
  33. // Whether or not playback is paused
  34. bool paused;
  35. /// <summary>
  36. /// Gets the clip currently being decoded.
  37. /// </summary>
  38. public ModelAnimationClip CurrentClip
  39. {
  40. get { return currentClipValue; }
  41. }
  42. /// <summary>
  43. /// Get/Set the current key frame index
  44. /// </summary>
  45. public int CurrentKeyFrame
  46. {
  47. get { return currentKeyframe; }
  48. set
  49. {
  50. IList<ModelKeyframe> keyframes = currentClipValue.Keyframes;
  51. TimeSpan time = keyframes[value].Time;
  52. CurrentTimeValue = time;
  53. }
  54. }
  55. /// <summary>
  56. /// Gets/set the current play position.
  57. /// </summary>
  58. public TimeSpan CurrentTimeValue
  59. {
  60. get { return currentTimeValue; }
  61. set
  62. {
  63. TimeSpan time = value;
  64. // If the position moved backwards, reset the keyframe index.
  65. if (time < currentTimeValue)
  66. {
  67. currentKeyframe = 0;
  68. InitClip();
  69. }
  70. currentTimeValue = time;
  71. // Read keyframe matrices.
  72. IList<ModelKeyframe> keyframes = currentClipValue.Keyframes;
  73. while (currentKeyframe < keyframes.Count)
  74. {
  75. ModelKeyframe keyframe = keyframes[currentKeyframe];
  76. // Stop when we've read up to the current time position.
  77. if (keyframe.Time > currentTimeValue)
  78. break;
  79. // Use this keyframe
  80. SetKeyframe(keyframe);
  81. currentKeyframe++;
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// Invoked when playback has completed.
  87. /// </summary>
  88. public event EventHandler Completed;
  89. /// <summary>
  90. /// Starts decoding the specified animation clip.
  91. /// </summary>
  92. public void StartClip(ModelAnimationClip clip)
  93. {
  94. StartClip(clip, 1.0f, TimeSpan.MaxValue);
  95. }
  96. /// <summary>
  97. /// Starts playing a clip
  98. /// </summary>
  99. /// <param name="clip">Animation clip to play</param>
  100. /// <param name="playbackRate">Speed to playback</param>
  101. /// <param name="duration">Length of time to play (max is looping, 0 is once)</param>
  102. public void StartClip(ModelAnimationClip clip, float playbackRate, TimeSpan duration)
  103. {
  104. if (clip == null)
  105. throw new ArgumentNullException("Clip required");
  106. // Store the clip and reset playing data
  107. currentClipValue = clip;
  108. currentKeyframe = 0;
  109. CurrentTimeValue = TimeSpan.Zero;
  110. elapsedPlaybackTime = TimeSpan.Zero;
  111. paused = false;
  112. // Store the data about how we want to playback
  113. this.playbackRate = playbackRate;
  114. this.duration = duration;
  115. // Call the virtual to allow initialization of the clip
  116. InitClip();
  117. }
  118. /// <summary>
  119. /// Will pause the playback of the current clip
  120. /// </summary>
  121. public void PauseClip()
  122. {
  123. paused = true;
  124. }
  125. /// <summary>
  126. /// Will resume playback of the current clip
  127. /// </summary>
  128. public void ResumeClip()
  129. {
  130. paused = false;
  131. }
  132. /// <summary>
  133. /// Virtual method allowing subclasses to do any initialization of data when the clip is initialized.
  134. /// </summary>
  135. protected virtual void InitClip()
  136. {
  137. }
  138. /// <summary>
  139. /// Virtual method allowing subclasses to set any data associated with a particular keyframe.
  140. /// </summary>
  141. /// <param name="keyframe">Keyframe being set</param>
  142. protected virtual void SetKeyframe(ModelKeyframe keyframe)
  143. {
  144. }
  145. /// <summary>
  146. /// Virtual method allowing subclasses to perform data needed after the animation
  147. /// has been updated for a new time index.
  148. /// </summary>
  149. protected virtual void OnUpdate()
  150. {
  151. }
  152. /// <summary>
  153. /// Called during the update loop to move the animation forward
  154. /// </summary>
  155. /// <param name="gameTime"></param>
  156. public virtual void Update(GameTime gameTime)
  157. {
  158. if (currentClipValue == null)
  159. return;
  160. if (paused)
  161. return;
  162. TimeSpan time = gameTime.ElapsedGameTime;
  163. // Adjust for the rate
  164. if (playbackRate != 1.0f)
  165. time = TimeSpan.FromMilliseconds(time.TotalMilliseconds * playbackRate);
  166. elapsedPlaybackTime += time;
  167. // See if we should terminate
  168. if (elapsedPlaybackTime > duration && duration != TimeSpan.Zero ||
  169. elapsedPlaybackTime > currentClipValue.Duration && duration == TimeSpan.Zero)
  170. {
  171. if (Completed != null)
  172. Completed(this, EventArgs.Empty);
  173. currentClipValue = null;
  174. return;
  175. }
  176. // Update the animation position.
  177. time += currentTimeValue;
  178. // If we reached the end, loop back to the start.
  179. while (time >= currentClipValue.Duration)
  180. time -= currentClipValue.Duration;
  181. CurrentTimeValue = time;
  182. OnUpdate();
  183. }
  184. }
  185. }