Repository navigation
Expand file tree
/
Copy pathButton-lib.lua
More file actions
389 lines (351 loc) · 13.1 KB
/
Copy pathButton-lib.lua
File metadata and controls
389 lines (351 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
--[[
Button Library
Original by Moun Sok Dara (DaraHub)
Used in XyzHub with gratitude for making this lib available.
Thanks for the clean and functional button system :3
]]
local ButtonLib = {}
local CoreGui = game:GetService("CoreGui")
local camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
_G.DarahubLibBtn = {}
local function getDPIScale()
return camera.ViewportSize.Y / 1080
end
if not UserInputService.KeyboardEnabled then
local darahubGui = CoreGui:FindFirstChild("Darahub") or Instance.new("ScreenGui", CoreGui)
darahubGui.Name = "Darahub"
darahubGui.DisplayOrder = 78
darahubGui.IgnoreGuiInset = true
darahubGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
local floatingButton = Instance.new("ImageButton")
floatingButton.Name = "FloatingButton_Darahub"
floatingButton.Parent = darahubGui
floatingButton.Active = true
floatingButton.BorderSizePixel = 0
floatingButton.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
floatingButton.Size = UDim2.new(0, 80, 0, 80)
floatingButton.Position = UDim2.new(0.85, 0, 0.05, 0)
floatingButton.BackgroundTransparency = 1
floatingButton.Image = "rbxassetid://137330250139083"
floatingButton.Visible = false
local aspectRatio = Instance.new("UIAspectRatioConstraint", floatingButton)
aspectRatio.DominantAxis = Enum.DominantAxis.Height
local sizeConstraint = Instance.new("UISizeConstraint", floatingButton)
sizeConstraint.MinSize = Vector2.new(68, 68)
sizeConstraint.MaxSize = Vector2.new(100, 100)
local corner = Instance.new("UICorner", floatingButton)
corner.CornerRadius = UDim.new(1, 0)
local uiScale = Instance.new("UIScale", floatingButton)
uiScale.Scale = getDPIScale()
local dragDetector = Instance.new("UIDragDetector", floatingButton)
local dragStartPosition = nil
local CLICK_THRESHOLD = 10
local HOLD_TIME = 1.5
local holdStartTime = nil
local isHolding = false
local holdCheckConnection = nil
local holdStartPosition = nil
local resettingDragDetector = false
local function reinsertDragDetector()
resettingDragDetector = true
if dragDetector then
dragDetector.Enabled = false
task.wait(0.01)
dragDetector.Enabled = true
end
task.wait(0.2)
resettingDragDetector = false
dragStartPosition = nil
end
local function startHoldCheck()
holdStartTime = tick()
holdStartPosition = floatingButton.Position
isHolding = true
holdCheckConnection = RunService.Heartbeat:Connect(function()
if not isHolding then
holdCheckConnection:Disconnect()
return
end
local currentPosition = floatingButton.Position
local positionChanged = currentPosition.X.Offset ~= holdStartPosition.X.Offset or
currentPosition.Y.Offset ~= holdStartPosition.Y.Offset
if positionChanged then
isHolding = false
holdCheckConnection:Disconnect()
return
end
local holdDuration = tick() - holdStartTime
if holdDuration >= HOLD_TIME then
isHolding = false
holdCheckConnection:Disconnect()
reinsertDragDetector()
end
end)
end
floatingButton.InputBegan:Connect(function(input)
if resettingDragDetector then return end
if input.UserInputType == Enum.UserInputType.Touch or
input.UserInputType == Enum.UserInputType.MouseButton1 then
startHoldCheck()
end
end)
floatingButton.InputEnded:Connect(function(input)
if resettingDragDetector then return end
if input.UserInputType == Enum.UserInputType.Touch or
input.UserInputType == Enum.UserInputType.MouseButton1 then
isHolding = false
if holdCheckConnection then
holdCheckConnection:Disconnect()
end
end
end)
dragDetector.DragStart:Connect(function()
if resettingDragDetector then return end
dragStartPosition = floatingButton.Position
isHolding = false
if holdCheckConnection then
holdCheckConnection:Disconnect()
end
end)
dragDetector.DragEnd:Connect(function()
if resettingDragDetector then return end
if dragStartPosition then
local currentPosition = floatingButton.Position
local distance = math.sqrt(
(currentPosition.X.Offset - dragStartPosition.X.Offset)^2 +
(currentPosition.Y.Offset - dragStartPosition.Y.Offset)^2
)
if distance < CLICK_THRESHOLD then
if Window and Window.Open then
Window:Open()
end
end
dragStartPosition = nil
end
end)
end
ButtonLib.Create = {}
local function buildBaseFrame(config)
local darahubGui = CoreGui:FindFirstChild("Darahub") or Instance.new("ScreenGui", CoreGui)
darahubGui.Name = "Darahub"
darahubGui.IgnoreGuiInset = true
darahubGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
local frame = Instance.new("Frame")
local flag = config.Flag or config.Text or "Element"
if _G.DarahubLibBtn[flag] then
pcall(function() _G.DarahubLibBtn[flag]:Destroy() end)
end
frame.Name = flag
frame.Parent = darahubGui
frame.Visible = config.Visible ~= false
frame.Draggable = true
frame.Active = true
frame.Size = UDim2.new(0, 250, 0, 90)
frame.Position = config.Position or UDim2.new(0.5, -125, 0.5, -45)
frame.BackgroundColor3 = Color3.new(1, 1, 1)
frame.BackgroundTransparency = 0.2
local uiScale = Instance.new("UIScale", frame)
uiScale.Scale = getDPIScale()
local corner = Instance.new("UICorner", frame)
corner.CornerRadius = UDim.new(0, 10)
local strokeB = Instance.new("UIStroke", frame)
strokeB.Thickness = 2
strokeB.Color = Color3.new(0, 0, 0)
strokeB.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
local strokeW = Instance.new("UIStroke", frame)
strokeW.Thickness = 1
strokeW.Color = Color3.new(1, 1, 1)
strokeW.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
local grad = Instance.new("UIGradient", frame)
grad.Rotation = 90
grad.Color = ColorSequence.new(Color3.fromRGB(47, 47, 47), Color3.new(0, 0, 0))
local label = Instance.new("TextLabel", frame)
label.Name = "Label"
label.Size = UDim2.new(1, -20, 1, 0)
label.Position = UDim2.new(0, 10, 0, 0)
label.BackgroundTransparency = 1
label.TextColor3 = Color3.new(1, 1, 1)
label.TextScaled = true
label.Font = Enum.Font.FredokaOne
label.ZIndex = 5
local drag = Instance.new("UIDragDetector", frame)
drag.Parent.Draggable = true
local HOLD_TIME_FRAME = 1.5
local isHoldingFrame = false
local holdStartTimeFrame = nil
local holdCheckConnectionFrame = nil
local holdStartPositionFrame = nil
local resettingDragDetectorFrame = false
local function reinsertDragDetectorFrame()
resettingDragDetectorFrame = true
if drag then
drag.Parent.Draggable = false
drag.Enabled = false
task.wait(0.01)
drag.Enabled = true
drag.Parent.Draggable = true
end
-- Add a small delay before allowing click detection again
task.wait(0.2)
resettingDragDetectorFrame = false
end
local function startHoldCheckFrame()
holdStartTimeFrame = tick()
holdStartPositionFrame = frame.Position
isHoldingFrame = true
holdCheckConnectionFrame = RunService.Heartbeat:Connect(function()
if not isHoldingFrame then
holdCheckConnectionFrame:Disconnect()
return
end
local currentPosition = frame.Position
local positionChanged = currentPosition.X.Offset ~= holdStartPositionFrame.X.Offset or
currentPosition.Y.Offset ~= holdStartPositionFrame.Y.Offset
if positionChanged then
isHoldingFrame = false
holdCheckConnectionFrame:Disconnect()
return
end
local holdDuration = tick() - holdStartTimeFrame
if holdDuration >= HOLD_TIME_FRAME then
isHoldingFrame = false
holdCheckConnectionFrame:Disconnect()
reinsertDragDetectorFrame()
end
end)
end
frame.InputBegan:Connect(function(input)
if resettingDragDetectorFrame then return end
if input.UserInputType == Enum.UserInputType.Touch or
input.UserInputType == Enum.UserInputType.MouseButton1 then
startHoldCheckFrame()
end
end)
frame.InputEnded:Connect(function(input)
if resettingDragDetectorFrame then return end
if input.UserInputType == Enum.UserInputType.Touch or
input.UserInputType == Enum.UserInputType.MouseButton1 then
isHoldingFrame = false
if holdCheckConnectionFrame then
holdCheckConnectionFrame:Disconnect()
end
end
end)
drag.DragStart:Connect(function()
if resettingDragDetectorFrame then return end
isHoldingFrame = false
if holdCheckConnectionFrame then
holdCheckConnectionFrame:Disconnect()
end
end)
local api = {}
function api:Destroy()
if _G.DarahubLibBtn[flag] then
_G.DarahubLibBtn[flag] = nil
end
frame:Destroy()
end
function api:SetVisible(visible)
frame.Visible = visible
end
function api:ReinsertDragDetector()
reinsertDragDetectorFrame()
end
function api:SetDraggable(enabled)
if drag.Parent then
drag.Parent.Draggable = enabled
drag.Enabled = enabled
end
end
return api, frame, label, drag, flag
end
function ButtonLib.Create:Button(config)
local api, frame, label, drag, flag = buildBaseFrame(config)
local callback = config.Callback or config[1] or function() end
label.Text = config.Text or "Button"
local startPos
local resettingDragDetector = false
drag.DragStart:Connect(function()
if resettingDragDetector then return end
startPos = frame.Position
end)
drag.DragEnd:Connect(function()
if resettingDragDetector then return end
if not startPos then return end
local currentPos = frame.Position
local dist = math.sqrt(
(currentPos.X.Offset - startPos.X.Offset)^2 +
(currentPos.Y.Offset - startPos.Y.Offset)^2
)
if dist < 5 then
frame:TweenSize(UDim2.new(0, 240, 0, 85), "Out", "Quad", 0.05, true)
task.wait(0.05)
frame:TweenSize(UDim2.new(0, 250, 0, 90), "Out", "Quad", 0.05, true)
pcall(callback)
end
startPos = nil
end)
_G.DarahubLibBtn[flag] = setmetatable(api, {
__index = function(self, key)
return frame[key]
end,
__newindex = function(self, key, value)
frame[key] = value
end
})
return _G.DarahubLibBtn[flag]
end
function ButtonLib.Create:Toggle(config)
local api, frame, label, drag, flag = buildBaseFrame(config)
local callback = config.Callback or config[1] or function() end
local baseText = config.Text or "Toggle"
local state = config.Default or config.Deafult or false
local resettingDragDetector = false
local function updateUI()
label.Text = baseText .. (state and " : ON" or " : OFF")
end
updateUI()
function api:Set(val)
if state ~= val then
state = val
updateUI()
pcall(callback, state)
end
end
function api:Get()
return state
end
local startPos
drag.DragStart:Connect(function()
if resettingDragDetector then return end
startPos = frame.Position
end)
drag.DragEnd:Connect(function()
if resettingDragDetector then return end
if not startPos then return end
local currentPos = frame.Position
local dist = math.sqrt(
(currentPos.X.Offset - startPos.X.Offset)^2 +
(currentPos.Y.Offset - startPos.Y.Offset)^2
)
if dist < 5 then
frame:TweenSize(UDim2.new(0, 240, 0, 85), "Out", "Quad", 0.05, true)
task.wait(0.05)
frame:TweenSize(UDim2.new(0, 250, 0, 90), "Out", "Quad", 0.05, true)
api:Set(not state)
end
startPos = nil
end)
_G.DarahubLibBtn[flag] = setmetatable(api, {
__index = function(self, key)
return frame[key]
end,
__newindex = function(self, key, value)
frame[key] = value
end
})
return _G.DarahubLibBtn[flag]
end
return ButtonLib