BaseTimelineState.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2012-2017 DragonBones team and other contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. using System;
  24. using System.Collections.Generic;
  25. namespace DragonBones
  26. {
  27. /// <internal/>
  28. /// <private/>
  29. internal enum TweenState
  30. {
  31. None,
  32. Once,
  33. Always
  34. }
  35. /// <internal/>
  36. /// <private/>
  37. internal abstract class TimelineState : BaseObject
  38. {
  39. public int playState; // -1: start, 0: play, 1: complete;
  40. public int currentPlayTimes;
  41. public float currentTime;
  42. protected TweenState _tweenState;
  43. protected uint _frameRate;
  44. protected int _frameValueOffset;
  45. protected uint _frameCount;
  46. protected uint _frameOffset;
  47. protected int _frameIndex;
  48. protected float _frameRateR;
  49. protected float _position;
  50. protected float _duration;
  51. protected float _timeScale;
  52. protected float _timeOffset;
  53. protected DragonBonesData _dragonBonesData;
  54. protected AnimationData _animationData;
  55. protected TimelineData _timelineData;
  56. protected Armature _armature;
  57. protected AnimationState _animationState;
  58. protected TimelineState _actionTimeline;
  59. protected short[] _frameArray;
  60. protected short[] _frameIntArray;
  61. protected float[] _frameFloatArray;
  62. protected ushort[] _timelineArray;
  63. protected List<uint> _frameIndices;
  64. protected override void _OnClear()
  65. {
  66. this.playState = -1;
  67. this.currentPlayTimes = -1;
  68. this.currentTime = -1.0f;
  69. this._tweenState = TweenState.None;
  70. this._frameRate = 0;
  71. this._frameValueOffset = 0;
  72. this._frameCount = 0;
  73. this._frameOffset = 0;
  74. this._frameIndex = -1;
  75. this._frameRateR = 0.0f;
  76. this._position = 0.0f;
  77. this._duration = 0.0f;
  78. this._timeScale = 1.0f;
  79. this._timeOffset = 0.0f;
  80. this._dragonBonesData = null; //
  81. this._animationData = null; //
  82. this._timelineData = null; //
  83. this._armature = null; //
  84. this._animationState = null; //
  85. this._actionTimeline = null; //
  86. this._frameArray = null; //
  87. this._frameIntArray = null; //
  88. this._frameFloatArray = null; //
  89. this._timelineArray = null; //
  90. this._frameIndices = null; //
  91. }
  92. protected abstract void _OnArriveAtFrame();
  93. protected abstract void _OnUpdateFrame();
  94. protected bool _SetCurrentTime(float passedTime)
  95. {
  96. var prevState = this.playState;
  97. var prevPlayTimes = this.currentPlayTimes;
  98. var prevTime = this.currentTime;
  99. if (this._actionTimeline != null && this._frameCount <= 1)
  100. {
  101. // No frame or only one frame.
  102. this.playState = this._actionTimeline.playState >= 0 ? 1 : -1;
  103. this.currentPlayTimes = 1;
  104. this.currentTime = this._actionTimeline.currentTime;
  105. }
  106. else if (this._actionTimeline == null || this._timeScale != 1.0f || this._timeOffset != 0.0f)
  107. {
  108. var playTimes = this._animationState.playTimes;
  109. var totalTime = playTimes * this._duration;
  110. passedTime *= this._timeScale;
  111. if (this._timeOffset != 0.0f)
  112. {
  113. passedTime += this._timeOffset * this._animationData.duration;
  114. }
  115. if (playTimes > 0 && (passedTime >= totalTime || passedTime <= -totalTime))
  116. {
  117. if (this.playState <= 0 && this._animationState._playheadState == 3)
  118. {
  119. this.playState = 1;
  120. }
  121. this.currentPlayTimes = playTimes;
  122. if (passedTime < 0.0f)
  123. {
  124. this.currentTime = 0.0f;
  125. }
  126. else
  127. {
  128. this.currentTime = this._duration + 0.000001f; // Precision problem
  129. }
  130. }
  131. else
  132. {
  133. if (this.playState != 0 && this._animationState._playheadState == 3)
  134. {
  135. this.playState = 0;
  136. }
  137. if (passedTime < 0.0f)
  138. {
  139. passedTime = -passedTime;
  140. this.currentPlayTimes = (int)(passedTime / this._duration);
  141. this.currentTime = this._duration - (passedTime % this._duration);
  142. }
  143. else
  144. {
  145. this.currentPlayTimes = (int)(passedTime / this._duration);
  146. this.currentTime = passedTime % this._duration;
  147. }
  148. }
  149. this.currentTime += this._position;
  150. }
  151. else
  152. {
  153. // Multi frames.
  154. this.playState = this._actionTimeline.playState;
  155. this.currentPlayTimes = this._actionTimeline.currentPlayTimes;
  156. this.currentTime = this._actionTimeline.currentTime;
  157. }
  158. if (this.currentPlayTimes == prevPlayTimes && this.currentTime == prevTime)
  159. {
  160. return false;
  161. }
  162. // Clear frame flag when timeline start or loopComplete.
  163. if ((prevState < 0 && this.playState != prevState) || (this.playState <= 0 && this.currentPlayTimes != prevPlayTimes))
  164. {
  165. this._frameIndex = -1;
  166. }
  167. return true;
  168. }
  169. public virtual void Init(Armature armature, AnimationState animationState, TimelineData timelineData)
  170. {
  171. this._armature = armature;
  172. this._animationState = animationState;
  173. this._timelineData = timelineData;
  174. this._actionTimeline = this._animationState._actionTimeline;
  175. if (this == this._actionTimeline)
  176. {
  177. this._actionTimeline = null; //
  178. }
  179. this._frameRate = this._armature.armatureData.frameRate;
  180. this._frameRateR = 1.0f / this._frameRate;
  181. this._position = this._animationState._position;
  182. this._duration = this._animationState._duration;
  183. this._dragonBonesData = this._armature.armatureData.parent;
  184. this._animationData = this._animationState._animationData;
  185. if (this._timelineData != null)
  186. {
  187. this._frameIntArray = this._dragonBonesData.frameIntArray;
  188. this._frameFloatArray = this._dragonBonesData.frameFloatArray;
  189. this._frameArray = this._dragonBonesData.frameArray;
  190. this._timelineArray = this._dragonBonesData.timelineArray;
  191. this._frameIndices = this._dragonBonesData.frameIndices;
  192. this._frameCount = this._timelineArray[this._timelineData.offset + (int)BinaryOffset.TimelineKeyFrameCount];
  193. this._frameValueOffset = this._timelineArray[this._timelineData.offset + (int)BinaryOffset.TimelineFrameValueOffset];
  194. var timelineScale = this._timelineArray[this._timelineData.offset + (int)BinaryOffset.TimelineScale];
  195. this._timeScale = 100.0f / (timelineScale == 0 ? 100.0f : timelineScale);
  196. this._timeOffset = this._timelineArray[this._timelineData.offset + (int)BinaryOffset.TimelineOffset] * 0.01f;
  197. }
  198. }
  199. public virtual void FadeOut()
  200. {
  201. }
  202. public virtual void Update(float passedTime)
  203. {
  204. if (this._SetCurrentTime(passedTime))
  205. {
  206. if (this._frameCount > 1)
  207. {
  208. int timelineFrameIndex = (int)Math.Floor(this.currentTime * this._frameRate); // uint
  209. var frameIndex = this._frameIndices[(int)(this._timelineData as TimelineData).frameIndicesOffset + timelineFrameIndex];
  210. if (this._frameIndex != frameIndex)
  211. {
  212. this._frameIndex = (int)frameIndex;
  213. this._frameOffset = this._animationData.frameOffset + this._timelineArray[(this._timelineData as TimelineData).offset + (int)BinaryOffset.TimelineFrameOffset + this._frameIndex];
  214. this._OnArriveAtFrame();
  215. }
  216. }
  217. else if (this._frameIndex < 0)
  218. {
  219. this._frameIndex = 0;
  220. if (this._timelineData != null)
  221. {
  222. // May be pose timeline.
  223. this._frameOffset = this._animationData.frameOffset + this._timelineArray[this._timelineData.offset + (int)BinaryOffset.TimelineFrameOffset];
  224. }
  225. this._OnArriveAtFrame();
  226. }
  227. if (this._tweenState != TweenState.None)
  228. {
  229. this._OnUpdateFrame();
  230. }
  231. }
  232. }
  233. }
  234. /// <internal/>
  235. /// <private/>
  236. internal abstract class TweenTimelineState : TimelineState
  237. {
  238. private static float _GetEasingValue(TweenType tweenType, float progress, float easing)
  239. {
  240. var value = progress;
  241. switch (tweenType)
  242. {
  243. case TweenType.QuadIn:
  244. value = (float)Math.Pow(progress, 2.0f);
  245. break;
  246. case TweenType.QuadOut:
  247. value = 1.0f - (float)Math.Pow(1.0f - progress, 2.0f);
  248. break;
  249. case TweenType.QuadInOut:
  250. value = 0.5f * (1.0f - (float)Math.Cos(progress * Math.PI));
  251. break;
  252. }
  253. return (value - progress) * easing + progress;
  254. }
  255. private static float _GetEasingCurveValue(float progress, short[] samples, int count, int offset)
  256. {
  257. if (progress <= 0.0f)
  258. {
  259. return 0.0f;
  260. }
  261. else if (progress >= 1.0f)
  262. {
  263. return 1.0f;
  264. }
  265. var segmentCount = count + 1; // + 2 - 1
  266. var valueIndex = (int)Math.Floor(progress * segmentCount);
  267. var fromValue = valueIndex == 0 ? 0.0f : samples[offset + valueIndex - 1];
  268. var toValue = (valueIndex == segmentCount - 1) ? 10000.0f : samples[offset + valueIndex];
  269. return (fromValue + (toValue - fromValue) * (progress * segmentCount - valueIndex)) * 0.0001f;
  270. }
  271. protected TweenType _tweenType;
  272. protected int _curveCount;
  273. protected float _framePosition;
  274. protected float _frameDurationR;
  275. protected float _tweenProgress;
  276. protected float _tweenEasing;
  277. protected override void _OnClear()
  278. {
  279. base._OnClear();
  280. this._tweenType = TweenType.None;
  281. this._curveCount = 0;
  282. this._framePosition = 0.0f;
  283. this._frameDurationR = 0.0f;
  284. this._tweenProgress = 0.0f;
  285. this._tweenEasing = 0.0f;
  286. }
  287. protected override void _OnArriveAtFrame()
  288. {
  289. if (this._frameCount > 1 &&
  290. (this._frameIndex != this._frameCount - 1 ||
  291. this._animationState.playTimes == 0 ||
  292. this._animationState.currentPlayTimes < this._animationState.playTimes - 1))
  293. {
  294. this._tweenType = (TweenType)this._frameArray[this._frameOffset + (int)BinaryOffset.FrameTweenType]; // TODO recode ture tween type.
  295. this._tweenState = this._tweenType == TweenType.None ? TweenState.Once : TweenState.Always;
  296. if (this._tweenType == TweenType.Curve)
  297. {
  298. this._curveCount = this._frameArray[this._frameOffset + (int)BinaryOffset.FrameTweenEasingOrCurveSampleCount];
  299. }
  300. else if (this._tweenType != TweenType.None && this._tweenType != TweenType.Line)
  301. {
  302. this._tweenEasing = this._frameArray[this._frameOffset + (int)BinaryOffset.FrameTweenEasingOrCurveSampleCount] * 0.01f;
  303. }
  304. this._framePosition = this._frameArray[this._frameOffset] * this._frameRateR;
  305. if (this._frameIndex == this._frameCount - 1)
  306. {
  307. this._frameDurationR = 1.0f / (this._animationData.duration - this._framePosition);
  308. }
  309. else
  310. {
  311. var nextFrameOffset = this._animationData.frameOffset + this._timelineArray[(this._timelineData as TimelineData).offset + (int)BinaryOffset.TimelineFrameOffset + this._frameIndex + 1];
  312. var frameDuration = this._frameArray[nextFrameOffset] * this._frameRateR - this._framePosition;
  313. if (frameDuration > 0.0f)
  314. {
  315. this._frameDurationR = 1.0f / frameDuration;
  316. }
  317. else
  318. {
  319. this._frameDurationR = 0.0f;
  320. }
  321. }
  322. }
  323. else
  324. {
  325. this._tweenState = TweenState.Once;
  326. }
  327. }
  328. protected override void _OnUpdateFrame()
  329. {
  330. if (this._tweenState == TweenState.Always)
  331. {
  332. this._tweenProgress = (this.currentTime - this._framePosition) * this._frameDurationR;
  333. if (this._tweenType == TweenType.Curve)
  334. {
  335. this._tweenProgress = TweenTimelineState._GetEasingCurveValue(this._tweenProgress, this._frameArray, this._curveCount, (int)this._frameOffset + (int)BinaryOffset.FrameCurveSamples);
  336. }
  337. else if (this._tweenType != TweenType.Line)
  338. {
  339. this._tweenProgress = TweenTimelineState._GetEasingValue(this._tweenType, this._tweenProgress, this._tweenEasing);
  340. }
  341. }
  342. else
  343. {
  344. this._tweenProgress = 0.0f;
  345. }
  346. }
  347. }
  348. /// <internal/>
  349. /// <private/>
  350. internal abstract class BoneTimelineState : TweenTimelineState
  351. {
  352. public Bone bone;
  353. public BonePose bonePose;
  354. protected override void _OnClear()
  355. {
  356. base._OnClear();
  357. this.bone = null; //
  358. this.bonePose = null; //
  359. }
  360. public void Blend(int state)
  361. {
  362. var blendWeight = this.bone._blendState.blendWeight;
  363. var animationPose = this.bone.animationPose;
  364. var result = this.bonePose.result;
  365. if (state == 2)
  366. {
  367. animationPose.x += result.x * blendWeight;
  368. animationPose.y += result.y * blendWeight;
  369. animationPose.rotation += result.rotation * blendWeight;
  370. animationPose.skew += result.skew * blendWeight;
  371. animationPose.scaleX += (result.scaleX - 1.0f) * blendWeight;
  372. animationPose.scaleY += (result.scaleY - 1.0f) * blendWeight;
  373. }
  374. else if (blendWeight != 1.0f)
  375. {
  376. animationPose.x = result.x * blendWeight;
  377. animationPose.y = result.y * blendWeight;
  378. animationPose.rotation = result.rotation * blendWeight;
  379. animationPose.skew = result.skew * blendWeight;
  380. animationPose.scaleX = (result.scaleX - 1.0f) * blendWeight + 1.0f;
  381. animationPose.scaleY = (result.scaleY - 1.0f) * blendWeight + 1.0f;
  382. }
  383. else
  384. {
  385. animationPose.x = result.x;
  386. animationPose.y = result.y;
  387. animationPose.rotation = result.rotation;
  388. animationPose.skew = result.skew;
  389. animationPose.scaleX = result.scaleX;
  390. animationPose.scaleY = result.scaleY;
  391. }
  392. if (this._animationState._fadeState != 0 || this._animationState._subFadeState != 0)
  393. {
  394. this.bone._transformDirty = true;
  395. }
  396. }
  397. }
  398. /// <internal/>
  399. /// <private/>
  400. internal abstract class SlotTimelineState : TweenTimelineState
  401. {
  402. public Slot slot;
  403. protected override void _OnClear()
  404. {
  405. base._OnClear();
  406. this.slot = null; //
  407. }
  408. }
  409. /// <internal/>
  410. /// <private/>
  411. internal abstract class ConstraintTimelineState : TweenTimelineState
  412. {
  413. public Constraint constraint;
  414. protected override void _OnClear()
  415. {
  416. base._OnClear();
  417. this.constraint = null; //
  418. }
  419. }
  420. }