DragonBones.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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.Collections.Generic;
  24. using System.Diagnostics;
  25. using System;
  26. namespace DragonBones
  27. {
  28. /// <internal/>
  29. /// <private/>
  30. enum BinaryOffset
  31. {
  32. WeigthBoneCount = 0,
  33. WeigthFloatOffset = 1,
  34. WeigthBoneIndices = 2,
  35. MeshVertexCount = 0,
  36. MeshTriangleCount = 1,
  37. MeshFloatOffset = 2,
  38. MeshWeightOffset = 3,
  39. MeshVertexIndices = 4,
  40. TimelineScale = 0,
  41. TimelineOffset = 1,
  42. TimelineKeyFrameCount = 2,
  43. TimelineFrameValueCount = 3,
  44. TimelineFrameValueOffset = 4,
  45. TimelineFrameOffset = 5,
  46. FramePosition = 0,
  47. FrameTweenType = 1,
  48. FrameTweenEasingOrCurveSampleCount = 2,
  49. FrameCurveSamples = 3,
  50. DeformVertexOffset = 0,
  51. DeformCount = 1,
  52. DeformValueCount = 2,
  53. DeformValueOffset = 3,
  54. DeformFloatOffset = 4
  55. }
  56. /// <internal/>
  57. /// <private/>
  58. public enum ArmatureType
  59. {
  60. None = -1,
  61. Armature = 0,
  62. MovieClip = 1,
  63. Stage = 2
  64. }
  65. /// <private/>
  66. public enum DisplayType
  67. {
  68. None = -1,
  69. Image = 0,
  70. Armature = 1,
  71. Mesh = 2,
  72. BoundingBox = 3,
  73. Path = 4
  74. }
  75. /// <summary>
  76. /// - Bounding box type.
  77. /// </summary>
  78. /// <version>DragonBones 5.0</version>
  79. /// <language>en_US</language>
  80. /// <summary>
  81. /// - 边界框类型。
  82. /// </summary>
  83. /// <version>DragonBones 5.0</version>
  84. /// <language>zh_CN</language>
  85. public enum BoundingBoxType
  86. {
  87. None = -1,
  88. Rectangle = 0,
  89. Ellipse = 1,
  90. Polygon = 2
  91. }
  92. /// <internal/>
  93. /// <private/>
  94. public enum ActionType
  95. {
  96. Play = 0,
  97. Frame = 10,
  98. Sound = 11
  99. }
  100. /// <internal/>
  101. /// <private/>
  102. public enum BlendMode
  103. {
  104. Normal = 0,
  105. Add = 1,
  106. Alpha = 2,
  107. Darken = 3,
  108. Difference = 4,
  109. Erase = 5,
  110. HardLight = 6,
  111. Invert = 7,
  112. Layer = 8,
  113. Lighten = 9,
  114. Multiply = 10,
  115. Overlay = 11,
  116. Screen = 12,
  117. Subtract = 13
  118. }
  119. /// <internal/>
  120. /// <private/>
  121. public enum TweenType
  122. {
  123. None = 0,
  124. Line = 1,
  125. Curve = 2,
  126. QuadIn = 3,
  127. QuadOut = 4,
  128. QuadInOut = 5
  129. }
  130. /// <internal/>
  131. /// <private/>
  132. public enum TimelineType
  133. {
  134. Action = 0,
  135. ZOrder = 1,
  136. BoneAll = 10,
  137. BoneTranslate = 11,
  138. BoneRotate = 12,
  139. BoneScale = 13,
  140. SlotDisplay = 20,
  141. SlotColor = 21,
  142. SlotDeform = 22,
  143. IKConstraint = 30,
  144. AnimationTime = 40,
  145. AnimationWeight = 41
  146. }
  147. /// <summary>
  148. /// - Offset mode.
  149. /// </summary>
  150. /// <version>DragonBones 5.5</version>
  151. /// <language>en_US</language>
  152. /// <summary>
  153. /// - 偏移模式。
  154. /// </summary>
  155. /// <version>DragonBones 5.5</version>
  156. /// <language>zh_CN</language>
  157. public enum OffsetMode
  158. {
  159. None,
  160. Additive,
  161. Override
  162. }
  163. /// <summary>
  164. /// - Animation fade out mode.
  165. /// </summary>
  166. /// <version>DragonBones 4.5</version>
  167. /// <language>en_US</language>
  168. /// <summary>
  169. /// - 动画淡出模式。
  170. /// </summary>
  171. /// <version>DragonBones 4.5</version>
  172. /// <language>zh_CN</language>
  173. public enum AnimationFadeOutMode
  174. {
  175. /// <summary>
  176. /// - Do not fade out of any animation states.
  177. /// </summary>
  178. /// <language>en_US</language>
  179. /// <summary>
  180. /// - 不淡出任何的动画状态。
  181. /// </summary>
  182. /// <language>zh_CN</language>
  183. None = 0,
  184. /// <summary>
  185. /// - Fade out the animation states of the same layer.
  186. /// </summary>
  187. /// <language>en_US</language>
  188. /// <summary>
  189. /// - 淡出同层的动画状态。
  190. /// </summary>
  191. /// <language>zh_CN</language>
  192. SameLayer = 1,
  193. /// <summary>
  194. /// - Fade out the animation states of the same group.
  195. /// </summary>
  196. /// <language>en_US</language>
  197. /// <summary>
  198. /// - 淡出同组的动画状态。
  199. /// </summary>
  200. /// <language>zh_CN</language>
  201. SameGroup = 2,
  202. /// <summary>
  203. /// - Fade out the animation states of the same layer and group.
  204. /// </summary>
  205. /// <language>en_US</language>
  206. /// <summary>
  207. /// - 淡出同层并且同组的动画状态。
  208. /// </summary>
  209. /// <language>zh_CN</language>
  210. SameLayerAndGroup = 3,
  211. /// <summary>
  212. /// - Fade out of all animation states.
  213. /// </summary>
  214. /// <language>en_US</language>
  215. /// <summary>
  216. /// - 淡出所有的动画状态。
  217. /// </summary>
  218. /// <language>zh_CN</language>
  219. All = 4,
  220. /// <summary>
  221. /// - Does not replace the animation state with the same name.
  222. /// </summary>
  223. /// <language>en_US</language>
  224. /// <summary>
  225. /// - 不替换同名的动画状态。
  226. /// </summary>
  227. /// <language>zh_CN</language>
  228. Single = 5
  229. }
  230. internal static class Helper
  231. {
  232. public static readonly int INT16_SIZE = 2;
  233. public static readonly int UINT16_SIZE = 2;
  234. public static readonly int FLOAT_SIZE = 4;
  235. internal static void Assert(bool condition, string message)
  236. {
  237. Debug.Assert(condition, message);
  238. }
  239. internal static void ResizeList<T>(this List<T> list, int count, T value = default(T))
  240. {
  241. if (list.Count == count)
  242. {
  243. return;
  244. }
  245. if (list.Count > count)
  246. {
  247. list.RemoveRange(count, list.Count - count);
  248. }
  249. else
  250. {
  251. //fixed gc,may be memory will grow
  252. //list.Capacity = count;
  253. for (int i = list.Count, l = count; i < l; ++i)
  254. {
  255. list.Add(value);
  256. }
  257. }
  258. }
  259. internal static List<float> Convert(this List<object> list)
  260. {
  261. List<float> res = new List<float>();
  262. for (int i = 0; i < list.Count; i++)
  263. {
  264. res[i] = float.Parse(list[i].ToString());
  265. }
  266. return res;
  267. }
  268. internal static bool FloatEqual(float f0, float f1)
  269. {
  270. float f = Math.Abs(f0 - f1);
  271. return (f < 0.000000001f);
  272. }
  273. }
  274. /// <private/>
  275. public class DragonBones
  276. {
  277. public static bool yDown = true;
  278. public static bool debug = false;
  279. public static bool debugDraw = false;
  280. public static readonly string VERSION = "5.6.300";
  281. private readonly WorldClock _clock = new WorldClock();
  282. private readonly List<EventObject> _events = new List<EventObject>();
  283. private readonly List<BaseObject> _objects = new List<BaseObject>();
  284. private IEventDispatcher<EventObject> _eventManager = null;
  285. public DragonBones(IEventDispatcher<EventObject> eventManager)
  286. {
  287. this._eventManager = eventManager;
  288. }
  289. public void AdvanceTime(float passedTime)
  290. {
  291. if (this._objects.Count > 0)
  292. {
  293. for (int i = 0; i < this._objects.Count; ++i)
  294. {
  295. var obj = this._objects[i];
  296. obj.ReturnToPool();
  297. }
  298. this._objects.Clear();
  299. }
  300. if (this._events.Count > 0)
  301. {
  302. for (int i = 0; i < this._events.Count; ++i)
  303. {
  304. var eventObject = this._events[i];
  305. var armature = eventObject.armature;
  306. if (armature._armatureData != null)
  307. {
  308. // May be armature disposed before advanceTime.
  309. armature.eventDispatcher.DispatchDBEvent(eventObject.type, eventObject);
  310. if (eventObject.type == EventObject.SOUND_EVENT)
  311. {
  312. this._eventManager.DispatchDBEvent(eventObject.type, eventObject);
  313. }
  314. }
  315. this.BufferObject(eventObject);
  316. }
  317. this._events.Clear();
  318. }
  319. this._clock.AdvanceTime(passedTime);
  320. }
  321. public void BufferEvent(EventObject value)
  322. {
  323. if (!this._events.Contains(value))
  324. {
  325. this._events.Add(value);
  326. }
  327. }
  328. public void BufferObject(BaseObject value)
  329. {
  330. if (!this._objects.Contains(value))
  331. {
  332. this._objects.Add(value);
  333. }
  334. }
  335. public static implicit operator bool(DragonBones exists)
  336. {
  337. return exists != null;
  338. }
  339. public WorldClock clock
  340. {
  341. get { return this._clock; }
  342. }
  343. public IEventDispatcher<EventObject> eventManager
  344. {
  345. get { return this._eventManager; }
  346. }
  347. }
  348. }