-
Notifications
You must be signed in to change notification settings - Fork 10
/
automaticSize.lua
218 lines (173 loc) · 5.82 KB
/
automaticSize.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
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local function applyLayout(container, layout)
local axisName = container:GetAttribute("axis") or "XY"
local axis = Enum.AutomaticSize[axisName]
local maxSize = container:GetAttribute("maxSize") or Vector2.new(math.huge, math.huge)
local minSize = container:GetAttribute("minSize") or Vector2.new(0, 0)
if typeof(maxSize) == "UDim2" then
if container.Parent == nil then
maxSize = Vector2.new(0, 0)
else
local parentSize = container.Parent.AbsoluteSize
maxSize = Vector2.new(
(parentSize.X / maxSize.X.Scale) + maxSize.X.Offset,
(parentSize.Y / maxSize.Y.Scale) + maxSize.Y.Offset
)
end
end
local padX = 0
local padY = 0
local padding = container:FindFirstChildOfClass("UIPadding")
if padding ~= nil then
padX = padding.PaddingLeft.Offset + padding.PaddingRight.Offset
padY = padding.PaddingTop.Offset + padding.PaddingBottom.Offset
end
local contentSize
if layout then
contentSize = layout.AbsoluteContentSize
elseif container:IsA("TextButton") or container:IsA("TextLabel") then
contentSize = container.TextBounds
else
contentSize = Vector2.new(0, 0)
for _, child in container:GetChildren() do
if child:IsA("GuiObject") then
local farX = child.Position.X.Offset + child.Size.X.Offset
local farY = child.Position.Y.Offset + child.Size.Y.Offset
contentSize = Vector2.new(math.max(contentSize.X, farX), math.max(contentSize.Y, farY))
end
end
end
local baseX = math.max(contentSize.X + padX, minSize.X)
local baseY = math.max(contentSize.Y + padY, minSize.Y)
local x, y
local xClamped, yClamped
if axis == Enum.AutomaticSize.XY then
x = UDim.new(0, baseX)
y = UDim.new(0, baseY)
xClamped = UDim.new(0, math.min(baseX, maxSize.X))
yClamped = UDim.new(0, math.min(baseY, maxSize.Y))
elseif axis == Enum.AutomaticSize.X then
x = UDim.new(0, baseX)
y = container.Size.Y
xClamped = UDim.new(0, math.min(baseX, maxSize.X))
yClamped = container.Size.Y
else
x = container.Size.X
y = UDim.new(0, baseY)
xClamped = container.Size.X
yClamped = UDim.new(0, math.min(baseY, maxSize.Y))
end
if container:IsA("ScrollingFrame") then
local canvasX = x
local canvasY = y
if x.Offset > xClamped.Offset then
canvasY -= UDim.new(0, container.ScrollBarThickness)
end
if y.Offset > yClamped.Offset then
canvasX -= UDim.new(0, container.ScrollBarThickness)
end
container.CanvasSize = UDim2.new(canvasX, canvasY)
end
container.Size = UDim2.new(xClamped, yClamped)
end
local function trackParentSize(instance, callback)
local parent = nil
local connection = nil
local function parentChanged(newParent)
if parent == newParent then
return
end
if connection ~= nil then
connection:Disconnect()
connection = nil
end
if newParent == nil then
return
end
connection = newParent:GetPropertyChangedSignal("AbsoluteSize"):Connect(callback)
parent = newParent
end
parentChanged(instance.Parent)
instance:GetPropertyChangedSignal("Parent"):Connect(function()
parentChanged(instance.Parent)
end)
end
local defaultOptions = {}
--[=[
@within Plasma
@function automaticSize
@param container GuiObject -- The instance to apply automatic sizing to.
@param options { axis: Enum.AutomaticSize, maxSize: Vector2 | UDim2, minSize: Vector2 } | nil
@tag utilities
Applies padding-aware automatic size to the given GUI instance. This function sets up events to listen to further changes, so
should only be called once per object.
Also supports ScrollingFrames by correctly clamping actual and canvas sizes.
:::note
If this function is called from the server, if `maxSize` is a UDim2, because of differing screen sizes, it instead
configures the instance to be compatible with the [Plasma.hydrateAutomaticSize] function, adding the
CollectionService tag and other attributes.
You must also call `hydrateAutomaticSize` once on the client for this to work.
:::
::warning
There is currently no way to undo this other than destroying the instance. Once automatic sizing has been applied,
it is always applied to that instance.
:::
]=]
local function automaticSize(container, options)
options = options or defaultOptions
if options.maxSize then
container:SetAttribute("maxSize", options.maxSize)
end
if options.minSize then
container:SetAttribute("minSize", options.minSize)
end
if options.axis then
container:SetAttribute("axis", options.axis.Name)
end
if not RunService:IsClient() and typeof(container:GetAttribute("maxSize") or nil) == "UDim2" then
CollectionService:AddTag(container, "PlasmaAutomaticSize")
return
end
local layout = container:FindFirstChildWhichIsA("UIGridStyleLayout")
applyLayout(container, layout)
if typeof(container:GetAttribute("maxSize") or nil) == "UDim2" then
trackParentSize(container, function()
applyLayout(container, layout)
end)
end
if layout then
layout:GetPropertyChangedSignal("AbsoluteContentSize"):Connect(function()
applyLayout(container, layout)
end)
elseif container:IsA("TextLabel") or container:IsA("TextButton") then
container:GetPropertyChangedSignal("TextBounds"):Connect(function()
applyLayout(container)
end)
else
local function connect(child)
if child:IsA("GuiObject") then
child:GetPropertyChangedSignal("Size"):Connect(function()
applyLayout(container)
end)
end
end
for _, child in container:GetChildren() do
connect(child)
end
container.ChildAdded:Connect(function(child)
applyLayout(container)
connect(child)
end)
container.ChildRemoved:Connect(function()
applyLayout(container)
end)
end
container:GetAttributeChangedSignal("maxSize"):Connect(function()
applyLayout(container, layout)
end)
container:GetAttributeChangedSignal("minSize"):Connect(function()
applyLayout(container, layout)
end)
end
return automaticSize