Added generic layout class and HBox.

This commit is contained in:
2025-10-11 22:54:09 -04:00
parent 3f821d4258
commit 7cfffde56d
6 changed files with 212 additions and 79 deletions

62
src/ui/hbox.lua Normal file
View File

@@ -0,0 +1,62 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local make_class = require 'src.utils.classes'
local Layout = require 'src.ui.layout'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local HBox = make_class(Layout)
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
function HBox:_init(x, y, w, h, spacing)
Layout._init(self, x, y, w, h, spacing)
end
function HBox:load()
Layout.load(self)
local _x = self.x
-- Load required assets if needed and then compute the coordinates of each button.
for _, v in pairs(self.elements) do
v.y = self.y
v.x = _x
_x = _x + v.w + self.s
end
end
function HBox:set_dimensions()
if self.w == nil then
self.w = 0
for i, v in pairs(self.elements) do
self.w = self.w + v.w
if i < #self.elements then self.w = self.w + self.s end
end
end
if self.h == nil then
self.h = 0
for _, v in pairs(self.elements) do
if v.h > self.h then self.h = v.h end
end
end
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return HBox