Slot.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 System;
  31. namespace Spine {
  32. /// <summary>
  33. /// Stores a slot's current pose. Slots organize attachments for {@link Skeleton#drawOrder} purposes and provide a place to store
  34. /// state for an attachment.State cannot be stored in an attachment itself because attachments are stateless and may be shared
  35. /// across multiple skeletons.
  36. /// </summary>
  37. public class Slot {
  38. internal SlotData data;
  39. internal Bone bone;
  40. internal float r, g, b, a;
  41. internal float r2, g2, b2;
  42. internal bool hasSecondColor;
  43. internal Attachment attachment;
  44. internal float attachmentTime;
  45. internal ExposedList<float> attachmentVertices = new ExposedList<float>();
  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. }
  79. /// <summary>The slot's setup pose data.</summary>
  80. public SlotData Data { get { return data; } }
  81. /// <summary>The bone this slot belongs to.</summary>
  82. public Bone Bone { get { return bone; } }
  83. /// <summary>The skeleton this slot belongs to.</summary>
  84. public Skeleton Skeleton { get { return bone.skeleton; } }
  85. /// <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
  86. /// color tinting.</summary>
  87. public float R { get { return r; } set { r = value; } }
  88. /// <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
  89. /// color tinting.</summary>
  90. public float G { get { return g; } set { g = value; } }
  91. /// <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
  92. /// color tinting.</summary>
  93. public float B { get { return b; } set { b = value; } }
  94. /// <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
  95. /// color tinting.</summary>
  96. public float A { get { return a; } set { a = value; } }
  97. /// <summary>The dark color used to tint the slot's attachment for two color tinting, ignored if two color tinting is not used.</summary>
  98. /// <seealso cref="HasSecondColor"/>
  99. public float R2 { get { return r2; } set { r2 = value; } }
  100. /// <summary>The dark color used to tint the slot's attachment for two color tinting, ignored if two color tinting is not used.</summary>
  101. /// <seealso cref="HasSecondColor"/>
  102. public float G2 { get { return g2; } set { g2 = value; } }
  103. /// <summary>The dark color used to tint the slot's attachment for two color tinting, ignored if two color tinting is not used.</summary>
  104. /// <seealso cref="HasSecondColor"/>
  105. public float B2 { get { return b2; } set { b2 = value; } }
  106. /// <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>
  107. public bool HasSecondColor { get { return data.hasSecondColor; } set { data.hasSecondColor = value; } }
  108. public Attachment Attachment {
  109. /// <summary>The current attachment for the slot, or null if the slot has no attachment.</summary>
  110. get { return attachment; }
  111. /// <summary>
  112. /// Sets the slot's attachment and, if the attachment changed, resets <see cref="AttachmentTime"/> and clears
  113. /// <see cref="AttachmentVertices">.</summary>
  114. /// <param name="value">May be null.</param>
  115. set {
  116. if (attachment == value) return;
  117. attachment = value;
  118. attachmentTime = bone.skeleton.time;
  119. attachmentVertices.Clear(false);
  120. }
  121. }
  122. /// <summary> The time that has elapsed since the last time the attachment was set or cleared. Relies on Skeleton
  123. /// <see cref="Skeleton.Time"/></summary>
  124. public float AttachmentTime {
  125. get { return bone.skeleton.time - attachmentTime; }
  126. set { attachmentTime = bone.skeleton.time - value; }
  127. }
  128. /// <summary> Vertices to deform the slot's attachment. For an unweighted mesh, the entries are local positions for each vertex. For a
  129. /// weighted mesh, the entries are an offset for each vertex which will be added to the mesh's local vertex positions.
  130. /// <para />
  131. /// See <see cref="VertexAttachment.ComputeWorldVertices(Slot, int, int, float[], int, int)"/> and <see cref="DeformTimeline"/>.</summary>
  132. public ExposedList<float> AttachmentVertices {
  133. get {
  134. return attachmentVertices;
  135. }
  136. set {
  137. if (attachmentVertices == null) throw new ArgumentNullException("attachmentVertices", "attachmentVertices cannot be null.");
  138. attachmentVertices = value;
  139. }
  140. }
  141. /// <summary>Sets this slot to the setup pose.</summary>
  142. public void SetToSetupPose () {
  143. r = data.r;
  144. g = data.g;
  145. b = data.b;
  146. a = data.a;
  147. // if (darkColor != null) darkColor.set(data.darkColor);
  148. if (HasSecondColor) {
  149. r2 = data.r2;
  150. g2 = data.g2;
  151. b2 = data.b2;
  152. }
  153. if (data.attachmentName == null)
  154. Attachment = null;
  155. else {
  156. attachment = null;
  157. Attachment = bone.skeleton.GetAttachment(data.index, data.attachmentName);
  158. }
  159. }
  160. override public string ToString () {
  161. return data.name;
  162. }
  163. }
  164. }