RegionAttachment.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes License Agreement
  3. -- Last updated May 1, 2019. Replaces all prior versions.
  4. --
  5. -- Copyright (c) 2013-2019, 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. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. -- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. -- NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. -- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. -- INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. -- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. -- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. -------------------------------------------------------------------------------
  29. local AttachmentType = require "spine-lua.AttachmentType"
  30. local RegionAttachment = {}
  31. function RegionAttachment.new (name)
  32. if not name then error("name cannot be nil", 2) end
  33. local self = {
  34. name = name,
  35. type = AttachmentType.region,
  36. x = 0, y = 0,
  37. rotation = 0,
  38. scaleX = 1, scaleY = 1,
  39. width = 0, height = 0,
  40. offset = {},
  41. uvs = {},
  42. r = 1, g = 1, b = 1, a = 1,
  43. path = nil,
  44. rendererObject = nil,
  45. regionOffsetX = 0, regionOffsetY = 0,
  46. regionWidth = 0, regionHeight = 0,
  47. regionOriginalWidth = 0, regionOriginalHeight = 0
  48. }
  49. function self:updateOffset ()
  50. local regionScaleX = self.width / self.regionOriginalWidth * self.scaleX
  51. local regionScaleY = self.height / self.regionOriginalHeight * self.scaleY
  52. local localX = -self.width / 2 * self.scaleX + self.regionOffsetX * regionScaleX
  53. local localY = -self.height / 2 * self.scaleY + self.regionOffsetY * regionScaleY
  54. local localX2 = localX + self.regionWidth * regionScaleX
  55. local localY2 = localY + self.regionHeight * regionScaleY
  56. local radians = self.rotation * math.pi / 180
  57. local cos = math.cos(radians)
  58. local sin = math.sin(radians)
  59. local localXCos = localX * cos + self.x
  60. local localXSin = localX * sin
  61. local localYCos = localY * cos + self.y
  62. local localYSin = localY * sin
  63. local localX2Cos = localX2 * cos + self.x
  64. local localX2Sin = localX2 * sin
  65. local localY2Cos = localY2 * cos + self.y
  66. local localY2Sin = localY2 * sin
  67. local offset = self.offset
  68. offset[0] = localXCos - localYSin -- X1
  69. offset[1] = localYCos + localXSin -- Y1
  70. offset[2] = localXCos - localY2Sin -- X2
  71. offset[3] = localY2Cos + localXSin -- Y2
  72. offset[4] = localX2Cos - localY2Sin -- X3
  73. offset[5] = localY2Cos + localX2Sin -- Y3
  74. offset[6] = localX2Cos - localYSin -- X4
  75. offset[7] = localYCos + localX2Sin -- Y4
  76. end
  77. function self:computeWorldVertices (x, y, bone, worldVertices)
  78. x = x + bone.worldX
  79. y = y + bone.worldY
  80. local m00, m01, m10, m11 = bone.m00, bone.m01, bone.m10, bone.m11
  81. local offset = self.offset
  82. local vertices = self.vertices;
  83. vertices[0] = offset[0] * m00 + offset[1] * m01 + x
  84. vertices[1] = offset[0] * m10 + offset[1] * m11 + y
  85. vertices[2] = offset[2] * m00 + offset[3] * m01 + x
  86. vertices[3] = offset[2] * m10 + offset[3] * m11 + y
  87. vertices[4] = offset[4] * m00 + offset[5] * m01 + x
  88. vertices[5] = offset[4] * m10 + offset[5] * m11 + y
  89. vertices[6] = offset[6] * m00 + offset[7] * m01 + x
  90. vertices[7] = offset[6] * m10 + offset[7] * m11 + y
  91. end
  92. return self
  93. end
  94. return RegionAttachment