FrameData.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System.Collections.Generic;
  2. namespace DragonBones
  3. {
  4. /**
  5. * @private
  6. */
  7. public abstract class FrameData<T> : BaseObject where T : FrameData<T>
  8. {
  9. public float position;
  10. public float duration;
  11. public T prev;
  12. public T next;
  13. public FrameData()
  14. {
  15. }
  16. protected override void _onClear()
  17. {
  18. position = 0.0f;
  19. duration = 0.0f;
  20. prev = null;
  21. next = null;
  22. }
  23. }
  24. /**
  25. * @private
  26. */
  27. public abstract class TweenFrameData<T> : FrameData<T> where T : TweenFrameData<T>
  28. {
  29. private static void _getCurvePoint(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float t, Point result)
  30. {
  31. var l_t = 1 - t;
  32. var powA = l_t * l_t;
  33. var powB = t * t;
  34. var kA = l_t * powA;
  35. var kB = 3.0f * t * powA;
  36. var kC = 3.0f * l_t * powB;
  37. var kD = t * powB;
  38. result.x = kA * x1 + kB * x2 + kC * x3 + kD * x4;
  39. result.y = kA * y1 + kB * y2 + kC * y3 + kD * y4;
  40. }
  41. public static void SamplingEasingCurve(float[] curve, float[] samples)
  42. {
  43. var curveCount = curve.Length;
  44. var result = new Point();
  45. var stepIndex = -2;
  46. for (int i = 0, l = samples.Length; i < l; ++i) {
  47. var t = (float)(i + 1) / (l + 1);
  48. while ((stepIndex + 6 < curveCount ? curve[stepIndex + 6] : 1) < t) // stepIndex + 3 * 2
  49. {
  50. stepIndex += 6;
  51. }
  52. var isInCurve = stepIndex >= 0 && stepIndex + 6 < curveCount;
  53. var x1 = isInCurve ? curve[stepIndex] : 0.0f;
  54. var y1 = isInCurve ? curve[stepIndex + 1] : 0.0f;
  55. var x2 = curve[stepIndex + 2];
  56. var y2 = curve[stepIndex + 3];
  57. var x3 = curve[stepIndex + 4];
  58. var y3 = curve[stepIndex + 5];
  59. var x4 = isInCurve ? curve[stepIndex + 6] : 1.0f;
  60. var y4 = isInCurve ? curve[stepIndex + 7] : 1.0f;
  61. var lower = 0.0f;
  62. var higher = 1.0f;
  63. while (higher - lower > 0.01f)
  64. {
  65. var percentage = (higher + lower) / 2.0f;
  66. _getCurvePoint(x1, y1, x2, y2, x3, y3, x4, y4, percentage, result);
  67. if (t - result.x > 0.00f)
  68. {
  69. lower = percentage;
  70. }
  71. else
  72. {
  73. higher = percentage;
  74. }
  75. }
  76. samples[i] = result.y;
  77. }
  78. }
  79. public float tweenEasing;
  80. public float[] curve;
  81. public TweenFrameData()
  82. {
  83. }
  84. protected override void _onClear()
  85. {
  86. base._onClear();
  87. tweenEasing = 0.0f;
  88. curve = null;
  89. }
  90. }
  91. /**
  92. * @private
  93. */
  94. public class AnimationFrameData : FrameData<AnimationFrameData>
  95. {
  96. public readonly List<ActionData> actions = new List<ActionData>();
  97. public readonly List<EventData> events = new List<EventData>();
  98. public AnimationFrameData()
  99. {
  100. }
  101. protected override void _onClear()
  102. {
  103. base._onClear();
  104. foreach (var action in actions)
  105. {
  106. action.ReturnToPool();
  107. }
  108. foreach (var evt in events)
  109. {
  110. evt.ReturnToPool();
  111. }
  112. actions.Clear();
  113. events.Clear();
  114. }
  115. }
  116. /**
  117. * @private
  118. */
  119. public class ZOrderFrameData : FrameData<ZOrderFrameData>
  120. {
  121. public List<int> zOrder = new List<int>();
  122. public ZOrderFrameData()
  123. {
  124. }
  125. protected override void _onClear()
  126. {
  127. base._onClear();
  128. zOrder.Clear();
  129. }
  130. }
  131. /**
  132. * @private
  133. */
  134. public class BoneFrameData : TweenFrameData<BoneFrameData>
  135. {
  136. public bool tweenScale;
  137. public float tweenRotate;
  138. public readonly Transform transform = new Transform();
  139. public BoneFrameData()
  140. {
  141. }
  142. protected override void _onClear()
  143. {
  144. base._onClear();
  145. tweenScale = false;
  146. tweenRotate = 0.0f;
  147. transform.Identity();
  148. }
  149. }
  150. /**
  151. * @private
  152. */
  153. public class SlotFrameData : TweenFrameData<SlotFrameData>
  154. {
  155. public static readonly ColorTransform DEFAULT_COLOR = new ColorTransform();
  156. public static ColorTransform GenerateColor()
  157. {
  158. return new ColorTransform();
  159. }
  160. public int displayIndex;
  161. public ColorTransform color;
  162. public SlotFrameData()
  163. {
  164. }
  165. protected override void _onClear()
  166. {
  167. base._onClear();
  168. displayIndex = 0;
  169. color = null;
  170. }
  171. }
  172. /**
  173. * @private
  174. */
  175. public class ExtensionFrameData : TweenFrameData<ExtensionFrameData>
  176. {
  177. public readonly List<float> tweens = new List<float>();
  178. public ExtensionFrameData()
  179. {
  180. }
  181. protected override void _onClear()
  182. {
  183. base._onClear();
  184. tweens.Clear();
  185. }
  186. }
  187. }