mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[lua] Ported skin API changes, see #841.
This commit is contained in:
parent
e03a0ef659
commit
b5039e5f81
@ -247,7 +247,7 @@ function SkeletonJson.new (attachmentLoader)
|
|||||||
for attachmentName,attachmentMap in pairs(slotMap) do
|
for attachmentName,attachmentMap in pairs(slotMap) do
|
||||||
local attachment = readAttachment(attachmentMap, skin, slotIndex, attachmentName, skeletonData)
|
local attachment = readAttachment(attachmentMap, skin, slotIndex, attachmentName, skeletonData)
|
||||||
if attachment then
|
if attachment then
|
||||||
skin:addAttachment(slotIndex, attachmentName, attachment)
|
skin:setAttachment(slotIndex, attachmentName, attachment)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -350,6 +350,9 @@ function SkeletonJson.new (attachmentLoader)
|
|||||||
tonumber(color:sub(7, 8), 16) / 255)
|
tonumber(color:sub(7, 8), 16) / 255)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
mesh.width = getValue(map, "width", 0) * scale
|
||||||
|
mesh.height = getValue(map, "height", 0) * scale
|
||||||
|
|
||||||
local parent = map.parent
|
local parent = map.parent
|
||||||
if parent then
|
if parent then
|
||||||
mesh.inheritDeform = getValue(map, "deform", true)
|
mesh.inheritDeform = getValue(map, "deform", true)
|
||||||
|
|||||||
@ -29,6 +29,21 @@
|
|||||||
|
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local table_insert = table.insert
|
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 = {}
|
local Skin = {}
|
||||||
Skin.__index = Skin
|
Skin.__index = Skin
|
||||||
@ -38,19 +53,91 @@ function Skin.new (name)
|
|||||||
|
|
||||||
local self = {
|
local self = {
|
||||||
name = name,
|
name = name,
|
||||||
attachments = {}
|
attachments = {},
|
||||||
|
bones = {},
|
||||||
|
constraints = {}
|
||||||
}
|
}
|
||||||
setmetatable(self, Skin)
|
setmetatable(self, Skin)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
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 name then error("name cannot be nil.", 2) end
|
||||||
if not self.attachments[slotIndex] then self.attachments[slotIndex] = {} end
|
if not self.attachments[slotIndex] then self.attachments[slotIndex] = {} end
|
||||||
self.attachments[slotIndex][name] = attachment
|
self.attachments[slotIndex][name] = attachment
|
||||||
end
|
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)
|
function Skin:getAttachment (slotIndex, name)
|
||||||
if not name then error("name cannot be nil.", 2) end
|
if not name then error("name cannot be nil.", 2) end
|
||||||
local dictionary = self.attachments[slotIndex]
|
local dictionary = self.attachments[slotIndex]
|
||||||
@ -61,6 +148,46 @@ function Skin:getAttachment (slotIndex, name)
|
|||||||
end
|
end
|
||||||
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)
|
function Skin:attachAll(skeleton, oldSkin)
|
||||||
for i, slot in ipairs(skeleton.slots) do
|
for i, slot in ipairs(skeleton.slots) do
|
||||||
local slotAttachment = slot.attachment
|
local slotAttachment = slot.attachment
|
||||||
|
|||||||
@ -46,4 +46,8 @@ function Attachment.new (name, attachmentType)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Attachment:copy ()
|
||||||
|
error("Attachment copy not implemented.")
|
||||||
|
end
|
||||||
|
|
||||||
return Attachment
|
return Attachment
|
||||||
|
|||||||
@ -43,4 +43,11 @@ function BoundingBoxAttachment.new (name)
|
|||||||
setmetatable(self, BoundingBoxAttachment)
|
setmetatable(self, BoundingBoxAttachment)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function BoundingBoxAttachment:copy ()
|
||||||
|
local copy = BoundingBoxAttachment.new(self.name)
|
||||||
|
self:copyTo(copy)
|
||||||
|
copy.color:setFrom(self.color)
|
||||||
|
return copy
|
||||||
|
end
|
||||||
return BoundingBoxAttachment
|
return BoundingBoxAttachment
|
||||||
|
|||||||
@ -44,4 +44,13 @@ function ClippingAttachment.new (name)
|
|||||||
setmetatable(self, ClippingAttachment)
|
setmetatable(self, ClippingAttachment)
|
||||||
return self
|
return self
|
||||||
end
|
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
|
return ClippingAttachment
|
||||||
|
|||||||
@ -51,6 +51,8 @@ function MeshAttachment.new (name)
|
|||||||
self.parentMesh = nil
|
self.parentMesh = nil
|
||||||
self.inheritDeform = false
|
self.inheritDeform = false
|
||||||
self.tempColor = Color.newWith(1, 1, 1, 1)
|
self.tempColor = Color.newWith(1, 1, 1, 1)
|
||||||
|
self.width = 0
|
||||||
|
self.height = 0
|
||||||
setmetatable(self, MeshAttachment)
|
setmetatable(self, MeshAttachment)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
@ -143,4 +145,31 @@ function MeshAttachment:setParentMesh (parentMesh)
|
|||||||
end
|
end
|
||||||
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
|
return MeshAttachment
|
||||||
|
|||||||
@ -30,6 +30,7 @@
|
|||||||
local AttachmentType = require "spine-lua.attachments.AttachmentType"
|
local AttachmentType = require "spine-lua.attachments.AttachmentType"
|
||||||
local VertexAttachment = require "spine-lua.attachments.VertexAttachment"
|
local VertexAttachment = require "spine-lua.attachments.VertexAttachment"
|
||||||
local Color = require "spine-lua.Color"
|
local Color = require "spine-lua.Color"
|
||||||
|
local utils = require "spine-lua.utils"
|
||||||
|
|
||||||
local PathAttachment = {}
|
local PathAttachment = {}
|
||||||
PathAttachment.__index = PathAttachment
|
PathAttachment.__index = PathAttachment
|
||||||
@ -41,7 +42,20 @@ function PathAttachment.new (name)
|
|||||||
local self = VertexAttachment.new(name, AttachmentType.path)
|
local self = VertexAttachment.new(name, AttachmentType.path)
|
||||||
self.lengths = nil
|
self.lengths = nil
|
||||||
self.color = Color.newWith(1, 1, 1, 1)
|
self.color = Color.newWith(1, 1, 1, 1)
|
||||||
|
self.closed = false
|
||||||
|
self.constantSpeed = false
|
||||||
setmetatable(self, PathAttachment)
|
setmetatable(self, PathAttachment)
|
||||||
return self
|
return self
|
||||||
end
|
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
|
return PathAttachment
|
||||||
|
|||||||
@ -64,4 +64,13 @@ function PointAttachment:computeWorldRotation(bone)
|
|||||||
return math_deg(math_atan2(y, x))
|
return math_deg(math_atan2(y, x))
|
||||||
end
|
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
|
return PointAttachment
|
||||||
|
|||||||
@ -242,4 +242,23 @@ function RegionAttachment:computeWorldVertices (bone, worldVertices, offset, str
|
|||||||
worldVertices[offset + 1] = offsetX * c + offsetY * d + y
|
worldVertices[offset + 1] = offsetX * c + offsetY * d + y
|
||||||
end
|
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
|
return RegionAttachment
|
||||||
|
|||||||
@ -32,7 +32,7 @@
|
|||||||
-- to 1-based indexing eventually.
|
-- to 1-based indexing eventually.
|
||||||
|
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
|
local utils = require "spine-lua.utils"
|
||||||
local AttachmentType = require "spine-lua.attachments.AttachmentType"
|
local AttachmentType = require "spine-lua.attachments.AttachmentType"
|
||||||
local Attachment = require "spine-lua.attachments.Attachment"
|
local Attachment = require "spine-lua.attachments.Attachment"
|
||||||
|
|
||||||
@ -151,4 +151,20 @@ function VertexAttachment:applyDeform (sourceAttachment)
|
|||||||
return self == sourceAttachment
|
return self == sourceAttachment
|
||||||
end
|
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
|
return VertexAttachment
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user