This commit is contained in:
Michael Freno
2025-11-13 21:01:04 -05:00
parent 7e69e7f544
commit 9da8d589a1
2 changed files with 37 additions and 31 deletions

View File

@@ -521,6 +521,28 @@ function Element.new(props)
self.prevGameSize = { width = gw, height = gh } self.prevGameSize = { width = gw, height = gh }
self.autosizing = { width = false, height = false } self.autosizing = { width = false, height = false }
-- Initialize LayoutEngine early with default values for auto-sizing calculations
-- It will be re-configured later with actual layout properties
self._layoutEngine = LayoutEngine.new({
positioning = Positioning.RELATIVE,
flexDirection = FlexDirection.HORIZONTAL,
flexWrap = FlexWrap.NOWRAP,
justifyContent = JustifyContent.FLEX_START,
alignItems = AlignItems.STRETCH,
alignContent = AlignContent.STRETCH,
gap = 0,
gridRows = 1,
gridColumns = 1,
columnGap = 0,
rowGap = 0,
}, {
utils = utils,
Grid = Grid,
Units = Units,
Context = Context,
})
self._layoutEngine:initialize(self)
-- Store unit specifications for responsive behavior -- Store unit specifications for responsive behavior
self.units = { self.units = {
width = { value = nil, unit = "px" }, width = { value = nil, unit = "px" },
@@ -1219,27 +1241,19 @@ function Element.new(props)
self.alignSelf = props.alignSelf or AlignSelf.AUTO self.alignSelf = props.alignSelf or AlignSelf.AUTO
-- Initialize LayoutEngine for layout calculations -- Update the LayoutEngine with actual layout properties
self._layoutEngine = LayoutEngine.new({ -- (it was initialized early with defaults for auto-sizing calculations)
positioning = self.positioning, self._layoutEngine.positioning = self.positioning
flexDirection = self.flexDirection, if self.flexDirection then self._layoutEngine.flexDirection = self.flexDirection end
flexWrap = self.flexWrap, if self.flexWrap then self._layoutEngine.flexWrap = self.flexWrap end
justifyContent = self.justifyContent, if self.justifyContent then self._layoutEngine.justifyContent = self.justifyContent end
alignItems = self.alignItems, if self.alignItems then self._layoutEngine.alignItems = self.alignItems end
alignContent = self.alignContent, if self.alignContent then self._layoutEngine.alignContent = self.alignContent end
gap = self.gap, if self.gap then self._layoutEngine.gap = self.gap end
gridRows = self.gridRows, if self.gridRows then self._layoutEngine.gridRows = self.gridRows end
gridColumns = self.gridColumns, if self.gridColumns then self._layoutEngine.gridColumns = self.gridColumns end
columnGap = self.columnGap, if self.columnGap then self._layoutEngine.columnGap = self.columnGap end
rowGap = self.rowGap, if self.rowGap then self._layoutEngine.rowGap = self.rowGap end
}, {
utils = utils,
Grid = Grid,
Units = Units,
Context = Context,
})
-- Initialize immediately so it can be used for auto-sizing calculations
self._layoutEngine:initialize(self)
---animation ---animation
self.transform = props.transform or {} self.transform = props.transform or {}
@@ -2094,15 +2108,11 @@ function Element:update(dt)
end end
end end
--- Recalculate units based on new viewport dimensions (for vw, vh, % units)
---@param newViewportWidth number ---@param newViewportWidth number
---@param newViewportHeight number ---@param newViewportHeight number
function Element:recalculateUnits(newViewportWidth, newViewportHeight) function Element:recalculateUnits(newViewportWidth, newViewportHeight)
-- Delegate to LayoutEngine
if self._layoutEngine then
self._layoutEngine:recalculateUnits(newViewportWidth, newViewportHeight) self._layoutEngine:recalculateUnits(newViewportWidth, newViewportHeight)
end end
end
--- Resize element and its children based on game window size change --- Resize element and its children based on game window size change
---@param newGameWidth number ---@param newGameWidth number
@@ -2272,10 +2282,6 @@ end
--- Calculate auto height based on children --- Calculate auto height based on children
function Element:calculateAutoHeight() function Element:calculateAutoHeight()
if not self._layoutEngine then
return self:calculateTextHeight()
end
-- Delegate to LayoutEngine
return self._layoutEngine:calculateAutoHeight() return self._layoutEngine:calculateAutoHeight()
end end

View File

@@ -443,7 +443,7 @@ function LayoutEngine:layoutChildren()
-- Horizontal layout: main axis is X, cross axis is Y -- Horizontal layout: main axis is X, cross axis is Y
-- Position child at border box (x, y represents top-left including padding) -- Position child at border box (x, y represents top-left including padding)
-- Add reservedMainStart and left margin to account for absolutely positioned siblings and margins -- Add reservedMainStart and left margin to account for absolutely positioned siblings and margins
child.x = element.x + element.padding.left + reservedMainStart + currentMainPos + child.margin.left child.x = self.element.x + self.element.padding.left + reservedMainStart + currentMainPos + child.margin.left
-- BORDER-BOX MODEL: Use border-box dimensions for alignment calculations -- BORDER-BOX MODEL: Use border-box dimensions for alignment calculations
local childBorderBoxHeight = child:getBorderBoxHeight() local childBorderBoxHeight = child:getBorderBoxHeight()