Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions lib/instances/UIGridStyleLayout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,40 @@ local FillDirection = import("../Enum/FillDirection")
local HorizontalAlignment = import("../Enum/HorizontalAlignment")
local InstanceProperty = import("../InstanceProperty")
local SortOrder = import("../Enum/SortOrder")
local VerticalAlignment = import("../Enum/VerticalAlignment")
local Vector2 = import("../types/Vector2")
local VerticalAlignment = import("../Enum/VerticalAlignment")

local UIGridStyleLayout = BaseInstance:extend("UIGridStyleLayout")

UIGridStyleLayout.properties.AbsoluteContentSize = InstanceProperty.readOnly({
get = function(self)
local x = 0
local y = 0
if self:FindFirstAncestorWhichIsA("ScreenGui") then
-- Sum the size of the children in the fill direction and get
-- the maximum size of the children in the other direction.
for _, child in pairs(self.Parent:GetChildren()) do
if child:IsA("GuiObject") then
if self.FillDirection == FillDirection.Horizontal then
x = x + child.AbsoluteSize.X
local childHeight = child.AbsoluteSize.Y
if childHeight > y then
y = childHeight
end
else
y = y + child.AbsoluteSize.Y
local childWidth = child.AbsoluteSize.X
if childWidth > x then
x = childWidth
end
end
end
end
end
return Vector2.new(x, y)
end,
})

UIGridStyleLayout.properties.FillDirection = InstanceProperty.enum(FillDirection, {
getDefault = function()
return FillDirection.Horizontal
Expand All @@ -32,10 +61,4 @@ UIGridStyleLayout.properties.VerticalAlignment = InstanceProperty.enum(VerticalA
end,
})

UIGridStyleLayout.properties.AbsoluteContentSize = InstanceProperty.readOnly({
get = function(self)
return Vector2.new(0, 0)
end,
})

return UIGridStyleLayout
30 changes: 30 additions & 0 deletions lib/instances/UIGridStyleLayout_spec.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
local FillDirection = import("../Enum/FillDirection")
local HorizontalAlignment = import("../Enum/HorizontalAlignment")
local Instance = import("../Instance")
local SortOrder = import("../Enum/SortOrder")
local typeof = import("../functions/typeof")
local UDim2 = import("../types/UDim2")
local VerticalAlignment = import("../Enum/VerticalAlignment")

local UIGridStyleLayout = import("./UIGridStyleLayout")

local function extractVector2(vector2)
return { vector2.X, vector2.Y }
end

describe("instances.UIGridStyleLayout", function()
it("should instantiate", function()
local instance = UIGridStyleLayout:new()
Expand All @@ -16,6 +22,7 @@ describe("instances.UIGridStyleLayout", function()
it("should have properties defined", function()
local instance = UIGridStyleLayout:new()

assert.equals(typeof(instance.AbsoluteContentSize), "Vector2")
assert.equals(typeof(instance.FillDirection), "EnumItem")
assert.equals(instance.FillDirection.EnumType, FillDirection)
assert.equals(typeof(instance.HorizontalAlignment), "EnumItem")
Expand All @@ -26,4 +33,27 @@ describe("instances.UIGridStyleLayout", function()
assert.equal(instance.VerticalAlignment.EnumType, VerticalAlignment)
assert.equals(typeof(instance.AbsoluteContentSize), "Vector2")
end)

describe("AbsoluteContentSize", function()
it("should change based on the sizes of the layout's siblings", function()
local screenGui = Instance.new("ScreenGui")

local instance = UIGridStyleLayout:new()
instance.Parent = screenGui

local frame1 = Instance.new("Frame")
frame1.Size = UDim2.new(0, 300, 0, 400)
frame1.Parent = screenGui

local frame2 = Instance.new("Frame")
frame2.Size = UDim2.new(0, 200, 0, 500)
frame2.Parent = screenGui

local frame3 = Instance.new("Frame")
frame3.Size = UDim2.new(0, 100, 0, 100)
frame3.Parent = screenGui

assert.same({ 600, 500 }, extractVector2(instance.AbsoluteContentSize))
end)
end)
end)
31 changes: 31 additions & 0 deletions lib/instances/UIListLayout.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local UIGridStyleLayout = import("./UIGridStyleLayout")
local FillDirection = import("../Enum/FillDirection")
local InstanceProperty = import("../InstanceProperty")
local UDim = import("../types/UDim")
local Vector2 = import("../types/Vector2")

local UIListLayout = UIGridStyleLayout:extend("UIListLayout", {
creatable = true,
Expand All @@ -12,4 +14,33 @@ UIListLayout.properties.Padding = InstanceProperty.typed("UDim", {
end,
})

UIListLayout.properties.AbsoluteContentSize = InstanceProperty.readOnly({
get = function(self)
local size = UIGridStyleLayout.properties.AbsoluteContentSize.get(self)
if self.Padding ~= UDim.new(0, 0) and self:FindFirstAncestorWhichIsA("ScreenGui") then
local siblingCount = 0
for _, child in pairs(self.Parent:GetChildren()) do
if child:IsA("GuiObject") then
siblingCount = siblingCount + 1
end
end
if siblingCount > 0 then
local function getPadding(parentSizeInFillDirection)
return (siblingCount - 1) * (parentSizeInFillDirection * self.Padding.Scale + self.Padding.Offset)
end
local parentSize = self.Parent.AbsoluteSize
local x = size.X
local y = size.Y
if self.FillDirection == FillDirection.Horizontal then
x = x + getPadding(parentSize.X)
else
y = y + getPadding(parentSize.Y)
end
size = Vector2.new(x, y)
end
end
return size
end,
})

return UIListLayout
55 changes: 55 additions & 0 deletions lib/instances/UIListLayout_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
local FillDirection = import("../Enum/FillDirection")
local Instance = import("../Instance")
local typeof = import("../functions/typeof")
local UDim = import("../types/UDim")
local UDim2 = import("../types/UDim2")

local function extractVector2(vector2)
return { vector2.X, vector2.Y }
end

describe("instances.UIListLayout", function()
it("should instantiate", function()
Expand All @@ -19,4 +26,52 @@ describe("instances.UIListLayout", function()

assert.equals(typeof(instance.Padding), "UDim")
end)

describe("AbsoluteContentSize", function()
it("should include padding vertically", function()
local screenGui = Instance.new("ScreenGui")

local instance = Instance.new("UIListLayout")
instance.Padding = UDim.new(0, 10)
instance.FillDirection = FillDirection.Vertical
instance.Parent = screenGui

local frame1 = Instance.new("Frame")
frame1.Size = UDim2.new(0, 300, 0, 400)
frame1.Parent = screenGui

local frame2 = Instance.new("Frame")
frame2.Size = UDim2.new(0, 200, 0, 500)
frame2.Parent = screenGui

local frame3 = Instance.new("Frame")
frame3.Size = UDim2.new(0, 100, 0, 100)
frame3.Parent = screenGui

assert.same({ 300, 1020 }, extractVector2(instance.AbsoluteContentSize))
end)

it("should include padding horizonally", function()
local screenGui = Instance.new("ScreenGui")

local instance = Instance.new("UIListLayout")
instance.Padding = UDim.new(0, 10)
instance.FillDirection = FillDirection.Horizontal
instance.Parent = screenGui

local frame1 = Instance.new("Frame")
frame1.Size = UDim2.new(0, 300, 0, 400)
frame1.Parent = screenGui

local frame2 = Instance.new("Frame")
frame2.Size = UDim2.new(0, 200, 0, 500)
frame2.Parent = screenGui

local frame3 = Instance.new("Frame")
frame3.Size = UDim2.new(0, 100, 0, 100)
frame3.Parent = screenGui

assert.same({ 620, 500 }, extractVector2(instance.AbsoluteContentSize))
end)
end)
end)