KeyFramesStateMachine`1.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Data;
  4. namespace Avalonia.Animation.Keyframes
  5. {
  6. /// <summary>
  7. /// Provides statefulness for an iteration of a keyframe group animation.
  8. /// </summary>
  9. internal class KeyFramesStateMachine<T> : IObservable<object>, IDisposable
  10. {
  11. object _lastInterpValue;
  12. object _firstKFValue;
  13. private ulong _delayTotalFrameCount,
  14. _durationTotalFrameCount,
  15. _delayFrameCount,
  16. _durationFrameCount,
  17. _repeatCount,
  18. _currentIteration;
  19. private bool _isLooping,
  20. _isRepeating,
  21. _isReversed,
  22. _checkLoopAndRepeat,
  23. _gotFirstKFValue;
  24. private FillMode _fillMode;
  25. private PlaybackDirection _animationDirection;
  26. private KeyFramesStates _currentState, _savedState;
  27. private KeyFrames<T> _parent;
  28. private Animation _targetAnimation;
  29. private Animatable _targetControl;
  30. internal bool _unsubscribe = false;
  31. private IObserver<object> _targetObserver;
  32. private enum KeyFramesStates
  33. {
  34. INITIALIZE,
  35. DO_DELAY,
  36. DO_RUN,
  37. RUN_FORWARDS,
  38. RUN_BACKWARDS,
  39. RUN_APPLYVALUE,
  40. RUN_COMPLETE,
  41. PAUSE,
  42. STOP,
  43. DISPOSED
  44. }
  45. public void Initialize(Animation animation, Animatable control, KeyFrames<T> keyframes)
  46. {
  47. _parent = keyframes;
  48. _targetAnimation = animation;
  49. _targetControl = control;
  50. _delayTotalFrameCount = (ulong)(animation.Delay.Ticks / Timing.FrameTick.Ticks);
  51. _durationTotalFrameCount = (ulong)(animation.Duration.Ticks / Timing.FrameTick.Ticks);
  52. switch (animation.RepeatBehavior)
  53. {
  54. case RepeatBehavior.Loop:
  55. _isLooping = true;
  56. _checkLoopAndRepeat = true;
  57. break;
  58. case RepeatBehavior.Repeat:
  59. if (animation.RepeatCount != null)
  60. {
  61. if (animation.RepeatCount == 0)
  62. {
  63. throw new InvalidOperationException
  64. ($"RepeatCount should be greater than zero when RepeatBehavior is set to Repeat.");
  65. }
  66. _isRepeating = true;
  67. _checkLoopAndRepeat = true;
  68. _repeatCount = (ulong)animation.RepeatCount;
  69. }
  70. else
  71. {
  72. throw new InvalidOperationException
  73. ($"RepeatCount should be defined when RepeatBehavior is set to Repeat.");
  74. }
  75. break;
  76. }
  77. switch (animation.PlaybackDirection)
  78. {
  79. case PlaybackDirection.Reverse:
  80. case PlaybackDirection.AlternateReverse:
  81. _isReversed = true;
  82. break;
  83. default:
  84. _isReversed = false;
  85. break;
  86. }
  87. _animationDirection = _targetAnimation.PlaybackDirection;
  88. _fillMode = _targetAnimation.FillMode;
  89. if (_durationTotalFrameCount > 0)
  90. _currentState = KeyFramesStates.DO_DELAY;
  91. else
  92. _currentState = KeyFramesStates.DO_RUN;
  93. }
  94. public void Step(PlayState _playState, Func<double, T> Interpolator)
  95. {
  96. try
  97. {
  98. InternalStep(_playState, Interpolator);
  99. }
  100. catch (Exception e)
  101. {
  102. _targetObserver?.OnError(e);
  103. }
  104. }
  105. private void InternalStep(PlayState _playState, Func<double, T> Interpolator)
  106. {
  107. if (!_gotFirstKFValue)
  108. {
  109. _firstKFValue = _parent.First().Value;
  110. _gotFirstKFValue = true;
  111. }
  112. if (_currentState == KeyFramesStates.DISPOSED)
  113. throw new InvalidProgramException("This KeyFrames Animation is already disposed.");
  114. if (_playState == PlayState.Stop)
  115. _currentState = KeyFramesStates.STOP;
  116. // Save state and pause the machine
  117. if (_playState == PlayState.Pause && _currentState != KeyFramesStates.PAUSE)
  118. {
  119. _savedState = _currentState;
  120. _currentState = KeyFramesStates.PAUSE;
  121. }
  122. // Resume the previous state
  123. if (_playState != PlayState.Pause && _currentState == KeyFramesStates.PAUSE)
  124. _currentState = _savedState;
  125. double _tempDuration = 0d, _easedTime;
  126. checkstate:
  127. switch (_currentState)
  128. {
  129. case KeyFramesStates.DO_DELAY:
  130. if (_fillMode == FillMode.Backward
  131. || _fillMode == FillMode.Both)
  132. {
  133. if (_currentIteration == 0)
  134. {
  135. _targetObserver.OnNext(_firstKFValue);
  136. }
  137. else
  138. {
  139. _targetObserver.OnNext(_lastInterpValue);
  140. }
  141. }
  142. if (_delayFrameCount > _delayTotalFrameCount)
  143. {
  144. _currentState = KeyFramesStates.DO_RUN;
  145. goto checkstate;
  146. }
  147. _delayFrameCount++;
  148. break;
  149. case KeyFramesStates.DO_RUN:
  150. if (_isReversed)
  151. _currentState = KeyFramesStates.RUN_BACKWARDS;
  152. else
  153. _currentState = KeyFramesStates.RUN_FORWARDS;
  154. goto checkstate;
  155. case KeyFramesStates.RUN_FORWARDS:
  156. if (_durationFrameCount > _durationTotalFrameCount)
  157. {
  158. _currentState = KeyFramesStates.RUN_COMPLETE;
  159. goto checkstate;
  160. }
  161. _tempDuration = (double)_durationFrameCount / _durationTotalFrameCount;
  162. _currentState = KeyFramesStates.RUN_APPLYVALUE;
  163. goto checkstate;
  164. case KeyFramesStates.RUN_BACKWARDS:
  165. if (_durationFrameCount > _durationTotalFrameCount)
  166. {
  167. _currentState = KeyFramesStates.RUN_COMPLETE;
  168. goto checkstate;
  169. }
  170. _tempDuration = (double)(_durationTotalFrameCount - _durationFrameCount) / _durationTotalFrameCount;
  171. _currentState = KeyFramesStates.RUN_APPLYVALUE;
  172. goto checkstate;
  173. case KeyFramesStates.RUN_APPLYVALUE:
  174. _easedTime = _targetAnimation.Easing.Ease(_tempDuration);
  175. _durationFrameCount++;
  176. _lastInterpValue = Interpolator(_easedTime);
  177. _targetObserver.OnNext(_lastInterpValue);
  178. _currentState = KeyFramesStates.DO_RUN;
  179. break;
  180. case KeyFramesStates.RUN_COMPLETE:
  181. if (_checkLoopAndRepeat)
  182. {
  183. _delayFrameCount = 0;
  184. _durationFrameCount = 0;
  185. if (_isLooping)
  186. {
  187. _currentState = KeyFramesStates.DO_RUN;
  188. }
  189. else if (_isRepeating)
  190. {
  191. if (_currentIteration >= _repeatCount)
  192. {
  193. _currentState = KeyFramesStates.STOP;
  194. }
  195. else
  196. {
  197. _currentState = KeyFramesStates.DO_RUN;
  198. }
  199. _currentIteration++;
  200. }
  201. if (_animationDirection == PlaybackDirection.Alternate
  202. || _animationDirection == PlaybackDirection.AlternateReverse)
  203. _isReversed = !_isReversed;
  204. break;
  205. }
  206. _currentState = KeyFramesStates.STOP;
  207. goto checkstate;
  208. case KeyFramesStates.STOP:
  209. if (_fillMode == FillMode.Forward
  210. || _fillMode == FillMode.Both)
  211. {
  212. _targetControl.SetValue(_parent.Property, _lastInterpValue, BindingPriority.Animation);
  213. }
  214. _targetObserver.OnCompleted();
  215. break;
  216. }
  217. }
  218. public IDisposable Subscribe(IObserver<object> observer)
  219. {
  220. _targetObserver = observer;
  221. return this;
  222. }
  223. public void Dispose()
  224. {
  225. _unsubscribe = true;
  226. _currentState = KeyFramesStates.DISPOSED;
  227. }
  228. }
  229. }