helium/layout/container.lua
TotoEnF5 fc2d5c862a fix uninitialized variables in container layout
variables `x` and `y` shadowed the parameters, and `containerX`, `containerY`,
`containerWidth` and `containerHeight` were used without being initialized.
they could be replaced by the parameters `width` and `height`
2025-03-25 14:25:33 +01:00

75 lines
1.5 KiB
Lua

local path = string.sub(..., 1, string.len(...) - string.len(".container"))
local layout = require(path .. '.layout')
---@class Container
local container = {}
container.__index = container
---Positions an element within a container
---@param halign "'left'"|"'center'"|"'right'"|"'stretch'"
---@param valign "'top'"|"'center'"|"'bottom'"|"'stretch'"
---@return layout
function container.new(halign, valign)
local self = setmetatable({
halign = halign or 'left',
valign = valign or 'top',
}, container)
return layout(self, self.draw)
end
local function alignLeft(x, wroot, wchild)
return x
end
local function alignCenter(x, wroot, wchild)
return x + (wroot / 2 - wchild / 2)
end
local function alignRight(x, wroot, wchild)
return x + (wroot - wchild)
end
local function alignHandlerX(mode, x, wr, wc)
if mode == 'center' then
return alignCenter(x, wr, wc)
elseif mode == 'right' then
return alignRight(x, wr, wc)
else
return alignLeft(x)
end
end
local function alignHandlerY(mode, y, hr, hc)
if mode == 'center' then
return alignCenter(y, hr, hc)
elseif mode == 'bottom' then
return alignRight(y, hr, hc)
else
return alignLeft(y)
end
end
function container:draw(x, y, width, height, children, hpad, vpad, alignX)
local w, h = children[1]:getSize()
if self.halign == 'stretch' then
w = width
x = x
else
x = alignHandlerX(self.halign, x, width, w)
end
if self.valign == 'stretch' then
h = height
y = y
else
y = alignHandlerY(self.valign, y, height, h)
end
children[1]:draw(x, y, w, h)
end
return container