Skeleton.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. namespace Spine {
  31. public class Skeleton {
  32. internal SkeletonData data;
  33. internal ExposedList<Bone> bones;
  34. internal ExposedList<Slot> slots;
  35. internal ExposedList<Slot> drawOrder;
  36. internal ExposedList<IkConstraint> ikConstraints;
  37. internal ExposedList<TransformConstraint> transformConstraints;
  38. internal ExposedList<PathConstraint> pathConstraints;
  39. internal ExposedList<IUpdatable> updateCache = new ExposedList<IUpdatable>();
  40. internal Skin skin;
  41. internal float r = 1, g = 1, b = 1, a = 1;
  42. internal float time;
  43. private float scaleX = 1, scaleY = 1;
  44. internal float x, y;
  45. public SkeletonData Data { get { return data; } }
  46. public ExposedList<Bone> Bones { get { return bones; } }
  47. public ExposedList<IUpdatable> UpdateCacheList { get { return updateCache; } }
  48. public ExposedList<Slot> Slots { get { return slots; } }
  49. public ExposedList<Slot> DrawOrder { get { return drawOrder; } }
  50. public ExposedList<IkConstraint> IkConstraints { get { return ikConstraints; } }
  51. public ExposedList<PathConstraint> PathConstraints { get { return pathConstraints; } }
  52. public ExposedList<TransformConstraint> TransformConstraints { get { return transformConstraints; } }
  53. public Skin Skin {
  54. /// <summary>The skeleton's current skin. May be null.</summary>
  55. get { return skin; }
  56. /// <summary>Sets a skin, <see cref="SetSkin(Skin)"/>.</summary>
  57. set { SetSkin(value); }
  58. }
  59. public float R { get { return r; } set { r = value; } }
  60. public float G { get { return g; } set { g = value; } }
  61. public float B { get { return b; } set { b = value; } }
  62. public float A { get { return a; } set { a = value; } }
  63. public float Time { get { return time; } set { time = value; } }
  64. public float X { get { return x; } set { x = value; } }
  65. public float Y { get { return y; } set { y = value; } }
  66. public float ScaleX { get { return scaleX; } set { scaleX = value; } }
  67. public float ScaleY { get { return scaleY * (Bone.yDown ? -1 : 1); } set { scaleY = value; } }
  68. [Obsolete("Use ScaleX instead. FlipX is when ScaleX is negative.")]
  69. public bool FlipX { get { return scaleX < 0; } set { scaleX = value ? -1f : 1f; } }
  70. [Obsolete("Use ScaleY instead. FlipY is when ScaleY is negative.")]
  71. public bool FlipY { get { return scaleY < 0; } set { scaleY = value ? -1f : 1f; } }
  72. /// <summary>Returns the root bone, or null if the skeleton has no bones.</summary>
  73. public Bone RootBone {
  74. get { return bones.Count == 0 ? null : bones.Items[0]; }
  75. }
  76. public Skeleton (SkeletonData data) {
  77. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  78. this.data = data;
  79. bones = new ExposedList<Bone>(data.bones.Count);
  80. Bone[] bonesItems = this.bones.Items;
  81. foreach (BoneData boneData in data.bones) {
  82. Bone bone;
  83. if (boneData.parent == null) {
  84. bone = new Bone(boneData, this, null);
  85. } else {
  86. Bone parent = bonesItems[boneData.parent.index];
  87. bone = new Bone(boneData, this, parent);
  88. parent.children.Add(bone);
  89. }
  90. this.bones.Add(bone);
  91. }
  92. slots = new ExposedList<Slot>(data.slots.Count);
  93. drawOrder = new ExposedList<Slot>(data.slots.Count);
  94. foreach (SlotData slotData in data.slots) {
  95. Bone bone = bonesItems[slotData.boneData.index];
  96. Slot slot = new Slot(slotData, bone);
  97. slots.Add(slot);
  98. drawOrder.Add(slot);
  99. }
  100. ikConstraints = new ExposedList<IkConstraint>(data.ikConstraints.Count);
  101. foreach (IkConstraintData ikConstraintData in data.ikConstraints)
  102. ikConstraints.Add(new IkConstraint(ikConstraintData, this));
  103. transformConstraints = new ExposedList<TransformConstraint>(data.transformConstraints.Count);
  104. foreach (TransformConstraintData transformConstraintData in data.transformConstraints)
  105. transformConstraints.Add(new TransformConstraint(transformConstraintData, this));
  106. pathConstraints = new ExposedList<PathConstraint>(data.pathConstraints.Count);
  107. foreach (PathConstraintData pathConstraintData in data.pathConstraints)
  108. pathConstraints.Add(new PathConstraint(pathConstraintData, this));
  109. UpdateCache();
  110. }
  111. /// <summary>Caches information about bones and constraints. Must be called if the <see cref="Skin"/> is modified or if bones, constraints, or
  112. /// constraints, or weighted path attachments are added or removed.</summary>
  113. public void UpdateCache () {
  114. var updateCache = this.updateCache;
  115. updateCache.Clear();
  116. int boneCount = this.bones.Count;
  117. Bone[] bones = this.bones.Items;
  118. for (int i = 0; i < boneCount; i++) {
  119. Bone bone = bones[i];
  120. bone.sorted = bone.data.skinRequired;
  121. bone.active = !bone.sorted;
  122. }
  123. if (skin != null) {
  124. BoneData[] skinBones = skin.bones.Items;
  125. for (int i = 0, n = skin.bones.Count; i < n; i++) {
  126. var bone = bones[skinBones[i].index];
  127. do {
  128. bone.sorted = false;
  129. bone.active = true;
  130. bone = bone.parent;
  131. } while (bone != null);
  132. }
  133. }
  134. int ikCount = this.ikConstraints.Count, transformCount = this.transformConstraints.Count, pathCount = this.pathConstraints.Count;
  135. IkConstraint[] ikConstraints = this.ikConstraints.Items;
  136. TransformConstraint[] transformConstraints = this.transformConstraints.Items;
  137. PathConstraint[] pathConstraints = this.pathConstraints.Items;
  138. int constraintCount = ikCount + transformCount + pathCount;
  139. for (int i = 0; i < constraintCount; i++) {
  140. for (int ii = 0; ii < ikCount; ii++) {
  141. IkConstraint constraint = ikConstraints[ii];
  142. if (constraint.data.order == i) {
  143. SortIkConstraint(constraint);
  144. goto continue_outer;
  145. }
  146. }
  147. for (int ii = 0; ii < transformCount; ii++) {
  148. TransformConstraint constraint = transformConstraints[ii];
  149. if (constraint.data.order == i) {
  150. SortTransformConstraint(constraint);
  151. goto continue_outer;
  152. }
  153. }
  154. for (int ii = 0; ii < pathCount; ii++) {
  155. PathConstraint constraint = pathConstraints[ii];
  156. if (constraint.data.order == i) {
  157. SortPathConstraint(constraint);
  158. goto continue_outer;
  159. }
  160. }
  161. continue_outer: { }
  162. }
  163. for (int i = 0; i < boneCount; i++)
  164. SortBone(bones[i]);
  165. }
  166. private void SortIkConstraint (IkConstraint constraint) {
  167. constraint.active = constraint.target.active
  168. && (!constraint.data.skinRequired || (skin != null && skin.constraints.Contains(constraint.data)));
  169. if (!constraint.active) return;
  170. Bone target = constraint.target;
  171. SortBone(target);
  172. var constrained = constraint.bones;
  173. Bone parent = constrained.Items[0];
  174. SortBone(parent);
  175. if (constrained.Count == 1) {
  176. updateCache.Add(constraint);
  177. SortReset(parent.children);
  178. } else {
  179. Bone child = constrained.Items[constrained.Count - 1];
  180. SortBone(child);
  181. updateCache.Add(constraint);
  182. SortReset(parent.children);
  183. child.sorted = true;
  184. }
  185. }
  186. private void SortPathConstraint (PathConstraint constraint) {
  187. constraint.active = constraint.target.bone.active
  188. && (!constraint.data.skinRequired || (skin != null && skin.constraints.Contains(constraint.data)));
  189. if (!constraint.active) return;
  190. Slot slot = constraint.target;
  191. int slotIndex = slot.data.index;
  192. Bone slotBone = slot.bone;
  193. if (skin != null) SortPathConstraintAttachment(skin, slotIndex, slotBone);
  194. if (data.defaultSkin != null && data.defaultSkin != skin)
  195. SortPathConstraintAttachment(data.defaultSkin, slotIndex, slotBone);
  196. Attachment attachment = slot.attachment;
  197. if (attachment is PathAttachment) SortPathConstraintAttachment(attachment, slotBone);
  198. var constrained = constraint.bones.Items;
  199. int boneCount = constraint.bones.Count;
  200. for (int i = 0; i < boneCount; i++)
  201. SortBone(constrained[i]);
  202. updateCache.Add(constraint);
  203. for (int i = 0; i < boneCount; i++)
  204. SortReset(constrained[i].children);
  205. for (int i = 0; i < boneCount; i++)
  206. constrained[i].sorted = true;
  207. }
  208. private void SortTransformConstraint (TransformConstraint constraint) {
  209. constraint.active = constraint.target.active
  210. && (!constraint.data.skinRequired || (skin != null && skin.constraints.Contains(constraint.data)));
  211. if (!constraint.active) return;
  212. SortBone(constraint.target);
  213. var constrained = constraint.bones.Items;
  214. int boneCount = constraint.bones.Count;
  215. if (constraint.data.local) {
  216. for (int i = 0; i < boneCount; i++) {
  217. Bone child = constrained[i];
  218. SortBone(child.parent);
  219. SortBone(child);
  220. }
  221. } else {
  222. for (int i = 0; i < boneCount; i++)
  223. SortBone(constrained[i]);
  224. }
  225. updateCache.Add(constraint);
  226. for (int i = 0; i < boneCount; i++)
  227. SortReset(constrained[i].children);
  228. for (int i = 0; i < boneCount; i++)
  229. constrained[i].sorted = true;
  230. }
  231. private void SortPathConstraintAttachment (Skin skin, int slotIndex, Bone slotBone) {
  232. foreach (var entry in skin.Attachments)
  233. if (entry.SlotIndex == slotIndex) SortPathConstraintAttachment(entry.Attachment, slotBone);
  234. }
  235. private void SortPathConstraintAttachment (Attachment attachment, Bone slotBone) {
  236. if (!(attachment is PathAttachment)) return;
  237. int[] pathBones = ((PathAttachment)attachment).bones;
  238. if (pathBones == null)
  239. SortBone(slotBone);
  240. else {
  241. var bones = this.bones.Items;
  242. for (int i = 0, n = pathBones.Length; i < n;) {
  243. int nn = pathBones[i++];
  244. nn += i;
  245. while (i < nn)
  246. SortBone(bones[pathBones[i++]]);
  247. }
  248. }
  249. }
  250. private void SortBone (Bone bone) {
  251. if (bone.sorted) return;
  252. Bone parent = bone.parent;
  253. if (parent != null) SortBone(parent);
  254. bone.sorted = true;
  255. updateCache.Add(bone);
  256. }
  257. private static void SortReset (ExposedList<Bone> bones) {
  258. Bone[] bonesItems = bones.Items;
  259. for (int i = 0, n = bones.Count; i < n; i++) {
  260. Bone bone = bonesItems[i];
  261. if (!bone.active) continue;
  262. if (bone.sorted) SortReset(bone.children);
  263. bone.sorted = false;
  264. }
  265. }
  266. /// <summary>
  267. /// Updates the world transform for each bone and applies all constraints.
  268. /// <para>
  269. /// See <a href="http://esotericsoftware.com/spine-runtime-skeletons#World-transforms">World transforms</a> in the Spine
  270. /// Runtimes Guide.</para>
  271. /// </summary>
  272. public void UpdateWorldTransform () {
  273. Bone[] bones = this.bones.Items;
  274. for (int i = 0, n = this.bones.Count; i < n; i++) {
  275. Bone bone = bones[i];
  276. bone.ax = bone.x;
  277. bone.ay = bone.y;
  278. bone.arotation = bone.rotation;
  279. bone.ascaleX = bone.scaleX;
  280. bone.ascaleY = bone.scaleY;
  281. bone.ashearX = bone.shearX;
  282. bone.ashearY = bone.shearY;
  283. }
  284. var updateCache = this.updateCache.Items;
  285. for (int i = 0, n = this.updateCache.Count; i < n; i++)
  286. updateCache[i].Update();
  287. }
  288. /// <summary>
  289. /// Temporarily sets the root bone as a child of the specified bone, then updates the world transform for each bone and applies
  290. /// all constraints.
  291. /// </summary>
  292. public void UpdateWorldTransform (Bone parent) {
  293. if (parent == null) throw new ArgumentNullException("parent", "parent cannot be null.");
  294. // Apply the parent bone transform to the root bone. The root bone always inherits scale, rotation and reflection.
  295. Bone rootBone = this.RootBone;
  296. float pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
  297. rootBone.worldX = pa * x + pb * y + parent.worldX;
  298. rootBone.worldY = pc * x + pd * y + parent.worldY;
  299. float rotationY = rootBone.rotation + 90 + rootBone.shearY;
  300. float la = MathUtils.CosDeg(rootBone.rotation + rootBone.shearX) * rootBone.scaleX;
  301. float lb = MathUtils.CosDeg(rotationY) * rootBone.scaleY;
  302. float lc = MathUtils.SinDeg(rootBone.rotation + rootBone.shearX) * rootBone.scaleX;
  303. float ld = MathUtils.SinDeg(rotationY) * rootBone.scaleY;
  304. rootBone.a = (pa * la + pb * lc) * scaleX;
  305. rootBone.b = (pa * lb + pb * ld) * scaleX;
  306. rootBone.c = (pc * la + pd * lc) * scaleY;
  307. rootBone.d = (pc * lb + pd * ld) * scaleY;
  308. // Update everything except root bone.
  309. var updateCache = this.updateCache.Items;
  310. for (int i = 0, n = this.updateCache.Count; i < n; i++) {
  311. var updatable = updateCache[i];
  312. if (updatable != rootBone) updatable.Update();
  313. }
  314. }
  315. /// <summary>Sets the bones, constraints, and slots to their setup pose values.</summary>
  316. public void SetToSetupPose () {
  317. SetBonesToSetupPose();
  318. SetSlotsToSetupPose();
  319. }
  320. /// <summary>Sets the bones and constraints to their setup pose values.</summary>
  321. public void SetBonesToSetupPose () {
  322. var bones = this.bones.Items;
  323. for (int i = 0, n = this.bones.Count; i < n; i++)
  324. bones[i].SetToSetupPose();
  325. var ikConstraints = this.ikConstraints.Items;
  326. for (int i = 0, n = this.ikConstraints.Count; i < n; i++) {
  327. IkConstraint constraint = ikConstraints[i];
  328. IkConstraintData data = constraint.data;
  329. constraint.mix = data.mix;
  330. constraint.softness = data.softness;
  331. constraint.bendDirection = data.bendDirection;
  332. constraint.compress = data.compress;
  333. constraint.stretch = data.stretch;
  334. }
  335. var transformConstraints = this.transformConstraints.Items;
  336. for (int i = 0, n = this.transformConstraints.Count; i < n; i++) {
  337. TransformConstraint constraint = transformConstraints[i];
  338. TransformConstraintData data = constraint.data;
  339. constraint.mixRotate = data.mixRotate;
  340. constraint.mixX = data.mixX;
  341. constraint.mixY = data.mixY;
  342. constraint.mixScaleX = data.mixScaleX;
  343. constraint.mixScaleY = data.mixScaleY;
  344. constraint.mixShearY = data.mixShearY;
  345. }
  346. var pathConstraints = this.pathConstraints.Items;
  347. for (int i = 0, n = this.pathConstraints.Count; i < n; i++) {
  348. PathConstraint constraint = pathConstraints[i];
  349. PathConstraintData data = constraint.data;
  350. constraint.position = data.position;
  351. constraint.spacing = data.spacing;
  352. constraint.mixRotate = data.mixRotate;
  353. constraint.mixX = data.mixX;
  354. constraint.mixY = data.mixY;
  355. }
  356. }
  357. public void SetSlotsToSetupPose () {
  358. var slots = this.slots.Items;
  359. int n = this.slots.Count;
  360. Array.Copy(slots, 0, drawOrder.Items, 0, n);
  361. for (int i = 0; i < n; i++)
  362. slots[i].SetToSetupPose();
  363. }
  364. /// <summary>Finds a bone by comparing each bone's name. It is more efficient to cache the results of this method than to call it
  365. /// repeatedly.</summary>
  366. /// <returns>May be null.</returns>
  367. public Bone FindBone (string boneName) {
  368. if (boneName == null) throw new ArgumentNullException("boneName", "boneName cannot be null.");
  369. var bones = this.bones.Items;
  370. for (int i = 0, n = this.bones.Count; i < n; i++) {
  371. Bone bone = bones[i];
  372. if (bone.data.name == boneName) return bone;
  373. }
  374. return null;
  375. }
  376. /// <summary>Finds a slot by comparing each slot's name. It is more efficient to cache the results of this method than to call it
  377. /// repeatedly.</summary>
  378. /// <returns>May be null.</returns>
  379. public Slot FindSlot (string slotName) {
  380. if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
  381. var slots = this.slots.Items;
  382. for (int i = 0, n = this.slots.Count; i < n; i++) {
  383. Slot slot = slots[i];
  384. if (slot.data.name == slotName) return slot;
  385. }
  386. return null;
  387. }
  388. /// <summary>Sets a skin by name (<see cref="SetSkin(Skin)"/>).</summary>
  389. public void SetSkin (string skinName) {
  390. Skin foundSkin = data.FindSkin(skinName);
  391. if (foundSkin == null) throw new ArgumentException("Skin not found: " + skinName, "skinName");
  392. SetSkin(foundSkin);
  393. }
  394. /// <summary>
  395. /// <para>Sets the skin used to look up attachments before looking in the <see cref="SkeletonData.DefaultSkin"/>. If the
  396. /// skin is changed, <see cref="UpdateCache()"/> is called.
  397. /// </para>
  398. /// <para>Attachments from the new skin are attached if the corresponding attachment from the old skin was attached.
  399. /// If there was no old skin, each slot's setup mode attachment is attached from the new skin.
  400. /// </para>
  401. /// <para>After changing the skin, the visible attachments can be reset to those attached in the setup pose by calling
  402. /// <see cref="Skeleton.SetSlotsToSetupPose()"/>.
  403. /// Also, often <see cref="AnimationState.Apply(Skeleton)"/> is called before the next time the
  404. /// skeleton is rendered to allow any attachment keys in the current animation(s) to hide or show attachments from the new skin.</para>
  405. /// </summary>
  406. /// <param name="newSkin">May be null.</param>
  407. public void SetSkin (Skin newSkin) {
  408. if (newSkin == skin) return;
  409. if (newSkin != null) {
  410. if (skin != null)
  411. newSkin.AttachAll(this, skin);
  412. else {
  413. Slot[] slots = this.slots.Items;
  414. for (int i = 0, n = this.slots.Count; i < n; i++) {
  415. Slot slot = slots[i];
  416. string name = slot.data.attachmentName;
  417. if (name != null) {
  418. Attachment attachment = newSkin.GetAttachment(i, name);
  419. if (attachment != null) slot.Attachment = attachment;
  420. }
  421. }
  422. }
  423. }
  424. skin = newSkin;
  425. UpdateCache();
  426. }
  427. /// <summary>Finds an attachment by looking in the <see cref="Skeleton.Skin"/> and <see cref="SkeletonData.DefaultSkin"/> using the slot name and attachment name.</summary>
  428. /// <returns>May be null.</returns>
  429. public Attachment GetAttachment (string slotName, string attachmentName) {
  430. return GetAttachment(data.FindSlot(slotName).index, attachmentName);
  431. }
  432. /// <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>
  433. /// <returns>May be null.</returns>
  434. public Attachment GetAttachment (int slotIndex, string attachmentName) {
  435. if (attachmentName == null) throw new ArgumentNullException("attachmentName", "attachmentName cannot be null.");
  436. if (skin != null) {
  437. Attachment attachment = skin.GetAttachment(slotIndex, attachmentName);
  438. if (attachment != null) return attachment;
  439. }
  440. return data.defaultSkin != null ? data.defaultSkin.GetAttachment(slotIndex, attachmentName) : null;
  441. }
  442. /// <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>
  443. /// <param name="attachmentName">May be null to clear the slot's attachment.</param>
  444. public void SetAttachment (string slotName, string attachmentName) {
  445. if (slotName == null) throw new ArgumentNullException("slotName", "slotName cannot be null.");
  446. Slot[] slots = this.slots.Items;
  447. for (int i = 0, n = this.slots.Count; i < n; i++) {
  448. Slot slot = slots[i];
  449. if (slot.data.name == slotName) {
  450. Attachment attachment = null;
  451. if (attachmentName != null) {
  452. attachment = GetAttachment(i, attachmentName);
  453. if (attachment == null) throw new Exception("Attachment not found: " + attachmentName + ", for slot: " + slotName);
  454. }
  455. slot.Attachment = attachment;
  456. return;
  457. }
  458. }
  459. throw new Exception("Slot not found: " + slotName);
  460. }
  461. /// <summary>Finds an IK constraint by comparing each IK constraint's name. It is more efficient to cache the results of this method
  462. /// than to call it repeatedly.</summary>
  463. /// <returns>May be null.</returns>
  464. public IkConstraint FindIkConstraint (string constraintName) {
  465. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  466. var ikConstraints = this.ikConstraints.Items;
  467. for (int i = 0, n = this.ikConstraints.Count; i < n; i++) {
  468. IkConstraint ikConstraint = ikConstraints[i];
  469. if (ikConstraint.data.name == constraintName) return ikConstraint;
  470. }
  471. return null;
  472. }
  473. /// <summary>Finds a transform constraint by comparing each transform constraint's name. It is more efficient to cache the results of
  474. /// this method than to call it repeatedly.</summary>
  475. /// <returns>May be null.</returns>
  476. public TransformConstraint FindTransformConstraint (string constraintName) {
  477. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  478. var transformConstraints = this.transformConstraints.Items;
  479. for (int i = 0, n = this.transformConstraints.Count; i < n; i++) {
  480. TransformConstraint transformConstraint = transformConstraints[i];
  481. if (transformConstraint.data.Name == constraintName) return transformConstraint;
  482. }
  483. return null;
  484. }
  485. /// <summary>Finds a path constraint by comparing each path constraint's name. It is more efficient to cache the results of this method
  486. /// than to call it repeatedly.</summary>
  487. /// <returns>May be null.</returns>
  488. public PathConstraint FindPathConstraint (string constraintName) {
  489. if (constraintName == null) throw new ArgumentNullException("constraintName", "constraintName cannot be null.");
  490. var pathConstraints = this.pathConstraints.Items;
  491. for (int i = 0, n = this.pathConstraints.Count; i < n; i++) {
  492. PathConstraint constraint = pathConstraints[i];
  493. if (constraint.data.Name.Equals(constraintName)) return constraint;
  494. }
  495. return null;
  496. }
  497. public void Update (float delta) {
  498. time += delta;
  499. }
  500. /// <summary>Returns the axis aligned bounding box (AABB) of the region and mesh attachments for the current pose.</summary>
  501. /// <param name="x">The horizontal distance between the skeleton origin and the left side of the AABB.</param>
  502. /// <param name="y">The vertical distance between the skeleton origin and the bottom side of the AABB.</param>
  503. /// <param name="width">The width of the AABB</param>
  504. /// <param name="height">The height of the AABB.</param>
  505. /// <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>
  506. public void GetBounds (out float x, out float y, out float width, out float height, ref float[] vertexBuffer) {
  507. float[] temp = vertexBuffer;
  508. temp = temp ?? new float[8];
  509. var drawOrder = this.drawOrder.Items;
  510. float minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue;
  511. for (int i = 0, n = this.drawOrder.Count; i < n; i++) {
  512. Slot slot = drawOrder[i];
  513. if (!slot.bone.active) continue;
  514. int verticesLength = 0;
  515. float[] vertices = null;
  516. Attachment attachment = slot.attachment;
  517. var regionAttachment = attachment as RegionAttachment;
  518. if (regionAttachment != null) {
  519. verticesLength = 8;
  520. vertices = temp;
  521. if (vertices.Length < 8) vertices = temp = new float[8];
  522. regionAttachment.ComputeWorldVertices(slot.bone, temp, 0);
  523. } else {
  524. var meshAttachment = attachment as MeshAttachment;
  525. if (meshAttachment != null) {
  526. MeshAttachment mesh = meshAttachment;
  527. verticesLength = mesh.WorldVerticesLength;
  528. vertices = temp;
  529. if (vertices.Length < verticesLength) vertices = temp = new float[verticesLength];
  530. mesh.ComputeWorldVertices(slot, 0, verticesLength, temp, 0);
  531. }
  532. }
  533. if (vertices != null) {
  534. for (int ii = 0; ii < verticesLength; ii += 2) {
  535. float vx = vertices[ii], vy = vertices[ii + 1];
  536. minX = Math.Min(minX, vx);
  537. minY = Math.Min(minY, vy);
  538. maxX = Math.Max(maxX, vx);
  539. maxY = Math.Max(maxY, vy);
  540. }
  541. }
  542. }
  543. x = minX;
  544. y = minY;
  545. width = maxX - minX;
  546. height = maxY - minY;
  547. vertexBuffer = temp;
  548. }
  549. }
  550. }