Skeleton.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 utils = require "lib/spine-lua/utils"
  31. local Bone = require "lib/spine-lua/Bone"
  32. local Slot = require "lib/spine-lua/Slot"
  33. local IkConstraint = require "lib/spine-lua/IkConstraint"
  34. local AttachmentLoader = require "lib/spine-lua/AttachmentLoader"
  35. local Skeleton = {}
  36. function Skeleton.new (skeletonData)
  37. if not skeletonData then error("skeletonData cannot be nil", 2) end
  38. local self = {
  39. data = skeletonData,
  40. bones = {},
  41. slots = {},
  42. slotsByName = {},
  43. drawOrder = {},
  44. ikConstraints = {},
  45. r = 1, g = 1, b = 1, a = 1,
  46. skin = nil,
  47. flipX = false, flipY = false,
  48. time = 0,
  49. x = 0, y = 0
  50. }
  51. -- Caches information about bones and IK constraints. Must be called if bones or IK constraints are added or removed.
  52. function self:updateCache ()
  53. self.boneCache = {}
  54. local boneCache = self.boneCache
  55. local ikConstraints = self.ikConstraints
  56. local ikConstraintsCount = #ikConstraints
  57. local arrayCount = ikConstraintsCount + 1
  58. while #boneCache < arrayCount do
  59. table.insert(boneCache, {})
  60. end
  61. local nonIkBones = boneCache[1]
  62. for i,bone in ipairs(self.bones) do
  63. local current = bone
  64. local continueOuter
  65. repeat
  66. for ii,ikConstraint in ipairs(ikConstraints) do
  67. local parent = ikConstraint.bones[0]
  68. local child = ikConstraint.bones[#ikConstraint.bones - 1]
  69. while true do
  70. if current == child then
  71. table.insert(boneCache[ii], bone)
  72. table.insert(boneCache[ii + 1], bone)
  73. ii = ikConstraintsCount
  74. continueOuter = true
  75. break
  76. end
  77. if child == parent then break end
  78. child = child.parent
  79. end
  80. end
  81. if continueOuter then break end
  82. current = current.parent
  83. until not current
  84. table.insert(nonIkBones, bone)
  85. end
  86. end
  87. -- Updates the world transform for each bone and applies IK constraints.
  88. function self:updateWorldTransform ()
  89. local bones = self.bones
  90. for i,bone in ipairs(self.bones) do
  91. bone.rotationIK = bone.rotation
  92. end
  93. local boneCache = self.boneCache
  94. local ikConstraints = self.ikConstraints
  95. local i = 1
  96. local last = #boneCache
  97. while true do
  98. for ii,bone in ipairs(boneCache[i]) do
  99. bone:updateWorldTransform()
  100. end
  101. if i == last then break end
  102. ikConstraints[i]:apply()
  103. i = i + 1
  104. end
  105. end
  106. function self:setToSetupPose ()
  107. self:setBonesToSetupPose()
  108. self:setSlotsToSetupPose()
  109. end
  110. function self:setBonesToSetupPose ()
  111. for i,bone in ipairs(self.bones) do
  112. bone:setToSetupPose()
  113. end
  114. for i,ikConstraint in ipairs(self.ikConstraints) do
  115. ikConstraint.bendDirection = ikConstraint.data.bendDirection
  116. ikConstraint.mix = ikConstraint.data.mix
  117. end
  118. end
  119. function self:setSlotsToSetupPose ()
  120. for i,slot in ipairs(self.slots) do
  121. self.drawOrder[i] = slot
  122. slot:setToSetupPose()
  123. end
  124. end
  125. function self:getRootBone ()
  126. return self.bones[1]
  127. end
  128. function self:findBone (boneName)
  129. if not boneName then error("boneName cannot be nil.", 2) end
  130. for i,bone in ipairs(self.bones) do
  131. if bone.data.name == boneName then return bone end
  132. end
  133. return nil
  134. end
  135. function self:findSlot (slotName)
  136. if not slotName then error("slotName cannot be nil.", 2) end
  137. return self.slotsByName[slotName]
  138. end
  139. -- Sets the skin used to look up attachments before looking in the {@link SkeletonData#getDefaultSkin() default skin}.
  140. -- Attachments from the new skin are attached if the corresponding attachment from the old skin was attached. If there was
  141. -- no old skin, each slot's setup mode attachment is attached from the new skin.
  142. function self:setSkin (skinName)
  143. local newSkin
  144. if skinName then
  145. newSkin = self.data:findSkin(skinName)
  146. if not newSkin then error("Skin not found = " .. skinName, 2) end
  147. if self.skin then
  148. -- Attach all attachments from the new skin if the corresponding attachment from the old skin is currently attached.
  149. for k,v in pairs(self.skin.attachments) do
  150. local attachment = v[3]
  151. local slotIndex = v[1]
  152. local slot = self.slots[slotIndex]
  153. if slot.attachment == attachment then
  154. local name = v[2]
  155. local newAttachment = newSkin:getAttachment(slotIndex, name)
  156. if newAttachment then slot:setAttachment(newAttachment) end
  157. end
  158. end
  159. else
  160. -- No previous skin, attach setup pose attachments.
  161. for i,slot in ipairs(self.slots) do
  162. local name = slot.data.attachmentName
  163. if name then
  164. local attachment = newSkin:getAttachment(i, name)
  165. if attachment then slot:setAttachment(attachment) end
  166. end
  167. end
  168. end
  169. end
  170. self.skin = newSkin
  171. end
  172. function self:getAttachment (slotName, attachmentName)
  173. if not slotName then error("slotName cannot be nil.", 2) end
  174. if not attachmentName then error("attachmentName cannot be nil.", 2) end
  175. local slotIndex = skeletonData.slotNameIndices[slotName]
  176. if slotIndex == -1 then error("Slot not found = " .. slotName, 2) end
  177. if self.skin then
  178. local attachment = self.skin:getAttachment(slotIndex, attachmentName)
  179. if attachment then return attachment end
  180. end
  181. if self.data.defaultSkin then
  182. return self.data.defaultSkin:getAttachment(slotIndex, attachmentName)
  183. end
  184. return nil
  185. end
  186. function self:setAttachment (slotName, attachmentName)
  187. if not slotName then error("slotName cannot be nil.", 2) end
  188. for i,slot in ipairs(self.slots) do
  189. if slot.data.name == slotName then
  190. if not attachmentName then
  191. slot:setAttachment(nil)
  192. else
  193. slot:setAttachment(self:getAttachment(slotName, attachmentName))
  194. end
  195. return
  196. end
  197. end
  198. error("Slot not found = " .. slotName, 2)
  199. end
  200. function self:update (delta)
  201. self.time = self.time + delta
  202. end
  203. function self:setColor (r, g, b, a)
  204. self.r = r
  205. self.g = g
  206. self.b = b
  207. self.a = a
  208. end
  209. for i,boneData in ipairs(skeletonData.bones) do
  210. local parent
  211. if boneData.parent then parent = self.bones[utils.indexOf(skeletonData.bones, boneData.parent)] end
  212. table.insert(self.bones, Bone.new(boneData, self, parent))
  213. end
  214. for i,slotData in ipairs(skeletonData.slots) do
  215. local bone = self.bones[utils.indexOf(skeletonData.bones, slotData.boneData)]
  216. local slot = Slot.new(slotData, bone)
  217. table.insert(self.slots, slot)
  218. self.slotsByName[slot.data.name] = slot
  219. table.insert(self.drawOrder, slot)
  220. end
  221. for i,ikConstraintData in ipairs(skeletonData.ikConstraints) do
  222. table.insert(self.ikConstraints, IkConstraint.new(ikConstraintData, self))
  223. end
  224. self:updateCache()
  225. return self
  226. end
  227. return Skeleton