AnimationData.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. namespace DragonBones
  4. {
  5. /**
  6. * @language zh_CN
  7. * 动画数据。
  8. * @version DragonBones 3.0
  9. */
  10. public class AnimationData : TimelineData<AnimationFrameData>
  11. {
  12. /**
  13. * @language zh_CN
  14. * 持续的帧数。
  15. * @version DragonBones 3.0
  16. */
  17. public uint frameCount;
  18. /**
  19. * @language zh_CN
  20. * 播放次数。 [0: 无限循环播放, [1~N]: 循环播放 N 次]
  21. * @version DragonBones 3.0
  22. */
  23. public uint playTimes;
  24. /**
  25. * @language zh_CN
  26. * 持续时间。 (以秒为单位)
  27. * @version DragonBones 3.0
  28. */
  29. public float duration;
  30. /**
  31. * @language zh_CN
  32. * 淡入时间。 (以秒为单位)
  33. * @version DragonBones 3.0
  34. */
  35. public float fadeInTime;
  36. /**
  37. * @private
  38. */
  39. public float cacheFrameRate;
  40. /**
  41. * @language zh_CN
  42. * 数据名称。
  43. * @version DragonBones 3.0
  44. */
  45. public string name;
  46. /**
  47. * @private
  48. */
  49. public readonly Dictionary<string, BoneTimelineData> boneTimelines = new Dictionary<string, BoneTimelineData>();
  50. /**
  51. * @private
  52. */
  53. public readonly Dictionary<string, SlotTimelineData> slotTimelines = new Dictionary<string, SlotTimelineData>();
  54. /**
  55. * @private
  56. */
  57. public readonly Dictionary<string, Dictionary<string, Dictionary<string, FFDTimelineData>>> ffdTimelines = new Dictionary<string, Dictionary<string, Dictionary<string, FFDTimelineData>>>(); // skin slot mesh
  58. /**
  59. * @private
  60. */
  61. public readonly List<bool> cachedFrames = new List<bool>();
  62. /**
  63. * @private
  64. */
  65. public readonly Dictionary<string, List<int>> boneCachedFrameIndices = new Dictionary<string, List<int>>();
  66. /**
  67. * @private
  68. */
  69. public readonly Dictionary<string, List<int>> slotCachedFrameIndices = new Dictionary<string, List<int>>();
  70. /**
  71. * @private
  72. */
  73. public ZOrderTimelineData zOrderTimeline;
  74. /**
  75. * @private
  76. */
  77. public AnimationData()
  78. {
  79. }
  80. /**
  81. * @private
  82. */
  83. protected override void _onClear()
  84. {
  85. base._onClear();
  86. foreach (var pair in boneTimelines)
  87. {
  88. pair.Value.ReturnToPool();
  89. }
  90. foreach (var pair in slotTimelines)
  91. {
  92. pair.Value.ReturnToPool();
  93. }
  94. foreach (var pair in ffdTimelines)
  95. {
  96. foreach (var pairA in pair.Value)
  97. {
  98. foreach (var pairB in pairA.Value)
  99. {
  100. pairB.Value.ReturnToPool();
  101. }
  102. }
  103. }
  104. frameCount = 0;
  105. playTimes = 0;
  106. duration = 0.0f;
  107. fadeInTime = 0.0f;
  108. cacheFrameRate = 0.0f;
  109. name = null;
  110. boneTimelines.Clear();
  111. slotTimelines.Clear();
  112. ffdTimelines.Clear();
  113. cachedFrames.Clear();
  114. boneCachedFrameIndices.Clear();
  115. slotCachedFrameIndices.Clear();
  116. zOrderTimeline = null;
  117. }
  118. /**
  119. * @private
  120. */
  121. public void CacheFrames(float frameRate)
  122. {
  123. if (cacheFrameRate > 0.0f)
  124. {
  125. return;
  126. }
  127. cacheFrameRate = Math.Max((float)Math.Ceiling(frameRate * scale), 1.0f);
  128. var cacheFrameCount = (int)Math.Ceiling(cacheFrameRate * duration) + 1;
  129. DragonBones.ResizeList(cachedFrames, 0, false);
  130. DragonBones.ResizeList(cachedFrames, cacheFrameCount, false);
  131. foreach (var k in boneTimelines.Keys)
  132. {
  133. var indices = new List<int>(cacheFrameCount);
  134. for (int i = 0, l = indices.Capacity; i < l; ++i)
  135. {
  136. indices.Add(-1);
  137. }
  138. boneCachedFrameIndices[k] = indices;
  139. }
  140. foreach (var k in slotTimelines.Keys)
  141. {
  142. var indices = new List<int>(cacheFrameCount);
  143. for (int i = 0, l = indices.Capacity; i < l; ++i)
  144. {
  145. indices.Add(-1);
  146. }
  147. slotCachedFrameIndices[k] = indices;
  148. }
  149. }
  150. /**
  151. * @private
  152. */
  153. public void AddBoneTimeline(BoneTimelineData value)
  154. {
  155. if (value != null && value.bone != null && !boneTimelines.ContainsKey(value.bone.name))
  156. {
  157. boneTimelines[value.bone.name] = value;
  158. }
  159. else
  160. {
  161. DragonBones.Assert(false, DragonBones.ARGUMENT_ERROR);
  162. }
  163. }
  164. /**
  165. * @private
  166. */
  167. public void AddSlotTimeline(SlotTimelineData value)
  168. {
  169. if (value != null && value.slot != null && !slotTimelines.ContainsKey(value.slot.name))
  170. {
  171. slotTimelines[value.slot.name] = value;
  172. }
  173. else
  174. {
  175. DragonBones.Assert(false, DragonBones.ARGUMENT_ERROR);
  176. }
  177. }
  178. /**
  179. * @private
  180. */
  181. public void AddFFDTimeline(FFDTimelineData value)
  182. {
  183. if (value != null && value.skin != null && value.slot != null)
  184. {
  185. var skin = ffdTimelines.ContainsKey(value.skin.name) ?
  186. ffdTimelines[value.skin.name] :
  187. (ffdTimelines[value.skin.name] = new Dictionary<string, Dictionary<string, FFDTimelineData>>());
  188. var slot = skin.ContainsKey(value.slot.slot.name) ?
  189. skin[value.slot.slot.name] :
  190. (skin[value.slot.slot.name] = new Dictionary<string, FFDTimelineData>());
  191. if (!slot.ContainsKey(value.display.name))
  192. {
  193. slot[value.display.name] = value;
  194. }
  195. else
  196. {
  197. DragonBones.Assert(false, DragonBones.ARGUMENT_ERROR);
  198. }
  199. }
  200. else
  201. {
  202. DragonBones.Assert(false, DragonBones.ARGUMENT_ERROR);
  203. }
  204. }
  205. /**
  206. * @private
  207. */
  208. public BoneTimelineData GetBoneTimeline(string name)
  209. {
  210. return boneTimelines.ContainsKey(name) ? boneTimelines[name] : null;
  211. }
  212. /**
  213. * @private
  214. */
  215. public SlotTimelineData GetSlotTimeline(string name)
  216. {
  217. return slotTimelines.ContainsKey(name) ? slotTimelines[name] : null;
  218. }
  219. /**
  220. * @private
  221. */
  222. public Dictionary<string, FFDTimelineData> GetFFDTimeline(string skinName, string slotName)
  223. {
  224. if (ffdTimelines.ContainsKey(skinName))
  225. {
  226. var skin = ffdTimelines[skinName];
  227. if (skin.ContainsKey(slotName))
  228. {
  229. return skin.ContainsKey(slotName) ? skin[slotName] : null;
  230. }
  231. }
  232. return null;
  233. }
  234. /**
  235. * @private
  236. */
  237. public List<int> GetBoneCachedFrameIndices(string name)
  238. {
  239. return boneCachedFrameIndices.ContainsKey(name) ? boneCachedFrameIndices[name] : null;
  240. }
  241. /**
  242. * @private
  243. */
  244. public List<int> GetSlotCachedFrameIndices(string name)
  245. {
  246. return slotCachedFrameIndices.ContainsKey(name) ? slotCachedFrameIndices[name] : null;
  247. }
  248. }
  249. }