RegionAttachment.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 AttachmentType = require "spine-lua.AttachmentType"
  31. local RegionAttachment = {}
  32. function RegionAttachment.new (name)
  33. if not name then error("name cannot be nil", 2) end
  34. local self = {
  35. name = name,
  36. type = AttachmentType.region,
  37. x = 0, y = 0,
  38. rotation = 0,
  39. scaleX = 1, scaleY = 1,
  40. width = 0, height = 0,
  41. offset = {},
  42. uvs = {},
  43. r = 1, g = 1, b = 1, a = 1,
  44. path = nil,
  45. rendererObject = nil,
  46. regionOffsetX = 0, regionOffsetY = 0,
  47. regionWidth = 0, regionHeight = 0,
  48. regionOriginalWidth = 0, regionOriginalHeight = 0
  49. }
  50. function self:updateOffset ()
  51. local regionScaleX = self.width / self.regionOriginalWidth * self.scaleX
  52. local regionScaleY = self.height / self.regionOriginalHeight * self.scaleY
  53. local localX = -self.width / 2 * self.scaleX + self.regionOffsetX * regionScaleX
  54. local localY = -self.height / 2 * self.scaleY + self.regionOffsetY * regionScaleY
  55. local localX2 = localX + self.regionWidth * regionScaleX
  56. local localY2 = localY + self.regionHeight * regionScaleY
  57. local radians = self.rotation * math.pi / 180
  58. local cos = math.cos(radians)
  59. local sin = math.sin(radians)
  60. local localXCos = localX * cos + self.x
  61. local localXSin = localX * sin
  62. local localYCos = localY * cos + self.y
  63. local localYSin = localY * sin
  64. local localX2Cos = localX2 * cos + self.x
  65. local localX2Sin = localX2 * sin
  66. local localY2Cos = localY2 * cos + self.y
  67. local localY2Sin = localY2 * sin
  68. local offset = self.offset
  69. offset[0] = localXCos - localYSin -- X1
  70. offset[1] = localYCos + localXSin -- Y1
  71. offset[2] = localXCos - localY2Sin -- X2
  72. offset[3] = localY2Cos + localXSin -- Y2
  73. offset[4] = localX2Cos - localY2Sin -- X3
  74. offset[5] = localY2Cos + localX2Sin -- Y3
  75. offset[6] = localX2Cos - localYSin -- X4
  76. offset[7] = localYCos + localX2Sin -- Y4
  77. end
  78. function self:computeWorldVertices (x, y, bone, worldVertices)
  79. x = x + bone.worldX
  80. y = y + bone.worldY
  81. local m00, m01, m10, m11 = bone.m00, bone.m01, bone.m10, bone.m11
  82. local offset = self.offset
  83. local vertices = self.vertices;
  84. vertices[0] = offset[0] * m00 + offset[1] * m01 + x
  85. vertices[1] = offset[0] * m10 + offset[1] * m11 + y
  86. vertices[2] = offset[2] * m00 + offset[3] * m01 + x
  87. vertices[3] = offset[2] * m10 + offset[3] * m11 + y
  88. vertices[4] = offset[4] * m00 + offset[5] * m01 + x
  89. vertices[5] = offset[4] * m10 + offset[5] * m11 + y
  90. vertices[6] = offset[6] * m00 + offset[7] * m01 + x
  91. vertices[7] = offset[6] * m10 + offset[7] * m11 + y
  92. end
  93. return self
  94. end
  95. return RegionAttachment