diff --git a/spine-lua/SkeletonJson.lua b/spine-lua/SkeletonJson.lua index d5e716820..0d96c9d2f 100644 --- a/spine-lua/SkeletonJson.lua +++ b/spine-lua/SkeletonJson.lua @@ -247,7 +247,7 @@ function SkeletonJson.new (attachmentLoader) for attachmentName,attachmentMap in pairs(slotMap) do local attachment = readAttachment(attachmentMap, skin, slotIndex, attachmentName, skeletonData) if attachment then - skin:addAttachment(slotIndex, attachmentName, attachment) + skin:setAttachment(slotIndex, attachmentName, attachment) end end end @@ -349,6 +349,9 @@ function SkeletonJson.new (attachmentLoader) tonumber(color:sub(5, 6), 16) / 255, tonumber(color:sub(7, 8), 16) / 255) end + + mesh.width = getValue(map, "width", 0) * scale + mesh.height = getValue(map, "height", 0) * scale local parent = map.parent if parent then diff --git a/spine-lua/Skin.lua b/spine-lua/Skin.lua index 71ab9db78..54deb01ea 100644 --- a/spine-lua/Skin.lua +++ b/spine-lua/Skin.lua @@ -29,6 +29,21 @@ local setmetatable = setmetatable local table_insert = table.insert +local AttachmentType = require "spine-lua.attachments.AttachmentType" + +local SkinEntry = {} +SkinEntry.__index = SkinEntry + +function SkinEntry.new (slotIndex, name, attachment) + local self = { + slotIndex = slotIndex, + name = name, + attachment = attachment + } + setmetatable(self, SkinEntry) + + return self +end local Skin = {} Skin.__index = Skin @@ -38,19 +53,91 @@ function Skin.new (name) local self = { name = name, - attachments = {} + attachments = {}, + bones = {}, + constraints = {} } setmetatable(self, Skin) return self end -function Skin:addAttachment (slotIndex, name, attachment) +function Skin:setAttachment (slotIndex, name, attachment) if not name then error("name cannot be nil.", 2) end if not self.attachments[slotIndex] then self.attachments[slotIndex] = {} end self.attachments[slotIndex][name] = attachment end +function Skin:addSkin (skin) + for i, bone in ipairs(skin.bones) do + local contained = false + for j, otherBone in ipairs(self.bones) do + if otherBone == bone then + contained = true + break + end + end + if not contained then table_insert(self.bones, bone) end + end + + for i, constraint in ipairs(skin.constraints) do + local contained = false + for j, otherConstraint in ipairs(self.constraints) do + if otherConstraint == constraint then + contained = true + break + end + end + if not contained then table_insert(self.constraints, constraint) end + end + + local attachments = skin:getAttachments() + for i, entry in ipairs(attachments) do + self:setAttachment(entry.slotIndex, entry.name, entry.attachment) + end +end + +function Skin:copySkin (skin) + for i, bone in ipairs(skin.bones) do + local contained = false + for j, otherBone in ipairs(self.bones) do + if otherBone == bone then + contained = true + break + end + end + if not contained then table_insert(self.bones, bone) end + end + + for i, constraint in ipairs(skin.constraints) do + local contained = false + for j, otherConstraint in ipairs(self.constraints) do + if otherConstraint == constraint then + contained = true + break + end + end + if not contained then table_insert(self.constraints, constraint) end + end + + local attachments = skin:getAttachments() + for i, entry in ipairs(attachments) do + entry.attachment = entry.attachment:copy() + self:setAttachment(entry.slotIndex, entry.name, entry.attachment) + end + + attachments = self:getAttachments() + for i, entry in ipairs(attachments) do + if entry.attachment.type == AttachmentType.mesh then + local mesh = entry.attachment + if mesh.parentMesh then + mesh:setParentMesh(self:getAttachment(entry.slotIndex, mesh:getParentMesh().name)) + mesh:updateUVs() + end + end + end +end + function Skin:getAttachment (slotIndex, name) if not name then error("name cannot be nil.", 2) end local dictionary = self.attachments[slotIndex] @@ -61,6 +148,46 @@ function Skin:getAttachment (slotIndex, name) end end +function Skin:removeAttachment (slotIndex, name) + local slotAttachments = self.attachments[slotIndex] + if slotAttachments then + slotAttachments[name] = nil + end +end + +function Skin:getAttachments () + local entries = {} + for slotIndex, slotAttachments in pairs(self.attachments) do + if slotAttachments then + for name, attachment in pairs(slotAttachments) do + if attachment then + table_insert(entries, SkinEntry.new(slotIndex, name, attachment)) + end + end + end + end + return entries +end + +function Skin:getAttachmentsForSlot (slotIndex) + local entries = {} + local slotAttachments = self.attachments[slotIndex] + if slotAttachments then + for name, attachment in pairs(slotAttachments) do + if attachment then + table_insert(entries, SkinEntry.new(slotIndex, name, attachment)) + end + end + end + return entries +end + +function Skin:clear () + self.attachments = {} + self.bones = {} + self.constraints = {} +end + function Skin:attachAll(skeleton, oldSkin) for i, slot in ipairs(skeleton.slots) do local slotAttachment = slot.attachment diff --git a/spine-lua/attachments/Attachment.lua b/spine-lua/attachments/Attachment.lua index d680f3800..34b315b23 100644 --- a/spine-lua/attachments/Attachment.lua +++ b/spine-lua/attachments/Attachment.lua @@ -46,4 +46,8 @@ function Attachment.new (name, attachmentType) return self end +function Attachment:copy () + error("Attachment copy not implemented.") +end + return Attachment diff --git a/spine-lua/attachments/BoundingBoxAttachment.lua b/spine-lua/attachments/BoundingBoxAttachment.lua index e2d65bf16..3144e7098 100644 --- a/spine-lua/attachments/BoundingBoxAttachment.lua +++ b/spine-lua/attachments/BoundingBoxAttachment.lua @@ -43,4 +43,11 @@ function BoundingBoxAttachment.new (name) setmetatable(self, BoundingBoxAttachment) return self end + +function BoundingBoxAttachment:copy () + local copy = BoundingBoxAttachment.new(self.name) + self:copyTo(copy) + copy.color:setFrom(self.color) + return copy +end return BoundingBoxAttachment diff --git a/spine-lua/attachments/ClippingAttachment.lua b/spine-lua/attachments/ClippingAttachment.lua index 27c06c382..52933141b 100644 --- a/spine-lua/attachments/ClippingAttachment.lua +++ b/spine-lua/attachments/ClippingAttachment.lua @@ -44,4 +44,13 @@ function ClippingAttachment.new (name) setmetatable(self, ClippingAttachment) return self end + +function ClippingAttachment:copy () + local copy = ClippingAttachment.new(self.name) + self:copyTo(copy) + copy.endSlot = self.endSlot + copy.color:setFrom(self.color) + return copy +end + return ClippingAttachment diff --git a/spine-lua/attachments/MeshAttachment.lua b/spine-lua/attachments/MeshAttachment.lua index c4fef32d5..e7823c517 100644 --- a/spine-lua/attachments/MeshAttachment.lua +++ b/spine-lua/attachments/MeshAttachment.lua @@ -51,6 +51,8 @@ function MeshAttachment.new (name) self.parentMesh = nil self.inheritDeform = false self.tempColor = Color.newWith(1, 1, 1, 1) + self.width = 0 + self.height = 0 setmetatable(self, MeshAttachment) return self end @@ -143,4 +145,31 @@ function MeshAttachment:setParentMesh (parentMesh) end end +function MeshAttachment:copy () + local copy = MeshAttachment.new(self.name) + copy.region = self.region + copy.path = self.path + + if not self.parentMesh then + self:copyTo(copy) + copy.regionUVs = utils.copy(self.regionUVs) + copy.uvs = utils.copy(self.uvs) + copy.triangles = utils.copy(self.triangles) + copy.color:setFrom(self.color) + copy.hullLength = self.hullLength + copy.inheritDeform = self.inheritDeform + copy.tempColor:setFrom(self.tempColor) + if self.edges then + copy.edges = utils.copy(edges) + end + copy.width = self.width + copy.height = self.height + else + copy:setParentMesh(self.parentMesh) + copy.updateUVs() + end + + return copy +end + return MeshAttachment diff --git a/spine-lua/attachments/PathAttachment.lua b/spine-lua/attachments/PathAttachment.lua index dccc45335..d94d2a43b 100644 --- a/spine-lua/attachments/PathAttachment.lua +++ b/spine-lua/attachments/PathAttachment.lua @@ -30,6 +30,7 @@ local AttachmentType = require "spine-lua.attachments.AttachmentType" local VertexAttachment = require "spine-lua.attachments.VertexAttachment" local Color = require "spine-lua.Color" +local utils = require "spine-lua.utils" local PathAttachment = {} PathAttachment.__index = PathAttachment @@ -41,7 +42,20 @@ function PathAttachment.new (name) local self = VertexAttachment.new(name, AttachmentType.path) self.lengths = nil self.color = Color.newWith(1, 1, 1, 1) + self.closed = false + self.constantSpeed = false setmetatable(self, PathAttachment) return self end + +function PathAttachment:copy () + local copy = PathAttachment.new(self.name) + self.copyTo(copy) + copy.length = utils.copy(self.lengths) + copy.closed = self.closed + copy.constantSpeed = self.constantSpeed + copy.color:setFrom(self.color) + return copy +end + return PathAttachment diff --git a/spine-lua/attachments/PointAttachment.lua b/spine-lua/attachments/PointAttachment.lua index 7a300def1..6c94d6b51 100644 --- a/spine-lua/attachments/PointAttachment.lua +++ b/spine-lua/attachments/PointAttachment.lua @@ -64,4 +64,13 @@ function PointAttachment:computeWorldRotation(bone) return math_deg(math_atan2(y, x)) end +function PointAttachment:copy () + local copy = PointAttachment.new(self.name) + copy.x = self.x + copy.y = self.y + copy.rotation = self.rotation + copy.color:setFrom(self.color) + return copy +end + return PointAttachment diff --git a/spine-lua/attachments/RegionAttachment.lua b/spine-lua/attachments/RegionAttachment.lua index c6ca12013..9f8865b8a 100644 --- a/spine-lua/attachments/RegionAttachment.lua +++ b/spine-lua/attachments/RegionAttachment.lua @@ -242,4 +242,23 @@ function RegionAttachment:computeWorldVertices (bone, worldVertices, offset, str worldVertices[offset + 1] = offsetX * c + offsetY * d + y end +function RegionAttachment:copy () + local copy = RegionAttachment.new(self.name) + copy.x = self.x + copy.y = self.y + copy.scaleX = self.scaleX + copy.scaleY = self.scaleY + copy.rotation = self.rotation + copy.width = self.width + copy.height = self.height + copy.color:setFrom(self.color) + copy.path = self.path + copy.rendererObject = self.rendererObject + copy.region = self.region + copy.offset = Utils.copy(self.offset) + copy.uvs = Utils.copy(self.uvs) + copy.tempColor:setFrom(self.tempColor) + return copy +end + return RegionAttachment diff --git a/spine-lua/attachments/VertexAttachment.lua b/spine-lua/attachments/VertexAttachment.lua index e79a1031f..d83b78e93 100644 --- a/spine-lua/attachments/VertexAttachment.lua +++ b/spine-lua/attachments/VertexAttachment.lua @@ -32,7 +32,7 @@ -- to 1-based indexing eventually. local setmetatable = setmetatable - +local utils = require "spine-lua.utils" local AttachmentType = require "spine-lua.attachments.AttachmentType" local Attachment = require "spine-lua.attachments.Attachment" @@ -151,4 +151,20 @@ function VertexAttachment:applyDeform (sourceAttachment) return self == sourceAttachment end +function VertexAttachment:copyTo (attachment) + if self.bones then + attachment.bones = utils.copy(self.bones) + else + attachment.bones = nil + end + + if self.vertices then + attachment.vertices = utils.copy(self.vertices) + else + attachment.vertices = nil + end + + attachment.worldVerticesLength = self.worldVerticesLength +end + return VertexAttachment