diff --git a/spine-corona/main.lua b/spine-corona/main.lua index 763e72da4..89206dc6c 100644 --- a/spine-corona/main.lua +++ b/spine-corona/main.lua @@ -1,13 +1,15 @@ local spine = require "spine.spine" --- Optional attachment resolver customizes where images are loaded. Eg, could use an image sheet. -local attachmentResolver = spine.AttachmentResolver.new() -function attachmentResolver:createImage (attachment) +-- Using your own attachment loader is optional. It can customizes the path where images are +-- loaded. To load from a texture atlas, use an image sheet. It also creates instances of +-- all attachments, which can be used for customization. +local attachmentLoader = spine.AttachmentLoader.new() +function attachmentLoader:createImage (attachment) return display.newImage("data/" .. attachment.name .. ".png") end -local json = spine.SkeletonJson.new(attachmentResolver) +local json = spine.SkeletonJson.new(attachmentLoader) json.scale = 1 local skeletonData = json:readSkeletonDataFile("data/spineboy-skeleton.json") local walkAnimation = json:readAnimationFile(skeletonData, "data/spineboy-walk.json") diff --git a/spine-corona/spine/AttachmentResolver.lua b/spine-corona/spine/AttachmentLoader.lua similarity index 76% rename from spine-corona/spine/AttachmentResolver.lua rename to spine-corona/spine/AttachmentLoader.lua index c298949a4..87cb74a3c 100644 --- a/spine-corona/spine/AttachmentResolver.lua +++ b/spine-corona/spine/AttachmentLoader.lua @@ -23,26 +23,20 @@ -- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ------------------------------------------------------------------------------ -local AttachmentResolver = { - failed = {} -} -function AttachmentResolver.new () - local self = { - images = {} - } +local RegionAttachment = require "spine.RegionAttachment" - function self:resolve (skeleton, attachment) - local image = self:createImage(attachment) - if image then - image:setReferencePoint(display.CenterReferencePoint); - image.width = attachment.width - image.height = attachment.height - else - print("Error creating image: " .. attachment.name) - image = AttachmentResolver.failed +local AttachmentLoader = { + failed = {}, + ATTACHMENT_REGION = "region" +} +function AttachmentLoader.new () + local self = {} + + function self:newAttachment (type, name) + if type == AttachmentLoader.ATTACHMENT_REGION then + return RegionAttachment.new(name) end - skeleton.images[attachment] = image - return image + error("Unknown attachment type: " .. type .. " (" + name + ")") end function self:createImage (attachment) @@ -51,4 +45,4 @@ function AttachmentResolver.new () return self end -return AttachmentResolver +return AttachmentLoader diff --git a/spine-corona/spine/Skeleton.lua b/spine-corona/spine/Skeleton.lua index 13ee28270..5efbd0d93 100644 --- a/spine-corona/spine/Skeleton.lua +++ b/spine-corona/spine/Skeleton.lua @@ -26,7 +26,7 @@ local utils = require "spine.utils" local Bone = require "spine.Bone" local Slot = require "spine.Slot" -local AttachmentResolver = require "spine.AttachmentResolver" +local AttachmentLoader = require "spine.AttachmentLoader" local Skeleton = {} function Skeleton.new (skeletonData, group) @@ -45,15 +45,27 @@ function Skeleton.new (skeletonData, group) end for i,slot in ipairs(self.drawOrder) do - if slot.attachment then - local image = self.images[slot.attachment] - if not image then image = self.data.attachmentResolver:resolve(self, slot.attachment) end - if image ~= AttachmentResolver.failed then - image.x = slot.bone.worldX + slot.attachment.x * slot.bone.m00 + slot.attachment.y * slot.bone.m01 - image.y = -(slot.bone.worldY + slot.attachment.x * slot.bone.m10 + slot.attachment.y * slot.bone.m11) - image.rotation = -(slot.bone.worldRotation + slot.attachment.rotation) - image.xScale = slot.bone.worldScaleX + slot.attachment.scaleX - 1 - image.yScale = slot.bone.worldScaleY + slot.attachment.scaleY - 1 + local attachment = slot.attachment + if attachment then + local image = self.images[attachment] + if not image then + image = self.data.attachmentLoader:createImage(attachment) + if image then + image:setReferencePoint(display.CenterReferencePoint); + image.width = attachment.width + 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 image.xScale = -image.xScale image.rotation = -image.rotation diff --git a/spine-corona/spine/SkeletonData.lua b/spine-corona/spine/SkeletonData.lua index de48a6c31..6b877d208 100644 --- a/spine-corona/spine/SkeletonData.lua +++ b/spine-corona/spine/SkeletonData.lua @@ -24,11 +24,11 @@ ------------------------------------------------------------------------------ local SkeletonData = {} -function SkeletonData.new (attachmentResolver) - if not attachmentResolver then error("attachmentResolver cannot be nil", 2) end +function SkeletonData.new (attachmentLoader) + if not attachmentLoader then error("attachmentLoader cannot be nil", 2) end local self = { - attachmentResolver = attachmentResolver, + attachmentLoader = attachmentLoader, bones = {}, slots = {}, skins = {} diff --git a/spine-corona/spine/SkeletonJson.lua b/spine-corona/spine/SkeletonJson.lua index febc2a511..e2d3d7244 100644 --- a/spine-corona/spine/SkeletonJson.lua +++ b/spine-corona/spine/SkeletonJson.lua @@ -28,8 +28,7 @@ local SkeletonData = require "spine.SkeletonData" local BoneData = require "spine.BoneData" local SlotData = require "spine.SlotData" local Skin = require "spine.Skin" -local RegionAttachment = require "spine.RegionAttachment" -local AttachmentResolver = require "spine.AttachmentResolver" +local AttachmentLoader = require "spine.AttachmentLoader" local Animation = require "spine.Animation" local json = require "json" @@ -39,15 +38,12 @@ local TIMELINE_TRANSLATE = "translate" local TIMELINE_ATTACHMENT = "attachment" local TIMELINE_COLOR = "color" -local ATTACHMENT_REGION = "region" -local ATTACHMENT_ANIMATED_REGION = "animatedRegion" - local SkeletonJson = {} -function SkeletonJson.new (attachmentResolver) - if not attachmentResolver then attachmentResolver = AttachmentResolver.new() end +function SkeletonJson.new (attachmentLoader) + if not attachmentLoader then attachmentLoader = AttachmentLoader.new() end local self = { - attachmentResolver = attachmentResolver, + attachmentLoader = attachmentLoader, scale = 1 } @@ -58,7 +54,7 @@ function SkeletonJson.new (attachmentResolver) local readAttachment function self:readSkeletonData (jsonText) - local skeletonData = SkeletonData.new(self.attachmentResolver) + local skeletonData = SkeletonData.new(self.attachmentLoader) local root = json.decode(jsonText) if not root then error("Invalid JSON: " .. jsonText, 2) end @@ -116,7 +112,9 @@ function SkeletonJson.new (attachmentResolver) local slotIndex = skeletonData:findSlotIndex(slotName) for attachmentName,attachmentMap in pairs(slotMap) do local attachment = readAttachment(attachmentName, attachmentMap, self.scale) - skin:addAttachment(slotIndex, attachmentName, attachment) + if attachment then + skin:addAttachment(slotIndex, attachmentName, attachment) + end end end if skin.name == "default" then @@ -133,12 +131,9 @@ function SkeletonJson.new (attachmentResolver) readAttachment = function (name, map, scale) name = map["name"] or name local attachment - local type = map["type"] or ATTACHMENT_REGION - if type == ATTACHMENT_REGION then - attachment = RegionAttachment.new(name) - else - error("Unknown attachment type: " .. type .. " (" + name + ")") - end + local type = map["type"] or AttachmentLoader.ATTACHMENT_REGION + attachment = attachmentLoader:newAttachment(type, name) + if not attachment then return nil end attachment.x = (map["x"] or 0) * scale attachment.y = (map["y"] or 0) * scale diff --git a/spine-corona/spine/spine.lua b/spine-corona/spine/spine.lua index 722638455..26aad04e9 100644 --- a/spine-corona/spine/spine.lua +++ b/spine-corona/spine/spine.lua @@ -35,7 +35,7 @@ spine.RegionAttachment = require "spine.RegionAttachment" spine.Skeleton = require "spine.Skeleton" spine.Bone = require "spine.Bone" spine.Slot = require "spine.Slot" -spine.AttachmentResolver = require "spine.AttachmentResolver" +spine.AttachmentLoader = require "spine.AttachmentLoader" spine.Animation = require "spine.Animation" return spine