RegionAttachment.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/RegionAttachment.h>
  33. #include <spine/Bone.h>
  34. #include <assert.h>
  35. using namespace spine;
  36. RTTI_IMPL(RegionAttachment, Attachment)
  37. const int RegionAttachment::BLX = 0;
  38. const int RegionAttachment::BLY = 1;
  39. const int RegionAttachment::ULX = 2;
  40. const int RegionAttachment::ULY = 3;
  41. const int RegionAttachment::URX = 4;
  42. const int RegionAttachment::URY = 5;
  43. const int RegionAttachment::BRX = 6;
  44. const int RegionAttachment::BRY = 7;
  45. RegionAttachment::RegionAttachment(const String &name) : Attachment(name), HasRendererObject(),
  46. _x(0),
  47. _y(0),
  48. _rotation(0),
  49. _scaleX(1),
  50. _scaleY(1),
  51. _width(0),
  52. _height(0),
  53. _regionOffsetX(0),
  54. _regionOffsetY(0),
  55. _regionWidth(0),
  56. _regionHeight(0),
  57. _regionOriginalWidth(0),
  58. _regionOriginalHeight(0),
  59. _path(),
  60. _regionU(0),
  61. _regionV(0),
  62. _regionU2(0),
  63. _regionV2(0),
  64. _color(1, 1, 1, 1)
  65. {
  66. _vertexOffset.setSize(NUM_UVS, 0);
  67. _uvs.setSize(NUM_UVS, 0);
  68. }
  69. void RegionAttachment::updateOffset() {
  70. float regionScaleX = _width / _regionOriginalWidth * _scaleX;
  71. float regionScaleY = _height / _regionOriginalHeight * _scaleY;
  72. float localX = -_width / 2 * _scaleX + _regionOffsetX * regionScaleX;
  73. float localY = -_height / 2 * _scaleY + _regionOffsetY * regionScaleY;
  74. float localX2 = localX + _regionWidth * regionScaleX;
  75. float localY2 = localY + _regionHeight * regionScaleY;
  76. float cos = MathUtil::cosDeg(_rotation);
  77. float sin = MathUtil::sinDeg(_rotation);
  78. float localXCos = localX * cos + _x;
  79. float localXSin = localX * sin;
  80. float localYCos = localY * cos + _y;
  81. float localYSin = localY * sin;
  82. float localX2Cos = localX2 * cos + _x;
  83. float localX2Sin = localX2 * sin;
  84. float localY2Cos = localY2 * cos + _y;
  85. float localY2Sin = localY2 * sin;
  86. _vertexOffset[BLX] = localXCos - localYSin;
  87. _vertexOffset[BLY] = localYCos + localXSin;
  88. _vertexOffset[ULX] = localXCos - localY2Sin;
  89. _vertexOffset[ULY] = localY2Cos + localXSin;
  90. _vertexOffset[URX] = localX2Cos - localY2Sin;
  91. _vertexOffset[URY] = localY2Cos + localX2Sin;
  92. _vertexOffset[BRX] = localX2Cos - localYSin;
  93. _vertexOffset[BRY] = localYCos + localX2Sin;
  94. }
  95. void RegionAttachment::setUVs(float u, float v, float u2, float v2, bool rotate) {
  96. if (rotate) {
  97. _uvs[URX] = u;
  98. _uvs[URY] = v2;
  99. _uvs[BRX] = u;
  100. _uvs[BRY] = v;
  101. _uvs[BLX] = u2;
  102. _uvs[BLY] = v;
  103. _uvs[ULX] = u2;
  104. _uvs[ULY] = v2;
  105. } else {
  106. _uvs[ULX] = u;
  107. _uvs[ULY] = v2;
  108. _uvs[URX] = u;
  109. _uvs[URY] = v;
  110. _uvs[BRX] = u2;
  111. _uvs[BRY] = v;
  112. _uvs[BLX] = u2;
  113. _uvs[BLY] = v2;
  114. }
  115. }
  116. void RegionAttachment::computeWorldVertices(Bone &bone, Vector<float> &worldVertices, size_t offset, size_t stride) {
  117. assert(worldVertices.size() >= (offset + 8));
  118. computeWorldVertices(bone, worldVertices.buffer(), offset, stride);
  119. }
  120. void RegionAttachment::computeWorldVertices(Bone &bone, float* worldVertices, size_t offset, size_t stride) {
  121. float x = bone.getWorldX(), y = bone.getWorldY();
  122. float a = bone.getA(), b = bone.getB(), c = bone.getC(), d = bone.getD();
  123. float offsetX, offsetY;
  124. offsetX = _vertexOffset[BRX];
  125. offsetY = _vertexOffset[BRY];
  126. worldVertices[offset] = offsetX * a + offsetY * b + x; // br
  127. worldVertices[offset + 1] = offsetX * c + offsetY * d + y;
  128. offset += stride;
  129. offsetX = _vertexOffset[BLX];
  130. offsetY = _vertexOffset[BLY];
  131. worldVertices[offset] = offsetX * a + offsetY * b + x; // bl
  132. worldVertices[offset + 1] = offsetX * c + offsetY * d + y;
  133. offset += stride;
  134. offsetX = _vertexOffset[ULX];
  135. offsetY = _vertexOffset[ULY];
  136. worldVertices[offset] = offsetX * a + offsetY * b + x; // ul
  137. worldVertices[offset + 1] = offsetX * c + offsetY * d + y;
  138. offset += stride;
  139. offsetX = _vertexOffset[URX];
  140. offsetY = _vertexOffset[URY];
  141. worldVertices[offset] = offsetX * a + offsetY * b + x; // ur
  142. worldVertices[offset + 1] = offsetX * c + offsetY * d + y;
  143. }
  144. float RegionAttachment::getX() {
  145. return _x;
  146. }
  147. void RegionAttachment::setX(float inValue) {
  148. _x = inValue;
  149. }
  150. float RegionAttachment::getY() {
  151. return _y;
  152. }
  153. void RegionAttachment::setY(float inValue) {
  154. _y = inValue;
  155. }
  156. float RegionAttachment::getRotation() {
  157. return _rotation;
  158. }
  159. void RegionAttachment::setRotation(float inValue) {
  160. _rotation = inValue;
  161. }
  162. float RegionAttachment::getScaleX() {
  163. return _scaleX;
  164. }
  165. void RegionAttachment::setScaleX(float inValue) {
  166. _scaleX = inValue;
  167. }
  168. float RegionAttachment::getScaleY() {
  169. return _scaleY;
  170. }
  171. void RegionAttachment::setScaleY(float inValue) {
  172. _scaleY = inValue;
  173. }
  174. float RegionAttachment::getWidth() {
  175. return _width;
  176. }
  177. void RegionAttachment::setWidth(float inValue) {
  178. _width = inValue;
  179. }
  180. float RegionAttachment::getHeight() {
  181. return _height;
  182. }
  183. void RegionAttachment::setHeight(float inValue) {
  184. _height = inValue;
  185. }
  186. const String &RegionAttachment::getPath() {
  187. return _path;
  188. }
  189. void RegionAttachment::setPath(const String &inValue) {
  190. _path = inValue;
  191. }
  192. float RegionAttachment::getRegionOffsetX() {
  193. return _regionOffsetX;
  194. }
  195. void RegionAttachment::setRegionOffsetX(float inValue) {
  196. _regionOffsetX = inValue;
  197. }
  198. float RegionAttachment::getRegionOffsetY() {
  199. return _regionOffsetY;
  200. }
  201. void RegionAttachment::setRegionOffsetY(float inValue) {
  202. _regionOffsetY = inValue;
  203. }
  204. float RegionAttachment::getRegionWidth() {
  205. return _regionWidth;
  206. }
  207. void RegionAttachment::setRegionWidth(float inValue) {
  208. _regionWidth = inValue;
  209. }
  210. float RegionAttachment::getRegionHeight() {
  211. return _regionHeight;
  212. }
  213. void RegionAttachment::setRegionHeight(float inValue) {
  214. _regionHeight = inValue;
  215. }
  216. float RegionAttachment::getRegionOriginalWidth() {
  217. return _regionOriginalWidth;
  218. }
  219. void RegionAttachment::setRegionOriginalWidth(float inValue) {
  220. _regionOriginalWidth = inValue;
  221. }
  222. float RegionAttachment::getRegionOriginalHeight() {
  223. return _regionOriginalHeight;
  224. }
  225. void RegionAttachment::setRegionOriginalHeight(float inValue) {
  226. _regionOriginalHeight = inValue;
  227. }
  228. Vector<float> &RegionAttachment::getOffset() {
  229. return _vertexOffset;
  230. }
  231. Vector<float> &RegionAttachment::getUVs() {
  232. return _uvs;
  233. }
  234. spine::Color &RegionAttachment::getColor() {
  235. return _color;
  236. }
  237. Attachment* RegionAttachment::copy() {
  238. RegionAttachment* copy = new (__FILE__, __LINE__) RegionAttachment(getName());
  239. copy->_regionWidth = _regionWidth;
  240. copy->_regionHeight = _regionHeight;
  241. copy->_regionOffsetX = _regionOffsetX;
  242. copy->_regionOffsetY = _regionOffsetY;
  243. copy->_regionOriginalWidth = _regionOriginalWidth;
  244. copy->_regionOriginalHeight = _regionOriginalHeight;
  245. copy->setRendererObject(getRendererObject());
  246. copy->_path = _path;
  247. copy->_x = _x;
  248. copy->_y = _y;
  249. copy->_scaleX = _scaleX;
  250. copy->_scaleY = _scaleY;
  251. copy->_rotation = _rotation;
  252. copy->_width = _width;
  253. copy->_height = _height;
  254. copy->_uvs.clearAndAddAll(_uvs);
  255. copy->_vertexOffset.clearAndAddAll(_vertexOffset);
  256. copy->_color.set(_color);
  257. return copy;
  258. }