Slot.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /// <summary>
  32. /// Stores a slot's current pose. Slots organize attachments for <see cref="Skeleton.DrawOrder"/> purposes and provide a place to store
  33. /// state for an attachment.State cannot be stored in an attachment itself because attachments are stateless and may be shared
  34. /// across multiple skeletons.
  35. /// </summary>
  36. public class Slot {
  37. internal SlotData data;
  38. internal Bone bone;
  39. internal float r, g, b, a;
  40. internal float r2, g2, b2;
  41. internal bool hasSecondColor;
  42. internal Attachment attachment;
  43. internal float attachmentTime;
  44. internal ExposedList<float> deform = new ExposedList<float>();
  45. internal int attachmentState;
  46. public Slot (SlotData data, Bone bone) {
  47. if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
  48. if (bone == null) throw new ArgumentNullException("bone", "bone cannot be null.");
  49. this.data = data;
  50. this.bone = bone;
  51. // darkColor = data.darkColor == null ? null : new Color();
  52. if (data.hasSecondColor) {
  53. r2 = g2 = b2 = 0;
  54. }
  55. SetToSetupPose();
  56. }
  57. /// <summary>Copy constructor.</summary>
  58. public Slot (Slot slot, Bone bone) {
  59. if (slot == null) throw new ArgumentNullException("slot", "slot cannot be null.");
  60. if (bone == null) throw new ArgumentNullException("bone", "bone cannot be null.");
  61. data = slot.data;
  62. this.bone = bone;
  63. r = slot.r;
  64. g = slot.g;
  65. b = slot.b;
  66. a = slot.a;
  67. // darkColor = slot.darkColor == null ? null : new Color(slot.darkColor);
  68. if (slot.hasSecondColor) {
  69. r2 = slot.r2;
  70. g2 = slot.g2;
  71. b2 = slot.b2;
  72. } else {
  73. r2 = g2 = b2 = 0;
  74. }
  75. hasSecondColor = slot.hasSecondColor;
  76. attachment = slot.attachment;
  77. attachmentTime = slot.attachmentTime;
  78. deform.AddRange(slot.deform);
  79. }
  80. /// <summary>The slot's setup pose data.</summary>
  81. public SlotData Data { get { return data; } }
  82. /// <summary>The bone this slot belongs to.</summary>
  83. public Bone Bone { get { return bone; } }
  84. /// <summary>The skeleton this slot belongs to.</summary>
  85. public Skeleton Skeleton { get { return bone.skeleton; } }
  86. /// <summary>The color used to tint the slot's attachment. If <see cref="HasSecondColor"/> is set, this is used as the light color for two
  87. /// color tinting.</summary>
  88. public float R { get { return r; } set { r = value; } }
  89. /// <summary>The color used to tint the slot's attachment. If <see cref="HasSecondColor"/> is set, this is used as the light color for two
  90. /// color tinting.</summary>
  91. public float G { get { return g; } set { g = value; } }
  92. /// <summary>The color used to tint the slot's attachment. If <see cref="HasSecondColor"/> is set, this is used as the light color for two
  93. /// color tinting.</summary>
  94. public float B { get { return b; } set { b = value; } }
  95. /// <summary>The color used to tint the slot's attachment. If <see cref="HasSecondColor"/> is set, this is used as the light color for two
  96. /// color tinting.</summary>
  97. public float A { get { return a; } set { a = value; } }
  98. public void ClampColor () {
  99. r = MathUtils.Clamp(r, 0, 1);
  100. g = MathUtils.Clamp(g, 0, 1);
  101. b = MathUtils.Clamp(b, 0, 1);
  102. a = MathUtils.Clamp(a, 0, 1);
  103. }
  104. /// <summary>The dark color used to tint the slot's attachment for two color tinting, ignored if two color tinting is not used.</summary>
  105. /// <seealso cref="HasSecondColor"/>
  106. public float R2 { get { return r2; } set { r2 = value; } }
  107. /// <summary>The dark color used to tint the slot's attachment for two color tinting, ignored if two color tinting is not used.</summary>
  108. /// <seealso cref="HasSecondColor"/>
  109. public float G2 { get { return g2; } set { g2 = value; } }
  110. /// <summary>The dark color used to tint the slot's attachment for two color tinting, ignored if two color tinting is not used.</summary>
  111. /// <seealso cref="HasSecondColor"/>
  112. public float B2 { get { return b2; } set { b2 = value; } }
  113. /// <summary>Whether R2 G2 B2 are used to tint the slot's attachment for two color tinting. False if two color tinting is not used.</summary>
  114. public bool HasSecondColor { get { return data.hasSecondColor; } set { data.hasSecondColor = value; } }
  115. public void ClampSecondColor () {
  116. r2 = MathUtils.Clamp(r2, 0, 1);
  117. g2 = MathUtils.Clamp(g2, 0, 1);
  118. b2 = MathUtils.Clamp(b2, 0, 1);
  119. }
  120. public Attachment Attachment {
  121. /// <summary>The current attachment for the slot, or null if the slot has no attachment.</summary>
  122. get { return attachment; }
  123. /// <summary>
  124. /// Sets the slot's attachment and, if the attachment changed, resets <see cref="AttachmentTime"/> and clears the <see cref="Deform"/>.
  125. /// The deform is not cleared if the old attachment has the same <see cref="VertexAttachment.DeformAttachment"/> as the specified
  126. /// attachment.</summary>
  127. /// <param name="value">May be null.</param>
  128. set {
  129. if (attachment == value) return;
  130. if (!(value is VertexAttachment) || !(this.attachment is VertexAttachment)
  131. || ((VertexAttachment)value).DeformAttachment != ((VertexAttachment)this.attachment).DeformAttachment) {
  132. deform.Clear();
  133. }
  134. this.attachment = value;
  135. attachmentTime = bone.skeleton.time;
  136. }
  137. }
  138. /// <summary> The time that has elapsed since the last time the attachment was set or cleared. Relies on Skeleton
  139. /// <see cref="Skeleton.Time"/></summary>
  140. public float AttachmentTime {
  141. get { return bone.skeleton.time - attachmentTime; }
  142. set { attachmentTime = bone.skeleton.time - value; }
  143. }
  144. /// <summary> Vertices to deform the slot's attachment. For an unweighted mesh, the entries are local positions for each vertex. For a
  145. /// weighted mesh, the entries are an offset for each vertex which will be added to the mesh's local vertex positions.
  146. /// <para />
  147. /// See <see cref="VertexAttachment.ComputeWorldVertices(Slot, int, int, float[], int, int)"/> and <see cref="DeformTimeline"/>.</summary>
  148. public ExposedList<float> Deform {
  149. get {
  150. return deform;
  151. }
  152. set {
  153. if (deform == null) throw new ArgumentNullException("deform", "deform cannot be null.");
  154. deform = value;
  155. }
  156. }
  157. /// <summary>Sets this slot to the setup pose.</summary>
  158. public void SetToSetupPose () {
  159. r = data.r;
  160. g = data.g;
  161. b = data.b;
  162. a = data.a;
  163. // if (darkColor != null) darkColor.set(data.darkColor);
  164. if (HasSecondColor) {
  165. r2 = data.r2;
  166. g2 = data.g2;
  167. b2 = data.b2;
  168. }
  169. if (data.attachmentName == null)
  170. Attachment = null;
  171. else {
  172. attachment = null;
  173. Attachment = bone.skeleton.GetAttachment(data.index, data.attachmentName);
  174. }
  175. }
  176. override public string ToString () {
  177. return data.name;
  178. }
  179. }
  180. }