mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Update tween.luau
fix test fail again! yes i'm a retard
This commit is contained in:
parent
8ca9e9b440
commit
4edaddf151
1 changed files with 44 additions and 21 deletions
|
|
@ -2,11 +2,12 @@
|
||||||
local TweenService = game and game:GetService("TweenService") or nil
|
local TweenService = game and game:GetService("TweenService") or nil
|
||||||
local HasTweenInfo = (TweenInfo ~= nil)
|
local HasTweenInfo = (TweenInfo ~= nil)
|
||||||
|
|
||||||
-- Factory: inject action + cleanup so tweens are stopped on scope destruction
|
--cleanup and action injection so tweens are stopped on scope destruction
|
||||||
|
|
||||||
return function(action, cleanup)
|
return function(action, cleanup)
|
||||||
local tween = {}
|
local tween = {}
|
||||||
|
|
||||||
-- Presets (simple, useful)
|
-- Presets: only available when TweenInfo exists (Roblox runtime)
|
||||||
if HasTweenInfo then
|
if HasTweenInfo then
|
||||||
tween.presets = {
|
tween.presets = {
|
||||||
instant = TweenInfo.new(0),
|
instant = TweenInfo.new(0),
|
||||||
|
|
@ -14,34 +15,48 @@ return function(action, cleanup)
|
||||||
smooth = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
|
smooth = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
|
||||||
snappy = TweenInfo.new(0.16, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out),
|
snappy = TweenInfo.new(0.16, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out),
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
tween.presets = {}
|
||||||
|
end
|
||||||
|
|
||||||
-- Imperative helper (for events): vide.tweenTo(instance, {Position=...}, info)
|
-- Imperative helper: plays a tween (or applies instantly in tests)
|
||||||
function tween.tweenTo(instance, goals, info)
|
function tween.tweenTo(instance, goals, info)
|
||||||
|
if not instance or not goals then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Non-Roblox / unit tests: apply instantly
|
||||||
if not TweenService then
|
if not TweenService then
|
||||||
-- Non-Roblox / tests: just apply instantly
|
|
||||||
if instance and goals then
|
|
||||||
for k, v in pairs(goals) do
|
for k, v in pairs(goals) do
|
||||||
instance[k] = v
|
instance[k] = v
|
||||||
end
|
end
|
||||||
end
|
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local tweenInfo = info or tween.presets.smooth
|
|
||||||
|
local tweenInfo = info
|
||||||
|
if tweenInfo == nil then
|
||||||
|
if tween.presets.smooth then
|
||||||
|
tweenInfo = tween.presets.smooth
|
||||||
|
else
|
||||||
|
-- Fallback in case presets are not available for any reason
|
||||||
|
tweenInfo = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local tw = TweenService:Create(instance, tweenInfo, goals)
|
local tw = TweenService:Create(instance, tweenInfo, goals)
|
||||||
tw:Play()
|
tw:Play()
|
||||||
return tw
|
return tw
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Action helper (for create trees):
|
-- Action helper: attach tweening in create() trees
|
||||||
-- create "Frame" { tween({Position=...}, presets.fast), ... }
|
|
||||||
function tween.tween(goals, info)
|
function tween.tween(goals, info)
|
||||||
return action(function(instance)
|
return action(function(instance)
|
||||||
if not goals then
|
if not goals then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If TweenService not available, apply instantly (tests outside of roblox environment)
|
-- Non-Roblox / unit tests: apply instantly
|
||||||
if not TweenService then
|
if not TweenService then
|
||||||
for k, v in pairs(goals) do
|
for k, v in pairs(goals) do
|
||||||
instance[k] = v
|
instance[k] = v
|
||||||
|
|
@ -49,11 +64,19 @@ return function(action, cleanup)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local tweenInfo = info or tween.presets.smooth
|
local tweenInfo = info
|
||||||
|
if tweenInfo == nil then
|
||||||
|
if tween.presets.smooth then
|
||||||
|
tweenInfo = tween.presets.smooth
|
||||||
|
else
|
||||||
|
tweenInfo = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local tw = TweenService:Create(instance, tweenInfo, goals)
|
local tw = TweenService:Create(instance, tweenInfo, goals)
|
||||||
tw:Play()
|
tw:Play()
|
||||||
|
|
||||||
-- Stop on scope destruction to avoid leaking connections/tweens
|
-- Ensure tween is cancelled when the scope is destroyed
|
||||||
cleanup(function()
|
cleanup(function()
|
||||||
pcall(function()
|
pcall(function()
|
||||||
tw:Cancel()
|
tw:Cancel()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue