-
Notifications
You must be signed in to change notification settings - Fork 10
/
window.lua
298 lines (236 loc) · 7.23 KB
/
window.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
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
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
--[=[
@interface WindowOptions
@within Plasma
.title? string
.closable? boolean
.movable? boolean
.resizable? boolean
]=]
--[=[
@within Plasma
@function window
@param options string | WindowOptions -- The title of the window, or options
@param children () -> () -- Children
@tag widgets
@return WindowWidgetHandle
A window widget. Contains children.
- Closable
- Draggable
- Resizable
Returns a widget handle, which has the field:
- `closed`, a function you can call to check if the close button was clicked.
![Window with checkboxes](https://i.eryn.io/2150/TVkkOnxj.png)
]=]
local Runtime = require(script.Parent.Parent.Runtime)
local createConnect = require(script.Parent.Parent.createConnect)
local Style = require(script.Parent.Parent.Style)
local automaticSize = require(script.Parent.Parent.automaticSize)
local c = require(script.Parent.Parent.create)
local MIN_SIZE = Vector2.new(50, 50)
local MAX_SIZE = Vector2.new(1500, 500)
return Runtime.widget(function(options, fn)
local closed, setClosed = Runtime.useState(false)
local refs = Runtime.useInstance(function(ref)
local style = Style.get()
local dragConnection
local connect = createConnect()
c("Frame", {
[ref] = "frame",
BackgroundColor3 = style.bg2,
Position = UDim2.new(0, 0, 0, 0),
Size = UDim2.new(0, 50, 0, 40),
c("UICorner", {}),
c("UIPadding", {
PaddingBottom = UDim.new(0, 20),
PaddingLeft = UDim.new(0, 20),
PaddingRight = UDim.new(0, 20),
PaddingTop = UDim.new(0, 20),
}),
c("UIStroke", {}),
c("TextButton", {
[ref] = "titleBar",
Size = UDim2.new(1, 0, 0, 40),
BackgroundTransparency = 1,
Text = "",
InputBegan = function(clickInput)
if not ref.titleBar.Active then
return
end
if clickInput.UserInputType ~= Enum.UserInputType.MouseButton1 then
return
end
local lastMousePosition = clickInput.Position
if
ref.frame.Parent:FindFirstChildWhichIsA("UIGridStyleLayout")
and not ref.frame.Parent:IsA("ScreenGui")
then
local beforePosition = ref.frame.AbsolutePosition
local screenGui = ref.frame:FindFirstAncestorOfClass("ScreenGui")
if screenGui.IgnoreGuiInset then
beforePosition += GuiService:GetGuiInset()
end
ref.frame.Parent = screenGui
ref.frame.Position = UDim2.new(0, beforePosition.X, 0, beforePosition.Y)
end
dragConnection = connect(UserInputService, "InputChanged", function(moveInput)
local delta = lastMousePosition - moveInput.Position
lastMousePosition = moveInput.Position
ref.frame.Position = ref.frame.Position - UDim2.new(0, delta.X, 0, delta.Y)
end)
end,
InputEnded = function(input)
if dragConnection and input.UserInputType == Enum.UserInputType.MouseButton1 then
dragConnection:Disconnect()
dragConnection = nil
end
end,
c("Frame", {
[ref] = "handle",
Position = UDim2.new(0, -5, 0, 0),
c("TextLabel", {
Text = "..",
Position = UDim2.new(0, 0, 0, 0),
BackgroundTransparency = 1,
TextSize = 20,
TextColor3 = style.mutedTextColor,
}),
c("TextLabel", {
Text = "..",
Position = UDim2.new(0, 0, 0, 7),
BackgroundTransparency = 1,
TextSize = 20,
TextColor3 = style.mutedTextColor,
}),
c("TextLabel", {
Text = "..",
Position = UDim2.new(0, 0, 0, -7),
BackgroundTransparency = 1,
TextSize = 20,
TextColor3 = style.mutedTextColor,
}),
}),
c("TextLabel", {
[ref] = "title",
BackgroundTransparency = 1,
Font = Enum.Font.GothamBold,
Size = UDim2.new(1, 0, 1, 0),
TextColor3 = style.mutedTextColor,
TextSize = 20,
TextXAlignment = Enum.TextXAlignment.Left,
TextYAlignment = Enum.TextYAlignment.Top,
TextTruncate = Enum.TextTruncate.AtEnd,
}),
c("TextButton", {
[ref] = "close",
BackgroundColor3 = Color3.fromHex("e74c3c"),
Size = UDim2.new(0, 20, 0, 20),
Text = "",
AnchorPoint = Vector2.new(0.5, 0),
Position = UDim2.new(1, -10, 0, 0),
TextColor3 = Color3.fromHex("#71190f"),
TextSize = 20,
Font = Enum.Font.Gotham,
MouseEnter = function()
ref.close.Text = "x"
end,
MouseLeave = function()
ref.close.Text = ""
end,
Activated = function()
setClosed(true)
end,
c("UICorner", {
CornerRadius = UDim.new(1, 0),
}),
}),
}),
c("ScrollingFrame", {
[ref] = "container",
BackgroundTransparency = 1,
VerticalScrollBarInset = Enum.ScrollBarInset.ScrollBar,
HorizontalScrollBarInset = Enum.ScrollBarInset.ScrollBar,
BorderSizePixel = 0,
ScrollBarThickness = 6,
Position = UDim2.new(0, 0, 0, 40),
c("UIListLayout", {
SortOrder = Enum.SortOrder.LayoutOrder,
Padding = UDim.new(0, 10),
}),
}),
c("TextButton", {
[ref] = "resizeHandle",
Size = UDim2.new(0, 20, 0, 20),
Text = "≡",
Font = Enum.Font.SourceSans,
TextSize = 20,
Rotation = -45,
BackgroundTransparency = 1,
TextColor3 = style.mutedTextColor,
Position = UDim2.new(1, 0, 1, 0),
InputBegan = function(clickInput)
if clickInput.UserInputType ~= Enum.UserInputType.MouseButton1 then
return
end
local initialMousePosition = clickInput.Position
local initialSize = ref.container.AbsoluteSize
dragConnection = connect(UserInputService, "InputChanged", function(moveInput)
if moveInput.UserInputType ~= Enum.UserInputType.MouseMovement then
return
end
local delta = Vector2.new(
(moveInput.Position.X - initialMousePosition.X),
(moveInput.Position.Y - initialMousePosition.Y)
)
local size = initialSize + delta
ref.container:SetAttribute(
"maxSize",
Vector2.new(math.max(MIN_SIZE.X, size.X), math.max(MIN_SIZE.Y, size.Y))
)
end)
end,
InputEnded = function(input)
if dragConnection and input.UserInputType == Enum.UserInputType.MouseButton1 then
dragConnection:Disconnect()
dragConnection = nil
end
end,
}),
})
automaticSize(ref.container)
automaticSize(ref.frame)
return ref.frame, ref.container
end)
if type(options) == "string" then
options = {
title = options,
}
end
local movable = if options.movable ~= nil then options.movable else true
local resizable = if options.resizable ~= nil then options.movable else true
refs.close.Visible = options.closable or false
refs.handle.Visible = movable
refs.titleBar.Active = movable
refs.resizeHandle.Visible = resizable
refs.title.Size = UDim2.new(1, if options.closable then -30 else 0, 1, 0)
local spaces = if movable then " " else ""
refs.title.Text = options.title and spaces .. string.upper(options.title) or ""
Runtime.useEffect(function()
refs.container:SetAttribute("maxSize", options.maxSize or MAX_SIZE)
end, options.maxSize)
Runtime.useEffect(function()
refs.container:SetAttribute("minSize", options.minSize)
end, options.minSize)
Runtime.scope(fn)
local handle = {
closed = function()
if closed then
setClosed(false)
return true
end
return false
end,
}
return handle
end)