RegionAttachment.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. local setmetatable = setmetatable
  31. local math_pi = math.pi
  32. local math_sin = math.sin
  33. local math_cos = math.cos
  34. local AttachmentType = require "spine-lua.attachments.AttachmentType"
  35. local Attachment = require "spine-lua.attachments.Attachment"
  36. local Color = require "spine-lua.Color"
  37. local Utils = require "spine-lua.utils"
  38. local OX1 = 1
  39. local OY1 = 2
  40. local OX2 = 3
  41. local OY2 = 4
  42. local OX3 = 5
  43. local OY3 = 6
  44. local OX4 = 7
  45. local OY4 = 8
  46. local X1 = 1
  47. local Y1 = 2
  48. local U1 = 3
  49. local V1 = 4
  50. local C1R = 5
  51. local C1G = 6
  52. local C1B = 7
  53. local C1A = 8
  54. local X2 = 9
  55. local Y2 = 10
  56. local U2 = 11
  57. local V2 = 12
  58. local C2R = 13
  59. local C2G = 14
  60. local C2B = 15
  61. local C2A = 16
  62. local X3 = 17
  63. local Y3 = 18
  64. local U3 = 19
  65. local V3 = 20
  66. local C3R = 21
  67. local C3G = 22
  68. local C3B = 23
  69. local C3A = 24
  70. local X4 = 25
  71. local Y4 = 26
  72. local U4 = 27
  73. local V4 = 28
  74. local C4R = 29
  75. local C4G = 30
  76. local C4B = 31
  77. local C4A = 32
  78. local RegionAttachment = {}
  79. RegionAttachment.__index = RegionAttachment
  80. setmetatable(RegionAttachment, { __index = Attachment })
  81. RegionAttachment.OX1 = 1
  82. RegionAttachment.OY1 = 2
  83. RegionAttachment.OX2 = 3
  84. RegionAttachment.OY2 = 4
  85. RegionAttachment.OX3 = 5
  86. RegionAttachment.OY3 = 6
  87. RegionAttachment.OX4 = 7
  88. RegionAttachment.OY4 = 8
  89. RegionAttachment.X1 = 1
  90. RegionAttachment.Y1 = 2
  91. RegionAttachment.U1 = 3
  92. RegionAttachment.V1 = 4
  93. RegionAttachment.C1R = 5
  94. RegionAttachment.C1G = 6
  95. RegionAttachment.C1B = 7
  96. RegionAttachment.C1A = 8
  97. RegionAttachment.X2 = 9
  98. RegionAttachment.Y2 = 10
  99. RegionAttachment.U2 = 11
  100. RegionAttachment.V2 = 12
  101. RegionAttachment.C2R = 13
  102. RegionAttachment.C2G = 14
  103. RegionAttachment.C2B = 15
  104. RegionAttachment.C2A = 16
  105. RegionAttachment.X3 = 17
  106. RegionAttachment.Y3 = 18
  107. RegionAttachment.U3 = 19
  108. RegionAttachment.V3 = 20
  109. RegionAttachment.C3R = 21
  110. RegionAttachment.C3G = 22
  111. RegionAttachment.C3B = 23
  112. RegionAttachment.C3A = 24
  113. RegionAttachment.X4 = 25
  114. RegionAttachment.Y4 = 26
  115. RegionAttachment.U4 = 27
  116. RegionAttachment.V4 = 28
  117. RegionAttachment.C4R = 29
  118. RegionAttachment.C4G = 30
  119. RegionAttachment.C4B = 31
  120. RegionAttachment.C4A = 32
  121. function RegionAttachment.new (name)
  122. if not name then error("name cannot be nil", 2) end
  123. local self = Attachment.new(name, AttachmentType.region)
  124. self.x = 0
  125. self.y = 0
  126. self.scaleX = 1
  127. self.scaleY = 1
  128. self.rotation = 0
  129. self.width = 0
  130. self.height = 0
  131. self.color = Color.newWith(1, 1, 1, 1)
  132. self.path = nil
  133. self.rendererObject = nil
  134. self.region = nil
  135. self.offset = Utils.newNumberArray(8)
  136. self.uvs = Utils.newNumberArray(8)
  137. self.tempColor = Color.newWith(1, 1, 1, 1)
  138. setmetatable(self, RegionAttachment)
  139. return self
  140. end
  141. function RegionAttachment:updateOffset ()
  142. local regionScaleX = self.width / self.region.originalWidth * self.scaleX
  143. local regionScaleY = self.height / self.region.originalHeight * self.scaleY
  144. local localX = -self.width / 2 * self.scaleX + self.region.offsetX * regionScaleX
  145. local localY = -self.height / 2 * self.scaleY + self.region.offsetY * regionScaleY
  146. local localX2 = localX + self.region.width * regionScaleX
  147. local localY2 = localY + self.region.height * regionScaleY
  148. local radians = self.rotation * math_pi / 180
  149. local cos = math_cos(radians)
  150. local sin = math_sin(radians)
  151. local localXCos = localX * cos + self.x
  152. local localXSin = localX * sin
  153. local localYCos = localY * cos + self.y
  154. local localYSin = localY * sin
  155. local localX2Cos = localX2 * cos + self.x
  156. local localX2Sin = localX2 * sin
  157. local localY2Cos = localY2 * cos + self.y
  158. local localY2Sin = localY2 * sin
  159. local offset = self.offset
  160. offset[OX1] = localXCos - localYSin
  161. offset[OY1] = localYCos + localXSin
  162. offset[OX2] = localXCos - localY2Sin
  163. offset[OY2] = localY2Cos + localXSin
  164. offset[OX3] = localX2Cos - localY2Sin
  165. offset[OY3] = localY2Cos + localX2Sin
  166. offset[OX4] = localX2Cos - localYSin
  167. offset[OY4] = localYCos + localX2Sin
  168. end
  169. function RegionAttachment:setRegion (region)
  170. local uvs = self.uvs
  171. if region.rotate then
  172. uvs[5] = region.u
  173. uvs[6] = region.v2
  174. uvs[7] = region.u
  175. uvs[8] = region.v
  176. uvs[1] = region.u2
  177. uvs[2] = region.v
  178. uvs[3] = region.u2
  179. uvs[4] = region.v2
  180. else
  181. uvs[3] = region.u
  182. uvs[4] = region.v2
  183. uvs[5] = region.u
  184. uvs[6] = region.v
  185. uvs[7] = region.u2
  186. uvs[8] = region.v
  187. uvs[1] = region.u2
  188. uvs[2] = region.v2
  189. end
  190. end
  191. function RegionAttachment:computeWorldVertices (bone, worldVertices, offset, stride)
  192. offset = offset + 1
  193. local vertexOffset = self.offset
  194. local x = bone.worldX
  195. local y = bone.worldY
  196. local a = bone.a
  197. local b = bone.b
  198. local c = bone.c
  199. local d = bone.d
  200. local offsetX = 0
  201. local offsetY = 0
  202. offsetX = vertexOffset[7]
  203. offsetY = vertexOffset[8]
  204. worldVertices[offset] = offsetX * a + offsetY * b + x -- br
  205. worldVertices[offset + 1] = offsetX * c + offsetY * d + y
  206. offset = offset + stride
  207. offsetX = vertexOffset[1]
  208. offsetY = vertexOffset[2]
  209. worldVertices[offset] = offsetX * a + offsetY * b + x -- bl
  210. worldVertices[offset + 1] = offsetX * c + offsetY * d + y
  211. offset = offset + stride
  212. offsetX = vertexOffset[3]
  213. offsetY = vertexOffset[4]
  214. worldVertices[offset] = offsetX * a + offsetY * b + x -- ul
  215. worldVertices[offset + 1] = offsetX * c + offsetY * d + y
  216. offset = offset + stride
  217. offsetX = vertexOffset[5]
  218. offsetY = vertexOffset[6]
  219. worldVertices[offset] = offsetX * a + offsetY * b + x -- ur
  220. worldVertices[offset + 1] = offsetX * c + offsetY * d + y
  221. end
  222. return RegionAttachment