Selaa lähdekoodia

Refactored spine-corona to use an attachment loader like the other runtimes. This is more flexible than the old attachment resolver.

NathanSweet 12 vuotta sitten
vanhempi
commit
0c2e2b1d3e

+ 6 - 4
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")

+ 12 - 18
spine-corona/spine/AttachmentResolver.lua → spine-corona/spine/AttachmentLoader.lua

@@ -23,26 +23,20 @@
  -- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  ------------------------------------------------------------------------------
 
-local AttachmentResolver = {
-	failed = {}
+local RegionAttachment = require "spine.RegionAttachment"
+
+local AttachmentLoader = {
+	failed = {},
+	ATTACHMENT_REGION = "region"
 }
-function AttachmentResolver.new ()
-	local self = {
-		images = {}
-	}
+function AttachmentLoader.new ()
+	local self = {}
 
-	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
+	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

+ 22 - 10
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

+ 3 - 3
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 = {}

+ 11 - 16
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

+ 1 - 1
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