-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
69 lines (57 loc) · 2.62 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
local LayoutController = {}
LayoutController.__index = LayoutController
local INVALID_ARG = [[bad argument #%d to 'LayoutUtil' (%s expected, got %s)]]
local camera = workspace.CurrentCamera
local function toOffset(udim, parentOffset)
return (udim.Scale * parentOffset) + udim.Offset
end
local function absoluteSizeFromUDim2(childUDim2, parentAbsoluteSize)
return Vector2.new(toOffset(childUDim2.X, parentAbsoluteSize.X), toOffset(childUDim2.Y, parentAbsoluteSize.Y))
end
local function addConstraint(object, absoluteSize)
local constraint = object:FindFirstChildOfClass("UIAspectRatioConstraint")
or Instance.new("UIAspectRatioConstraint")
constraint.AspectRatio = absoluteSize.X / absoluteSize.Y
constraint.Parent = object
end
--[=[
Automatically inserts and calculates UIAspectRatioConstraints into the corresponding UIGridLayout/UIListLayout. The
parent parameter is optional, but should be explicitly provided if the layout hasn't been parented. In order to
calculate the AspectRatio, I need the size of the UILayout (or children of a UIListLayout) in offset. Therefor if the
parent isn't a GuiObject or a Vector2, it will assume the parent size is the screens resolution.
@param {UIGridLayout | UIListLayout} layout The UILayout to be applied.
@param {GuiObject | Instance | Vector2} [parentObjectOrSize] The object (or Vector2) to be recognized as the parent
in order to retrieve the AbsoluteSize (non-GuiObjects or nil, defaults to the screens resolution).
]=]
function LayoutController.new()
local self = setmetatable({}, LayoutController)
return self
end
function LayoutController:FixLayout(layout: UILayout, parentObjectOrSize: ScrollingFrame)
local className = typeof(layout) == "Instance" and layout.ClassName
assert(
className == "UIGridLayout" or className == "UIListLayout",
INVALID_ARG:format(1, "UIGridLayout or UIListLayout", className)
)
local parent = parentObjectOrSize or layout.Parent
local parentAbsoluteSize
if parent:IsA("GuiObject") then
parentAbsoluteSize = parent.AbsoluteSize
elseif typeof(parentObjectOrSize) == "Vector2" then
parentAbsoluteSize = parentObjectOrSize
else
parentAbsoluteSize = camera.ViewportSize
end
parent.CanvasSize = UDim2.new(0, layout.AbsoluteContentSize.X, 0, layout.AbsoluteContentSize.Y)
if layout.ClassName == "UIGridLayout" then
addConstraint(layout, absoluteSizeFromUDim2(layout.CellSize, parentAbsoluteSize))
else -- UIListLayout
for _, child in ipairs(parent:GetChildren()) do
if child:IsA("GuiObject") then
addConstraint(child, absoluteSizeFromUDim2(child.Size, parentAbsoluteSize))
end
end
end
end
local singleton = LayoutController.new()
return singleton