Skeleton.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. using System.Collections.Generic;
  31. namespace Spine {
  32. public class Skeleton {
  33. internal SkeletonData data;
  34. internal ExposedList<Bone> bones;
  35. internal ExposedList<Slot> slots;
  36. internal ExposedList<Slot> drawOrder;
  37. internal ExposedList<IkConstraint> ikConstraints;
  38. internal ExposedList<TransformConstraint> transformConstraints;
  39. internal ExposedList<PathConstraint> pathConstraints;
  40. internal ExposedList<IUpdatable> updateCache = new ExposedList<IUpdatable>();
  41. internal ExposedList<Bone> updateCacheReset = new ExposedList<Bone>();
  42. internal Skin skin;
  43. internal float r = 1, g = 1, b = 1, a = 1;
  44. internal float time;
  45. private float scaleX = 1, scaleY = 1;
  46. internal float x, y;
  47. public SkeletonData Data { get { return data; } }
  48. public ExposedList<Bone> Bones { get { return bones; } }
  49. public ExposedList<IUpdatable> UpdateCacheList { get { return updateCache; } }
  50. public ExposedList<Slot> Slots { get { return slots; } }
  51. public ExposedList<Slot> DrawOrder { get { return drawOrder; } }
  52. public ExposedList<IkConstraint> IkConstraints { get { return ikConstraints; } }
  53. public ExposedList<PathConstraint> PathConstraints { get { return pathConstraints; } }
  54. public ExposedList<TransformConstraint> TransformConstraints { get { return transformConstraints; } }
  55. public Skin Skin { get { return skin; } set { SetSkin(value); } }
  56. public float R { get { return r; } set { r = value; } }
  57. public float G { get { return g; } set { g = value; } }
  58. public float B { get { return b; } set { b = value; } }
  59. public float A { get { return a; } set { a = value; } }
  60. public float Time { get { return time; } set { time = value; } }
  61. public float X { get { return x; } set { x = value; } }
  62. public float Y { get { return y; } set { y = value; } }
  63. public float ScaleX { get { return scaleX; } set { scaleX = value; } }
  64. public float ScaleY { get { return scaleY * (Bone.yDown ? -1 : 1); } set { scaleY = value; } }
  65. [Obsolete("Use ScaleX instead. FlipX is when ScaleX is negative.")]
  66. public bool FlipX { get { return scaleX < 0; } set { scaleX = value ? -1f : 1f; } }
  67. [Obsolete("Use ScaleY instead. FlipY is when ScaleY is negative.")]
  68. public bool FlipY { get { return scaleY < 0; } set { scaleY = value ? -1f : 1f; } }
  69. public Bone RootBone {
  70. get { return bones.Count == 0 ? null : bones.Items[0]; }
  71. }
  72. public Skeleton (SkeletonData data) {
  73. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  74. this.data = data;
  75. bones = new ExposedList<Bone>(data.bones.Count);
  76. foreach (BoneData boneData in data.bones) {
  77. Bone bone;
  78. if (boneData.parent == null) {
  79. bone = new Bone(boneData, this, null);
  80. } else {
  81. Bone parent = bones.Items[boneData.parent.index];
  82. bone = new Bone(boneData, this, parent);
  83. parent.children.Add(bone);
  84. }
  85. bones.Add(bone);
  86. }
  87. slots = new ExposedList<Slot>(data.slots.Count);
  88. drawOrder = new ExposedList<Slot>(data.slots.Count);
  89. foreach (SlotData slotData in data.slots) {
  90. Bone bone = bones.Items[slotData.boneData.index];
  91. Slot slot = new Slot(slotData, bone);
  92. slots.Add(slot);
  93. drawOrder.Add(slot);
  94. }
  95. ikConstraints = new ExposedList<IkConstraint>(data.ikConstraints.Count);
  96. foreach (IkConstraintData ikConstraintData in data.ikConstraints)
  97. ikConstraints.Add(new IkConstraint(ikConstraintData, this));
  98. transformConstraints = new ExposedList<TransformConstraint>(data.transformConstraints.Count);
  99. foreach (TransformConstraintData transformConstraintData in data.transformConstraints)
  100. transformConstraints.Add(new TransformConstraint(transformConstraintData, this));
  101. pathConstraints = new ExposedList<PathConstraint> (data.pathConstraints.Count);
  102. foreach (PathConstraintData pathConstraintData in data.pathConstraints)
  103. pathConstraints.Add(new PathConstraint(pathConstraintData, this));
  104. UpdateCache();
  105. UpdateWorldTransform();
  106. }
  107. /// <summary>Caches information about bones and constraints. Must be called if the <see cref="Skin"/> is modified or if bones, constraints, or
  108. /// constraints, or weighted path attachments are added or removed.</summary>
  109. public void UpdateCache () {
  110. var updateCache = this.updateCache;
  111. updateCache.Clear();
  112. this.updateCacheReset.Clear();
  113. int boneCount = this.bones.Items.Length;
  114. var bones = this.bones;
  115. for (int i = 0; i < boneCount; i++) {
  116. Bone bone = bones.Items[i];
  117. bone.sorted = bone.data.skinRequired;
  118. bone.active = !bone.sorted;
  119. }
  120. if (skin != null) {
  121. Object[] skinBones = skin.bones.Items;
  122. for (int i = 0, n = skin.bones.Count; i < n; i++) {
  123. Bone bone = (Bone)bones.Items[((BoneData)skinBones[i]).index];
  124. do {
  125. bone.sorted = false;
  126. bone.active = true;
  127. bone = bone.parent;
  128. } while (bone != null);
  129. }
  130. }
  131. int ikCount = this.ikConstraints.Count, transformCount = this.transformConstraints.Count, pathCount = this.pathConstraints.Count;
  132. var ikConstraints = this.ikConstraints;
  133. var transformConstraints = this.transformConstraints;
  134. var pathConstraints = this.pathConstraints;
  135. int constraintCount = ikCount + transformCount + pathCount;
  136. //outer:
  137. for (int i = 0; i < constraintCount; i++) {
  138. for (int ii = 0; ii < ikCount; ii++) {
  139. IkConstraint constraint = ikConstraints.Items[ii];
  140. if (constraint.data.order == i) {
  141. SortIkConstraint(constraint);
  142. goto continue_outer; //continue outer;
  143. }
  144. }
  145. for (int ii = 0; ii < transformCount; ii++) {
  146. TransformConstraint constraint = transformConstraints.Items[ii];
  147. if (constraint.data.order == i) {
  148. SortTransformConstraint(constraint);
  149. goto continue_outer; //continue outer;
  150. }
  151. }
  152. for (int ii = 0; ii < pathCount; ii++) {
  153. PathConstraint constraint = pathConstraints.Items[ii];
  154. if (constraint.data.order == i) {
  155. SortPathConstraint(constraint);
  156. goto continue_outer; //continue outer;
  157. }
  158. }
  159. continue_outer: {}
  160. }
  161. for (int i = 0; i < boneCount; i++)
  162. SortBone(bones.Items[i]);
  163. }
  164. private void SortIkConstraint (IkConstraint constraint) {
  165. constraint.active = constraint.target.active
  166. && (!constraint.data.skinRequired || (skin != null && skin.constraints.Contains(constraint.data)));
  167. if (!constraint.active) return;
  168. Bone target = constraint.target;
  169. SortBone(target);
  170. var constrained = constraint.bones;
  171. Bone parent = constrained.Items[0];
  172. SortBone(parent);
  173. if (constrained.Count > 1) {
  174. Bone child = constrained.Items[constrained.Count - 1];
  175. if (!updateCache.Contains(child))
  176. updateCacheReset.Add(child);
  177. }
  178. updateCache.Add(constraint);
  179. SortReset(parent.children);
  180. constrained.Items[constrained.Count - 1].sorted = true;
  181. }
  182. private void SortPathConstraint (PathConstraint constraint) {
  183. constraint.active = constraint.target.bone.active
  184. && (!constraint.data.skinRequired || (skin != null && skin.constraints.Contains(constraint.data)));
  185. if (!constraint.active) return;
  186. Slot slot = constraint.target;
  187. int slotIndex = slot.data.index;
  188. Bone slotBone = slot.bone;
  189. if (skin != null) SortPathConstraintAttachment(skin, slotIndex, slotBone);
  190. if (data.defaultSkin != null && data.defaultSkin != skin)
  191. SortPathConstraintAttachment(data.defaultSkin, slotIndex, slotBone);
  192. Attachment attachment = slot.attachment;
  193. if (attachment is PathAttachment) SortPathConstraintAttachment(attachment, slotBone);
  194. var constrained = constraint.bones;
  195. int boneCount = constrained.Count;
  196. for (int i = 0; i < boneCount; i++)
  197. SortBone(constrained.Items[i]);
  198. updateCache.Add(constraint);
  199. for (int i = 0; i < boneCount; i++)
  200. SortReset(constrained.Items[i].children);
  201. for (int i = 0; i < boneCount; i++)
  202. constrained.Items[i].sorted = true;
  203. }
  204. private void SortTransformConstraint (TransformConstraint constraint) {
  205. constraint.active = constraint.target.active
  206. && (!constraint.data.skinRequired || (skin != null && skin.constraints.Contains(constraint.data)));
  207. if (!constraint.active) return;
  208. SortBone(constraint.target);
  209. var constrained = constraint.bones;
  210. int boneCount = constrained.Count;
  211. if (constraint.data.local) {
  212. for (int i = 0; i < boneCount; i++) {
  213. Bone child = constrained.Items[i];
  214. SortBone(child.parent);
  215. if (!updateCache.Contains(child)) updateCacheReset.Add(child);
  216. }
  217. } else {
  218. for (int i = 0; i < boneCount; i++)
  219. SortBone(constrained.Items[i]);
  220. }
  221. updateCache.Add(constraint);
  222. for (int i = 0; i < boneCount; i++)
  223. SortReset(constrained.Items[i].children);
  224. for (int i = 0; i < boneCount; i++)
  225. constrained.Items[i].sorted = true;
  226. }
  227. private void SortPathConstraintAttachment (Skin skin, int slotIndex, Bone slotBone) {
  228. foreach (var entryObj in skin.Attachments.Keys) {
  229. var entry = (Skin.SkinEntry)entryObj;
  230. if (entry.SlotIndex == slotIndex) SortPathConstraintAttachment(entry.Attachment, slotBone);
  231. }
  232. }
  233. private void SortPathConstraintAttachment (Attachment attachment, Bone slotBone) {
  234. if (!(attachment is PathAttachment)) return;
  235. int[] pathBones = ((PathAttachment)attachment).bones;
  236. if (pathBones == null)
  237. SortBone(slotBone);
  238. else {
  239. var bones = this.bones;
  240. for (int i = 0, n = pathBones.Length; i < n;) {
  241. int nn = pathBones[i++];
  242. nn += i;
  243. while (i < nn)
  244. SortBone(bones.Items[pathBones[i++]]);
  245. }
  246. }
  247. }
  248. private void SortBone (Bone bone) {
  249. if (bone.sorted) return;
  250. Bone parent = bone.parent;
  251. if (parent != null) SortBone(parent);
  252. bone.sorted = true;
  253. updateCache.Add(bone);
  254. }
  255. private static void SortReset (ExposedList<Bone> bones) {
  256. var bonesItems = bones.Items;
  257. for (int i = 0, n = bones.Count; i < n; i++) {
  258. Bone bone = bonesItems[i];
  259. if (!bone.active) continue;
  260. if (bone.sorted) SortReset(bone.children);
  261. bone.sorted = false;
  262. }
  263. }
  264. /// <summary>Updates the world transform for each bone and applies constraints.</summary>
  265. public void UpdateWorldTransform () {
  266. var updateCacheReset = this.updateCacheReset;
  267. var updateCacheResetItems = updateCacheReset.Items;
  268. for (int i = 0, n = updateCacheReset.Count; i < n; i++) {
  269. Bone bone = updateCacheResetItems[i];
  270. bone.ax = bone.x;
  271. bone.ay = bone.y;
  272. bone.arotation = bone.rotation;
  273. bone.ascaleX = bone.scaleX;
  274. bone.ascaleY = bone.scaleY;
  275. bone.ashearX = bone.shearX;
  276. bone.ashearY = bone.shearY;
  277. bone.appliedValid = true;
  278. }
  279. var updateItems = this.updateCache.Items;
  280. for (int i = 0, n = updateCache.Count; i < n; i++)
  281. updateItems[i].Update();
  282. }
  283. /// <summary>
  284. /// Temporarily sets the root bone as a child of the specified bone, then updates the world transform for each bone and applies
  285. /// all constraints.
  286. /// </summary>
  287. public void UpdateWorldTransform (Bone parent) {
  288. // This partial update avoids computing the world transform for constrained bones when 1) the bone is not updated
  289. // before the constraint, 2) the constraint only needs to access the applied local transform, and 3) the constraint calls
  290. // updateWorldTransform.
  291. var updateCacheReset = this.updateCacheReset;
  292. var updateCacheResetItems = updateCacheReset.Items;
  293. for (int i = 0, n = updateCacheReset.Count; i < n; i++) {
  294. Bone bone = updateCacheResetItems[i];
  295. bone.ax = bone.x;
  296. bone.ay = bone.y;
  297. bone.arotation = bone.rotation;
  298. bone.ascaleX = bone.scaleX;
  299. bone.ascaleY = bone.scaleY;
  300. bone.ashearX = bone.shearX;
  301. bone.ashearY = bone.shearY;
  302. bone.appliedValid = true;
  303. }
  304. // Apply the parent bone transform to the root bone. The root bone always inherits scale, rotation and reflection.
  305. Bone rootBone = this.RootBone;
  306. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  307. rootBone.worldX = pa * x + pb * y + parent.worldX;
  308. rootBone.worldY = pc * x + pd * y + parent.worldY;
  309. float rotationY = rootBone.rotation + 90 + rootBone.shearY;
  310. float la = MathUtils.CosDeg(rootBone.rotation + rootBone.shearX) * rootBone.scaleX;
  311. float lb = MathUtils.CosDeg(rotationY) * rootBone.scaleY;
  312. float lc = MathUtils.SinDeg(rootBone.rotation + rootBone.shearX) * rootBone.scaleX;
  313. float ld = MathUtils.SinDeg(rotationY) * rootBone.scaleY;
  314. rootBone.a = (pa * la + pb * lc) * scaleX;
  315. rootBone.b = (pa * lb + pb * ld) * scaleX;
  316. rootBone.c = (pc * la + pd * lc) * scaleY;
  317. rootBone.d = (pc * lb + pd * ld) * scaleY;
  318. // Update everything except root bone.
  319. var updateCache = this.updateCache;
  320. var updateCacheItems = updateCache.Items;
  321. for (int i = 0, n = updateCache.Count; i < n; i++) {
  322. var updatable = updateCacheItems[i];
  323. if (updatable != rootBone)
  324. updatable.Update();
  325. }
  326. }
  327. /// <summary>Sets the bones, constraints, and slots to their setup pose values.</summary>
  328. public void SetToSetupPose () {
  329. SetBonesToSetupPose();
  330. SetSlotsToSetupPose();
  331. }
  332. /// <summary>Sets the bones and constraints to their setup pose values.</summary>
  333. public void SetBonesToSetupPose () {
  334. var bonesItems = this.bones.Items;
  335. for (int i = 0, n = bones.Count; i < n; i++)
  336. bonesItems[i].SetToSetupPose();
  337. var ikConstraintsItems = this.ikConstraints.Items;
  338. for (int i = 0, n = ikConstraints.Count; i < n; i++) {
  339. IkConstraint constraint = ikConstraintsItems[i];
  340. constraint.mix = constraint.data.mix;
  341. constraint.softness = constraint.data.softness;
  342. constraint.bendDirection = constraint.data.bendDirection;
  343. constraint.compress = constraint.data.compress;
  344. constraint.stretch = constraint.data.stretch;
  345. }
  346. var transformConstraintsItems = this.transformConstraints.Items;
  347. for (int i = 0, n = transformConstraints.Count; i < n; i++) {
  348. TransformConstraint constraint = transformConstraintsItems[i];
  349. TransformConstraintData constraintData = constraint.data;
  350. constraint.rotateMix = constraintData.rotateMix;
  351. constraint.translateMix = constraintData.translateMix;
  352. constraint.scaleMix = constraintData.scaleMix;
  353. constraint.shearMix = constraintData.shearMix;
  354. }
  355. var pathConstraintItems = this.pathConstraints.Items;
  356. for (int i = 0, n = pathConstraints.Count; i < n; i++) {
  357. PathConstraint constraint = pathConstraintItems[i];
  358. PathConstraintData constraintData = constraint.data;
  359. constraint.position = constraintData.position;
  360. constraint.spacing = constraintData.spacing;
  361. constraint.rotateMix = constraintData.rotateMix;
  362. constraint.translateMix = constraintData.translateMix;
  363. }
  364. }
  365. public void SetSlotsToSetupPose () {
  366. var slots = this.slots;
  367. var slotsItems = slots.Items;
  368. drawOrder.Clear();
  369. for (int i = 0, n = slots.Count; i < n; i++)
  370. drawOrder.Add(slotsItems[i]);
  371. for (int i = 0, n = slots.Count; i < n; i++)
  372. slotsItems[i].SetToSetupPose();
  373. }
  374. /// <returns>May be null.</returns>
  375. public Bone FindBone (string boneName) {
  376. if (boneName == null) throw new ArgumentNullException("boneName", "boneName cannot be null.");
  377. var bones = this.bones;
  378. var bonesItems = bones.Items;
  379. for (int i = 0, n = bones.Count; i < n; i++) {
  380. Bone bone = bonesItems[i];
  381. if (bone.data.name == boneName) return bone;
  382. }
  383. return null;
  384. }
  385. /// <returns>-1 if the bone was not found.</returns>
  386. public int FindBoneIndex (string boneName) {
  387. if (boneName == null) throw new ArgumentNullException("boneName", "boneName cannot be null.");
  388. var bones = this.bones;
  389. var bonesItems = bones.Items;
  390. for (int i = 0, n = bones.Count; i < n; i++)
  391. if (bonesItems[i].data.name == boneName) return i;
  392. return -1;
  393. }
  394. /// <returns>May be null.</returns>
  395. public Slot FindSlot (string slotName) {
  396. if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
  397. var slots = this.slots;
  398. var slotsItems = slots.Items;
  399. for (int i = 0, n = slots.Count; i < n; i++) {
  400. Slot slot = slotsItems[i];
  401. if (slot.data.name == slotName) return slot;
  402. }
  403. return null;
  404. }
  405. /// <returns>-1 if the bone was not found.</returns>
  406. public int FindSlotIndex (string slotName) {
  407. if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
  408. var slots = this.slots;
  409. var slotsItems = slots.Items;
  410. for (int i = 0, n = slots.Count; i < n; i++)
  411. if (slotsItems[i].data.name.Equals(slotName)) return i;
  412. return -1;
  413. }
  414. /// <summary>Sets a skin by name (see SetSkin).</summary>
  415. public void SetSkin (string skinName) {
  416. Skin foundSkin = data.FindSkin(skinName);
  417. if (foundSkin == null) throw new ArgumentException("Skin not found: " + skinName, "skinName");
  418. SetSkin(foundSkin);
  419. }
  420. /// <summary>
  421. /// <para>Sets the skin used to look up attachments before looking in the <see cref="SkeletonData.DefaultSkin"/>. If the
  422. /// skin is changed, <see cref="UpdateCache()"/> is called.
  423. /// </para>
  424. /// <para>Attachments from the new skin are attached if the corresponding attachment from the old skin was attached.
  425. /// If there was no old skin, each slot's setup mode attachment is attached from the new skin.
  426. /// </para>
  427. /// <para>After changing the skin, the visible attachments can be reset to those attached in the setup pose by calling
  428. /// <see cref="Skeleton.SetSlotsToSetupPose()"/>.
  429. /// Also, often <see cref="AnimationState.Apply(Skeleton)"/> is called before the next time the
  430. /// skeleton is rendered to allow any attachment keys in the current animation(s) to hide or show attachments from the new skin.</para>
  431. /// </summary>
  432. /// <param name="newSkin">May be null.</param>
  433. public void SetSkin (Skin newSkin) {
  434. if (newSkin == skin) return;
  435. if (newSkin != null) {
  436. if (skin != null)
  437. newSkin.AttachAll(this, skin);
  438. else {
  439. ExposedList<Slot> slots = this.slots;
  440. for (int i = 0, n = slots.Count; i < n; i++) {
  441. Slot slot = slots.Items[i];
  442. string name = slot.data.attachmentName;
  443. if (name != null) {
  444. Attachment attachment = newSkin.GetAttachment(i, name);
  445. if (attachment != null) slot.Attachment = attachment;
  446. }
  447. }
  448. }
  449. }
  450. skin = newSkin;
  451. UpdateCache();
  452. }
  453. /// <summary>Finds an attachment by looking in the {@link #skin} and {@link SkeletonData#defaultSkin} using the slot name and attachment name.</summary>
  454. /// <returns>May be null.</returns>
  455. public Attachment GetAttachment (string slotName, string attachmentName) {
  456. return GetAttachment(data.FindSlotIndex(slotName), attachmentName);
  457. }
  458. /// <summary>Finds an attachment by looking in the skin and skeletonData.defaultSkin using the slot index and attachment name.First the skin is checked and if the attachment was not found, the default skin is checked.</summary>
  459. /// <returns>May be null.</returns>
  460. public Attachment GetAttachment (int slotIndex, string attachmentName) {
  461. if (attachmentName == null) throw new ArgumentNullException("attachmentName", "attachmentName cannot be null.");
  462. if (skin != null) {
  463. Attachment attachment = skin.GetAttachment(slotIndex, attachmentName);
  464. if (attachment != null) return attachment;
  465. }
  466. return data.defaultSkin != null ? data.defaultSkin.GetAttachment(slotIndex, attachmentName) : null;
  467. }
  468. /// <summary>A convenience method to set an attachment by finding the slot with FindSlot, finding the attachment with GetAttachment, then setting the slot's slot.Attachment.</summary>
  469. /// <param name="attachmentName">May be null to clear the slot's attachment.</param>
  470. public void SetAttachment (string slotName, string attachmentName) {
  471. if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
  472. ExposedList<Slot> slots = this.slots;
  473. for (int i = 0, n = slots.Count; i < n; i++) {
  474. Slot slot = slots.Items[i];
  475. if (slot.data.name == slotName) {
  476. Attachment attachment = null;
  477. if (attachmentName != null) {
  478. attachment = GetAttachment(i, attachmentName);
  479. if (attachment == null) throw new Exception("Attachment not found: " + attachmentName + ", for slot: " + slotName);
  480. }
  481. slot.Attachment = attachment;
  482. return;
  483. }
  484. }
  485. throw new Exception("Slot not found: " + slotName);
  486. }
  487. /// <returns>May be null.</returns>
  488. public IkConstraint FindIkConstraint (string constraintName) {
  489. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  490. ExposedList<IkConstraint> ikConstraints = this.ikConstraints;
  491. for (int i = 0, n = ikConstraints.Count; i < n; i++) {
  492. IkConstraint ikConstraint = ikConstraints.Items[i];
  493. if (ikConstraint.data.name == constraintName) return ikConstraint;
  494. }
  495. return null;
  496. }
  497. /// <returns>May be null.</returns>
  498. public TransformConstraint FindTransformConstraint (string constraintName) {
  499. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  500. ExposedList<TransformConstraint> transformConstraints = this.transformConstraints;
  501. for (int i = 0, n = transformConstraints.Count; i < n; i++) {
  502. TransformConstraint transformConstraint = transformConstraints.Items[i];
  503. if (transformConstraint.data.Name == constraintName) return transformConstraint;
  504. }
  505. return null;
  506. }
  507. /// <returns>May be null.</returns>
  508. public PathConstraint FindPathConstraint (string constraintName) {
  509. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  510. ExposedList<PathConstraint> pathConstraints = this.pathConstraints;
  511. for (int i = 0, n = pathConstraints.Count; i < n; i++) {
  512. PathConstraint constraint = pathConstraints.Items[i];
  513. if (constraint.data.Name.Equals(constraintName)) return constraint;
  514. }
  515. return null;
  516. }
  517. public void Update (float delta) {
  518. time += delta;
  519. }
  520. /// <summary>Returns the axis aligned bounding box (AABB) of the region and mesh attachments for the current pose.</summary>
  521. /// <param name="x">The horizontal distance between the skeleton origin and the left side of the AABB.</param>
  522. /// <param name="y">The vertical distance between the skeleton origin and the bottom side of the AABB.</param>
  523. /// <param name="width">The width of the AABB</param>
  524. /// <param name="height">The height of the AABB.</param>
  525. /// <param name="vertexBuffer">Reference to hold a float[]. May be a null reference. This method will assign it a new float[] with the appropriate size as needed.</param>
  526. public void GetBounds (out float x, out float y, out float width, out float height, ref float[] vertexBuffer) {
  527. float[] temp = vertexBuffer;
  528. temp = temp ?? new float[8];
  529. var drawOrderItems = this.drawOrder.Items;
  530. float minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue;
  531. for (int i = 0, n = drawOrderItems.Length; i < n; i++) {
  532. Slot slot = drawOrderItems[i];
  533. if (!slot.bone.active) continue;
  534. int verticesLength = 0;
  535. float[] vertices = null;
  536. Attachment attachment = slot.attachment;
  537. var regionAttachment = attachment as RegionAttachment;
  538. if (regionAttachment != null) {
  539. verticesLength = 8;
  540. vertices = temp;
  541. if (vertices.Length < 8) vertices = temp = new float[8];
  542. regionAttachment.ComputeWorldVertices(slot.bone, temp, 0);
  543. } else {
  544. var meshAttachment = attachment as MeshAttachment;
  545. if (meshAttachment != null) {
  546. MeshAttachment mesh = meshAttachment;
  547. verticesLength = mesh.WorldVerticesLength;
  548. vertices = temp;
  549. if (vertices.Length < verticesLength) vertices = temp = new float[verticesLength];
  550. mesh.ComputeWorldVertices(slot, 0, verticesLength, temp, 0);
  551. }
  552. }
  553. if (vertices != null) {
  554. for (int ii = 0; ii < verticesLength; ii += 2) {
  555. float vx = vertices[ii], vy = vertices[ii + 1];
  556. minX = Math.Min(minX, vx);
  557. minY = Math.Min(minY, vy);
  558. maxX = Math.Max(maxX, vx);
  559. maxY = Math.Max(maxY, vy);
  560. }
  561. }
  562. }
  563. x = minX;
  564. y = minY;
  565. width = maxX - minX;
  566. height = maxY - minY;
  567. vertexBuffer = temp;
  568. }
  569. }
  570. }