-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
163 lines (124 loc) · 3.78 KB
/
init.lua
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
--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Packages = ReplicatedStorage:WaitForChild("Packages")
local Signal = require(Packages.Signal)
local Prototype = require(script.Prototype)
local EventHanlder = require(script.EventHandler)
--[=[
@client
@class Draggable
]=]
local Draggable = {}
type Schema = Prototype.Schema
export type Class = Prototype.Class
--[=[
This function creates a Draggable object, which turns the GuiObject into a Draggable GuiObject.
@param guiObject GuiObject
@return Draggable
]=]
function Draggable.new(guiObject: GuiObject): Class
if typeof(guiObject) ~= "Instance" then
return error("Argument #1 must be a type of Instance", 2)
end
if not guiObject:IsA("GuiObject") then
return error("Argument #1 must be a class under GuiObject", 2)
end
if not guiObject:FindFirstAncestorWhichIsA("ScreenGui") then
return error("Argument #1 must be a descendant of ScreenGui", 2)
end
if guiObject:IsA("GuiButton") then
return error("Argument #1 is a GuiButton, it is not allowed to be draggable", 2)
end
local schema: Schema = {
_mouseOffset = Vector2.new(),
_guiObject = guiObject,
_include = {},
Enabled = true,
Dragging = Signal.new(),
Began = Signal.new(),
Ended = Signal.new(),
}
local self = setmetatable(schema, Prototype)
EventHanlder.Draggables[guiObject] = self
guiObject.Destroying:Connect(function()
self:Destroy()
end)
return self
end
--[[
* Initialize functions that use the EventHandler module.
? For what purpose? It is to counter the Cyclic Module Dependecy.
]]
--[=[
Removes the draggable capability from the associated GuiObject. `Since v1.1.0`, this function is automatically called when the GuiObject is destroyed.
@within Draggable
@method Destroy
]=]
function Prototype.Destroy(self: Class)
EventHanlder.Draggables[self._guiObject] = nil
for _, guiObject in self._include do
EventHanlder.Draggables[guiObject] = nil
end
self.Dragging:Destroy()
self.Began:Destroy()
self.Ended:Destroy()
table.clear(self)
end
--[=[
Allows the user to drag the Draggable GuiObject on its descendants.
@within Draggable
@method IncludeDescendants
]=]
function Prototype.IncludeDescendants(self: Class)
for _, instance in self._guiObject:GetDescendants() do
if not instance:IsA("GuiObject") or instance:IsA("GuiButton") then
continue
end
table.insert(self._include, instance)
EventHanlder.Draggables[instance] = self
end
end
--[=[
This function works similarly to [`IncludeDescendants`](#IncludeDescendants), but the developer can pass an array containing specific descendants of the GuiObject to be draggable.
Example:
```lua
local draggableObject = Draggable.new(exampleFrame)
local list = [] -- Do not include TextButtons
for i, guiObject in exampleFrame:GetDescendants() do
if guiObject:IsA("TextButton") then
continue
end
table.insert(list, guiObject)
end
draggableObject:Include(list)
```
:::important
Make sure that every element on the list is a GuiObject and that it is a descendant of the Draggable GuiObject.
:::
@within Draggable
@param guiObjects {GuiObject}
@method Include
]=]
function Prototype.Include(self: Class, guiObjects: { GuiObject })
for i, guiObject in guiObjects do
if typeof(guiObject) ~= "Instance" then
warn(`Index #{i} is not a type of Instance`)
continue
end
if not guiObject:IsA("GuiObject") then
warn(`Index #{i} is not a Class under GuiObject`)
continue
end
if not guiObject:IsDescendantOf(self._guiObject) then
warn(`Index #{i} is not a descendant of Draggable`)
continue
end
if guiObject:IsA("GuiButton") then
warn(`Index #{i} is a GuiButton, it is not allowed to be draggable`)
continue
end
table.insert(self._include, guiObject)
EventHanlder.Draggables[guiObject] = self
end
end
return Draggable