mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-20 00:36:43 +08:00
spine-lua: bounding boxes.
This commit is contained in:
parent
a793d2bfa6
commit
ed4cf32726
@ -17,8 +17,11 @@ skeleton.group.y = 325
|
||||
skeleton.flipX = false
|
||||
skeleton.flipY = false
|
||||
skeleton.debug = true -- Omit or set to false to not draw debug lines on top of the images.
|
||||
skeleton.debugAabb = true
|
||||
skeleton:setToSetupPose()
|
||||
|
||||
local bounds = spine.SkeletonBounds.new()
|
||||
|
||||
-- AnimationStateData defines crossfade durations between animations.
|
||||
local stateData = spine.AnimationStateData.new(skeletonData)
|
||||
stateData:setMix("walk", "jump", 0.2)
|
||||
@ -44,16 +47,38 @@ state.onEvent = function (trackIndex, event)
|
||||
end
|
||||
|
||||
local lastTime = 0
|
||||
local animationTime = 0
|
||||
local touchX = 999999
|
||||
local touchY = 999999
|
||||
local headSlot = skeleton:findSlot("head")
|
||||
Runtime:addEventListener("enterFrame", function (event)
|
||||
-- Compute time in seconds since last frame.
|
||||
local currentTime = event.time / 1000
|
||||
local delta = currentTime - lastTime
|
||||
lastTime = currentTime
|
||||
|
||||
-- Bounding box hit detection.
|
||||
bounds:update(skeleton, true)
|
||||
if bounds:containsPoint(touchX, touchY) then
|
||||
headSlot.g = 0;
|
||||
headSlot.b = 0;
|
||||
else
|
||||
headSlot.g = 1;
|
||||
headSlot.b = 1;
|
||||
end
|
||||
|
||||
-- Update the state with the delta time, apply it, and update the world transforms.
|
||||
state:update(delta)
|
||||
state:apply(skeleton)
|
||||
skeleton:updateWorldTransform()
|
||||
end)
|
||||
|
||||
Runtime:addEventListener("touch", function (event)
|
||||
if event.phase ~= "ended" and event.phase ~= "cancelled" then
|
||||
-- Make the coordinates relative to the skeleton's group.
|
||||
touchX = event.x - skeleton.group.x
|
||||
touchY = skeleton.group.y - event.y
|
||||
else
|
||||
touchX = 999999
|
||||
touchY = 999999
|
||||
end
|
||||
end)
|
||||
|
||||
@ -43,12 +43,14 @@ spine.RegionAttachment = require "spine-lua.RegionAttachment"
|
||||
spine.Skeleton = require "spine-lua.Skeleton"
|
||||
spine.Bone = require "spine-lua.Bone"
|
||||
spine.Slot = require "spine-lua.Slot"
|
||||
spine.AttachmentType = require "spine-lua.AttachmentType"
|
||||
spine.AttachmentLoader = require "spine-lua.AttachmentLoader"
|
||||
spine.Animation = require "spine-lua.Animation"
|
||||
spine.AnimationStateData = require "spine-lua.AnimationStateData"
|
||||
spine.AnimationState = require "spine-lua.AnimationState"
|
||||
spine.EventData = require "spine-lua.EventData"
|
||||
spine.Event = require "spine-lua.Event"
|
||||
spine.SkeletonBounds = require "spine-lua.SkeletonBounds"
|
||||
|
||||
spine.utils.readFile = function (fileName, base)
|
||||
if not base then base = system.ResourceDirectory end
|
||||
@ -69,115 +71,123 @@ spine.Skeleton.failed = {} -- Placeholder for an image that failed to load.
|
||||
|
||||
spine.Skeleton.new_super = spine.Skeleton.new
|
||||
function spine.Skeleton.new (skeletonData, group)
|
||||
-- Skeleton extends a group.
|
||||
local self = spine.Skeleton.new_super(skeletonData)
|
||||
self.group = group or display.newGroup()
|
||||
|
||||
self.images = {}
|
||||
-- createImage can customize where images are found.
|
||||
|
||||
-- Customizes where images are found.
|
||||
function self:createImage (attachment)
|
||||
return display.newImage(attachment.name .. ".png")
|
||||
end
|
||||
|
||||
|
||||
-- Customizes what happens when an image changes, return false to recreate the image.
|
||||
function self:modifyImage (attachment)
|
||||
return false
|
||||
end
|
||||
|
||||
-- updateWorldTransform positions images.
|
||||
local updateWorldTransform_super = self.updateWorldTransform
|
||||
function self:updateWorldTransform ()
|
||||
updateWorldTransform_super(self)
|
||||
local images = self.images
|
||||
|
||||
local skeletonR, skeletonG, skeletonB, skeletonA = self.r, self.g, self.b, self.a
|
||||
local skeletonR, skeletonG, skeletonB, skeletonA = self.r * 255, self.g * 255, self.b * 255, self.a
|
||||
for i,slot in ipairs(self.drawOrder) do
|
||||
local attachment = slot.attachment
|
||||
local image = images[slot]
|
||||
if not attachment then -- Attachment is gone, remove the image.
|
||||
if image then
|
||||
image:removeSelf()
|
||||
images[slot] = nil
|
||||
end
|
||||
else
|
||||
if image and image.attachment ~= attachment then -- Attachment image has changed.
|
||||
if self.modifyImage then
|
||||
self:modifyImage(image, attachment)
|
||||
image.lastR, image.lastG, image.lastB, image.lastA = nil, nil, nil, nil
|
||||
image.attachment = attachment
|
||||
else -- If no modifier supplied just remove the image and it will be recreated.
|
||||
if attachment.type == spine.AttachmentType.region then
|
||||
local image = images[slot]
|
||||
if not attachment then -- Attachment is gone, remove the image.
|
||||
if image then
|
||||
image:removeSelf()
|
||||
images[slot] = nil
|
||||
image = nil
|
||||
end
|
||||
end
|
||||
if not image then -- Create new image.
|
||||
image = self:createImage(attachment)
|
||||
if image then
|
||||
image.attachment = attachment
|
||||
image:setReferencePoint(display.CenterReferencePoint)
|
||||
image.width = attachment.width
|
||||
image.height = attachment.height
|
||||
else
|
||||
print("Error creating image: " .. attachment.name)
|
||||
image = spine.Skeleton.failed
|
||||
end
|
||||
if slot.data.additiveBlending then image.blendMode = "add" end
|
||||
images[slot] = image
|
||||
end
|
||||
-- Position image based on attachment and bone.
|
||||
if image ~= spine.Skeleton.failed then
|
||||
local flipX, flipY = ((self.flipX and -1) or 1), ((self.flipY and -1) or 1)
|
||||
|
||||
local x = slot.bone.worldX + attachment.x * slot.bone.m00 + attachment.y * slot.bone.m01
|
||||
local y = -(slot.bone.worldY + attachment.x * slot.bone.m10 + attachment.y * slot.bone.m11)
|
||||
if not image.lastX then
|
||||
image.x, image.y = x, y
|
||||
image.lastX, image.lastY = x, y
|
||||
elseif image.lastX ~= x or image.lastY ~= y then
|
||||
image:translate(x - image.lastX, y - image.lastY)
|
||||
image.lastX, image.lastY = x, y
|
||||
end
|
||||
|
||||
local xScale = attachment.scaleX * flipX
|
||||
local yScale = attachment.scaleY * flipY
|
||||
-- Fix scaling when attachment is rotated 90 or -90.
|
||||
local rotation = math.abs(attachment.rotation) % 180
|
||||
if (rotation == 90) then
|
||||
xScale = xScale * slot.bone.worldScaleY
|
||||
yScale = yScale * slot.bone.worldScaleX
|
||||
else
|
||||
xScale = xScale * slot.bone.worldScaleX
|
||||
yScale = yScale * slot.bone.worldScaleY
|
||||
if rotation ~= 0 and xScale ~= yScale and not image.rotationWarning then
|
||||
image.rotationWarning = true
|
||||
print("WARNING: Non-uniform bone scaling with attachments not rotated to\n"
|
||||
.." cardinal angles will not work as expected with Corona.\n"
|
||||
.." Bone: "..slot.bone.data.name..", slot: "..slot.data.name..", attachment: "..attachment.name)
|
||||
else
|
||||
if image and image.attachment ~= attachment then -- Attachment image has changed.
|
||||
if self:modifyImage(image, attachment) then
|
||||
image.lastR, image.lastA = nil, nil
|
||||
image.attachment = attachment
|
||||
else -- If not modified, remove the image and it will be recreated.
|
||||
image:removeSelf()
|
||||
images[slot] = nil
|
||||
image = nil
|
||||
end
|
||||
end
|
||||
if not image.lastScaleX then
|
||||
image.xScale, image.yScale = xScale, yScale
|
||||
image.lastScaleX, image.lastScaleY = xScale, yScale
|
||||
elseif image.lastScaleX ~= xScale or image.lastScaleY ~= yScale then
|
||||
image:scale(xScale / image.lastScaleX, yScale / image.lastScaleY)
|
||||
image.lastScaleX, image.lastScaleY = xScale, yScale
|
||||
if not image then -- Create new image.
|
||||
image = self:createImage(attachment)
|
||||
if image then
|
||||
image.attachment = attachment
|
||||
image:setReferencePoint(display.CenterReferencePoint)
|
||||
image.width = attachment.width
|
||||
image.height = attachment.height
|
||||
else
|
||||
print("Error creating image: " .. attachment.name)
|
||||
image = spine.Skeleton.failed
|
||||
end
|
||||
if slot.data.additiveBlending then image.blendMode = "add" end
|
||||
images[slot] = image
|
||||
end
|
||||
-- Position image based on attachment and bone.
|
||||
if image ~= spine.Skeleton.failed then
|
||||
local flipX, flipY = ((self.flipX and -1) or 1), ((self.flipY and -1) or 1)
|
||||
|
||||
rotation = -(slot.bone.worldRotation + attachment.rotation) * flipX * flipY
|
||||
if not image.lastRotation then
|
||||
image.rotation = rotation
|
||||
image.lastRotation = rotation
|
||||
elseif rotation ~= image.lastRotation then
|
||||
image:rotate(rotation - image.lastRotation)
|
||||
image.lastRotation = rotation
|
||||
end
|
||||
local x = slot.bone.worldX + attachment.x * slot.bone.m00 + attachment.y * slot.bone.m01
|
||||
local y = -(slot.bone.worldY + attachment.x * slot.bone.m10 + attachment.y * slot.bone.m11)
|
||||
if not image.lastX then
|
||||
image.x, image.y = x, y
|
||||
image.lastX, image.lastY = x, y
|
||||
elseif image.lastX ~= x or image.lastY ~= y then
|
||||
image:translate(x - image.lastX, y - image.lastY)
|
||||
image.lastX, image.lastY = x, y
|
||||
end
|
||||
|
||||
local r, g, b, a = skeletonR * slot.r, skeletonG * slot.g, skeletonB * slot.b, skeletonA * slot.a
|
||||
if image.lastR ~= r or image.lastG ~= g or image.lastB ~= b or not image.lastR then
|
||||
image:setFillColor(r, g, b)
|
||||
image.lastR, image.lastG, image.lastB = r, g, b
|
||||
local xScale = attachment.scaleX * flipX
|
||||
local yScale = attachment.scaleY * flipY
|
||||
-- Fix scaling when attachment is rotated 90 or -90.
|
||||
local rotation = math.abs(attachment.rotation) % 180
|
||||
if (rotation == 90) then
|
||||
xScale = xScale * slot.bone.worldScaleY
|
||||
yScale = yScale * slot.bone.worldScaleX
|
||||
else
|
||||
xScale = xScale * slot.bone.worldScaleX
|
||||
yScale = yScale * slot.bone.worldScaleY
|
||||
if rotation ~= 0 and xScale ~= yScale and not image.rotationWarning then
|
||||
image.rotationWarning = true
|
||||
print("WARNING: Non-uniform bone scaling with attachments not rotated to\n"
|
||||
.." cardinal angles will not work as expected with Corona.\n"
|
||||
.." Bone: "..slot.bone.data.name..", slot: "..slot.data.name..", attachment: "..attachment.name)
|
||||
end
|
||||
end
|
||||
if not image.lastScaleX then
|
||||
image.xScale, image.yScale = xScale, yScale
|
||||
image.lastScaleX, image.lastScaleY = xScale, yScale
|
||||
elseif image.lastScaleX ~= xScale or image.lastScaleY ~= yScale then
|
||||
image:scale(xScale / image.lastScaleX, yScale / image.lastScaleY)
|
||||
image.lastScaleX, image.lastScaleY = xScale, yScale
|
||||
end
|
||||
|
||||
rotation = -(slot.bone.worldRotation + attachment.rotation) * flipX * flipY
|
||||
if not image.lastRotation then
|
||||
image.rotation = rotation
|
||||
image.lastRotation = rotation
|
||||
elseif rotation ~= image.lastRotation then
|
||||
image:rotate(rotation - image.lastRotation)
|
||||
image.lastRotation = rotation
|
||||
end
|
||||
|
||||
local r, g, b = skeletonR * slot.r, skeletonG * slot.g, skeletonB * slot.b
|
||||
if image.lastR ~= r or image.lastG ~= g or image.lastB ~= b or not image.lastR then
|
||||
image:setFillColor(r, g, b)
|
||||
image.lastR, image.lastG, image.lastB = r, g, b
|
||||
end
|
||||
local a = skeletonA * slot.a
|
||||
if a and (image.lastA ~= a or not image.lastA) then
|
||||
image.lastA = a
|
||||
image.alpha = image.lastA
|
||||
end
|
||||
|
||||
self.group:insert(image)
|
||||
end
|
||||
if a and (image.lastA ~= a or not image.lastA) then
|
||||
image.lastA = a / 255
|
||||
image.alpha = image.lastA
|
||||
end
|
||||
|
||||
self.group:insert(image)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -214,6 +224,24 @@ function spine.Skeleton.new (skeletonData, group)
|
||||
self.group:insert(bone.circle)
|
||||
end
|
||||
end
|
||||
|
||||
if self.debugAabb then
|
||||
if not self.bounds then
|
||||
self.bounds = spine.SkeletonBounds.new()
|
||||
self.boundsRect = display.newRect(self.group, 0, 0, 0, 0)
|
||||
self.boundsRect:setFillColor(0, 0, 0, 0)
|
||||
self.boundsRect.strokeWidth = 1
|
||||
self.boundsRect:setStrokeColor(0, 255, 0, 255)
|
||||
end
|
||||
self.bounds:update(self, true)
|
||||
local width = self.bounds:getWidth()
|
||||
local height = self.bounds:getHeight()
|
||||
self.boundsRect.x = self.bounds.minX + width / 2
|
||||
self.boundsRect.y = -self.bounds.minY - height / 2
|
||||
self.boundsRect.width = width
|
||||
self.boundsRect.height = height
|
||||
self.group:insert(self.boundsRect)
|
||||
end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
@ -31,22 +31,22 @@
|
||||
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local AttachmentType = require "spine-lua.AttachmentType"
|
||||
local RegionAttachment = require "spine-lua.RegionAttachment"
|
||||
local BoundingBoxAttachment = require "spine-lua.BoundingBoxAttachment"
|
||||
|
||||
local AttachmentLoader = {
|
||||
failed = {},
|
||||
ATTACHMENT_REGION = "region",
|
||||
ATTACHMENT_BOUNDINGBOX = "boundingbox",
|
||||
failed = {}
|
||||
}
|
||||
function AttachmentLoader.new ()
|
||||
local self = {}
|
||||
|
||||
function self:newAttachment (type, name)
|
||||
if type == AttachmentLoader.ATTACHMENT_REGION then
|
||||
if type == AttachmentType.region then
|
||||
return RegionAttachment.new(name)
|
||||
end
|
||||
if type == AttachmentLoader.ATTACHMENT_BOUNDINGBOX then
|
||||
return nil -- BOZO - Implement bounding boxes.
|
||||
if type == AttachmentType.boundingbox then
|
||||
return BoundingBoxAttachment.new(name)
|
||||
end
|
||||
error("Unknown attachment type: " .. type .. " (" .. name .. ")")
|
||||
end
|
||||
|
||||
38
spine-lua/AttachmentType.lua
Normal file
38
spine-lua/AttachmentType.lua
Normal file
@ -0,0 +1,38 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Spine Runtime Software License - Version 1.0
|
||||
--
|
||||
-- Copyright (c) 2013, Esoteric Software
|
||||
-- All rights reserved.
|
||||
--
|
||||
-- Redistribution and use in source and binary forms in whole or in part, with
|
||||
-- or without modification, are permitted provided that the following conditions
|
||||
-- are met:
|
||||
--
|
||||
-- 1. A Spine Essential, Professional, Enterprise, or Education License must
|
||||
-- be purchased from Esoteric Software and the license must remain valid:
|
||||
-- http://esotericsoftware.com/
|
||||
-- 2. Redistributions of source code must retain this license, which is the
|
||||
-- above copyright notice, this declaration of conditions and the following
|
||||
-- disclaimer.
|
||||
-- 3. Redistributions in binary form must reproduce this license, which is the
|
||||
-- above copyright notice, this declaration of conditions and the following
|
||||
-- disclaimer, in the documentation and/or other materials provided with the
|
||||
-- distribution.
|
||||
--
|
||||
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
-- ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local AttachmentType = {
|
||||
region = 0,
|
||||
boundingbox = 1
|
||||
}
|
||||
return AttachmentType
|
||||
@ -41,7 +41,11 @@ function Bone.new (data, parent)
|
||||
parent = parent,
|
||||
x = 0, y = 0,
|
||||
rotation = 0,
|
||||
scaleX = 1, scaleY = 1
|
||||
scaleX = 1, scaleY = 1,
|
||||
m00 = 0, m01 = 0, worldX = 0, -- a b x
|
||||
m10 = 0, m11 = 0, worldY = 0, -- c d y
|
||||
worldRotation = 0,
|
||||
worldScaleX = 1, worldScaleY = 1,
|
||||
}
|
||||
|
||||
function self:updateWorldTransform (flipX, flipY)
|
||||
|
||||
@ -37,7 +37,13 @@ function BoneData.new (name, parent)
|
||||
|
||||
local self = {
|
||||
name = name,
|
||||
parent = parent
|
||||
parent = parent,
|
||||
length = 0,
|
||||
x = 0, y = 0,
|
||||
rotation = 0,
|
||||
scaleX = 1, scaleY = 1,
|
||||
inheritScale = true,
|
||||
inheritRotation = true
|
||||
}
|
||||
|
||||
return self
|
||||
|
||||
65
spine-lua/BoundingBoxAttachment.lua
Normal file
65
spine-lua/BoundingBoxAttachment.lua
Normal file
@ -0,0 +1,65 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Spine Runtime Software License - Version 1.0
|
||||
--
|
||||
-- Copyright (c) 2013, Esoteric Software
|
||||
-- All rights reserved.
|
||||
--
|
||||
-- Redistribution and use in source and binary forms in whole or in part, with
|
||||
-- or without modification, are permitted provided that the following conditions
|
||||
-- are met:
|
||||
--
|
||||
-- 1. A Spine Essential, Professional, Enterprise, or Education License must
|
||||
-- be purchased from Esoteric Software and the license must remain valid:
|
||||
-- http://esotericsoftware.com/
|
||||
-- 2. Redistributions of source code must retain this license, which is the
|
||||
-- above copyright notice, this declaration of conditions and the following
|
||||
-- disclaimer.
|
||||
-- 3. Redistributions in binary form must reproduce this license, which is the
|
||||
-- above copyright notice, this declaration of conditions and the following
|
||||
-- disclaimer, in the documentation and/or other materials provided with the
|
||||
-- distribution.
|
||||
--
|
||||
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
-- ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local AttachmentType = require "spine-lua.AttachmentType"
|
||||
|
||||
local BoundingBoxAttachment = {}
|
||||
function BoundingBoxAttachment.new (name)
|
||||
if not name then error("name cannot be nil", 2) end
|
||||
|
||||
local self = {
|
||||
name = name,
|
||||
type = AttachmentType.boundingbox,
|
||||
vertices = {}
|
||||
}
|
||||
|
||||
function self:computeWorldVertices (x, y, bone, worldVertices)
|
||||
x = x + bone.worldX
|
||||
y = y + bone.worldY
|
||||
local m00 = bone.m00
|
||||
local m01 = bone.m01
|
||||
local m10 = bone.m10
|
||||
local m11 = bone.m11
|
||||
local vertices = self.vertices
|
||||
local count = #vertices
|
||||
for i = 1, count, 2 do
|
||||
local px = vertices[i]
|
||||
local py = vertices[i + 1]
|
||||
worldVertices[i] = px * m00 + py * m01 + x
|
||||
worldVertices[i + 1] = px * m10 + py * m11 + y
|
||||
end
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
return BoundingBoxAttachment
|
||||
@ -31,12 +31,15 @@
|
||||
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local AttachmentType = require "spine-lua.AttachmentType"
|
||||
|
||||
local RegionAttachment = {}
|
||||
function RegionAttachment.new (name)
|
||||
if not name then error("name cannot be nil", 2) end
|
||||
|
||||
local self = {
|
||||
name = name
|
||||
name = name,
|
||||
type = AttachmentType.region
|
||||
}
|
||||
|
||||
return self
|
||||
|
||||
@ -45,7 +45,8 @@ function Skeleton.new (skeletonData)
|
||||
slots = {},
|
||||
slotsByName = {},
|
||||
drawOrder = {},
|
||||
r = 1, g = 1, b = 1, a = 1
|
||||
r = 1, g = 1, b = 1, a = 1,
|
||||
x = 0, y = 0
|
||||
}
|
||||
|
||||
function self:updateWorldTransform ()
|
||||
|
||||
185
spine-lua/SkeletonBounds.lua
Normal file
185
spine-lua/SkeletonBounds.lua
Normal file
@ -0,0 +1,185 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Spine Runtime Software License - Version 1.0
|
||||
--
|
||||
-- Copyright (c) 2013, Esoteric Software
|
||||
-- All rights reserved.
|
||||
--
|
||||
-- Redistribution and use in source and binary forms in whole or in part, with
|
||||
-- or without modification, are permitted provided that the following conditions
|
||||
-- are met:
|
||||
--
|
||||
-- 1. A Spine Essential, Professional, Enterprise, or Education License must
|
||||
-- be purchased from Esoteric Software and the license must remain valid:
|
||||
-- http://esotericsoftware.com/
|
||||
-- 2. Redistributions of source code must retain self license, which is the
|
||||
-- above copyright notice, self declaration of conditions and the following
|
||||
-- disclaimer.
|
||||
-- 3. Redistributions in binary form must reproduce self license, which is the
|
||||
-- above copyright notice, self declaration of conditions and the following
|
||||
-- disclaimer, in the documentation and/or other materials provided with the
|
||||
-- distribution.
|
||||
--
|
||||
-- self SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
-- ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF self
|
||||
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local AttachmentType = require "spine-lua.AttachmentType"
|
||||
local utils = require "spine-lua.utils"
|
||||
|
||||
local SkeletonBounds = {}
|
||||
function SkeletonBounds.new ()
|
||||
local self = {
|
||||
polygons = {},
|
||||
boundingBoxes = {},
|
||||
minX = 0, minY = 0, maxX = 0, maxY = 0
|
||||
}
|
||||
|
||||
function aabbCompute ()
|
||||
local polygons = self.polygons
|
||||
local minX, minY, maxX, maxY = 9999999, 9999999, -9999999, -9999999
|
||||
for i,vertices in ipairs(polygons) do
|
||||
local count = #vertices
|
||||
for ii = 1, count, 2 do
|
||||
local x = vertices[ii]
|
||||
local y = vertices[ii + 1]
|
||||
minX = math.min(minX, x)
|
||||
minY = math.min(minY, y)
|
||||
maxX = math.max(maxX, x)
|
||||
maxY = math.max(maxY, y)
|
||||
end
|
||||
end
|
||||
self.minX = minX
|
||||
self.minY = minY
|
||||
self.maxX = maxX
|
||||
self.maxY = maxY
|
||||
end
|
||||
|
||||
function self:update (skeleton, updateAabb)
|
||||
local x = skeleton.x
|
||||
local y = skeleton.y
|
||||
|
||||
local polygons = {}
|
||||
self.polygons = polygons
|
||||
|
||||
local boundingBoxes = {}
|
||||
self.boundingBoxes = boundingBoxes
|
||||
|
||||
for i,slot in ipairs(skeleton.slots) do
|
||||
local boundingBox = slot.attachment
|
||||
if boundingBox.type == AttachmentType.boundingbox then
|
||||
table.insert(boundingBoxes, boundingBox)
|
||||
|
||||
local polygon = {}
|
||||
table.insert(polygons, polygon)
|
||||
|
||||
boundingBox:computeWorldVertices(x, y, slot.bone, polygon)
|
||||
end
|
||||
end
|
||||
|
||||
if updateAabb then aabbCompute() end
|
||||
end
|
||||
|
||||
function self:aabbContainsPoint (x, y)
|
||||
return x >= self.minX and x <= self.maxX and y >= self.minY and y <= self.maxY
|
||||
end
|
||||
|
||||
function self:aabbIntersectsSegment (x1, y1, x2, y2)
|
||||
local minX, minY, maxX, maxY = self.minX, self.minY, self.maxX, self.maxY
|
||||
if (x1 <= minX and x2 <= minX) or (y1 <= minY and y2 <= minY) or (x1 >= maxX and x2 >= maxX) or (y1 >= maxY and y2 >= maxY) then
|
||||
return false
|
||||
end
|
||||
local m = (y2 - y1) / (x2 - x1)
|
||||
local y = m * (minX - x1) + y1
|
||||
if y > minY and y < maxY then return true end
|
||||
y = m * (maxX - x1) + y1
|
||||
if y > minY and y < maxY then return true end
|
||||
local x = (minY - y1) / m + x1
|
||||
if x > minX and x < maxX then return true end
|
||||
x = (maxY - y1) / m + x1
|
||||
if x > minX and x < maxX then return true end
|
||||
return false
|
||||
end
|
||||
|
||||
function self:aabbIntersectsSkeleton (bounds)
|
||||
return self.minX < bounds.maxX and self.maxX > bounds.minX and self.minY < bounds.maxY and self.maxY > bounds.minY
|
||||
end
|
||||
|
||||
function self:containsPoint (x, y)
|
||||
for i,polygon in ipairs(self.polygons) do
|
||||
if self:polygonContainsPoint(polygon, x, y) then return self.boundingBoxes[i] end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function self:intersectsSegment (x1, y1, x2, y2)
|
||||
for i,polygon in ipairs(self.polygons) do
|
||||
if self:polygonIntersectsSegment(polygon, x1, y1, x2, y2) then return boundingBoxes[i] end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function self:polygonContainsPoint (polygon, x, y)
|
||||
local nn = #polygon
|
||||
local prevIndex = nn - 1
|
||||
local inside = false
|
||||
for ii = 1, nn, 2 do
|
||||
local vertexY = polygon[ii + 1]
|
||||
local prevY = polygon[prevIndex + 1]
|
||||
if (vertexY < y and prevY >= y) or (prevY < y and vertexY >= y) then
|
||||
local vertexX = polygon[ii]
|
||||
if vertexX + (y - vertexY) / (prevY - vertexY) * (polygon[prevIndex] - vertexX) < x then inside = not inside end
|
||||
end
|
||||
prevIndex = ii
|
||||
end
|
||||
return inside
|
||||
end
|
||||
|
||||
function self:polygonIntersectsSegment (polygon, x1, y1, x2, y2)
|
||||
local nn = polygon.length
|
||||
local width12, height12 = x1 - x2, y1 - y2
|
||||
local det1 = x1 * y2 - y1 * x2
|
||||
local x3, y3 = polygon[nn - 2], polygon[nn - 1]
|
||||
for ii = 1, nn, 2 do
|
||||
local x4, y4 = polygon[ii], polygon[ii + 1]
|
||||
local det2 = x3 * y4 - y3 * x4
|
||||
local width34, height34 = x3 - x4, y3 - y4
|
||||
local det3 = width12 * height34 - height12 * width34
|
||||
local x = (det1 * width34 - width12 * det2) / det3
|
||||
if ((x >= x3 and x <= x4) or (x >= x4 and x <= x3)) and ((x >= x1 and x <= x2) or (x >= x2 and x <= x1)) then
|
||||
local y = (det1 * height34 - height12 * det2) / det3
|
||||
if ((y >= y3 and y <= y4) or (y >= y4 and y <= y3)) and ((y >= y1 and y <= y2) or (y >= y2 and y <= y1)) then return true end
|
||||
end
|
||||
x3 = x4
|
||||
y3 = y4
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function self:getPolygon (attachment)
|
||||
local index = spine.utils.indexOf(self.boundingBoxes, attachment)
|
||||
if index == -1 then
|
||||
return nil
|
||||
else
|
||||
return self.polygons[index]
|
||||
end
|
||||
end
|
||||
|
||||
function self:getWidth ()
|
||||
return self.maxX - self.minX
|
||||
end
|
||||
|
||||
function self:getHeight ()
|
||||
return self.maxY - self.minY
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
return SkeletonBounds
|
||||
@ -39,6 +39,7 @@ local AttachmentLoader = require "spine-lua.AttachmentLoader"
|
||||
local Animation = require "spine-lua.Animation"
|
||||
local EventData = require "spine-lua.EventData"
|
||||
local Event = require "spine-lua.Event"
|
||||
local AttachmentType = require "spine-lua.AttachmentType"
|
||||
|
||||
local TIMELINE_SCALE = "scale"
|
||||
local TIMELINE_ROTATE = "rotate"
|
||||
@ -170,17 +171,25 @@ function SkeletonJson.new (attachmentLoader)
|
||||
readAttachment = function (name, map, scale)
|
||||
name = map["name"] or name
|
||||
local attachment
|
||||
local type = map["type"] or AttachmentLoader.ATTACHMENT_REGION
|
||||
local type = AttachmentType[map["type"] or "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
|
||||
attachment.scaleX = (map["scaleX"] or 1)
|
||||
attachment.scaleY = (map["scaleY"] or 1)
|
||||
attachment.rotation = (map["rotation"] or 0)
|
||||
attachment.width = map["width"] * scale
|
||||
attachment.height = map["height"] * scale
|
||||
if type == AttachmentType.region then
|
||||
attachment.x = (map["x"] or 0) * scale
|
||||
attachment.y = (map["y"] or 0) * scale
|
||||
attachment.scaleX = (map["scaleX"] or 1)
|
||||
attachment.scaleY = (map["scaleY"] or 1)
|
||||
attachment.rotation = (map["rotation"] or 0)
|
||||
attachment.width = map["width"] * scale
|
||||
attachment.height = map["height"] * scale
|
||||
elseif type == AttachmentType.boundingbox then
|
||||
local vertices = map["vertices"]
|
||||
for i,point in ipairs(vertices) do
|
||||
table.insert(attachment.vertices, vertices[i] * scale)
|
||||
end
|
||||
end
|
||||
|
||||
return attachment
|
||||
end
|
||||
|
||||
|
||||
@ -40,7 +40,8 @@ function Slot.new (slotData, skeleton, bone)
|
||||
local self = {
|
||||
data = slotData,
|
||||
skeleton = skeleton,
|
||||
bone = bone
|
||||
bone = bone,
|
||||
r = 1, g = 1, b = 1, a = 1
|
||||
}
|
||||
|
||||
function self:setColor (r, g, b, a)
|
||||
|
||||
@ -38,7 +38,8 @@ function SlotData.new (name, boneData)
|
||||
|
||||
local self = {
|
||||
name = name,
|
||||
boneData = boneData
|
||||
boneData = boneData,
|
||||
r = 1, g = 1, b = 1, a = 1
|
||||
}
|
||||
|
||||
function self:setColor (r, g, b, a)
|
||||
@ -48,8 +49,6 @@ function SlotData.new (name, boneData)
|
||||
self.a = a
|
||||
end
|
||||
|
||||
self:setColor(255, 255, 255, 255)
|
||||
|
||||
return self;
|
||||
end
|
||||
return SlotData
|
||||
|
||||
@ -35,7 +35,6 @@ local utils = {}
|
||||
|
||||
function tablePrint (tt, indent, done)
|
||||
done = done or {}
|
||||
indent = indent or 0
|
||||
for key, value in pairs(tt) do
|
||||
local spaces = string.rep (" ", indent)
|
||||
if type(value) == "table" and not done [value] then
|
||||
@ -51,12 +50,14 @@ function tablePrint (tt, indent, done)
|
||||
end
|
||||
|
||||
function utils.print (value, indent, done)
|
||||
indent = indent or 0
|
||||
if "nil" == type(value) then
|
||||
print(tostring(nil))
|
||||
elseif "table" == type(value) then
|
||||
print("{")
|
||||
tablePrint(value, 2)
|
||||
print("}")
|
||||
local spaces = string.rep (" ", indent)
|
||||
print(spaces .. "{")
|
||||
tablePrint(value, indent + 2)
|
||||
print(spaces .. "}")
|
||||
elseif "string" == type(value) then
|
||||
print("\"" .. value .. "\"")
|
||||
else
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user