[lua] Timelines 4.0 port.

This commit is contained in:
Nathan Sweet 2021-06-04 22:15:32 -04:00
parent a8b79a66e1
commit 3b650f8db1
16 changed files with 2121 additions and 1669 deletions

File diff suppressed because it is too large Load Diff

View File

@ -313,9 +313,9 @@ function AnimationState:updateMixingFrom (to, delta)
from.trackLast = from.nextTrackLast
-- Require mixTime > 0 to ensure the mixing from entry was applied at least once.
if (to.mixTime > 0 and to.mixTime >= to.mixDuration) then
if to.mixTime > 0 and to.mixTime >= to.mixDuration then
-- Require totalAlpha == 0 to ensure mixing is complete, unless mixDuration == 0 (the transition is a single frame).
if (from.totalAlpha == 0 or to.mixDuration == 0) then
if from.totalAlpha == 0 or to.mixDuration == 0 then
to.mixingFrom = from.mixingFrom
if from.mixingFrom then from.mixingFrom.mixingTo = to end
to.interruptAlpha = from.interruptAlpha
@ -365,7 +365,7 @@ function AnimationState:apply (skeleton)
if timeline.type == Animation.TimelineType.attachment then
self:applyAttachmentTimeline(timeline, skeleton, animationTime, blend, true)
else
timeline:apply(skeleton, animationLast, animationTime, self.events, mix, blend, MixDirection._in)
timeline:apply(skeleton, animationLast, animationTime, self.events, mix, blend, MixDirection.mixIn)
end
end
else
@ -383,7 +383,7 @@ function AnimationState:apply (skeleton)
elseif timeline.type == Animation.TimelineType.attachment then
self:applyAttachmentTimeline(timeline, skeleton, animationTime, timelineBlend, true)
else
timeline:apply(skeleton, animationLast, animationTime, self.events, mix, timelineBlend, MixDirection._in)
timeline:apply(skeleton, animationLast, animationTime, self.events, mix, timelineBlend, MixDirection.mixIn)
end
end
end
@ -444,7 +444,7 @@ function AnimationState:applyMixingFrom (to, skeleton, blend)
if blend == MixBlend.add then
for i,timeline in ipairs(timelines) do
timeline:apply(skeleton, animationLast, animationTime, events, alphaMix, blend, MixDirection.out)
timeline:apply(skeleton, animationLast, animationTime, events, alphaMix, blend, MixDirection.mixOut)
end
else
local timelineMode = from.timelineMode
@ -457,7 +457,7 @@ function AnimationState:applyMixingFrom (to, skeleton, blend)
for i,timeline in ipairs(timelines) do
local skipSubsequent = false
local direction = MixDirection.out
local direction = MixDirection.mixOut
local timelineBlend = MixBlend.setup
local alpha = 0
if timelineMode[i] == SUBSEQUENT then
@ -486,8 +486,8 @@ function AnimationState:applyMixingFrom (to, skeleton, blend)
elseif timeline.type == Animation.TimelineType.attachment then
self:applyAttachmentTimeline(timeline, skeleton, animationTime, timelineBlend, attachments)
else
if (drawOrder and timeline.type == Animation.TimelineType.drawOrder and timelineBlend == MixBlend.setup) then
direction = MixDirection._in
if drawOrder and timeline.type == Animation.TimelineType.drawOrder and timelineBlend == MixBlend.setup then
direction = MixDirection.mixIn
end
timeline:apply(skeleton, animationLast, animationTime, self.events, alpha, timelineBlend, direction)
end
@ -495,7 +495,7 @@ function AnimationState:applyMixingFrom (to, skeleton, blend)
end
end
if (to.mixDuration > 0) then
if to.mixDuration > 0 then
self:queueEvents(from, animationTime)
end
self.events = {}
@ -516,7 +516,7 @@ function AnimationState:applyAttachmentTimeline(timeline, skeleton, time, blend,
end
else
local frameIndex = 0
if (time >= frames[zlen(frames) - 1]) then -- Time is after last frame.
if time >= frames[zlen(frames) - 1] then -- Time is after last frame.
frameIndex = zlen(frames) - 1
else
frameIndex = Animation.binarySearch(frames, time, 1) - 1
@ -529,7 +529,7 @@ function AnimationState:applyAttachmentTimeline(timeline, skeleton, time, blend,
end
function AnimationState:setAttachment(skeleton, slot, attachmentName, attachments)
if (attachmentName == nil) then
if attachmentName == nil then
slot.attachment = nil
else
slot.attachment = skeleton:getAttachmentByIndex(slot.data.index, attachmentName)
@ -544,7 +544,7 @@ function AnimationState:applyRotateTimeline (timeline, skeleton, time, alpha, bl
end
if alpha == 1 then
timeline:apply(skeleton, 0, time, nil, 1, blend, MixDirection._in)
timeline:apply(skeleton, 0, time, nil, 1, blend, MixDirection.mixIn)
return
end
@ -730,7 +730,7 @@ function AnimationState:setAnimationByName (trackIndex, animationName, loop)
end
function AnimationState:setAnimation (trackIndex, animation, loop)
if not animation then error("animation cannot be null.") end
if not animation then error("animation cannot be null.", 2) end
local interrupt = true
local current = self:expandToIndex(trackIndex)
local queue = self.queue
@ -756,12 +756,12 @@ end
function AnimationState:addAnimationByName (trackIndex, animationName, loop, delay)
local animation = self.data.skeletonData:findAnimation(animationName)
if not animation then error("Animation not found: " + animationName) end
if not animation then error("Animation not found: " + animationName, 2) end
return self:addAnimation(trackIndex, animation, loop, delay)
end
function AnimationState:addAnimation (trackIndex, animation, loop, delay)
if not animation then error("animation cannot be null.") end
if not animation then error("animation cannot be null.", 2) end
local last = self:expandToIndex(trackIndex)
if last then
@ -911,7 +911,7 @@ function AnimationState:_animationsChanged ()
end
repeat
if (entry.mixingTo == nil or entry.mixBlend ~= MixBlend.add) then
if entry.mixingTo == nil or entry.mixBlend ~= MixBlend.add then
self:computeHold(entry)
end
entry = entry.mixingTo
@ -930,7 +930,7 @@ function AnimationState:computeHold(entry)
local timelineHoldMix = entry.timelineHoldMix
local propertyIDs = self.propertyIDs
if (to and to.holdPrevious) then
if to and to.holdPrevious then
local i = 1
while i <= timelinesCount do
local id = "" .. timelines[i]:getPropertyId()

View File

@ -43,13 +43,13 @@ function Atlas.parse(atlasPath, atlasBase)
end
if not atlasPath then
error("Error: " .. atlasPath .. ".atlas" .. " doesn't exist!")
error("Error: " .. atlasPath .. ".atlas" .. " doesn't exist!", 2)
return nil
end
local atlasLines = spine.utils.readFile( atlasPath, atlasBase )
if not atlasLines then
error("Error: " .. atlasPath .. ".atlas" .. " unable to read!")
error("Error: " .. atlasPath .. ".atlas" .. " unable to read!", 2)
return nil
end

View File

@ -51,7 +51,7 @@ end
function AtlasAttachmentLoader:newRegionAttachment (skin, name, path)
local region = self.atlas:findRegion(path)
if not region then error("Region not found in atlas: " .. path .. " (region attachment: " .. name .. ")") end
if not region then error("Region not found in atlas: " .. path .. " (region attachment: " .. name .. ")", 2) end
region.renderObject = region
local attachment = RegionAttachment.new(name)
attachment:setRegion(region)
@ -61,7 +61,7 @@ end
function AtlasAttachmentLoader:newMeshAttachment (skin, name, path)
local region = self.atlas:findRegion(path)
if not region then error("Region not found in atlas: " .. path .. " (mesh attachment: " .. name .. ")") end
if not region then error("Region not found in atlas: " .. path .. " (mesh attachment: " .. name .. ")", 2) end
region.renderObject = region
local attachment = MeshAttachment.new(name)
attachment.region = region

View File

@ -125,9 +125,9 @@ function IkConstraint:apply1 (bone, targetX, targetY, compress, stretch, uniform
rotationIK = rotationIK + math_deg(math_atan2(ty, tx))
if bone.ascaleX < 0 then rotationIK = rotationIK + 180 end
if rotationIK > 180 then
rotationIK = rotationIK - 360
elseif (rotationIK < -180) then
rotationIK = rotationIK + 360
rotationIK = rotationIK - 360
elseif rotationIK < -180 then
rotationIK = rotationIK + 360
end
local sx = bone.ascaleX
local sy = bone.ascaleY
@ -262,7 +262,7 @@ function IkConstraint:apply2 (parent, child, targetX, targetY, bendDir, stretch,
d = c1 * c1 - 4 * c2 * c
if d >= 0 then
local q = math_sqrt(d)
if (c1 < 0) then q = -q end
if c1 < 0 then q = -q end
q = -(c1 + q) / 2
local r0 = q / c2
local r1 = c / q
@ -285,7 +285,7 @@ function IkConstraint:apply2 (parent, child, targetX, targetY, bendDir, stretch,
local maxDist = maxX * maxX
local maxY = 0
c = -a * l1 / (aa - bb)
if (c >= -1 and c <= 1) then
if c >= -1 and c <= 1 then
c = math_acos(c)
x = a * math_cos(c) + l1
y = b * math_sin(c)

View File

@ -36,7 +36,7 @@ function interpolation.apply (func, start, _end, a)
end
function interpolation.pow2(a)
if (a <= 0.5) then return math_pow(a * 2, 2) / 2 end
if a <= 0.5 then return math_pow(a * 2, 2) / 2 end
return math_pow((a - 1) * 2, 2) / -2 + 1
end

View File

@ -438,7 +438,7 @@ function Skeleton:setSkin (skinName)
end
function Skeleton:setSkinByReference(newSkin)
if (self.skin == newSkin) then return end
if self.skin == newSkin then return end
if newSkin then
if self.skin then
newSkin:attachAll(self, self.skin)

View File

@ -59,7 +59,7 @@ function SkeletonBounds:update (skeleton, updateAabb)
local slots = skeleton.slots
for _,slot in ipairs(skeleton.slots) do
if (slot.bone.active) then
if slot.bone.active then
local attachment = slot.attachment
if attachment and attachment.type == AttachmentType.boundingbox then
local boundingBox = attachment
@ -110,7 +110,7 @@ end
function SkeletonBounds: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
return false
end
local m = (y2 - y1) / (x2 - x1)
local y = m * (minX - x1) + y1

View File

@ -128,9 +128,9 @@ function SkeletonClipping:clipTriangles(vertices, uvs, triangles, trianglesLengt
while p <= polygonsCount do
local s = #clippedVertices + 1
local clipOutput = {}
if (self:clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) then
if self:clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput) then
local clipOutputLength = #clipOutput
if (clipOutputLength > 0) then
if clipOutputLength > 0 then
local d0 = y2 - y3
local d1 = x3 - x2
local d2 = x1 - x3
@ -295,7 +295,7 @@ function SkeletonClipping:clip(x1, y1, x2, y2, x3, y3, clippingArea, output)
table_insert(output, output[1])
table_insert(output, output[2])
if (i == clippingVerticesLast) then break end
if i == clippingVerticesLast then break end
local temp = output
output = input
for i, _ in ipairs(output) do
@ -340,7 +340,7 @@ function SkeletonClipping:makeClockwise(polygon)
area = area + p1x * p2y - p2x * p1y
i = i + 2
end
if (area < 0) then return end
if area < 0 then return end
i = 1
local lastX = verticesLength - 2 + 1

View File

@ -48,6 +48,9 @@ local TransformMode = require "spine-lua.TransformMode"
local utils = require "spine-lua.utils"
local Color = require "spine-lua.Color"
local math_max = math.max
local math_floor = math.floor
local SkeletonJson = {}
function SkeletonJson.new (attachmentLoader)
if not attachmentLoader then attachmentLoader = AttachmentLoader.new() end
@ -200,13 +203,13 @@ function SkeletonJson.new (attachmentLoader)
for _,boneName in ipairs(constraintMap.bones) do
local bone = skeletonData:findBone(boneName)
if not bone then error("Transform constraint bone not found: " .. boneName, 2) end
if not bone then error("Transform constraint bone not found: " .. boneName) end
table_insert(data.bones, bone)
end
local targetName = constraintMap.target
data.target = skeletonData:findBone(targetName)
if not data.target then error("Transform constraint target bone not found: " .. (targetName or "none"), 2) end
if not data.target then error("Transform constraint target bone not found: " .. (targetName or "none")) end
data.local_ = getValue(constraintMap, "local", false)
data.relative = getValue(constraintMap, "relative", false)
@ -237,13 +240,13 @@ function SkeletonJson.new (attachmentLoader)
for _,boneName in ipairs(constraintMap.bones) do
local bone = skeletonData:findBone(boneName)
if not bone then error("Path constraint bone not found: " .. boneName, 2) end
if not bone then error("Path constraint bone not found: " .. boneName) end
table_insert(data.bones, bone)
end
local targetName = constraintMap.target
data.target = skeletonData:findSlot(targetName)
if data.target == nil then error("Path target slot not found: " .. targetName, 2) end
if data.target == nil then error("Path target slot not found: " .. targetName) end
data.positionMode = PathConstraintData.PositionMode[getValue(constraintMap, "positionMode", "percent"):lower()]
data.spacingMode = PathConstraintData.SpacingMode[getValue(constraintMap, "spacingMode", "length"):lower()]
@ -269,7 +272,7 @@ function SkeletonJson.new (attachmentLoader)
if skinMap["bones"] then
for _, entry in ipairs(skinMap["bones"]) do
local bone = skeletonData:findBone(entry)
if bone == nil then error("Skin bone not found: " .. entry, 2) end
if bone == nil then error("Skin bone not found: " .. entry) end
table_insert(skin.bones, bone)
end
end
@ -277,7 +280,7 @@ function SkeletonJson.new (attachmentLoader)
if skinMap["ik"] then
for _, entry in ipairs(skinMap["ik"]) do
local constraint = skeletonData:findIkConstraint(entry)
if constraint == nil then error("Skin IK constraint not found: " .. entry, 2) end
if constraint == nil then error("Skin IK constraint not found: " .. entry) end
table_insert(skin.constraints, constraint)
end
end
@ -285,7 +288,7 @@ function SkeletonJson.new (attachmentLoader)
if skinMap["transform"] then
for _, entry in ipairs(skinMap["transform"]) do
local constraint = skeletonData:findTransformConstraint(entry)
if constraint == nil then error("Skin transform constraint not found: " .. entry, 2) end
if constraint == nil then error("Skin transform constraint not found: " .. entry) end
table_insert(skin.constraints, constraint)
end
end
@ -293,7 +296,7 @@ function SkeletonJson.new (attachmentLoader)
if skinMap["path"] then
for _, entry in ipairs(skinMap["path"]) do
local constraint = skeletonData:findPathConstraint(entry)
if constraint == nil then error("Skin path constraint not found: " .. entry, 2) end
if constraint == nil then error("Skin path constraint not found: " .. entry) end
table_insert(skin.constraints, constraint)
end
end
@ -915,7 +918,7 @@ function SkeletonJson.new (attachmentLoader)
table_insert(timelines, readTimeline1(timelineMap, timeline, 0, timelineScale))
elseif timelineName == "spacing" then
local timeline = Animation.PathConstraintSpacingTimeline.new(#timelineMap, #timelineMap, constraintIndex)
local timelineScale = 1;
local timelineScale = 1
if data.spacingMode == SpacingMode.Length or data.spacingMode == SpacingMode.Fixed then timelineScale = scale end
table_insert(timelines, readTimeline1(timelineMap, timeline, 0, timelineScale))
elseif timelineName == "mix" then
@ -960,19 +963,19 @@ function SkeletonJson.new (attachmentLoader)
if map.deform then
for deformName, deformMap in pairs(map.deform) do
local skin = skeletonData:findSkin(deformName)
if not skin then error("Skin not found: " .. deformName, 2) end
if not skin then error("Skin not found: " .. deformName) end
for slotName,slotMap in pairs(deformMap) do
local slotIndex = skeletonData:findSlot(slotName).index
if slotIndex == -1 then error("Slot not found: " .. slotMap.name, 2) end
if slotIndex == -1 then error("Slot not found: " .. slotMap.name) end
for timelineName,timelineMap in pairs(slotMap) do
local keyMap = timelineMap[1]
if keyMap then
local attachment = skin:getAttachment(slotIndex, timelineName)
if not attachment then error("Deform attachment not found: " .. timelineMap.name, 2) end
if not attachment then error("Deform attachment not found: " .. timelineMap.name) end
local weighted = attachment.bones ~= nil
local vertices = attachment.vertices
local deformLength = #vertices
if weighted then deformLength = math.floor(deformLength / 3) * 2 end
if weighted then deformLength = math_floor(deformLength / 3) * 2 end
local timeline = Animation.DeformTimeline.new(#timelineMap, #timelineMap, slotIndex, attachment)
local bezier = 0
@ -1104,7 +1107,7 @@ function SkeletonJson.new (attachmentLoader)
local duration = 0
for _,timeline in ipairs(timelines) do
duration = math.max(duration, timeline:getDuration())
duration = math_max(duration, timeline:getDuration())
end
table_insert(skeletonData.animations, Animation.new(name, timelines, duration))
end

View File

@ -187,7 +187,7 @@ function Skin:attachAll(skeleton, oldSkin)
local slotAttachment = slot.attachment
if slotAttachment then
local dictionary = oldSkin.attachments[i]
if (dictionary) then
if dictionary then
for key, value in pairs(dictionary) do
local skinAttachment = value
if slotAttachment == skinAttachment then

View File

@ -111,7 +111,7 @@ function Triangulator:triangulate (verticesArray)
end
ii = (ii + 1) % vertexCount
end
if (not goToHead) then
if not goToHead then
breakLoop = true
break
end
@ -244,7 +244,7 @@ function Triangulator:decompose(verticesArray, triangles)
n = #convexPolygons
while i <= n do
polygonIndices = convexPolygonsIndices[i]
if (#polygonIndices > 0) then
if #polygonIndices > 0 then
local firstIndex = polygonIndices[1]
local lastIndex = polygonIndices[#polygonIndices]
@ -265,7 +265,7 @@ function Triangulator:decompose(verticesArray, triangles)
while ii <= n do
if ii ~= i then
local otherIndices = convexPolygonsIndices[ii]
if (#otherIndices == 3) then
if #otherIndices == 3 then
local otherFirstIndex = otherIndices[1]
local otherSecondIndex = otherIndices[2]
local otherLastIndex = otherIndices[3]

View File

@ -47,7 +47,7 @@ function Attachment.new (name, attachmentType)
end
function Attachment:copy ()
error("Attachment copy not implemented.")
error("Attachment copy not implemented.", 2)
end
return Attachment

View File

@ -45,6 +45,7 @@ setmetatable(VertexAttachment, { __index = Attachment })
function VertexAttachment.new (name, attachmentType)
local self = Attachment.new(name, attachmentType)
self.vertexAttachment = true
self.bones = nil
self.vertices = nil
self.worldVerticesLength = 0

View File

@ -169,17 +169,17 @@ end
function utils.randomTriangularWith(min, max, mode)
local u = math.random()
local d = max - min
if (u <= (mode - min) / d) then return min + math_sqrt(u * d * (mode - min)) end
if u <= (mode - min) / d then return min + math_sqrt(u * d * (mode - min)) end
return max - math_sqrt((1 - u) * d * (max - mode))
end
function utils.testBit(value, bit)
if (value == nil) then return 0 end
if value == nil then return 0 end
return value % (2 * bit) >= bit
end
function utils.setBit(value, bit)
if (value == nil) then return 0 end
if value == nil then return 0 end
if value % (2 * bit) >= bit then
return value
end
@ -187,7 +187,7 @@ function utils.setBit(value, bit)
end
function utils.clearBit(value, bit)
if (value == nil) then return 0 end
if value == nil then return 0 end
if value % (2 * bit) >= bit then
return value - bit
end

View File

@ -69,7 +69,7 @@ function SwirlEffect:transform (vertex)
local x = vertex.x - self.worldX
local y = vertex.y - self.worldY
local dist = math_sqrt(x * x + y * y)
if (dist < self.radius) then
if dist < self.radius then
local theta = interpolation.apply(self.interpolation, 0, self.angleRad, (self.radius - dist) / self.radius)
local cos = math_cos(theta)
local sin = math_sin(theta)