Add tweening functionality with presets and cleanup on scope destruction

This commit is contained in:
sindri 2026-03-03 10:03:12 +01:00 committed by GitHub
parent 7d89cb5a1b
commit ec35a0fddd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

64
src/tween.luau Normal file
View file

@ -0,0 +1,64 @@
-- src/tween.luau
local TweenService = game and game:GetService("TweenService") or nil
-- Factory: inject action + cleanup so tweens are stopped on scope destruction
return function(action, cleanup)
local tween = {}
-- Presets (simple, useful)
tween.presets = {
instant = TweenInfo.new(0),
fast = TweenInfo.new(0.12, 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),
}
-- Imperative helper (for events): vide.tweenTo(instance, {Position=...}, info)
function tween.tweenTo(instance, goals, info)
if not TweenService then
-- Non-Roblox / tests: just apply instantly
if instance and goals then
for k, v in pairs(goals) do
instance[k] = v
end
end
return nil
end
local tweenInfo = info or tween.presets.smooth
local tw = TweenService:Create(instance, tweenInfo, goals)
tw:Play()
return tw
end
-- Action helper (for create trees):
-- create "Frame" { tween({Position=...}, presets.fast), ... }
function tween.tween(goals, info)
return action(function(instance)
if not goals then
return
end
-- If TweenService not available, apply instantly (tests outside of roblox environment)
if not TweenService then
for k, v in pairs(goals) do
instance[k] = v
end
return
end
local tweenInfo = info or tween.presets.smooth
local tw = TweenService:Create(instance, tweenInfo, goals)
tw:Play()
-- Stop on scope destruction to avoid leaking connections/tweens
cleanup(function()
pcall(function()
tw:Cancel()
end)
end)
end)
end
return tween
end