RegionAttachment.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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>Attachment that displays a texture region.</summary>
  32. public class RegionAttachment : Attachment, IHasRendererObject {
  33. public const int BLX = 0;
  34. public const int BLY = 1;
  35. public const int ULX = 2;
  36. public const int ULY = 3;
  37. public const int URX = 4;
  38. public const int URY = 5;
  39. public const int BRX = 6;
  40. public const int BRY = 7;
  41. internal float x, y, rotation, scaleX = 1, scaleY = 1, width, height;
  42. internal float regionOffsetX, regionOffsetY, regionWidth, regionHeight, regionOriginalWidth, regionOriginalHeight;
  43. internal float[] offset = new float[8], uvs = new float[8];
  44. internal float r = 1, g = 1, b = 1, a = 1;
  45. public float X { get { return x; } set { x = value; } }
  46. public float Y { get { return y; } set { y = value; } }
  47. public float Rotation { get { return rotation; } set { rotation = value; } }
  48. public float ScaleX { get { return scaleX; } set { scaleX = value; } }
  49. public float ScaleY { get { return scaleY; } set { scaleY = value; } }
  50. public float Width { get { return width; } set { width = value; } }
  51. public float Height { get { return height; } set { height = value; } }
  52. public float R { get { return r; } set { r = value; } }
  53. public float G { get { return g; } set { g = value; } }
  54. public float B { get { return b; } set { b = value; } }
  55. public float A { get { return a; } set { a = value; } }
  56. public string Path { get; set; }
  57. public object RendererObject { get; set; }
  58. public float RegionOffsetX { get { return regionOffsetX; } set { regionOffsetX = value; } }
  59. public float RegionOffsetY { get { return regionOffsetY; } set { regionOffsetY = value; } } // Pixels stripped from the bottom left, unrotated.
  60. public float RegionWidth { get { return regionWidth; } set { regionWidth = value; } }
  61. public float RegionHeight { get { return regionHeight; } set { regionHeight = value; } } // Unrotated, stripped size.
  62. public float RegionOriginalWidth { get { return regionOriginalWidth; } set { regionOriginalWidth = value; } }
  63. public float RegionOriginalHeight { get { return regionOriginalHeight; } set { regionOriginalHeight = value; } } // Unrotated, unstripped size.
  64. public float[] Offset { get { return offset; } }
  65. public float[] UVs { get { return uvs; } }
  66. public RegionAttachment (string name)
  67. : base(name) {
  68. }
  69. public void UpdateOffset () {
  70. float width = this.width;
  71. float height = this.height;
  72. float localX2 = width * 0.5f;
  73. float localY2 = height * 0.5f;
  74. float localX = -localX2;
  75. float localY = -localY2;
  76. if (regionOriginalWidth != 0) { // if (region != null)
  77. localX += regionOffsetX / regionOriginalWidth * width;
  78. localY += regionOffsetY / regionOriginalHeight * height;
  79. localX2 -= (regionOriginalWidth - regionOffsetX - regionWidth) / regionOriginalWidth * width;
  80. localY2 -= (regionOriginalHeight - regionOffsetY - regionHeight) / regionOriginalHeight * height;
  81. }
  82. float scaleX = this.scaleX;
  83. float scaleY = this.scaleY;
  84. localX *= scaleX;
  85. localY *= scaleY;
  86. localX2 *= scaleX;
  87. localY2 *= scaleY;
  88. float rotation = this.rotation;
  89. float cos = MathUtils.CosDeg(rotation);
  90. float sin = MathUtils.SinDeg(rotation);
  91. float x = this.x;
  92. float y = this.y;
  93. float localXCos = localX * cos + x;
  94. float localXSin = localX * sin;
  95. float localYCos = localY * cos + y;
  96. float localYSin = localY * sin;
  97. float localX2Cos = localX2 * cos + x;
  98. float localX2Sin = localX2 * sin;
  99. float localY2Cos = localY2 * cos + y;
  100. float localY2Sin = localY2 * sin;
  101. float[] offset = this.offset;
  102. offset[BLX] = localXCos - localYSin;
  103. offset[BLY] = localYCos + localXSin;
  104. offset[ULX] = localXCos - localY2Sin;
  105. offset[ULY] = localY2Cos + localXSin;
  106. offset[URX] = localX2Cos - localY2Sin;
  107. offset[URY] = localY2Cos + localX2Sin;
  108. offset[BRX] = localX2Cos - localYSin;
  109. offset[BRY] = localYCos + localX2Sin;
  110. }
  111. public void SetUVs (float u, float v, float u2, float v2, bool rotate) {
  112. float[] uvs = this.uvs;
  113. // UV values differ from RegionAttachment.java
  114. if (rotate) {
  115. uvs[URX] = u;
  116. uvs[URY] = v2;
  117. uvs[BRX] = u;
  118. uvs[BRY] = v;
  119. uvs[BLX] = u2;
  120. uvs[BLY] = v;
  121. uvs[ULX] = u2;
  122. uvs[ULY] = v2;
  123. } else {
  124. uvs[ULX] = u;
  125. uvs[ULY] = v2;
  126. uvs[URX] = u;
  127. uvs[URY] = v;
  128. uvs[BRX] = u2;
  129. uvs[BRY] = v;
  130. uvs[BLX] = u2;
  131. uvs[BLY] = v2;
  132. }
  133. }
  134. /// <summary>Transforms the attachment's four vertices to world coordinates.</summary>
  135. /// <param name="bone">The parent bone.</param>
  136. /// <param name="worldVertices">The output world vertices. Must have a length greater than or equal to offset + 8.</param>
  137. /// <param name="offset">The worldVertices index to begin writing values.</param>
  138. /// <param name="stride">The number of worldVertices entries between the value pairs written.</param>
  139. public void ComputeWorldVertices (Bone bone, float[] worldVertices, int offset, int stride = 2) {
  140. float[] vertexOffset = this.offset;
  141. float bwx = bone.worldX, bwy = bone.worldY;
  142. float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
  143. float offsetX, offsetY;
  144. // Vertex order is different from RegionAttachment.java
  145. offsetX = vertexOffset[BRX]; // 0
  146. offsetY = vertexOffset[BRY]; // 1
  147. worldVertices[offset] = offsetX * a + offsetY * b + bwx; // bl
  148. worldVertices[offset + 1] = offsetX * c + offsetY * d + bwy;
  149. offset += stride;
  150. offsetX = vertexOffset[BLX]; // 2
  151. offsetY = vertexOffset[BLY]; // 3
  152. worldVertices[offset] = offsetX * a + offsetY * b + bwx; // ul
  153. worldVertices[offset + 1] = offsetX * c + offsetY * d + bwy;
  154. offset += stride;
  155. offsetX = vertexOffset[ULX]; // 4
  156. offsetY = vertexOffset[ULY]; // 5
  157. worldVertices[offset] = offsetX * a + offsetY * b + bwx; // ur
  158. worldVertices[offset + 1] = offsetX * c + offsetY * d + bwy;
  159. offset += stride;
  160. offsetX = vertexOffset[URX]; // 6
  161. offsetY = vertexOffset[URY]; // 7
  162. worldVertices[offset] = offsetX * a + offsetY * b + bwx; // br
  163. worldVertices[offset + 1] = offsetX * c + offsetY * d + bwy;
  164. //offset += stride;
  165. }
  166. public override Attachment Copy () {
  167. RegionAttachment copy = new RegionAttachment(this.Name);
  168. copy.RendererObject = RendererObject;
  169. copy.regionOffsetX = regionOffsetX;
  170. copy.regionOffsetY = regionOffsetY;
  171. copy.regionWidth = regionWidth;
  172. copy.regionHeight = regionHeight;
  173. copy.regionOriginalWidth = regionOriginalWidth;
  174. copy.regionOriginalHeight = regionOriginalHeight;
  175. copy.Path = Path;
  176. copy.x = x;
  177. copy.y = y;
  178. copy.scaleX = scaleX;
  179. copy.scaleY = scaleY;
  180. copy.rotation = rotation;
  181. copy.width = width;
  182. copy.height = height;
  183. Array.Copy(uvs, 0, copy.uvs, 0, 8);
  184. Array.Copy(offset, 0, copy.offset, 0, 8);
  185. copy.r = r;
  186. copy.g = g;
  187. copy.b = b;
  188. copy.a = a;
  189. return copy;
  190. }
  191. }
  192. }