RegionAttachment.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License
  3. -- Version 2.1
  4. --
  5. -- Copyright (c) 2013, Esoteric Software
  6. -- All rights reserved.
  7. --
  8. -- You are granted a perpetual, non-exclusive, non-sublicensable and
  9. -- non-transferable license to install, execute and perform the Spine Runtimes
  10. -- Software (the "Software") solely for internal use. Without the written
  11. -- permission of Esoteric Software (typically granted by licensing Spine), you
  12. -- may not (a) modify, translate, adapt or otherwise create derivative works,
  13. -- improvements of the Software or develop new applications using the Software
  14. -- or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. -- trademark, patent or other intellectual property or proprietary rights
  16. -- notices on or in the Software, including any copy thereof. Redistributions
  17. -- in binary or source 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 SOFTARE 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; LOSS OF USE, DATA, OR PROFITS;
  25. -- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. -- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. -- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. -- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local AttachmentType = require "lib/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 = null,
  45. rendererObject = null,
  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. 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