源战役客户端
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

617 righe
24 KiB

  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using UnityEngine;
  31. namespace Spine.Unity {
  32. public static class SkeletonExtensions {
  33. #region Colors
  34. const float ByteToFloat = 1f / 255f;
  35. public static Color GetColor (this Skeleton s) { return new Color(s.r, s.g, s.b, s.a); }
  36. public static Color GetColor (this RegionAttachment a) { return new Color(a.r, a.g, a.b, a.a); }
  37. public static Color GetColor (this MeshAttachment a) { return new Color(a.r, a.g, a.b, a.a); }
  38. public static Color GetColor (this Slot s) { return new Color(s.r, s.g, s.b, s.a); }
  39. public static Color GetColorTintBlack (this Slot s) { return new Color(s.r2, s.g2, s.b2, 1f); }
  40. public static void SetColor (this Skeleton skeleton, Color color) {
  41. skeleton.A = color.a;
  42. skeleton.R = color.r;
  43. skeleton.G = color.g;
  44. skeleton.B = color.b;
  45. }
  46. public static void SetColor (this Skeleton skeleton, Color32 color) {
  47. skeleton.A = color.a * ByteToFloat;
  48. skeleton.R = color.r * ByteToFloat;
  49. skeleton.G = color.g * ByteToFloat;
  50. skeleton.B = color.b * ByteToFloat;
  51. }
  52. public static void SetColor (this Slot slot, Color color) {
  53. slot.A = color.a;
  54. slot.R = color.r;
  55. slot.G = color.g;
  56. slot.B = color.b;
  57. }
  58. public static void SetColor (this Slot slot, Color32 color) {
  59. slot.A = color.a * ByteToFloat;
  60. slot.R = color.r * ByteToFloat;
  61. slot.G = color.g * ByteToFloat;
  62. slot.B = color.b * ByteToFloat;
  63. }
  64. public static void SetColor (this RegionAttachment attachment, Color color) {
  65. attachment.A = color.a;
  66. attachment.R = color.r;
  67. attachment.G = color.g;
  68. attachment.B = color.b;
  69. }
  70. public static void SetColor (this RegionAttachment attachment, Color32 color) {
  71. attachment.A = color.a * ByteToFloat;
  72. attachment.R = color.r * ByteToFloat;
  73. attachment.G = color.g * ByteToFloat;
  74. attachment.B = color.b * ByteToFloat;
  75. }
  76. public static void SetColor (this MeshAttachment attachment, Color color) {
  77. attachment.A = color.a;
  78. attachment.R = color.r;
  79. attachment.G = color.g;
  80. attachment.B = color.b;
  81. }
  82. public static void SetColor (this MeshAttachment attachment, Color32 color) {
  83. attachment.A = color.a * ByteToFloat;
  84. attachment.R = color.r * ByteToFloat;
  85. attachment.G = color.g * ByteToFloat;
  86. attachment.B = color.b * ByteToFloat;
  87. }
  88. #endregion
  89. #region Bone
  90. /// <summary>Sets the bone's (local) X and Y according to a Vector2</summary>
  91. public static void SetPosition (this Bone bone, Vector2 position) {
  92. bone.X = position.x;
  93. bone.Y = position.y;
  94. }
  95. /// <summary>Sets the bone's (local) X and Y according to a Vector3. The z component is ignored.</summary>
  96. public static void SetPosition (this Bone bone, Vector3 position) {
  97. bone.X = position.x;
  98. bone.Y = position.y;
  99. }
  100. /// <summary>Gets the bone's local X and Y as a Vector2.</summary>
  101. public static Vector2 GetLocalPosition (this Bone bone) {
  102. return new Vector2(bone.x, bone.y);
  103. }
  104. /// <summary>Gets the position of the bone in Skeleton-space.</summary>
  105. public static Vector2 GetSkeletonSpacePosition (this Bone bone) {
  106. return new Vector2(bone.worldX, bone.worldY);
  107. }
  108. /// <summary>Gets a local offset from the bone and converts it into Skeleton-space.</summary>
  109. public static Vector2 GetSkeletonSpacePosition (this Bone bone, Vector2 boneLocal) {
  110. Vector2 o;
  111. bone.LocalToWorld(boneLocal.x, boneLocal.y, out o.x, out o.y);
  112. return o;
  113. }
  114. /// <summary>Gets the bone's Unity World position using its Spine GameObject Transform. UpdateWorldTransform needs to have been called for this to return the correct, updated value.</summary>
  115. public static Vector3 GetWorldPosition (this Bone bone, UnityEngine.Transform spineGameObjectTransform) {
  116. return spineGameObjectTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY));
  117. }
  118. public static Vector3 GetWorldPosition (this Bone bone, UnityEngine.Transform spineGameObjectTransform, float positionScale) {
  119. return spineGameObjectTransform.TransformPoint(new Vector3(bone.worldX * positionScale, bone.worldY * positionScale));
  120. }
  121. /// <summary>Gets a skeleton space UnityEngine.Quaternion representation of bone.WorldRotationX.</summary>
  122. public static Quaternion GetQuaternion (this Bone bone) {
  123. var halfRotation = Mathf.Atan2(bone.c, bone.a) * 0.5f;
  124. return new Quaternion(0, 0, Mathf.Sin(halfRotation), Mathf.Cos(halfRotation));
  125. }
  126. /// <summary>Gets a bone-local space UnityEngine.Quaternion representation of bone.rotation.</summary>
  127. public static Quaternion GetLocalQuaternion (this Bone bone) {
  128. var halfRotation = bone.rotation * Mathf.Deg2Rad * 0.5f;
  129. return new Quaternion(0, 0, Mathf.Sin(halfRotation), Mathf.Cos(halfRotation));
  130. }
  131. /// <summary>Gets the internal bone matrix as a Unity bonespace-to-skeletonspace transformation matrix.</summary>
  132. public static Matrix4x4 GetMatrix4x4 (this Bone bone) {
  133. return new Matrix4x4 {
  134. m00 = bone.a, m01 = bone.b, m03 = bone.worldX,
  135. m10 = bone.c, m11 = bone.d, m13 = bone.worldY,
  136. m33 = 1
  137. };
  138. }
  139. /// <summary>Calculates a 2x2 Transformation Matrix that can convert a skeleton-space position to a bone-local position.</summary>
  140. public static void GetWorldToLocalMatrix (this Bone bone, out float ia, out float ib, out float ic, out float id) {
  141. float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
  142. float invDet = 1 / (a * d - b * c);
  143. ia = invDet * d;
  144. ib = invDet * -b;
  145. ic = invDet * -c;
  146. id = invDet * a;
  147. }
  148. /// <summary>UnityEngine.Vector2 override of Bone.WorldToLocal. This converts a skeleton-space position into a bone local position.</summary>
  149. public static Vector2 WorldToLocal (this Bone bone, Vector2 worldPosition) {
  150. Vector2 o;
  151. bone.WorldToLocal(worldPosition.x, worldPosition.y, out o.x, out o.y);
  152. return o;
  153. }
  154. /// <summary>Sets the skeleton-space position of a bone.</summary>
  155. /// <returns>The local position in its parent bone space, or in skeleton space if it is the root bone.</returns>
  156. public static Vector2 SetPositionSkeletonSpace (this Bone bone, Vector2 skeletonSpacePosition) {
  157. if (bone.parent == null) { // root bone
  158. bone.SetPosition(skeletonSpacePosition);
  159. return skeletonSpacePosition;
  160. } else {
  161. var parent = bone.parent;
  162. Vector2 parentLocal = parent.WorldToLocal(skeletonSpacePosition);
  163. bone.SetPosition(parentLocal);
  164. return parentLocal;
  165. }
  166. }
  167. #endregion
  168. #region Attachments
  169. public static Material GetMaterial (this Attachment a) {
  170. object rendererObject = null;
  171. var renderableAttachment = a as IHasRendererObject;
  172. if (renderableAttachment != null) {
  173. rendererObject = renderableAttachment.RendererObject;
  174. }
  175. if (rendererObject == null)
  176. return null;
  177. #if SPINE_TK2D
  178. return (rendererObject.GetType() == typeof(Material)) ? (Material)rendererObject : (Material)((AtlasRegion)rendererObject).page.rendererObject;
  179. #else
  180. return (Material)((AtlasRegion)rendererObject).page.rendererObject;
  181. #endif
  182. }
  183. /// <summary>Fills a Vector2 buffer with local vertices.</summary>
  184. /// <param name="va">The VertexAttachment</param>
  185. /// <param name="slot">Slot where the attachment belongs.</param>
  186. /// <param name="buffer">Correctly-sized buffer. Use attachment's .WorldVerticesLength to get the correct size. If null, a new Vector2[] of the correct size will be allocated.</param>
  187. public static Vector2[] GetLocalVertices (this VertexAttachment va, Slot slot, Vector2[] buffer) {
  188. int floatsCount = va.worldVerticesLength;
  189. int bufferTargetSize = floatsCount >> 1;
  190. buffer = buffer ?? new Vector2[bufferTargetSize];
  191. if (buffer.Length < bufferTargetSize) throw new System.ArgumentException(string.Format("Vector2 buffer too small. {0} requires an array of size {1}. Use the attachment's .WorldVerticesLength to get the correct size.", va.Name, floatsCount), "buffer");
  192. if (va.bones == null) {
  193. var localVerts = va.vertices;
  194. for (int i = 0; i < bufferTargetSize; i++) {
  195. int j = i * 2;
  196. buffer[i] = new Vector2(localVerts[j], localVerts[j+1]);
  197. }
  198. } else {
  199. var floats = new float[floatsCount];
  200. va.ComputeWorldVertices(slot, floats);
  201. Bone sb = slot.bone;
  202. float ia, ib, ic, id, bwx = sb.worldX, bwy = sb.worldY;
  203. sb.GetWorldToLocalMatrix(out ia, out ib, out ic, out id);
  204. for (int i = 0; i < bufferTargetSize; i++) {
  205. int j = i * 2;
  206. float x = floats[j] - bwx, y = floats[j+1] - bwy;
  207. buffer[i] = new Vector2(x * ia + y * ib, x * ic + y * id);
  208. }
  209. }
  210. return buffer;
  211. }
  212. /// <summary>Calculates world vertices and fills a Vector2 buffer.</summary>
  213. /// <param name="a">The VertexAttachment</param>
  214. /// <param name="slot">Slot where the attachment belongs.</param>
  215. /// <param name="buffer">Correctly-sized buffer. Use attachment's .WorldVerticesLength to get the correct size. If null, a new Vector2[] of the correct size will be allocated.</param>
  216. public static Vector2[] GetWorldVertices (this VertexAttachment a, Slot slot, Vector2[] buffer) {
  217. int worldVertsLength = a.worldVerticesLength;
  218. int bufferTargetSize = worldVertsLength >> 1;
  219. buffer = buffer ?? new Vector2[bufferTargetSize];
  220. if (buffer.Length < bufferTargetSize) throw new System.ArgumentException(string.Format("Vector2 buffer too small. {0} requires an array of size {1}. Use the attachment's .WorldVerticesLength to get the correct size.", a.Name, worldVertsLength), "buffer");
  221. var floats = new float[worldVertsLength];
  222. a.ComputeWorldVertices(slot, floats);
  223. for (int i = 0, n = worldVertsLength >> 1; i < n; i++) {
  224. int j = i * 2;
  225. buffer[i] = new Vector2(floats[j], floats[j + 1]);
  226. }
  227. return buffer;
  228. }
  229. /// <summary>Gets the PointAttachment's Unity World position using its Spine GameObject Transform.</summary>
  230. public static Vector3 GetWorldPosition (this PointAttachment attachment, Slot slot, Transform spineGameObjectTransform) {
  231. Vector3 skeletonSpacePosition;
  232. skeletonSpacePosition.z = 0;
  233. attachment.ComputeWorldPosition(slot.bone, out skeletonSpacePosition.x, out skeletonSpacePosition.y);
  234. return spineGameObjectTransform.TransformPoint(skeletonSpacePosition);
  235. }
  236. /// <summary>Gets the PointAttachment's Unity World position using its Spine GameObject Transform.</summary>
  237. public static Vector3 GetWorldPosition (this PointAttachment attachment, Bone bone, Transform spineGameObjectTransform) {
  238. Vector3 skeletonSpacePosition;
  239. skeletonSpacePosition.z = 0;
  240. attachment.ComputeWorldPosition(bone, out skeletonSpacePosition.x, out skeletonSpacePosition.y);
  241. return spineGameObjectTransform.TransformPoint(skeletonSpacePosition);
  242. }
  243. #endregion
  244. }
  245. }
  246. namespace Spine {
  247. using System;
  248. using System.Collections.Generic;
  249. public struct BoneMatrix {
  250. public float a, b, c, d, x, y;
  251. /// <summary>Recursively calculates a worldspace bone matrix based on BoneData.</summary>
  252. public static BoneMatrix CalculateSetupWorld (BoneData boneData) {
  253. if (boneData == null)
  254. return default(BoneMatrix);
  255. // End condition: isRootBone
  256. if (boneData.parent == null)
  257. return GetInheritedInternal(boneData, default(BoneMatrix));
  258. BoneMatrix result = CalculateSetupWorld(boneData.parent);
  259. return GetInheritedInternal(boneData, result);
  260. }
  261. static BoneMatrix GetInheritedInternal (BoneData boneData, BoneMatrix parentMatrix) {
  262. var parent = boneData.parent;
  263. if (parent == null) return new BoneMatrix(boneData); // isRootBone
  264. float pa = parentMatrix.a, pb = parentMatrix.b, pc = parentMatrix.c, pd = parentMatrix.d;
  265. BoneMatrix result = default(BoneMatrix);
  266. result.x = pa * boneData.x + pb * boneData.y + parentMatrix.x;
  267. result.y = pc * boneData.x + pd * boneData.y + parentMatrix.y;
  268. switch (boneData.transformMode) {
  269. case TransformMode.Normal: {
  270. float rotationY = boneData.rotation + 90 + boneData.shearY;
  271. float la = MathUtils.CosDeg(boneData.rotation + boneData.shearX) * boneData.scaleX;
  272. float lb = MathUtils.CosDeg(rotationY) * boneData.scaleY;
  273. float lc = MathUtils.SinDeg(boneData.rotation + boneData.shearX) * boneData.scaleX;
  274. float ld = MathUtils.SinDeg(rotationY) * boneData.scaleY;
  275. result.a = pa * la + pb * lc;
  276. result.b = pa * lb + pb * ld;
  277. result.c = pc * la + pd * lc;
  278. result.d = pc * lb + pd * ld;
  279. break;
  280. }
  281. case TransformMode.OnlyTranslation: {
  282. float rotationY = boneData.rotation + 90 + boneData.shearY;
  283. result.a = MathUtils.CosDeg(boneData.rotation + boneData.shearX) * boneData.scaleX;
  284. result.b = MathUtils.CosDeg(rotationY) * boneData.scaleY;
  285. result.c = MathUtils.SinDeg(boneData.rotation + boneData.shearX) * boneData.scaleX;
  286. result.d = MathUtils.SinDeg(rotationY) * boneData.scaleY;
  287. break;
  288. }
  289. case TransformMode.NoRotationOrReflection: {
  290. float s = pa * pa + pc * pc, prx;
  291. if (s > 0.0001f) {
  292. s = Math.Abs(pa * pd - pb * pc) / s;
  293. pb = pc * s;
  294. pd = pa * s;
  295. prx = MathUtils.Atan2(pc, pa) * MathUtils.RadDeg;
  296. } else {
  297. pa = 0;
  298. pc = 0;
  299. prx = 90 - MathUtils.Atan2(pd, pb) * MathUtils.RadDeg;
  300. }
  301. float rx = boneData.rotation + boneData.shearX - prx;
  302. float ry = boneData.rotation + boneData.shearY - prx + 90;
  303. float la = MathUtils.CosDeg(rx) * boneData.scaleX;
  304. float lb = MathUtils.CosDeg(ry) * boneData.scaleY;
  305. float lc = MathUtils.SinDeg(rx) * boneData.scaleX;
  306. float ld = MathUtils.SinDeg(ry) * boneData.scaleY;
  307. result.a = pa * la - pb * lc;
  308. result.b = pa * lb - pb * ld;
  309. result.c = pc * la + pd * lc;
  310. result.d = pc * lb + pd * ld;
  311. break;
  312. }
  313. case TransformMode.NoScale:
  314. case TransformMode.NoScaleOrReflection: {
  315. float cos = MathUtils.CosDeg(boneData.rotation), sin = MathUtils.SinDeg(boneData.rotation);
  316. float za = pa * cos + pb * sin;
  317. float zc = pc * cos + pd * sin;
  318. float s = (float)Math.Sqrt(za * za + zc * zc);
  319. if (s > 0.00001f)
  320. s = 1 / s;
  321. za *= s;
  322. zc *= s;
  323. s = (float)Math.Sqrt(za * za + zc * zc);
  324. float r = MathUtils.PI / 2 + MathUtils.Atan2(zc, za);
  325. float zb = MathUtils.Cos(r) * s;
  326. float zd = MathUtils.Sin(r) * s;
  327. float la = MathUtils.CosDeg(boneData.shearX) * boneData.scaleX;
  328. float lb = MathUtils.CosDeg(90 + boneData.shearY) * boneData.scaleY;
  329. float lc = MathUtils.SinDeg(boneData.shearX) * boneData.scaleX;
  330. float ld = MathUtils.SinDeg(90 + boneData.shearY) * boneData.scaleY;
  331. if (boneData.transformMode != TransformMode.NoScaleOrReflection ? pa * pd - pb * pc < 0 : false) {
  332. zb = -zb;
  333. zd = -zd;
  334. }
  335. result.a = za * la + zb * lc;
  336. result.b = za * lb + zb * ld;
  337. result.c = zc * la + zd * lc;
  338. result.d = zc * lb + zd * ld;
  339. break;
  340. }
  341. }
  342. return result;
  343. }
  344. /// <summary>Constructor for a local bone matrix based on Setup Pose BoneData.</summary>
  345. public BoneMatrix (BoneData boneData) {
  346. float rotationY = boneData.rotation + 90 + boneData.shearY;
  347. float rotationX = boneData.rotation + boneData.shearX;
  348. a = MathUtils.CosDeg(rotationX) * boneData.scaleX;
  349. c = MathUtils.SinDeg(rotationX) * boneData.scaleX;
  350. b = MathUtils.CosDeg(rotationY) * boneData.scaleY;
  351. d = MathUtils.SinDeg(rotationY) * boneData.scaleY;
  352. x = boneData.x;
  353. y = boneData.y;
  354. }
  355. /// <summary>Constructor for a local bone matrix based on a bone instance's current pose.</summary>
  356. public BoneMatrix (Bone bone) {
  357. float rotationY = bone.rotation + 90 + bone.shearY;
  358. float rotationX = bone.rotation + bone.shearX;
  359. a = MathUtils.CosDeg(rotationX) * bone.scaleX;
  360. c = MathUtils.SinDeg(rotationX) * bone.scaleX;
  361. b = MathUtils.CosDeg(rotationY) * bone.scaleY;
  362. d = MathUtils.SinDeg(rotationY) * bone.scaleY;
  363. x = bone.x;
  364. y = bone.y;
  365. }
  366. public BoneMatrix TransformMatrix (BoneMatrix local) {
  367. return new BoneMatrix {
  368. a = this.a * local.a + this.b * local.c,
  369. b = this.a * local.b + this.b * local.d,
  370. c = this.c * local.a + this.d * local.c,
  371. d = this.c * local.b + this.d * local.d,
  372. x = this.a * local.x + this.b * local.y + this.x,
  373. y = this.c * local.x + this.d * local.y + this.y
  374. };
  375. }
  376. }
  377. public static class SkeletonExtensions {
  378. public static bool IsWeighted (this VertexAttachment va) {
  379. return va.bones != null && va.bones.Length > 0;
  380. }
  381. public static bool IsRenderable (this Attachment a) {
  382. return a is IHasRendererObject;
  383. }
  384. #region Transform Modes
  385. public static bool InheritsRotation (this TransformMode mode) {
  386. const int RotationBit = 0;
  387. return ((int)mode & (1U << RotationBit)) == 0;
  388. }
  389. public static bool InheritsScale (this TransformMode mode) {
  390. const int ScaleBit = 1;
  391. return ((int)mode & (1U << ScaleBit)) == 0;
  392. }
  393. #endregion
  394. #region Posing
  395. internal static void SetPropertyToSetupPose (this Skeleton skeleton, int propertyID) {
  396. int tt = propertyID >> 24;
  397. var timelineType = (TimelineType)tt;
  398. int i = propertyID - (tt << 24);
  399. Bone bone;
  400. IkConstraint ikc;
  401. PathConstraint pc;
  402. switch (timelineType) {
  403. // Bone
  404. case TimelineType.Rotate:
  405. bone = skeleton.bones.Items[i];
  406. bone.rotation = bone.data.rotation;
  407. break;
  408. case TimelineType.Translate:
  409. bone = skeleton.bones.Items[i];
  410. bone.x = bone.data.x;
  411. bone.y = bone.data.y;
  412. break;
  413. case TimelineType.Scale:
  414. bone = skeleton.bones.Items[i];
  415. bone.scaleX = bone.data.scaleX;
  416. bone.scaleY = bone.data.scaleY;
  417. break;
  418. case TimelineType.Shear:
  419. bone = skeleton.bones.Items[i];
  420. bone.shearX = bone.data.shearX;
  421. bone.shearY = bone.data.shearY;
  422. break;
  423. // Slot
  424. case TimelineType.Attachment:
  425. skeleton.SetSlotAttachmentToSetupPose(i);
  426. break;
  427. case TimelineType.Color:
  428. skeleton.slots.Items[i].SetColorToSetupPose();
  429. break;
  430. case TimelineType.TwoColor:
  431. skeleton.slots.Items[i].SetColorToSetupPose();
  432. break;
  433. case TimelineType.Deform:
  434. skeleton.slots.Items[i].attachmentVertices.Clear();
  435. break;
  436. // Skeleton
  437. case TimelineType.DrawOrder:
  438. skeleton.SetDrawOrderToSetupPose();
  439. break;
  440. // IK Constraint
  441. case TimelineType.IkConstraint:
  442. ikc = skeleton.ikConstraints.Items[i];
  443. ikc.mix = ikc.data.mix;
  444. ikc.bendDirection = ikc.data.bendDirection;
  445. break;
  446. // TransformConstraint
  447. case TimelineType.TransformConstraint:
  448. var tc = skeleton.transformConstraints.Items[i];
  449. var tcData = tc.data;
  450. tc.rotateMix = tcData.rotateMix;
  451. tc.translateMix = tcData.translateMix;
  452. tc.scaleMix = tcData.scaleMix;
  453. tc.shearMix = tcData.shearMix;
  454. break;
  455. // Path Constraint
  456. case TimelineType.PathConstraintPosition:
  457. pc = skeleton.pathConstraints.Items[i];
  458. pc.position = pc.data.position;
  459. break;
  460. case TimelineType.PathConstraintSpacing:
  461. pc = skeleton.pathConstraints.Items[i];
  462. pc.spacing = pc.data.spacing;
  463. break;
  464. case TimelineType.PathConstraintMix:
  465. pc = skeleton.pathConstraints.Items[i];
  466. pc.rotateMix = pc.data.rotateMix;
  467. pc.translateMix = pc.data.translateMix;
  468. break;
  469. }
  470. }
  471. /// <summary>Resets the DrawOrder to the Setup Pose's draw order</summary>
  472. public static void SetDrawOrderToSetupPose (this Skeleton skeleton) {
  473. var slotsItems = skeleton.slots.Items;
  474. int n = skeleton.slots.Count;
  475. var drawOrder = skeleton.drawOrder;
  476. drawOrder.Clear(false);
  477. drawOrder.GrowIfNeeded(n);
  478. System.Array.Copy(slotsItems, drawOrder.Items, n);
  479. }
  480. /// <summary>Resets the color of a slot to Setup Pose value.</summary>
  481. public static void SetColorToSetupPose (this Slot slot) {
  482. slot.r = slot.data.r;
  483. slot.g = slot.data.g;
  484. slot.b = slot.data.b;
  485. slot.a = slot.data.a;
  486. slot.r2 = slot.data.r2;
  487. slot.g2 = slot.data.g2;
  488. slot.b2 = slot.data.b2;
  489. }
  490. /// <summary>Sets a slot's attachment to setup pose. If you have the slotIndex, Skeleton.SetSlotAttachmentToSetupPose is faster.</summary>
  491. public static void SetAttachmentToSetupPose (this Slot slot) {
  492. var slotData = slot.data;
  493. slot.Attachment = slot.bone.skeleton.GetAttachment(slotData.name, slotData.attachmentName);
  494. }
  495. /// <summary>Resets the attachment of slot at a given slotIndex to setup pose. This is faster than Slot.SetAttachmentToSetupPose.</summary>
  496. public static void SetSlotAttachmentToSetupPose (this Skeleton skeleton, int slotIndex) {
  497. var slot = skeleton.slots.Items[slotIndex];
  498. var attachmentName = slot.data.attachmentName;
  499. if (string.IsNullOrEmpty(attachmentName)) {
  500. slot.Attachment = null;
  501. } else {
  502. var attachment = skeleton.GetAttachment(slotIndex, attachmentName);
  503. slot.Attachment = attachment;
  504. }
  505. }
  506. /// <summary>
  507. /// Shortcut for posing a skeleton at a specific time. Time is in seconds. (frameNumber / 30f) will give you seconds.
  508. /// If you need to do this often, you should get the Animation object yourself using skeleton.data.FindAnimation. and call Apply on that.</summary>
  509. /// <param name = "skeleton">The skeleton to pose.</param>
  510. /// <param name="animationName">The name of the animation to use.</param>
  511. /// <param name = "time">The time of the pose within the animation.</param>
  512. /// <param name = "loop">Wraps the time around if it is longer than the duration of the animation.</param>
  513. public static void PoseWithAnimation (this Skeleton skeleton, string animationName, float time, bool loop = false) {
  514. // Fail loud when skeleton.data is null.
  515. Spine.Animation animation = skeleton.data.FindAnimation(animationName);
  516. if (animation == null) return;
  517. animation.Apply(skeleton, 0, time, loop, null, 1f, MixPose.Setup, MixDirection.In);
  518. }
  519. /// <summary>Pose a skeleton according to a given time in an animation.</summary>
  520. public static void PoseSkeleton (this Animation animation, Skeleton skeleton, float time, bool loop = false) {
  521. animation.Apply(skeleton, 0, time, loop, null, 1f, MixPose.Setup, MixDirection.In);
  522. }
  523. /// <summary>Resets Skeleton parts to Setup Pose according to a Spine.Animation's keyed items.</summary>
  524. public static void SetKeyedItemsToSetupPose (this Animation animation, Skeleton skeleton) {
  525. animation.Apply(skeleton, 0, 0, false, null, 0, MixPose.Setup, MixDirection.Out);
  526. }
  527. #endregion
  528. #region Skins
  529. /// <summary><see cref="Spine.Skin.FindNamesForSlot(int,List)"/></summary>
  530. public static void FindNamesForSlot (this Skin skin, string slotName, SkeletonData skeletonData, List<string> results) {
  531. int slotIndex = skeletonData.FindSlotIndex(slotName);
  532. skin.FindNamesForSlot(slotIndex, results);
  533. }
  534. /// <summary><see cref="Spine.Skin.FindAttachmentsForSlot(int,List)"/></summary>
  535. public static void FindAttachmentsForSlot (this Skin skin, string slotName, SkeletonData skeletonData, List<Attachment> results) {
  536. int slotIndex = skeletonData.FindSlotIndex(slotName);
  537. skin.FindAttachmentsForSlot(slotIndex, results);
  538. }
  539. #endregion
  540. }
  541. }