[lua] Ported skin API changes, see #841.

This commit is contained in:
badlogic 2019-05-16 16:05:44 +02:00
parent e03a0ef659
commit b5039e5f81
10 changed files with 241 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -46,4 +46,8 @@ function Attachment.new (name, attachmentType)
return self
end
function Attachment:copy ()
error("Attachment copy not implemented.")
end
return Attachment

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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