mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
Refactored spine-corona to use an attachment loader like the other runtimes. This is more flexible than the old attachment resolver.
This commit is contained in:
parent
462faef9aa
commit
0c2e2b1d3e
@ -1,13 +1,15 @@
|
|||||||
|
|
||||||
local spine = require "spine.spine"
|
local spine = require "spine.spine"
|
||||||
|
|
||||||
-- Optional attachment resolver customizes where images are loaded. Eg, could use an image sheet.
|
-- Using your own attachment loader is optional. It can customizes the path where images are
|
||||||
local attachmentResolver = spine.AttachmentResolver.new()
|
-- loaded. To load from a texture atlas, use an image sheet. It also creates instances of
|
||||||
function attachmentResolver:createImage (attachment)
|
-- all attachments, which can be used for customization.
|
||||||
|
local attachmentLoader = spine.AttachmentLoader.new()
|
||||||
|
function attachmentLoader:createImage (attachment)
|
||||||
return display.newImage("data/" .. attachment.name .. ".png")
|
return display.newImage("data/" .. attachment.name .. ".png")
|
||||||
end
|
end
|
||||||
|
|
||||||
local json = spine.SkeletonJson.new(attachmentResolver)
|
local json = spine.SkeletonJson.new(attachmentLoader)
|
||||||
json.scale = 1
|
json.scale = 1
|
||||||
local skeletonData = json:readSkeletonDataFile("data/spineboy-skeleton.json")
|
local skeletonData = json:readSkeletonDataFile("data/spineboy-skeleton.json")
|
||||||
local walkAnimation = json:readAnimationFile(skeletonData, "data/spineboy-walk.json")
|
local walkAnimation = json:readAnimationFile(skeletonData, "data/spineboy-walk.json")
|
||||||
|
|||||||
@ -23,26 +23,20 @@
|
|||||||
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
local AttachmentResolver = {
|
local RegionAttachment = require "spine.RegionAttachment"
|
||||||
failed = {}
|
|
||||||
}
|
|
||||||
function AttachmentResolver.new ()
|
|
||||||
local self = {
|
|
||||||
images = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
function self:resolve (skeleton, attachment)
|
local AttachmentLoader = {
|
||||||
local image = self:createImage(attachment)
|
failed = {},
|
||||||
if image then
|
ATTACHMENT_REGION = "region"
|
||||||
image:setReferencePoint(display.CenterReferencePoint);
|
}
|
||||||
image.width = attachment.width
|
function AttachmentLoader.new ()
|
||||||
image.height = attachment.height
|
local self = {}
|
||||||
else
|
|
||||||
print("Error creating image: " .. attachment.name)
|
function self:newAttachment (type, name)
|
||||||
image = AttachmentResolver.failed
|
if type == AttachmentLoader.ATTACHMENT_REGION then
|
||||||
|
return RegionAttachment.new(name)
|
||||||
end
|
end
|
||||||
skeleton.images[attachment] = image
|
error("Unknown attachment type: " .. type .. " (" + name + ")")
|
||||||
return image
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function self:createImage (attachment)
|
function self:createImage (attachment)
|
||||||
@ -51,4 +45,4 @@ function AttachmentResolver.new ()
|
|||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
return AttachmentResolver
|
return AttachmentLoader
|
||||||
@ -26,7 +26,7 @@
|
|||||||
local utils = require "spine.utils"
|
local utils = require "spine.utils"
|
||||||
local Bone = require "spine.Bone"
|
local Bone = require "spine.Bone"
|
||||||
local Slot = require "spine.Slot"
|
local Slot = require "spine.Slot"
|
||||||
local AttachmentResolver = require "spine.AttachmentResolver"
|
local AttachmentLoader = require "spine.AttachmentLoader"
|
||||||
|
|
||||||
local Skeleton = {}
|
local Skeleton = {}
|
||||||
function Skeleton.new (skeletonData, group)
|
function Skeleton.new (skeletonData, group)
|
||||||
@ -45,15 +45,27 @@ function Skeleton.new (skeletonData, group)
|
|||||||
end
|
end
|
||||||
|
|
||||||
for i,slot in ipairs(self.drawOrder) do
|
for i,slot in ipairs(self.drawOrder) do
|
||||||
if slot.attachment then
|
local attachment = slot.attachment
|
||||||
local image = self.images[slot.attachment]
|
if attachment then
|
||||||
if not image then image = self.data.attachmentResolver:resolve(self, slot.attachment) end
|
local image = self.images[attachment]
|
||||||
if image ~= AttachmentResolver.failed then
|
if not image then
|
||||||
image.x = slot.bone.worldX + slot.attachment.x * slot.bone.m00 + slot.attachment.y * slot.bone.m01
|
image = self.data.attachmentLoader:createImage(attachment)
|
||||||
image.y = -(slot.bone.worldY + slot.attachment.x * slot.bone.m10 + slot.attachment.y * slot.bone.m11)
|
if image then
|
||||||
image.rotation = -(slot.bone.worldRotation + slot.attachment.rotation)
|
image:setReferencePoint(display.CenterReferencePoint);
|
||||||
image.xScale = slot.bone.worldScaleX + slot.attachment.scaleX - 1
|
image.width = attachment.width
|
||||||
image.yScale = slot.bone.worldScaleY + slot.attachment.scaleY - 1
|
image.height = attachment.height
|
||||||
|
else
|
||||||
|
print("Error creating image: " .. attachment.name)
|
||||||
|
image = AttachmentLoader.failed
|
||||||
|
end
|
||||||
|
self.images[attachment] = image
|
||||||
|
end
|
||||||
|
if image ~= AttachmentLoader.failed then
|
||||||
|
image.x = slot.bone.worldX + attachment.x * slot.bone.m00 + attachment.y * slot.bone.m01
|
||||||
|
image.y = -(slot.bone.worldY + attachment.x * slot.bone.m10 + attachment.y * slot.bone.m11)
|
||||||
|
image.rotation = -(slot.bone.worldRotation + attachment.rotation)
|
||||||
|
image.xScale = slot.bone.worldScaleX + attachment.scaleX - 1
|
||||||
|
image.yScale = slot.bone.worldScaleY + attachment.scaleY - 1
|
||||||
if self.flipX then
|
if self.flipX then
|
||||||
image.xScale = -image.xScale
|
image.xScale = -image.xScale
|
||||||
image.rotation = -image.rotation
|
image.rotation = -image.rotation
|
||||||
|
|||||||
@ -24,11 +24,11 @@
|
|||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
|
|
||||||
local SkeletonData = {}
|
local SkeletonData = {}
|
||||||
function SkeletonData.new (attachmentResolver)
|
function SkeletonData.new (attachmentLoader)
|
||||||
if not attachmentResolver then error("attachmentResolver cannot be nil", 2) end
|
if not attachmentLoader then error("attachmentLoader cannot be nil", 2) end
|
||||||
|
|
||||||
local self = {
|
local self = {
|
||||||
attachmentResolver = attachmentResolver,
|
attachmentLoader = attachmentLoader,
|
||||||
bones = {},
|
bones = {},
|
||||||
slots = {},
|
slots = {},
|
||||||
skins = {}
|
skins = {}
|
||||||
|
|||||||
@ -28,8 +28,7 @@ local SkeletonData = require "spine.SkeletonData"
|
|||||||
local BoneData = require "spine.BoneData"
|
local BoneData = require "spine.BoneData"
|
||||||
local SlotData = require "spine.SlotData"
|
local SlotData = require "spine.SlotData"
|
||||||
local Skin = require "spine.Skin"
|
local Skin = require "spine.Skin"
|
||||||
local RegionAttachment = require "spine.RegionAttachment"
|
local AttachmentLoader = require "spine.AttachmentLoader"
|
||||||
local AttachmentResolver = require "spine.AttachmentResolver"
|
|
||||||
local Animation = require "spine.Animation"
|
local Animation = require "spine.Animation"
|
||||||
local json = require "json"
|
local json = require "json"
|
||||||
|
|
||||||
@ -39,15 +38,12 @@ local TIMELINE_TRANSLATE = "translate"
|
|||||||
local TIMELINE_ATTACHMENT = "attachment"
|
local TIMELINE_ATTACHMENT = "attachment"
|
||||||
local TIMELINE_COLOR = "color"
|
local TIMELINE_COLOR = "color"
|
||||||
|
|
||||||
local ATTACHMENT_REGION = "region"
|
|
||||||
local ATTACHMENT_ANIMATED_REGION = "animatedRegion"
|
|
||||||
|
|
||||||
local SkeletonJson = {}
|
local SkeletonJson = {}
|
||||||
function SkeletonJson.new (attachmentResolver)
|
function SkeletonJson.new (attachmentLoader)
|
||||||
if not attachmentResolver then attachmentResolver = AttachmentResolver.new() end
|
if not attachmentLoader then attachmentLoader = AttachmentLoader.new() end
|
||||||
|
|
||||||
local self = {
|
local self = {
|
||||||
attachmentResolver = attachmentResolver,
|
attachmentLoader = attachmentLoader,
|
||||||
scale = 1
|
scale = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +54,7 @@ function SkeletonJson.new (attachmentResolver)
|
|||||||
local readAttachment
|
local readAttachment
|
||||||
|
|
||||||
function self:readSkeletonData (jsonText)
|
function self:readSkeletonData (jsonText)
|
||||||
local skeletonData = SkeletonData.new(self.attachmentResolver)
|
local skeletonData = SkeletonData.new(self.attachmentLoader)
|
||||||
|
|
||||||
local root = json.decode(jsonText)
|
local root = json.decode(jsonText)
|
||||||
if not root then error("Invalid JSON: " .. jsonText, 2) end
|
if not root then error("Invalid JSON: " .. jsonText, 2) end
|
||||||
@ -116,7 +112,9 @@ function SkeletonJson.new (attachmentResolver)
|
|||||||
local slotIndex = skeletonData:findSlotIndex(slotName)
|
local slotIndex = skeletonData:findSlotIndex(slotName)
|
||||||
for attachmentName,attachmentMap in pairs(slotMap) do
|
for attachmentName,attachmentMap in pairs(slotMap) do
|
||||||
local attachment = readAttachment(attachmentName, attachmentMap, self.scale)
|
local attachment = readAttachment(attachmentName, attachmentMap, self.scale)
|
||||||
skin:addAttachment(slotIndex, attachmentName, attachment)
|
if attachment then
|
||||||
|
skin:addAttachment(slotIndex, attachmentName, attachment)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if skin.name == "default" then
|
if skin.name == "default" then
|
||||||
@ -133,12 +131,9 @@ function SkeletonJson.new (attachmentResolver)
|
|||||||
readAttachment = function (name, map, scale)
|
readAttachment = function (name, map, scale)
|
||||||
name = map["name"] or name
|
name = map["name"] or name
|
||||||
local attachment
|
local attachment
|
||||||
local type = map["type"] or ATTACHMENT_REGION
|
local type = map["type"] or AttachmentLoader.ATTACHMENT_REGION
|
||||||
if type == ATTACHMENT_REGION then
|
attachment = attachmentLoader:newAttachment(type, name)
|
||||||
attachment = RegionAttachment.new(name)
|
if not attachment then return nil end
|
||||||
else
|
|
||||||
error("Unknown attachment type: " .. type .. " (" + name + ")")
|
|
||||||
end
|
|
||||||
|
|
||||||
attachment.x = (map["x"] or 0) * scale
|
attachment.x = (map["x"] or 0) * scale
|
||||||
attachment.y = (map["y"] or 0) * scale
|
attachment.y = (map["y"] or 0) * scale
|
||||||
|
|||||||
@ -35,7 +35,7 @@ spine.RegionAttachment = require "spine.RegionAttachment"
|
|||||||
spine.Skeleton = require "spine.Skeleton"
|
spine.Skeleton = require "spine.Skeleton"
|
||||||
spine.Bone = require "spine.Bone"
|
spine.Bone = require "spine.Bone"
|
||||||
spine.Slot = require "spine.Slot"
|
spine.Slot = require "spine.Slot"
|
||||||
spine.AttachmentResolver = require "spine.AttachmentResolver"
|
spine.AttachmentLoader = require "spine.AttachmentLoader"
|
||||||
spine.Animation = require "spine.Animation"
|
spine.Animation = require "spine.Animation"
|
||||||
|
|
||||||
return spine
|
return spine
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user