Skeleton.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 = require "spine-lua.Bone"
  31. local Slot = require "spine-lua.Slot"
  32. local AttachmentLoader = require "spine-lua.AttachmentLoader"
  33. local Skeleton = {}
  34. function Skeleton.new (skeletonData)
  35. if not skeletonData then error("skeletonData cannot be nil", 2) end
  36. local self = {
  37. data = skeletonData,
  38. bones = {},
  39. slots = {},
  40. slotsByName = {},
  41. drawOrder = {},
  42. r = 1, g = 1, b = 1, a = 1,
  43. x = 0, y = 0,
  44. skin = nil,
  45. flipX = false, flipY = false,
  46. time = 0
  47. }
  48. function self:updateWorldTransform ()
  49. for i,bone in ipairs(self.bones) do
  50. bone:updateWorldTransform(self.flipX, self.flipY)
  51. end
  52. end
  53. function self:setToSetupPose ()
  54. self:setBonesToSetupPose()
  55. self:setSlotsToSetupPose()
  56. end
  57. function self:setBonesToSetupPose ()
  58. for i,bone in ipairs(self.bones) do
  59. bone:setToSetupPose()
  60. end
  61. end
  62. function self:setSlotsToSetupPose ()
  63. for i,slot in ipairs(self.slots) do
  64. self.drawOrder[i] = slot
  65. slot:setToSetupPose()
  66. end
  67. end
  68. function self:getRootBone ()
  69. return self.bones[1]
  70. end
  71. function self:findBone (boneName)
  72. if not boneName then error("boneName cannot be nil.", 2) end
  73. for i,bone in ipairs(self.bones) do
  74. if bone.data.name == boneName then return bone end
  75. end
  76. return nil
  77. end
  78. function self:findSlot (slotName)
  79. if not slotName then error("slotName cannot be nil.", 2) end
  80. return self.slotsByName[slotName]
  81. end
  82. function self:setSkin (skinName)
  83. local newSkin
  84. if skinName then
  85. newSkin = self.data:findSkin(skinName)
  86. if not newSkin then error("Skin not found = " .. skinName, 2) end
  87. if self.skin then
  88. -- Attach all attachments from the new skin if the corresponding attachment from the old skin is currently attached.
  89. for k,v in pairs(self.skin.attachments) do
  90. local attachment = v[3]
  91. local slotIndex = v[1]
  92. local slot = self.slots[slotIndex]
  93. if slot.attachment == attachment then
  94. local name = v[2]
  95. local newAttachment = newSkin:getAttachment(slotIndex, name)
  96. if newAttachment then slot:setAttachment(newAttachment) end
  97. end
  98. end
  99. else
  100. -- No previous skin, attach setup pose attachments.
  101. for i,slot in ipairs(self.slots) do
  102. local name = slot.data.attachmentName
  103. if name then
  104. local attachment = newSkin:getAttachment(i, name)
  105. if attachment then slot:setAttachment(attachment) end
  106. end
  107. end
  108. end
  109. end
  110. self.skin = newSkin
  111. end
  112. function self:getAttachment (slotName, attachmentName)
  113. if not slotName then error("slotName cannot be nil.", 2) end
  114. if not attachmentName then error("attachmentName cannot be nil.", 2) end
  115. local slotIndex = skeletonData.slotNameIndices[slotName]
  116. if slotIndex == -1 then error("Slot not found = " .. slotName, 2) end
  117. if self.skin then
  118. local attachment = self.skin:getAttachment(slotIndex, attachmentName)
  119. if attachment then return attachment end
  120. end
  121. if self.data.defaultSkin then
  122. return self.data.defaultSkin:getAttachment(slotIndex, attachmentName)
  123. end
  124. return nil
  125. end
  126. function self:setAttachment (slotName, attachmentName)
  127. if not slotName then error("slotName cannot be nil.", 2) end
  128. for i,slot in ipairs(self.slots) do
  129. if slot.data.name == slotName then
  130. if not attachmentName then
  131. slot:setAttachment(nil)
  132. else
  133. slot:setAttachment(self:getAttachment(slotName, attachmentName))
  134. end
  135. return
  136. end
  137. end
  138. error("Slot not found = " .. slotName, 2)
  139. end
  140. function self:update (delta)
  141. self.time = self.time + delta
  142. end
  143. function self:setColor (r, g, b, a)
  144. self.r = r
  145. self.g = g
  146. self.b = b
  147. self.a = a
  148. end
  149. for i,boneData in ipairs(skeletonData.bones) do
  150. local parent
  151. if boneData.parent then parent = self.bones[spine.utils.indexOf(skeletonData.bones, boneData.parent)] end
  152. table.insert(self.bones, Bone.new(boneData, parent))
  153. end
  154. for i,slotData in ipairs(skeletonData.slots) do
  155. local bone = self.bones[spine.utils.indexOf(skeletonData.bones, slotData.boneData)]
  156. local slot = Slot.new(slotData, self, bone)
  157. table.insert(self.slots, slot)
  158. self.slotsByName[slot.data.name] = slot
  159. table.insert(self.drawOrder, slot)
  160. end
  161. return self
  162. end
  163. return Skeleton