BaseTimelineState.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System;
  2. using System.Collections.Generic;
  3. namespace DragonBones
  4. {
  5. /**
  6. * @private
  7. */
  8. internal enum TweenType
  9. {
  10. None = 0,
  11. Once = 1,
  12. Always = 2
  13. }
  14. /**
  15. * @private
  16. */
  17. abstract public class TimelineState<T, M> : BaseObject
  18. where T : FrameData<T>
  19. where M : TimelineData<T>
  20. {
  21. internal int _playState; // -1 start 0 play 1 complete
  22. internal uint _currentPlayTimes;
  23. internal float _currentTime;
  24. internal M _timelineData;
  25. protected uint _frameRate;
  26. protected uint _keyFrameCount;
  27. protected uint _frameCount;
  28. protected float _position;
  29. protected float _duration;
  30. protected float _animationDutation;
  31. protected float _timeScale;
  32. protected float _timeOffset;
  33. protected T _currentFrame;
  34. protected Armature _armature;
  35. protected AnimationState _animationState;
  36. protected AnimationTimelineState _mainTimeline;
  37. public TimelineState()
  38. {
  39. }
  40. override protected void _onClear()
  41. {
  42. _playState = -1;
  43. _currentPlayTimes = 0;
  44. _currentTime = -1.0f;
  45. _timelineData = null;
  46. _frameRate = 0;
  47. _keyFrameCount = 0;
  48. _frameCount = 0;
  49. _position = 0.0f;
  50. _duration = 0.0f;
  51. _animationDutation = 0.0f;
  52. _timeScale = 1.0f;
  53. _timeOffset = 0.0f;
  54. _currentFrame = null;
  55. _armature = null;
  56. _animationState = null;
  57. _mainTimeline = null;
  58. }
  59. virtual protected void _onUpdateFrame() { }
  60. virtual protected void _onArriveAtFrame() { }
  61. protected bool _setCurrentTime(float passedTime)
  62. {
  63. var prevState = _playState;
  64. uint currentPlayTimes = 0;
  65. var currentTime = 0.0f;
  66. if (_mainTimeline != null && _keyFrameCount == 1)
  67. {
  68. _playState = _animationState._timeline._playState >= 0 ? 1 : -1;
  69. currentPlayTimes = 1;
  70. currentTime = _mainTimeline._currentTime;
  71. }
  72. else if (_mainTimeline == null || _timeScale != 1.0f || _timeOffset != 0.0f) // Scale and offset.
  73. {
  74. var playTimes = _animationState.playTimes;
  75. var totalTime = playTimes * _duration;
  76. passedTime *= _timeScale;
  77. if (_timeOffset != 0.0f)
  78. {
  79. passedTime += _timeOffset * _animationDutation;
  80. }
  81. if (playTimes > 0 && (passedTime >= totalTime || passedTime <= -totalTime))
  82. {
  83. if (_playState <= 0 && _animationState._playheadState == 3)
  84. {
  85. _playState = 1;
  86. }
  87. currentPlayTimes = playTimes;
  88. if (passedTime < 0.0f)
  89. {
  90. currentTime = 0.0f;
  91. }
  92. else
  93. {
  94. currentTime = _duration;
  95. }
  96. }
  97. else
  98. {
  99. if (_playState != 0 && _animationState._playheadState == 3)
  100. {
  101. _playState = 0;
  102. }
  103. if (passedTime < 0.0f)
  104. {
  105. passedTime = -passedTime;
  106. currentPlayTimes = (uint)(passedTime / _duration);
  107. currentTime = _duration - (passedTime % _duration);
  108. }
  109. else
  110. {
  111. currentPlayTimes = (uint)(passedTime / _duration);
  112. currentTime = passedTime % _duration;
  113. }
  114. currentTime += _position;
  115. }
  116. }
  117. else
  118. {
  119. _playState = _animationState._timeline._playState;
  120. currentPlayTimes = _animationState._timeline._currentPlayTimes;
  121. currentTime = _mainTimeline._currentTime;
  122. }
  123. if (_currentPlayTimes == currentPlayTimes && _currentTime == currentTime)
  124. {
  125. return false;
  126. }
  127. // Clear frame flag when timeline start or loopComplete.
  128. if (
  129. (prevState < 0 && _playState != prevState) ||
  130. (_playState <= 0 && _currentPlayTimes != currentPlayTimes)
  131. )
  132. {
  133. _currentFrame = null;
  134. }
  135. _currentPlayTimes = currentPlayTimes;
  136. _currentTime = currentTime;
  137. return true;
  138. }
  139. virtual public void _init(Armature armature, AnimationState animationState, M timelineData)
  140. {
  141. _armature = armature;
  142. _animationState = animationState;
  143. _timelineData = timelineData;
  144. _mainTimeline = _animationState._timeline;
  145. if (this == (object)_mainTimeline)
  146. {
  147. _mainTimeline = null;
  148. }
  149. _frameRate = _armature.armatureData.frameRate;
  150. _keyFrameCount = (uint)_timelineData.frames.Count;
  151. _frameCount = _animationState.animationData.frameCount;
  152. _position = _animationState._position;
  153. _duration = _animationState._duration;
  154. _animationDutation = _animationState.animationData.duration;
  155. _timeScale = _mainTimeline == null ? 1.0f : (1.0f / _timelineData.scale);
  156. _timeOffset = _mainTimeline == null ? 0.0f : _timelineData.offset;
  157. }
  158. virtual public void FadeOut() { }
  159. virtual public void Update(float passedTime)
  160. {
  161. if (_playState <= 0 && _setCurrentTime(passedTime))
  162. {
  163. var currentFrameIndex = _keyFrameCount > 1 ? (int)(_currentTime * _frameRate) : 0; // uint
  164. var currentFrame = _timelineData.frames[currentFrameIndex];
  165. if (_currentFrame != currentFrame)
  166. {
  167. _currentFrame = currentFrame;
  168. _onArriveAtFrame();
  169. }
  170. _onUpdateFrame();
  171. }
  172. }
  173. }
  174. /**
  175. * @private
  176. */
  177. abstract public class TweenTimelineState<T, M> : TimelineState<T, M>
  178. where T : TweenFrameData<T>
  179. where M : TimelineData<T>
  180. {
  181. internal static float _getEasingValue(float progress, float easing)
  182. {
  183. if (progress <= 0.0f)
  184. {
  185. return 0.0f;
  186. }
  187. else if (progress >= 1.0f)
  188. {
  189. return 1.0f;
  190. }
  191. var value = 1.0f;
  192. if (easing > 2.0f)
  193. {
  194. return progress;
  195. }
  196. else if (easing > 1.0f) // Ease in out.
  197. {
  198. value = 0.5f * (1.0f - (float)Math.Cos(progress * Math.PI));
  199. easing -= 1.0f;
  200. }
  201. else if (easing > 0.0f) // Ease out.
  202. {
  203. value = 1.0f - (float)Math.Pow(1.0f - progress, 2.0f);
  204. }
  205. else if (easing >= -1.0f) // Ease in.
  206. {
  207. easing *= -1.0f;
  208. value = (float)Math.Pow(progress, 2.0f);
  209. }
  210. else if (easing >= -2.0f) // Ease out in.
  211. {
  212. easing *= -1.0f;
  213. value = ((float)Math.Acos(1.0f - progress * 2.0f) / DragonBones.PI);
  214. easing -= 1.0f;
  215. }
  216. else
  217. {
  218. return progress;
  219. }
  220. return (value - progress) * easing + progress;
  221. }
  222. internal static float _getCurveEasingValue(float progress, float[] samples)
  223. {
  224. if (progress <= 0.0f)
  225. {
  226. return 0.0f;
  227. }
  228. else if (progress >= 1.0f)
  229. {
  230. return 1.0f;
  231. }
  232. var segmentCount = samples.Length + 1; // + 2 - 1
  233. var valueIndex = (int)(progress * segmentCount); // uint
  234. var fromValue = valueIndex == 0 ? 0.0f : samples[valueIndex - 1];
  235. var toValue = (valueIndex == segmentCount - 1) ? 1.0f : samples[valueIndex];
  236. return fromValue + (toValue - fromValue) * (progress * segmentCount - valueIndex);
  237. }
  238. protected float _tweenProgress;
  239. protected float _tweenEasing;
  240. protected float[] _curve;
  241. public TweenTimelineState()
  242. {
  243. }
  244. override protected void _onClear()
  245. {
  246. base._onClear();
  247. _tweenProgress = 0.0f;
  248. _tweenEasing = DragonBones.NO_TWEEN;
  249. _curve = null;
  250. }
  251. override protected void _onArriveAtFrame()
  252. {
  253. if (
  254. _keyFrameCount > 1 &&
  255. (
  256. _currentFrame.next != _timelineData.frames[0] ||
  257. _animationState.playTimes == 0 ||
  258. _animationState.currentPlayTimes < _animationState.playTimes - 1
  259. )
  260. )
  261. {
  262. _tweenEasing = _currentFrame.tweenEasing;
  263. _curve = _currentFrame.curve;
  264. }
  265. else
  266. {
  267. _tweenEasing = DragonBones.NO_TWEEN;
  268. _curve = null;
  269. }
  270. }
  271. override protected void _onUpdateFrame()
  272. {
  273. if (_tweenEasing != DragonBones.NO_TWEEN)
  274. {
  275. _tweenProgress = (_currentTime - _currentFrame.position) / _currentFrame.duration;
  276. if (_tweenEasing != 0.0f)
  277. {
  278. _tweenProgress = _getEasingValue(_tweenProgress, _tweenEasing);
  279. }
  280. }
  281. else if (_curve != null)
  282. {
  283. _tweenProgress = (_currentTime - _currentFrame.position) / _currentFrame.duration;
  284. _tweenProgress = _getCurveEasingValue(_tweenProgress, _curve);
  285. }
  286. else
  287. {
  288. _tweenProgress = 0.0f;
  289. }
  290. }
  291. }
  292. }