Removed cleanup parameter and use internal logic instead, vide.cleanup not compatible.

Refactor draggable factory to remove cleanup parameter and handle connections more efficiently.
This commit is contained in:
sindri 2026-03-03 12:49:04 +01:00 committed by GitHub
parent 09a98ab343
commit cf51d6da77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,18 +1,16 @@
-- src/draggable.luau -- src/draggable.luau
local UserInputService = game and game:GetService("UserInputService") or nil local UserInputService = game and game:GetService("UserInputService") or nil
-- Factory: inject vide.action and vide.cleanup to avoid cyclic requires -- Factory: injects action from vide
return function(action, cleanup) return function(action)
return function(opts) return function(opts)
opts = opts or {} opts = opts or {}
local axis = opts.axis or "both" -- "x" | "y" | "both" local axis = opts.axis or "both" -- "x" | "y" | "both"
return action(function(gui) return action(function(gui)
-- allow require in non-Roblox test environments
if not UserInputService then if not UserInputService then
return return
end end
if typeof(gui) ~= "Instance" or not gui:IsA("GuiObject") then if typeof(gui) ~= "Instance" or not gui:IsA("GuiObject") then
return return
end end
@ -22,6 +20,18 @@ return function(action, cleanup)
local dragStart local dragStart
local startPos local startPos
local beganConn
local inputChangedConn
local uisConn
local destroyingConn
local function disconnectAll()
if beganConn then beganConn:Disconnect(); beganConn = nil end
if inputChangedConn then inputChangedConn:Disconnect(); inputChangedConn = nil end
if uisConn then uisConn:Disconnect(); uisConn = nil end
if destroyingConn then destroyingConn:Disconnect(); destroyingConn = nil end
end
local function update(input) local function update(input)
local delta = input.Position - dragStart local delta = input.Position - dragStart
@ -36,56 +46,50 @@ return function(action, cleanup)
end end
gui.Position = UDim2.new(startPos.X.Scale, xOff, startPos.Y.Scale, yOff) gui.Position = UDim2.new(startPos.X.Scale, xOff, startPos.Y.Scale, yOff)
if opts.onDrag then
opts.onDrag(gui.Position)
end
end end
local beganConn = gui.InputBegan:Connect(function(input) beganConn = gui.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
dragging = true dragging = true
dragStart = input.Position dragStart = input.Position
startPos = gui.Position startPos = gui.Position
if opts.onDragStart then -- stop dragging when the same input ends
opts.onDragStart(startPos)
end
local changedConn local changedConn
changedConn = input.Changed:Connect(function() changedConn = input.Changed:Connect(function()
if input.UserInputState == Enum.UserInputState.End then if input.UserInputState == Enum.UserInputState.End then
dragging = false dragging = false
if changedConn then changedConn:Disconnect() end if changedConn then
if opts.onDragEnd then changedConn:Disconnect()
opts.onDragEnd(gui.Position) changedConn = nil
end end
end end
end) end)
cleanup(function()
if changedConn then changedConn:Disconnect() end
end)
end end
end) end)
local changedConn = gui.InputChanged:Connect(function(input) inputChangedConn = gui.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
dragInput = input dragInput = input
end end
end) end)
local uisConn = UserInputService.InputChanged:Connect(function(input) uisConn = UserInputService.InputChanged:Connect(function(input)
if input == dragInput and dragging then if input == dragInput and dragging then
update(input) update(input)
end end
end) end)
cleanup(function() -- Cleanup without vide.cleanup which printed false errors into console
beganConn:Disconnect() if gui.Destroying then
changedConn:Disconnect() destroyingConn = gui.Destroying:Connect(disconnectAll)
uisConn:Disconnect() else
end) destroyingConn = gui.AncestryChanged:Connect(function(_, parent)
if parent == nil then
disconnectAll()
end
end)
end
end) end)
end end
end end