源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

183 行
7.8 KiB

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