mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
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:
parent
09a98ab343
commit
cf51d6da77
1 changed files with 31 additions and 27 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue