AnimationController.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright (c) Craftwork Games. All rights reserved.
  2. // Licensed under the MIT license.
  3. // See LICENSE file in the project root for full license information.
  4. using System;
  5. using Microsoft.Xna.Framework;
  6. namespace MonoGame.Extended.Animations;
  7. /// <summary>
  8. /// Represents an animation controller with features to play, pause, stop, reset, and set the state of
  9. /// animation playback such as looping, reversing, and ping-pong effects.
  10. /// </summary>
  11. public class AnimationController : IAnimationController
  12. {
  13. private readonly IAnimation _definition;
  14. private int _direction;
  15. private int _internalFrame;
  16. /// <summary>
  17. /// Gets a value that indicates whether this animation has been disposed of.
  18. /// </summary>
  19. public bool IsDisposed { get; protected set; }
  20. /// <inheritdoc />
  21. public bool IsPaused { get; private set; }
  22. /// <inheritdoc />
  23. public bool IsAnimating { get; private set; }
  24. /// <inheritdoc />
  25. public bool IsLooping { get; set; }
  26. /// <inheritdoc />
  27. public bool IsReversed
  28. {
  29. get => _direction == -1;
  30. set => _direction = value ? -1 : 1;
  31. }
  32. /// <inheritdoc />
  33. public bool IsPingPong { get; set; }
  34. /// <inheritdoc />
  35. public double Speed { get; set; }
  36. /// <inheritdoc />
  37. public event Action<IAnimationController, AnimationEventTrigger> OnAnimationEvent;
  38. /// <inheritdoc />
  39. public TimeSpan CurrentFrameTimeRemaining { get; private set; }
  40. /// <inheritdoc />
  41. public int CurrentFrame => _definition.Frames[_internalFrame].FrameIndex;
  42. /// <inheritdoc />
  43. public int FrameCount => _definition.FrameCount;
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="AnimationController"/> class with the specified definition.
  46. /// </summary>
  47. /// <param name="definition">The definition of the animation.</param>
  48. public AnimationController(IAnimation definition)
  49. {
  50. _definition = definition;
  51. // Set initial properties but keep original values in the definition cached for Reset()
  52. IsLooping = definition.IsLooping;
  53. IsReversed = definition.IsReversed;
  54. IsPingPong = definition.IsPingPong;
  55. Speed = 1.0f;
  56. // Start off playing
  57. Play();
  58. }
  59. /// <inheritdoc />
  60. public bool Pause() => Pause(false);
  61. /// <inheritdoc />
  62. public bool Pause(bool resetFrameDuration)
  63. {
  64. // We can only pause something that is animating and not already paused. This is to prevent situations
  65. // that could accidentally reset frame duration if it was set to true.
  66. if (!IsAnimating || IsPaused)
  67. {
  68. return false;
  69. }
  70. IsPaused = true;
  71. if (resetFrameDuration)
  72. {
  73. CurrentFrameTimeRemaining = _definition.Frames[_internalFrame].Duration;
  74. }
  75. return true;
  76. }
  77. /// <inheritdoc />
  78. public bool Play() => Play(0);
  79. /// <inheritdoc />
  80. public bool Play(int startingFrame)
  81. {
  82. if (startingFrame < 0 || startingFrame >= _definition.FrameCount)
  83. {
  84. throw new ArgumentOutOfRangeException(nameof(startingFrame), $"{nameof(startingFrame)} cannot be less than zero or greater than or equal to the total number of frames in this {nameof(AnimationController)}");
  85. }
  86. // Cannot play something that is already playing
  87. if (IsAnimating)
  88. {
  89. return false;
  90. }
  91. IsAnimating = true;
  92. _internalFrame = startingFrame;
  93. CurrentFrameTimeRemaining = _definition.Frames[_internalFrame].Duration;
  94. return true;
  95. }
  96. /// <inheritdoc />
  97. public void Reset()
  98. {
  99. IsReversed = _definition.IsReversed;
  100. IsPingPong = _definition.IsPingPong;
  101. IsLooping = _definition.IsLooping;
  102. IsAnimating = false;
  103. IsPaused = true;
  104. Speed = 1.0d;
  105. _internalFrame = IsReversed ? _definition.FrameCount - 1 : 0;
  106. CurrentFrameTimeRemaining = _definition.Frames[_internalFrame].Duration;
  107. }
  108. /// <inheritdoc />
  109. public void SetFrame(int index)
  110. {
  111. if (index < 0 || index >= _definition.FrameCount)
  112. {
  113. throw new ArgumentOutOfRangeException(nameof(index), $"{nameof(index)} cannot be less than zero or greater than or equal to the total number of frames in this {nameof(AnimationController)}");
  114. }
  115. _internalFrame = index;
  116. CurrentFrameTimeRemaining = _definition.Frames[_internalFrame].Duration;
  117. OnAnimationEvent?.Invoke(this, AnimationEventTrigger.FrameBegin);
  118. }
  119. /// <inheritdoc />
  120. public bool Stop() => Stop(AnimationEventTrigger.AnimationStopped);
  121. private bool Stop(AnimationEventTrigger trigger)
  122. {
  123. // We can't stop something that's not animating. This is to prevent accidentally invoking OnAnimationEnd
  124. if (!IsAnimating)
  125. {
  126. return false;
  127. }
  128. IsAnimating = false;
  129. IsPaused = true;
  130. OnAnimationEvent?.Invoke(this, trigger);
  131. return true;
  132. }
  133. /// <inheritdoc />
  134. public bool Unpause() => Unpause(false);
  135. /// <inheritdoc />
  136. public bool Unpause(bool advanceToNextFrame)
  137. {
  138. // We can't unpause something that's not animating and also isn't paused. This is to prevent improper usage
  139. // that could accidentally advance to the next frame if it was set to true.
  140. if (!IsAnimating || !IsPaused)
  141. {
  142. return false;
  143. }
  144. IsPaused = false;
  145. if (advanceToNextFrame)
  146. {
  147. _ = AdvanceFrame();
  148. }
  149. return true;
  150. }
  151. /// <inheritdoc />
  152. public void Update(GameTime gameTime)
  153. {
  154. Update(gameTime.ElapsedGameTime);
  155. }
  156. /// <inheritdoc />
  157. public void Update(TimeSpan elapsedTime)
  158. {
  159. TimeSpan remainingTime = TimeSpan.Zero;
  160. if (!IsAnimating || IsPaused)
  161. {
  162. return;
  163. }
  164. CurrentFrameTimeRemaining -= elapsedTime * Speed;
  165. while (CurrentFrameTimeRemaining <= TimeSpan.Zero)
  166. {
  167. remainingTime += -CurrentFrameTimeRemaining;
  168. // End the current frame
  169. OnAnimationEvent?.Invoke(this, AnimationEventTrigger.FrameEnd);
  170. if (!AdvanceFrame())
  171. {
  172. break;
  173. }
  174. CurrentFrameTimeRemaining -= remainingTime;
  175. remainingTime = TimeSpan.Zero;
  176. }
  177. }
  178. private bool AdvanceFrame()
  179. {
  180. // Increment the current frame
  181. _internalFrame += _direction;
  182. // Ensure frame is in bounds
  183. if (_internalFrame < 0 || _internalFrame >= _definition.FrameCount)
  184. {
  185. // Is this a looping animation?
  186. if (IsLooping)
  187. {
  188. // Is this a standard loop or is it a ping pong?
  189. if (IsPingPong)
  190. {
  191. _direction = -_direction;
  192. _internalFrame += _direction * 2;
  193. }
  194. else
  195. {
  196. _internalFrame = IsReversed ? _definition.FrameCount - 1 : 0;
  197. }
  198. // We looped
  199. OnAnimationEvent?.Invoke(this, AnimationEventTrigger.AnimationLoop);
  200. }
  201. else
  202. {
  203. // No looping and we've reached the end, stop the animation
  204. _internalFrame -= _direction;
  205. Stop(AnimationEventTrigger.AnimationCompleted);
  206. return false;
  207. }
  208. }
  209. CurrentFrameTimeRemaining = _definition.Frames[_internalFrame].Duration;
  210. OnAnimationEvent?.Invoke(this, AnimationEventTrigger.FrameBegin);
  211. return true;
  212. }
  213. /// <inheritdoc />
  214. public void Dispose()
  215. {
  216. Dispose(true);
  217. GC.SuppressFinalize(this);
  218. }
  219. /// <inheritdoc cref="Dispose()"/>
  220. /// <remarks>
  221. /// <para>
  222. /// When overriding this method, check if <paramref name="disposing"/> is <see langword="true"/> or
  223. /// <see langword="false"/>. Only dispose of other managed resources when it is <see langword="true"/>.
  224. /// </para>
  225. /// <see href="https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/dispose-pattern#basic-dispose-pattern"/>
  226. /// </remarks>
  227. /// <param name="disposing">Indicates whether this was called from <see cref="Dispose()"/> or the finalizer.</param>
  228. protected virtual void Dispose(bool disposing)
  229. {
  230. if (IsDisposed)
  231. {
  232. return;
  233. }
  234. IsDisposed = true;
  235. }
  236. }