This commit is contained in:
2025-09-15 23:32:24 -04:00
parent a63854b686
commit 180eae2b12

View File

@@ -567,67 +567,67 @@ function Window:layoutChildren()
end end
end end
-- Position children -- Position children
local currentPos = spacing local currentPos = spacing
for _, child in ipairs(self.children) do for _, child in ipairs(self.children) do
if child.positioning == Positioning.ABSOLUTE then if child.positioning == Positioning.ABSOLUTE then
goto continue goto continue
end end
if self.flexDirection == FlexDirection.VERTICAL then if self.flexDirection == FlexDirection.VERTICAL then
child.x = currentPos child.x = currentPos + self.mx
child.y = 0 child.y = 0
-- Apply alignment to vertical axis (alignItems) -- Apply alignment to vertical axis (alignItems)
if self.alignItems == AlignItems.FLEX_START then if self.alignItems == AlignItems.FLEX_START then
--nothing, currentPos is all --nothing, currentPos is all
elseif self.alignItems == AlignItems.CENTER then elseif self.alignItems == AlignItems.CENTER then
child.y = (self.height - (child.height or 0)) / 2 child.y = (self.height - (child.height or 0)) / 2
elseif self.alignItems == AlignItems.FLEX_END then elseif self.alignItems == AlignItems.FLEX_END then
child.y = self.height - (child.height or 0) child.y = self.height - (child.height or 0)
elseif self.alignItems == AlignItems.STRETCH then elseif self.alignItems == AlignItems.STRETCH then
child.height = self.height child.height = self.height
end end
-- Apply self alignment to vertical axis (alignSelf) -- Apply self alignment to vertical axis (alignSelf)
if child.alignSelf == AlignSelf.FLEX_START then if child.alignSelf == AlignSelf.FLEX_START then
--nothing, currentPos is all --nothing, currentPos is all
elseif child.alignSelf == AlignSelf.CENTER then elseif child.alignSelf == AlignSelf.CENTER then
child.y = (self.height - (child.height or 0)) / 2 child.y = (self.height - (child.height or 0)) / 2
elseif child.alignSelf == AlignSelf.FLEX_END then elseif child.alignSelf == AlignSelf.FLEX_END then
child.y = self.height - (child.height or 0) child.y = self.height - (child.height or 0)
elseif child.alignSelf == AlignSelf.STRETCH then elseif child.alignSelf == AlignSelf.STRETCH then
child.height = self.height child.height = self.height
end end
currentPos = currentPos + (child.width or 0) + self.gap currentPos = currentPos + (child.width or 0) + self.gap + self.mx * 2
else else
child.y = currentPos child.y = currentPos + self.my
-- Apply alignment to horizontal axis (alignItems) -- Apply alignment to horizontal axis (alignItems)
if self.alignItems == AlignItems.FLEX_START then if self.alignItems == AlignItems.FLEX_START then
--nothing, currentPos is all --nothing, currentPos is all
elseif self.alignItems == AlignItems.CENTER then elseif self.alignItems == AlignItems.CENTER then
child.x = (self.width - (child.width or 0)) / 2 child.x = (self.width - (child.width or 0)) / 2
elseif self.alignItems == AlignItems.FLEX_END then elseif self.alignItems == AlignItems.FLEX_END then
child.x = self.width - (child.width or 0) child.x = self.width - (child.width or 0)
elseif self.alignItems == AlignItems.STRETCH then elseif self.alignItems == AlignItems.STRETCH then
child.width = self.width child.width = self.width
end end
-- Apply self alignment to horizontal axis (alignSelf) -- Apply self alignment to horizontal axis (alignSelf)
if child.alignSelf == AlignSelf.FLEX_START then if child.alignSelf == AlignSelf.FLEX_START then
--nothing, currentPos is all --nothing, currentPos is all
elseif child.alignSelf == AlignSelf.CENTER then elseif child.alignSelf == AlignSelf.CENTER then
child.x = (self.width - (child.width or 0)) / 2 child.x = (self.width - (child.width or 0)) / 2
elseif child.alignSelf == AlignSelf.FLEX_END then elseif child.alignSelf == AlignSelf.FLEX_END then
child.x = self.width - (child.width or 0) child.x = self.width - (child.width or 0)
elseif child.alignSelf == AlignSelf.STRETCH then elseif child.alignSelf == AlignSelf.STRETCH then
child.width = self.width child.width = self.width
end end
currentPos = currentPos + (child.height or 0) + self.gap currentPos = currentPos + (child.height or 0) + self.gap + self.my * 2
end end
::continue:: ::continue::
end end
end end
--- Destroy window and its children --- Destroy window and its children
@@ -794,53 +794,54 @@ function Window:resize(newGameWidth, newGameHeight)
end end
--- Calculate auto width based on children --- Calculate auto width based on children
function Window:calculateAutoWidth() function Window:calculateAutoWidth()
if self.autosizing == false then if self.autosizing == false then
return return
end end
if not self.children or #self.children == 0 then if not self.children or #self.children == 0 then
self.width = 0 self.width = 0
end end
Logger:debug("children count: " .. #self.children) Logger:debug("children count: " .. #self.children)
local maxWidth = 0 local maxWidth = 0
for _, child in ipairs(self.children) do for _, child in ipairs(self.children) do
local childWidth = child.width or 0 local childWidth = child.width or 0
local childX = child.x or 0 local childX = child.x or 0
local paddingAdjustment = child.px * 2 local paddingAdjustment = child.px * 2
local totalWidth = childX + childWidth + paddingAdjustment local totalWidth = childX + childWidth + paddingAdjustment
if totalWidth > maxWidth then if totalWidth > maxWidth then
maxWidth = totalWidth maxWidth = totalWidth
end end
end end
self.width = maxWidth + (self.px * 2) -- Add window's own px padding and mx margins to the final width
self.width = maxWidth + (self.px * 2) + (self.mx * 2)
end end
--- Calculate auto height based on children --- Calculate auto height based on children
function Window:calculateAutoHeight() function Window:calculateAutoHeight()
if self.autosizing == false then if self.autosizing == false then
return return
end end
if not self.children or #self.children == 0 then if not self.children or #self.children == 0 then
self.height = 0 self.height = 0
end end
local maxHeight = 0 local maxHeight = 0
for _, child in ipairs(self.children) do for _, child in ipairs(self.children) do
local childHeight = child.height or 0 local childHeight = child.height or 0
local childY = child.y or 0 local childY = child.y or 0
local paddingAdjustment = child.py * 2 local paddingAdjustment = child.py * 2
local totalHeight = childY + childHeight + paddingAdjustment local totalHeight = childY + childHeight + paddingAdjustment
if totalHeight > maxHeight then if totalHeight > maxHeight then
maxHeight = totalHeight maxHeight = totalHeight
end end
end end
-- Add window's own py padding to the final height -- Add window's own py padding and my margins to the final height
self.height = maxHeight + (self.py * 2) self.height = maxHeight + (self.py * 2) + (self.my * 2)
end end
--- Update window size to fit children automatically --- Update window size to fit children automatically
@@ -1097,18 +1098,18 @@ function Button:resize(ratioW, ratioH)
end end
---@param newText string ---@param newText string
---@param autoresize boolean? --default: false ---@param autoresize boolean? --default: false
function Button:updateText(newText, autoresize) function Button:updateText(newText, autoresize)
self.text = newText or self.text self.text = newText or self.text
if autoresize then if autoresize then
self.width = self:calculateTextWidth() + self.px self.width = self:calculateTextWidth() + (self.px * 2)
self.height = self:calculateTextHeight() + self.py self.height = self:calculateTextHeight() + (self.py * 2)
end end
-- If autosizing is enabled, recalculate size after text update -- If autosizing is enabled, recalculate size after text update
if self.autosizing then if self.autosizing then
self:autosize() self:autosize()
end end
end end
function Button:draw() function Button:draw()