Slot.lua 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes License Agreement
  3. -- Last updated May 1, 2019. Replaces all prior versions.
  4. --
  5. -- Copyright (c) 2013-2019, Esoteric Software LLC
  6. --
  7. -- Integration of the Spine Runtimes into software or otherwise creating
  8. -- derivative works of the Spine Runtimes is permitted under the terms and
  9. -- conditions of Section 2 of the Spine Editor License Agreement:
  10. -- http://esotericsoftware.com/spine-editor-license
  11. --
  12. -- Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. -- or otherwise create derivative works of the Spine Runtimes (collectively,
  14. -- "Products"), provided that each user of the Products must obtain their own
  15. -- Spine Editor license and redistribution of the Products in any form must
  16. -- include this license and copyright notice.
  17. --
  18. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. -- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. -- NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. -- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. -- INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. -- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. -- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. -------------------------------------------------------------------------------
  29. local setmetatable = setmetatable
  30. local Color = require "spine-lua.Color"
  31. local Slot = {}
  32. Slot.__index = Slot
  33. function Slot.new (data, bone)
  34. if not data then error("slotData cannot be nil", 2) end
  35. if not bone then error("bone cannot be nil", 2) end
  36. local self = {
  37. data = data,
  38. bone = bone,
  39. color = Color.newWith(1, 1, 1, 1),
  40. darkColor = nil,
  41. attachment = nil,
  42. attachmentTime = 0,
  43. attachmentVertices = {}
  44. }
  45. setmetatable(self, Slot)
  46. if data.darkColor then self.darkColor = Color.newWith(1, 1, 1, 1) end
  47. self:setToSetupPose()
  48. return self
  49. end
  50. function Slot:setAttachment (attachment)
  51. if self.attachment == attachment then return end
  52. self.attachment = attachment
  53. self.attachmentTime = self.bone.skeleton.time
  54. self.attachmentVertices = {}
  55. end
  56. function Slot:setAttachmentTime (time)
  57. self.attachmentTime = self.bone.skeleton.time - time
  58. end
  59. function Slot:getAttachmentTime ()
  60. return self.bone.skeleton.time - self.attachmentTime
  61. end
  62. function Slot:setToSetupPose ()
  63. local data = self.data
  64. self.color:setFrom(data.color)
  65. if self.darkColor then self.darkColor:setFrom(data.darkColor) end
  66. local attachment = nil
  67. if data.attachmentName then
  68. attachment = self.bone.skeleton:getAttachmentByIndex(data.index, data.attachmentName)
  69. end
  70. self:setAttachment(attachment)
  71. end
  72. return Slot