AnimationConfig.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. namespace DragonBones
  25. {
  26. /// <summary>
  27. /// - The animation config is used to describe all the information needed to play an animation state.
  28. /// The API is still in the experimental phase and may encounter bugs or stability or compatibility issues when used.
  29. /// </summary>
  30. /// <see cref="DragonBones.AnimationState"/>
  31. /// <beta/>
  32. /// <version>DragonBones 5.0</version>
  33. /// <language>en_US</language>
  34. /// <summary>
  35. /// - 动画配置用来描述播放一个动画状态所需要的全部信息。
  36. /// 该 API 仍在实验阶段,使用时可能遭遇 bug 或稳定性或兼容性问题。
  37. /// </summary>
  38. /// <see cref="DragonBones.AnimationState"/>
  39. /// <beta/>
  40. /// <version>DragonBones 5.0</version>
  41. /// <language>zh_CN</language>
  42. public class AnimationConfig : BaseObject
  43. {
  44. /// <private/>
  45. public bool pauseFadeOut;
  46. /// <summary>
  47. /// - Fade out the pattern of other animation states when the animation state is fade in.
  48. /// This property is typically used to specify the substitution of multiple animation states blend.
  49. /// </summary>
  50. /// <default>dragonBones.AnimationFadeOutMode.All</default>
  51. /// <version>DragonBones 5.0</version>
  52. /// <language>en_US</language>
  53. /// <summary>
  54. /// - 淡入动画状态时淡出其他动画状态的模式。
  55. /// 该属性通常用来指定多个动画状态混合时的相互替换关系。
  56. /// </summary>
  57. /// <default>dragonBones.AnimationFadeOutMode.All</default>
  58. /// <version>DragonBones 5.0</version>
  59. /// <language>zh_CN</language>
  60. public AnimationFadeOutMode fadeOutMode;
  61. /// <private/>
  62. public TweenType fadeOutTweenType;
  63. /// <private/>
  64. public float fadeOutTime;
  65. /// <private/>
  66. public bool pauseFadeIn;
  67. /// <private/>
  68. public bool actionEnabled;
  69. /// <private/>
  70. public bool additiveBlending;
  71. /// <summary>
  72. /// - Whether the animation state has control over the display property of the slots.
  73. /// Sometimes blend a animation state does not want it to control the display properties of the slots,
  74. /// especially if other animation state are controlling the display properties of the slots.
  75. /// </summary>
  76. /// <default>true</default>
  77. /// <version>DragonBones 5.0</version>
  78. /// <language>en_US</language>
  79. /// <summary>
  80. /// - 动画状态是否对插槽的显示对象属性有控制权。
  81. /// 有时混合一个动画状态并不希望其控制插槽的显示对象属性,
  82. /// 尤其是其他动画状态正在控制这些插槽的显示对象属性时。
  83. /// </summary>
  84. /// <default>true</default>
  85. /// <version>DragonBones 5.0</version>
  86. /// <language>zh_CN</language>
  87. public bool displayControl;
  88. /// <summary>
  89. /// - Whether to reset the objects without animation to the armature pose when the animation state is start to play.
  90. /// This property should usually be set to false when blend multiple animation states.
  91. /// </summary>
  92. /// <default>true</default>
  93. /// <version>DragonBones 5.1</version>
  94. /// <language>en_US</language>
  95. /// <summary>
  96. /// - 开始播放动画状态时是否将没有动画的对象重置为骨架初始值。
  97. /// 通常在混合多个动画状态时应该将该属性设置为 false。
  98. /// </summary>
  99. /// <default>true</default>
  100. /// <version>DragonBones 5.1</version>
  101. /// <language>zh_CN</language>
  102. public bool resetToPose;
  103. /// <private/>
  104. public TweenType fadeInTweenType;
  105. /// <summary>
  106. /// - The play times. [0: Loop play, [1~N]: Play N times]
  107. /// </summary>
  108. /// <version>DragonBones 3.0</version>
  109. /// <language>en_US</language>
  110. /// <summary>
  111. /// - 播放次数。 [0: 无限循环播放, [1~N]: 循环播放 N 次]
  112. /// </summary>
  113. /// <version>DragonBones 3.0</version>
  114. /// <language>zh_CN</language>
  115. public int playTimes;
  116. /// <summary>
  117. /// - The blend layer.
  118. /// High layer animation state will get the blend weight first.
  119. /// When the blend weight is assigned more than 1, the remaining animation states will no longer get the weight assigned.
  120. /// </summary>
  121. /// <readonly/>
  122. /// <version>DragonBones 5.0</version>
  123. /// <language>en_US</language>
  124. /// <summary>
  125. /// - 混合图层。
  126. /// 图层高的动画状态会优先获取混合权重。
  127. /// 当混合权重分配超过 1 时,剩余的动画状态将不再获得权重分配。
  128. /// </summary>
  129. /// <readonly/>
  130. /// <version>DragonBones 5.0</version>
  131. /// <language>zh_CN</language>
  132. public int layer;
  133. /// <summary>
  134. /// - The start time of play. (In seconds)
  135. /// </summary>
  136. /// <default>0.0</default>
  137. /// <version>DragonBones 5.0</version>
  138. /// <language>en_US</language>
  139. /// <summary>
  140. /// - 播放的开始时间。 (以秒为单位)
  141. /// </summary>
  142. /// <default>0.0</default>
  143. /// <version>DragonBones 5.0</version>
  144. /// <language>zh_CN</language>
  145. public float position;
  146. /// <summary>
  147. /// - The duration of play.
  148. /// [-1: Use the default value of the animation data, 0: Stop play, (0~N]: The duration] (In seconds)
  149. /// </summary>
  150. /// <default>-1.0</default>
  151. /// <version>DragonBones 5.0</version>
  152. /// <language>en_US</language>
  153. /// <summary>
  154. /// - 播放的持续时间。
  155. /// [-1: 使用动画数据默认值, 0: 动画停止, (0~N]: 持续时间] (以秒为单位)
  156. /// </summary>
  157. /// <default>-1.0</default>
  158. /// <version>DragonBones 5.0</version>
  159. /// <language>zh_CN</language>
  160. public float duration;
  161. /// <summary>
  162. /// - The play speed.
  163. /// The value is an overlay relationship with {@link dragonBones.Animation#timeScale}.
  164. /// [(-N~0): Reverse play, 0: Stop play, (0~1): Slow play, 1: Normal play, (1~N): Fast play]
  165. /// </summary>
  166. /// <default>1.0</default>
  167. /// <version>DragonBones 3.0</version>
  168. /// <language>en_US</language>
  169. /// <summary>
  170. /// - 播放速度。
  171. /// 该值与 {@link dragonBones.Animation#timeScale} 是叠加关系。
  172. /// [(-N~0): 倒转播放, 0: 停止播放, (0~1): 慢速播放, 1: 正常播放, (1~N): 快速播放]
  173. /// </summary>
  174. /// <default>1.0</default>
  175. /// <version>DragonBones 3.0</version>
  176. /// <language>zh_CN</language>
  177. public float timeScale;
  178. /// <summary>
  179. /// - The blend weight.
  180. /// </summary>
  181. /// <default>1.0</default>
  182. /// <version>DragonBones 5.0</version>
  183. /// <language>en_US</language>
  184. /// <summary>
  185. /// - 混合权重。
  186. /// </summary>
  187. /// <default>1.0</default>
  188. /// <version>DragonBones 5.0</version>
  189. /// <language>zh_CN</language>
  190. public float weight;
  191. /// <summary>
  192. /// - The fade in time.
  193. /// [-1: Use the default value of the animation data, [0~N]: The fade in time] (In seconds)
  194. /// </summary>
  195. /// <default>-1.0</default>
  196. /// <version>DragonBones 5.0</version>
  197. /// <language>en_US</language>
  198. /// <summary>
  199. /// - 淡入时间。
  200. /// [-1: 使用动画数据默认值, [0~N]: 淡入时间] (以秒为单位)
  201. /// </summary>
  202. /// <default>-1.0</default>
  203. /// <version>DragonBones 5.0</version>
  204. /// <language>zh_CN</language>
  205. public float fadeInTime;
  206. /// <summary>
  207. /// - The auto fade out time when the animation state play completed.
  208. /// [-1: Do not fade out automatically, [0~N]: The fade out time] (In seconds)
  209. /// </summary>
  210. /// <default>-1.0</default>
  211. /// <version>DragonBones 5.0</version>
  212. /// <language>en_US</language>
  213. /// <summary>
  214. /// - 动画状态播放完成后的自动淡出时间。
  215. /// [-1: 不自动淡出, [0~N]: 淡出时间] (以秒为单位)
  216. /// </summary>
  217. /// <default>-1.0</default>
  218. /// <version>DragonBones 5.0</version>
  219. /// <language>zh_CN</language>
  220. public float autoFadeOutTime;
  221. /// <summary>
  222. /// - The name of the animation state. (Can be different from the name of the animation data)
  223. /// </summary>
  224. /// <version>DragonBones 5.0</version>
  225. /// <language>en_US</language>
  226. /// <summary>
  227. /// - 动画状态名称。 (可以不同于动画数据)
  228. /// </summary>
  229. /// <version>DragonBones 5.0</version>
  230. /// <language>zh_CN</language>
  231. public string name;
  232. /// <summary>
  233. /// - The animation data name.
  234. /// </summary>
  235. /// <version>DragonBones 5.0</version>
  236. /// <language>en_US</language>
  237. /// <summary>
  238. /// - 动画数据名称。
  239. /// </summary>
  240. /// <version>DragonBones 5.0</version>
  241. /// <language>zh_CN</language>
  242. public string animation;
  243. /// <summary>
  244. /// - The blend group name of the animation state.
  245. /// This property is typically used to specify the substitution of multiple animation states blend.
  246. /// </summary>
  247. /// <readonly/>
  248. /// <version>DragonBones 5.0</version>
  249. /// <language>en_US</language>
  250. /// <summary>
  251. /// - 混合组名称。
  252. /// 该属性通常用来指定多个动画状态混合时的相互替换关系。
  253. /// </summary>
  254. /// <readonly/>
  255. /// <version>DragonBones 5.0</version>
  256. /// <language>zh_CN</language>
  257. public string group;
  258. /// <private/>
  259. public readonly List<string> boneMask = new List<string>();
  260. /// <private/>
  261. protected override void _OnClear()
  262. {
  263. this.pauseFadeOut = true;
  264. this.fadeOutMode = AnimationFadeOutMode.All;
  265. this.fadeOutTweenType = TweenType.Line;
  266. this.fadeOutTime = -1.0f;
  267. this.actionEnabled = true;
  268. this.additiveBlending = false;
  269. this.displayControl = true;
  270. this.pauseFadeIn = true;
  271. this.resetToPose = true;
  272. this.fadeInTweenType = TweenType.Line;
  273. this.playTimes = -1;
  274. this.layer = 0;
  275. this.position = 0.0f;
  276. this.duration = -1.0f;
  277. this.timeScale = -100.0f;
  278. this.weight = 1.0f;
  279. this.fadeInTime = -1.0f;
  280. this.autoFadeOutTime = -1.0f;
  281. this.name = "";
  282. this.animation = "";
  283. this.group = "";
  284. this.boneMask.Clear();
  285. }
  286. /// <private/>
  287. public void Clear()
  288. {
  289. this._OnClear();
  290. }
  291. /// <private/>
  292. public void CopyFrom(AnimationConfig value)
  293. {
  294. this.pauseFadeOut = value.pauseFadeOut;
  295. this.fadeOutMode = value.fadeOutMode;
  296. this.autoFadeOutTime = value.autoFadeOutTime;
  297. this.fadeOutTweenType = value.fadeOutTweenType;
  298. this.actionEnabled = value.actionEnabled;
  299. this.additiveBlending = value.additiveBlending;
  300. this.displayControl = value.displayControl;
  301. this.pauseFadeIn = value.pauseFadeIn;
  302. this.resetToPose = value.resetToPose;
  303. this.playTimes = value.playTimes;
  304. this.layer = value.layer;
  305. this.position = value.position;
  306. this.duration = value.duration;
  307. this.timeScale = value.timeScale;
  308. this.fadeInTime = value.fadeInTime;
  309. this.fadeOutTime = value.fadeOutTime;
  310. this.fadeInTweenType = value.fadeInTweenType;
  311. this.weight = value.weight;
  312. this.name = value.name;
  313. this.animation = value.animation;
  314. this.group = value.group;
  315. boneMask.ResizeList(value.boneMask.Count, null);
  316. for (int i = 0, l = boneMask.Count; i < l; ++i)
  317. {
  318. boneMask[i] = value.boneMask[i];
  319. }
  320. }
  321. /// <private/>
  322. public bool ContainsBoneMask(string boneName)
  323. {
  324. return boneMask.Count == 0 || boneMask.Contains(boneName);
  325. }
  326. /// <private/>
  327. public void AddBoneMask(Armature armature, string boneName, bool recursive = false)
  328. {
  329. var currentBone = armature.GetBone(boneName);
  330. if (currentBone == null)
  331. {
  332. return;
  333. }
  334. if (!boneMask.Contains(boneName)) // Add mixing
  335. {
  336. boneMask.Add(boneName);
  337. }
  338. if (recursive) // Add recursive mixing.
  339. {
  340. var bones = armature.GetBones();
  341. for (int i = 0, l = bones.Count; i < l; ++i)
  342. {
  343. var bone = bones[i];
  344. if (!boneMask.Contains(bone.name) && currentBone.Contains(bone))
  345. {
  346. boneMask.Add(bone.name);
  347. }
  348. }
  349. }
  350. }
  351. /// <private/>
  352. public void RemoveBoneMask(Armature armature, string name, bool recursive = true)
  353. {
  354. if (boneMask.Contains(name)) // Remove mixing.
  355. {
  356. boneMask.Remove(name);
  357. }
  358. if (recursive)
  359. {
  360. var currentBone = armature.GetBone(name);
  361. if (currentBone != null)
  362. {
  363. var bones = armature.GetBones();
  364. if (boneMask.Count > 0) // Remove recursive mixing.
  365. {
  366. for (int i = 0, l = bones.Count; i < l; ++i)
  367. {
  368. var bone = bones[i];
  369. if (boneMask.Contains(bone.name) && currentBone.Contains(bone))
  370. {
  371. boneMask.Remove(bone.name);
  372. }
  373. }
  374. }
  375. else // Add unrecursive mixing.
  376. {
  377. for (int i = 0, l = bones.Count; i < l; ++i)
  378. {
  379. var bone = bones[i];
  380. if (bone == currentBone)
  381. {
  382. continue;
  383. }
  384. if (!currentBone.Contains(bone))
  385. {
  386. boneMask.Add(bone.name);
  387. }
  388. }
  389. }
  390. }
  391. }
  392. }
  393. }
  394. }