AnimationData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /// <summary>
  28. /// - The animation data.
  29. /// </summary>
  30. /// <version>DragonBones 3.0</version>
  31. /// <language>en_US</language>
  32. /// <summary>
  33. /// - 动画数据。
  34. /// </summary>
  35. /// <version>DragonBones 3.0</version>
  36. /// <language>zh_CN</language>
  37. public class AnimationData : BaseObject
  38. {
  39. /// <summary>
  40. /// - FrameIntArray.
  41. /// </summary>
  42. /// <internal/>
  43. /// <private/>
  44. public uint frameIntOffset;
  45. /// <summary>
  46. /// - FrameFloatArray.
  47. /// </summary>
  48. /// <internal/>
  49. /// <private/>
  50. public uint frameFloatOffset;
  51. /// <summary>
  52. /// - FrameArray.
  53. /// </summary>
  54. /// <internal/>
  55. /// <private/>
  56. public uint frameOffset;
  57. /// <summary>
  58. /// - The frame count of the animation.
  59. /// </summary>
  60. /// <version>DragonBones 3.0</version>
  61. /// <language>en_US</language>
  62. /// <summary>
  63. /// - 动画的帧数。
  64. /// </summary>
  65. /// <version>DragonBones 3.0</version>
  66. /// <language>zh_CN</language>
  67. public uint frameCount;
  68. /// <summary>
  69. /// - The play times of the animation. [0: Loop play, [1~N]: Play N times]
  70. /// </summary>
  71. /// <version>DragonBones 3.0</version>
  72. /// <language>en_US</language>
  73. /// <summary>
  74. /// - 动画的播放次数。 [0: 无限循环播放, [1~N]: 循环播放 N 次]
  75. /// </summary>
  76. /// <version>DragonBones 3.0</version>
  77. /// <language>zh_CN</language>
  78. public uint playTimes;
  79. /// <summary>
  80. /// - The duration of the animation. (In seconds)
  81. /// </summary>
  82. /// <version>DragonBones 3.0</version>
  83. /// <language>en_US</language>
  84. /// <summary>
  85. /// - 动画的持续时间。 (以秒为单位)
  86. /// </summary>
  87. /// <version>DragonBones 3.0</version>
  88. /// <language>zh_CN</language>
  89. public float duration;
  90. /// <private/>
  91. public float scale;
  92. /// <summary>
  93. /// - The fade in time of the animation. (In seconds)
  94. /// </summary>
  95. /// <version>DragonBones 3.0</version>
  96. /// <language>en_US</language>
  97. /// <summary>
  98. /// - 动画的淡入时间。 (以秒为单位)
  99. /// </summary>
  100. /// <version>DragonBones 3.0</version>
  101. /// <language>zh_CN</language>
  102. public float fadeInTime;
  103. /// <private/>
  104. public float cacheFrameRate;
  105. /// <summary>
  106. /// - The animation name.
  107. /// </summary>
  108. /// <version>DragonBones 3.0</version>
  109. /// <language>en_US</language>
  110. /// <summary>
  111. /// - 动画名称。
  112. /// </summary>
  113. /// <version>DragonBones 3.0</version>
  114. /// <language>zh_CN</language>
  115. public string name;
  116. /// <private/>
  117. public readonly List<bool> cachedFrames = new List<bool>();
  118. /// <private/>
  119. public readonly Dictionary<string, List<TimelineData>> boneTimelines = new Dictionary<string, List<TimelineData>>();
  120. /// <private/>
  121. public readonly Dictionary<string, List<TimelineData>> slotTimelines = new Dictionary<string, List<TimelineData>>();
  122. /// <private/>
  123. public readonly Dictionary<string, List<TimelineData>> constraintTimelines = new Dictionary<string, List<TimelineData>>();
  124. /// <private/>
  125. public readonly Dictionary<string, List<int>> boneCachedFrameIndices = new Dictionary<string, List<int>>();
  126. /// <private/>
  127. public readonly Dictionary<string, List<int>> slotCachedFrameIndices = new Dictionary<string, List<int>>();
  128. /// <private/>
  129. public TimelineData actionTimeline = null; // Initial value.
  130. /// <private/>
  131. public TimelineData zOrderTimeline = null; // Initial value.
  132. /// <private/>
  133. public ArmatureData parent;
  134. public AnimationData()
  135. {
  136. }
  137. /// <inheritDoc/>
  138. protected override void _OnClear()
  139. {
  140. foreach (var pair in boneTimelines)
  141. {
  142. for (int i = 0; i < pair.Value.Count; ++i)
  143. {
  144. pair.Value[i].ReturnToPool();
  145. }
  146. }
  147. foreach (var pair in slotTimelines)
  148. {
  149. for (int i = 0; i < pair.Value.Count; ++i)
  150. {
  151. pair.Value[i].ReturnToPool();
  152. }
  153. }
  154. foreach (var pair in constraintTimelines)
  155. {
  156. for (int i = 0; i < pair.Value.Count; ++i)
  157. {
  158. pair.Value[i].ReturnToPool();
  159. }
  160. }
  161. if (this.actionTimeline != null)
  162. {
  163. this.actionTimeline.ReturnToPool();
  164. }
  165. if (this.zOrderTimeline != null)
  166. {
  167. this.zOrderTimeline.ReturnToPool();
  168. }
  169. this.frameIntOffset = 0;
  170. this.frameFloatOffset = 0;
  171. this.frameOffset = 0;
  172. this.frameCount = 0;
  173. this.playTimes = 0;
  174. this.duration = 0.0f;
  175. this.scale = 1.0f;
  176. this.fadeInTime = 0.0f;
  177. this.cacheFrameRate = 0.0f;
  178. this.name = "";
  179. this.boneTimelines.Clear();
  180. this.slotTimelines.Clear();
  181. this.constraintTimelines.Clear();
  182. this.boneCachedFrameIndices.Clear();
  183. this.slotCachedFrameIndices.Clear();
  184. this.cachedFrames.Clear();
  185. this.actionTimeline = null;
  186. this.zOrderTimeline = null;
  187. this.parent = null;
  188. }
  189. /// <internal/>
  190. /// <private/>
  191. public void CacheFrames(float frameRate)
  192. {
  193. if (this.cacheFrameRate > 0.0f)
  194. {
  195. // TODO clear cache.
  196. return;
  197. }
  198. this.cacheFrameRate = Math.Max((float)Math.Ceiling(frameRate * scale), 1.0f);
  199. var cacheFrameCount = (int)Math.Ceiling(this.cacheFrameRate * duration) + 1; // Cache one more frame.
  200. cachedFrames.ResizeList(0, false);
  201. cachedFrames.ResizeList(cacheFrameCount, false);
  202. foreach (var bone in this.parent.sortedBones)
  203. {
  204. var indices = new List<int>(cacheFrameCount);
  205. for (int i = 0, l = indices.Capacity; i < l; ++i)
  206. {
  207. indices.Add(-1);
  208. }
  209. this.boneCachedFrameIndices[bone.name] = indices;
  210. }
  211. foreach (var slot in this.parent.sortedSlots)
  212. {
  213. var indices = new List<int>(cacheFrameCount);
  214. for (int i = 0, l = indices.Capacity; i < l; ++i)
  215. {
  216. indices.Add(-1);
  217. }
  218. this.slotCachedFrameIndices[slot.name] = indices;
  219. }
  220. }
  221. /// <private/>
  222. public void AddBoneTimeline(BoneData bone, TimelineData tiemline)
  223. {
  224. if (bone == null || tiemline == null)
  225. {
  226. return;
  227. }
  228. if (!this.boneTimelines.ContainsKey(bone.name))
  229. {
  230. this.boneTimelines[bone.name] = new List<TimelineData>();
  231. }
  232. var timelines = this.boneTimelines[bone.name];
  233. if (!timelines.Contains(tiemline))
  234. {
  235. timelines.Add(tiemline);
  236. }
  237. }
  238. /// <private/>
  239. public void AddSlotTimeline(SlotData slot, TimelineData timeline)
  240. {
  241. if (slot == null || timeline == null)
  242. {
  243. return;
  244. }
  245. if (!this.slotTimelines.ContainsKey(slot.name))
  246. {
  247. this.slotTimelines[slot.name] = new List<TimelineData>();
  248. }
  249. var timelines = this.slotTimelines[slot.name];
  250. if (!timelines.Contains(timeline))
  251. {
  252. timelines.Add(timeline);
  253. }
  254. }
  255. /// <private/>
  256. public void AddConstraintTimeline(ConstraintData constraint, TimelineData timeline)
  257. {
  258. if (constraint == null || timeline == null)
  259. {
  260. return;
  261. }
  262. if (!this.constraintTimelines.ContainsKey(constraint.name))
  263. {
  264. this.constraintTimelines[constraint.name] = new List<TimelineData>();
  265. }
  266. var timelines = this.constraintTimelines[constraint.name];
  267. if (!timelines.Contains(timeline))
  268. {
  269. timelines.Add(timeline);
  270. }
  271. }
  272. /// <private/>
  273. public List<TimelineData> GetBoneTimelines(string timelineName)
  274. {
  275. return this.boneTimelines.ContainsKey(timelineName) ? this.boneTimelines[timelineName] : null;
  276. }
  277. /// <private/>
  278. public List<TimelineData> GetSlotTimelines(string timelineName)
  279. {
  280. return slotTimelines.ContainsKey(timelineName) ? slotTimelines[timelineName] : null;
  281. }
  282. /// <private/>
  283. public List<TimelineData> GetConstraintTimelines(string timelineName)
  284. {
  285. return constraintTimelines.ContainsKey(timelineName) ? constraintTimelines[timelineName] : null;
  286. }
  287. /// <private/>
  288. public List<int> GetBoneCachedFrameIndices(string boneName)
  289. {
  290. return this.boneCachedFrameIndices.ContainsKey(boneName) ? this.boneCachedFrameIndices[boneName] : null;
  291. }
  292. /// <private/>
  293. public List<int> GetSlotCachedFrameIndices(string slotName)
  294. {
  295. return this.slotCachedFrameIndices.ContainsKey(slotName) ? this.slotCachedFrameIndices[slotName] : null;
  296. }
  297. }
  298. /// <internal/>
  299. /// <private/>
  300. public class TimelineData : BaseObject
  301. {
  302. public TimelineType type;
  303. public uint offset; // TimelineArray.
  304. public int frameIndicesOffset; // FrameIndices.
  305. protected override void _OnClear()
  306. {
  307. this.type = TimelineType.BoneAll;
  308. this.offset = 0;
  309. this.frameIndicesOffset = -1;
  310. }
  311. }
  312. }