Bone.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Bone = {}
  31. function Bone.new (data, parent)
  32. if not data then error("data cannot be nil", 2) end
  33. local self = {
  34. data = data,
  35. parent = parent,
  36. x = 0, y = 0,
  37. rotation = 0,
  38. scaleX = 1, scaleY = 1,
  39. m00 = 0, m01 = 0, worldX = 0, -- a b x
  40. m10 = 0, m11 = 0, worldY = 0, -- c d y
  41. worldRotation = 0,
  42. worldScaleX = 1, worldScaleY = 1,
  43. }
  44. function self:updateWorldTransform (flipX, flipY)
  45. local parent = self.parent
  46. if parent then
  47. self.worldX = self.x * parent.m00 + self.y * parent.m01 + parent.worldX
  48. self.worldY = self.x * parent.m10 + self.y * parent.m11 + parent.worldY
  49. if (self.data.inheritScale) then
  50. self.worldScaleX = parent.worldScaleX * self.scaleX
  51. self.worldScaleY = parent.worldScaleY * self.scaleY
  52. else
  53. self.worldScaleX = self.scaleX
  54. self.worldScaleY = self.scaleY
  55. end
  56. if (self.data.inheritRotation) then
  57. self.worldRotation = parent.worldRotation + self.rotation
  58. else
  59. self.worldRotation = self.rotation
  60. end
  61. else
  62. if flipX then
  63. self.worldX = -self.x
  64. else
  65. self.worldX = self.x
  66. end
  67. if flipY then
  68. self.worldY = -self.y
  69. else
  70. self.worldY = self.y
  71. end
  72. self.worldScaleX = self.scaleX
  73. self.worldScaleY = self.scaleY
  74. self.worldRotation = self.rotation
  75. end
  76. local radians = math.rad(self.worldRotation)
  77. local cos = math.cos(radians)
  78. local sin = math.sin(radians)
  79. self.m00 = cos * self.worldScaleX
  80. self.m10 = sin * self.worldScaleX
  81. self.m01 = -sin * self.worldScaleY
  82. self.m11 = cos * self.worldScaleY
  83. if flipX then
  84. self.m00 = -self.m00
  85. self.m01 = -self.m01
  86. end
  87. if flipY then
  88. self.m10 = -self.m10
  89. self.m11 = -self.m11
  90. end
  91. end
  92. function self:setToSetupPose ()
  93. local data = self.data
  94. self.x = data.x
  95. self.y = data.y
  96. self.rotation = data.rotation
  97. self.scaleX = data.scaleX
  98. self.scaleY = data.scaleY
  99. end
  100. self:setToSetupPose()
  101. return self
  102. end
  103. return Bone