Sfoglia il codice sorgente

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

badlogic 6 anni fa
parent
commit
b5039e5f81

+ 4 - 1
spine-lua/SkeletonJson.lua

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

+ 129 - 2
spine-lua/Skin.lua

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

+ 4 - 0
spine-lua/attachments/Attachment.lua

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

+ 7 - 0
spine-lua/attachments/BoundingBoxAttachment.lua

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

+ 9 - 0
spine-lua/attachments/ClippingAttachment.lua

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

+ 29 - 0
spine-lua/attachments/MeshAttachment.lua

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

+ 14 - 0
spine-lua/attachments/PathAttachment.lua

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

+ 9 - 0
spine-lua/attachments/PointAttachment.lua

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

+ 19 - 0
spine-lua/attachments/RegionAttachment.lua

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

+ 17 - 1
spine-lua/attachments/VertexAttachment.lua

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