alignment with css
This commit is contained in:
82
FlexLove.lua
82
FlexLove.lua
@@ -28,14 +28,14 @@ end
|
|||||||
function Color.fromHex(hexWithTag)
|
function Color.fromHex(hexWithTag)
|
||||||
local hex = hexWithTag:gsub("#", "")
|
local hex = hexWithTag:gsub("#", "")
|
||||||
if #hex == 6 then
|
if #hex == 6 then
|
||||||
local r = tonumber("0x" .. hex:sub(1, 2))
|
local r = tonumber("0x" .. hex:sub(1, 2)) or 0
|
||||||
local g = tonumber("0x" .. hex:sub(3, 4))
|
local g = tonumber("0x" .. hex:sub(3, 4)) or 0
|
||||||
local b = tonumber("0x" .. hex:sub(5, 6))
|
local b = tonumber("0x" .. hex:sub(5, 6)) or 0
|
||||||
return Color.new(r, g, b, 1)
|
return Color.new(r, g, b, 1)
|
||||||
elseif #hex == 8 then
|
elseif #hex == 8 then
|
||||||
local r = tonumber("0x" .. hex:sub(1, 2))
|
local r = tonumber("0x" .. hex:sub(1, 2)) or 0
|
||||||
local g = tonumber("0x" .. hex:sub(3, 4))
|
local g = tonumber("0x" .. hex:sub(3, 4)) or 0
|
||||||
local b = tonumber("0x" .. hex:sub(5, 6))
|
local b = tonumber("0x" .. hex:sub(5, 6)) or 0
|
||||||
local a = tonumber("0x" .. hex:sub(7, 8)) / 255
|
local a = tonumber("0x" .. hex:sub(7, 8)) / 255
|
||||||
return Color.new(r, g, b, a)
|
return Color.new(r, g, b, a)
|
||||||
else
|
else
|
||||||
@@ -43,20 +43,6 @@ function Color.fromHex(hexWithTag)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Convert color to hex string
|
|
||||||
---@return string
|
|
||||||
function Color:toHex()
|
|
||||||
local r = math.floor(self.r * 255)
|
|
||||||
local g = math.floor(self.g * 255)
|
|
||||||
local b = math.floor(self.b * 255)
|
|
||||||
local a = math.floor(self.a * 255)
|
|
||||||
if self.a ~= 1 then
|
|
||||||
return string.format("#%02X%02X%02X%02X", r, g, b, a)
|
|
||||||
else
|
|
||||||
return string.format("#%02X%02X%02X", r, g, b)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@return number r, number g, number b, number a
|
---@return number r, number g, number b, number a
|
||||||
function Color:toRGBA()
|
function Color:toRGBA()
|
||||||
return self.r, self.g, self.b, self.a
|
return self.r, self.g, self.b, self.a
|
||||||
@@ -487,14 +473,14 @@ function Element.new(props)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.positioning == Positioning.FLEX then
|
if self.positioning == Positioning.FLEX then
|
||||||
self.flexDirection = props.flexDirection or FlexDirection.HORIZONTAL
|
self.flexDirection = props.flexDirection or FlexDirection.HORIZONTAL
|
||||||
self.justifyContent = props.justifyContent or JustifyContent.FLEX_START
|
self.justifyContent = props.justifyContent or JustifyContent.FLEX_START
|
||||||
self.alignItems = props.alignItems or AlignItems.STRETCH
|
self.alignItems = props.alignItems or AlignItems.STRETCH
|
||||||
self.alignContent = props.alignContent or AlignContent.STRETCH
|
self.alignContent = props.alignContent or AlignContent.STRETCH
|
||||||
self.justifySelf = props.justifySelf or AlignSelf.AUTO
|
self.justifySelf = props.justifySelf or AlignSelf.AUTO
|
||||||
self.alignSelf = props.alignSelf or AlignSelf.AUTO
|
self.alignSelf = props.alignSelf or AlignSelf.AUTO
|
||||||
end
|
end
|
||||||
|
|
||||||
self.autosizing = { width = false, height = false }
|
self.autosizing = { width = false, height = false }
|
||||||
|
|
||||||
@@ -514,8 +500,11 @@ if self.positioning == Positioning.FLEX then
|
|||||||
|
|
||||||
self.parent = props.parent
|
self.parent = props.parent
|
||||||
if self.parent then
|
if self.parent then
|
||||||
self.x = self.x + self.parent.x
|
-- Only add parent position to child coordinates if parent is not absolutely positioned
|
||||||
self.y = self.y + self.parent.y
|
if self.parent.positioning ~= Positioning.ABSOLUTE then
|
||||||
|
self.x = self.x + self.parent.x
|
||||||
|
self.y = self.y + self.parent.y
|
||||||
|
end
|
||||||
end
|
end
|
||||||
self.children = {}
|
self.children = {}
|
||||||
if props.parent then
|
if props.parent then
|
||||||
@@ -565,6 +554,7 @@ end
|
|||||||
|
|
||||||
function Element:layoutChildren()
|
function Element:layoutChildren()
|
||||||
if self.positioning == Positioning.ABSOLUTE then
|
if self.positioning == Positioning.ABSOLUTE then
|
||||||
|
-- Absolute positioned containers don't layout their children according to flex rules
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -614,6 +604,7 @@ function Element:layoutChildren()
|
|||||||
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
|
||||||
|
-- Skip positioning for absolute children as they should maintain their own coordinates
|
||||||
goto continue
|
goto continue
|
||||||
end
|
end
|
||||||
if self.flexDirection == FlexDirection.VERTICAL then
|
if self.flexDirection == FlexDirection.VERTICAL then
|
||||||
@@ -636,9 +627,11 @@ function Element:layoutChildren()
|
|||||||
if child.alignSelf == AlignSelf.AUTO then
|
if child.alignSelf == AlignSelf.AUTO then
|
||||||
effectiveAlignSelf = self.alignItems
|
effectiveAlignSelf = self.alignItems
|
||||||
end
|
end
|
||||||
|
|
||||||
if effectiveAlignSelf == AlignSelf.FLEX_START then
|
if effectiveAlignSelf == AlignSelf.FLEX_START then
|
||||||
--nothing, currentPos is all
|
-- nothing, currentPos is all - position should be at the beginning of cross axis
|
||||||
|
-- For VERTICAL flex, this means X = 0
|
||||||
|
child.x = 0
|
||||||
elseif effectiveAlignSelf == AlignSelf.CENTER then
|
elseif effectiveAlignSelf == AlignSelf.CENTER then
|
||||||
if self.flexDirection == FlexDirection.VERTICAL then
|
if self.flexDirection == FlexDirection.VERTICAL then
|
||||||
child.x = (self.width - (child.width or 0)) / 2
|
child.x = (self.width - (child.width or 0)) / 2
|
||||||
@@ -663,7 +656,7 @@ function Element:layoutChildren()
|
|||||||
else
|
else
|
||||||
child.x = currentPos + (self.margin.left or 0)
|
child.x = currentPos + (self.margin.left or 0)
|
||||||
child.y = self.margin.top or 0
|
child.y = self.margin.top or 0
|
||||||
|
|
||||||
-- 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
|
||||||
@@ -677,7 +670,9 @@ function Element:layoutChildren()
|
|||||||
|
|
||||||
-- 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 - position should be at the beginning of cross axis
|
||||||
|
-- For HORIZONTAL flex, this means Y = 0
|
||||||
|
child.y = 0
|
||||||
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
|
||||||
@@ -898,16 +893,20 @@ function Element:resize(newGameWidth, newGameHeight)
|
|||||||
local ratioW = newGameWidth / prevW
|
local ratioW = newGameWidth / prevW
|
||||||
local ratioH = newGameHeight / prevH
|
local ratioH = newGameHeight / prevH
|
||||||
-- Update element size
|
-- Update element size
|
||||||
self.width = self.width * ratioW
|
if self.positioning ~= Positioning.ABSOLUTE then
|
||||||
self.height = self.height * ratioH
|
self.width = self.width * ratioW
|
||||||
self.x = self.x * ratioW
|
self.height = self.height * ratioH
|
||||||
self.y = self.y * ratioH
|
self.x = self.x * ratioW
|
||||||
|
self.y = self.y * ratioH
|
||||||
|
end
|
||||||
-- Update children positions and sizes
|
-- Update children positions and sizes
|
||||||
for _, child in ipairs(self.children) do
|
for _, child in ipairs(self.children) do
|
||||||
child:resize(ratioW, ratioH)
|
child:resize(ratioW, ratioH)
|
||||||
end
|
end
|
||||||
-- Re-layout children after resizing
|
-- Re-layout children after resizing (only for non-absolute containers)
|
||||||
self:layoutChildren()
|
if self.positioning ~= Positioning.ABSOLUTE then
|
||||||
|
self:layoutChildren()
|
||||||
|
end
|
||||||
self.prevGameSize.width = newGameWidth
|
self.prevGameSize.width = newGameWidth
|
||||||
self.prevGameSize.height = newGameHeight
|
self.prevGameSize.height = newGameHeight
|
||||||
end
|
end
|
||||||
@@ -984,7 +983,8 @@ end
|
|||||||
function Element:updateText(newText, autoresize)
|
function Element:updateText(newText, autoresize)
|
||||||
self.text = newText or self.text
|
self.text = newText or self.text
|
||||||
if autoresize then
|
if autoresize then
|
||||||
Logger:error("need to implement resize in updateText")
|
self.width = self:calculateTextWidth()
|
||||||
|
self.height = self:calculateTextHeight()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user