rename 'private' fields in elements
This commit is contained in:
parent
d1525539e3
commit
3d5ac077be
@ -59,13 +59,13 @@ function builder:build_step(cur)
|
|||||||
child = orphan
|
child = orphan
|
||||||
if not child then return end
|
if not child then return end
|
||||||
|
|
||||||
child.parent = cur
|
child._parent_ = cur
|
||||||
cur.child = child
|
cur.child = child
|
||||||
|
|
||||||
self:build_step(cur.child)
|
self:build_step(cur.child)
|
||||||
elseif cur.children then
|
elseif cur.children then
|
||||||
for _, child in ipairs(cur.children) do
|
for _, child in ipairs(cur.children) do
|
||||||
child.parent = cur
|
child._parent_ = cur
|
||||||
|
|
||||||
self:build_step(child)
|
self:build_step(child)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,19 +2,19 @@ local Constraints = require "lib.simple_ui.core.constraints"
|
|||||||
local Vec3 = require "lib.utils.vec3"
|
local Vec3 = require "lib.utils.vec3"
|
||||||
|
|
||||||
--- @class UIElement
|
--- @class UIElement
|
||||||
--- @field type string
|
|
||||||
--- @field key? any Must be convertible to string
|
--- @field key? any Must be convertible to string
|
||||||
--- @field parent? UIElement
|
--- @field type string
|
||||||
--- @field constraints Constraints
|
--- @field _parent_? UIElement
|
||||||
--- @field offset Vec3 Положение левого верхнего угла элемента в локальных координатах {x, y}. Устанавливается родительским элементом.
|
--- @field _constraints_ Constraints
|
||||||
--- @field size Vec3 Размеры элемента {x, y}
|
--- @field _offset_ Vec3 Положение левого верхнего угла элемента в локальных координатах {x, y}. Устанавливается родительским элементом.
|
||||||
|
--- @field _size_ Vec3 Размеры элемента {x, y}
|
||||||
--- @field build? fun(self, ctx: UIElement): UIElement
|
--- @field build? fun(self, ctx: UIElement): UIElement
|
||||||
local element = {}
|
local element = {}
|
||||||
element.__index = element
|
element.__index = element
|
||||||
element.type = "Element"
|
element.type = "Element"
|
||||||
element.constraints = Constraints {}
|
element._constraints_ = Constraints {}
|
||||||
element.offset = Vec3 {}
|
element._offset_ = Vec3 {}
|
||||||
element.size = Vec3 {}
|
element._size_ = Vec3 {}
|
||||||
|
|
||||||
--- "Constraints go down. Sizes go up. Parent sets position."
|
--- "Constraints go down. Sizes go up. Parent sets position."
|
||||||
---
|
---
|
||||||
@ -23,11 +23,7 @@ function element:layout() end
|
|||||||
|
|
||||||
function element:update(dt) end
|
function element:update(dt) end
|
||||||
|
|
||||||
function element:draw()
|
function element:draw() end
|
||||||
if self.type == "SizedBox" then
|
|
||||||
print(self.offset)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
--- @param values {[string]: any}
|
--- @param values {[string]: any}
|
||||||
--- @return UIElement
|
--- @return UIElement
|
||||||
@ -42,9 +38,9 @@ end
|
|||||||
--- Обход заканчивается, если visitor возвращает false, или если родители кончились.
|
--- Обход заканчивается, если visitor возвращает false, или если родители кончились.
|
||||||
--- @param visitor fun(element: UIElement): boolean
|
--- @param visitor fun(element: UIElement): boolean
|
||||||
function element:traverseUp(visitor)
|
function element:traverseUp(visitor)
|
||||||
if not self.parent then return end
|
if not self._parent_ then return end
|
||||||
if not visitor(self.parent) then return end
|
if not visitor(self._parent_) then return end
|
||||||
return self.parent:traverseUp(visitor)
|
return self._parent_:traverseUp(visitor)
|
||||||
end
|
end
|
||||||
|
|
||||||
return element
|
return element
|
||||||
|
|||||||
@ -14,7 +14,7 @@ end
|
|||||||
|
|
||||||
function element:draw()
|
function element:draw()
|
||||||
love.graphics.push("transform")
|
love.graphics.push("transform")
|
||||||
love.graphics.translate(self.offset.x, self.offset.y)
|
love.graphics.translate(self._offset_.x, self._offset_.y)
|
||||||
for _, child in ipairs(self.children) do
|
for _, child in ipairs(self.children) do
|
||||||
child:draw()
|
child:draw()
|
||||||
end
|
end
|
||||||
@ -22,12 +22,12 @@ function element:draw()
|
|||||||
|
|
||||||
--- @TODO: сделать дебажный метод для отрисовки границ
|
--- @TODO: сделать дебажный метод для отрисовки границ
|
||||||
love.graphics.setColor(1, 0, 0)
|
love.graphics.setColor(1, 0, 0)
|
||||||
love.graphics.line(0, 0, 0 + self.size.x, 0)
|
love.graphics.line(0, 0, 0 + self._size_.x, 0)
|
||||||
love.graphics.line(0, 0, 0, self.offset.y + 0)
|
love.graphics.line(0, 0, 0, self._offset_.y + 0)
|
||||||
love.graphics.line(0 + self.size.x, self.offset.y, 0 + self.size.x,
|
love.graphics.line(0 + self._size_.x, self._offset_.y, 0 + self._size_.x,
|
||||||
0 + self.size.y)
|
0 + self._size_.y)
|
||||||
love.graphics.line(0, 0 + self.size.y, 0 + self.size.x,
|
love.graphics.line(0, 0 + self._size_.y, 0 + self._size_.x,
|
||||||
0 + self.size.y)
|
0 + self._size_.y)
|
||||||
love.graphics.setColor(1, 1, 1)
|
love.graphics.setColor(1, 1, 1)
|
||||||
|
|
||||||
love.graphics.pop()
|
love.graphics.pop()
|
||||||
|
|||||||
@ -16,9 +16,9 @@ function element:layout()
|
|||||||
--- получить назад размеры
|
--- получить назад размеры
|
||||||
--- разместить ребенка
|
--- разместить ребенка
|
||||||
if not self.child then return end
|
if not self.child then return end
|
||||||
self.child.constraints = Constraints(self.constraints)
|
self.child._constraints_ = Constraints(self._constraints_)
|
||||||
self.child:layout()
|
self.child:layout()
|
||||||
self.child.offset = Vec3 {}
|
self.child._offset_ = Vec3 {}
|
||||||
end
|
end
|
||||||
|
|
||||||
function element:update(dt)
|
function element:update(dt)
|
||||||
@ -27,7 +27,7 @@ end
|
|||||||
|
|
||||||
function element:draw()
|
function element:draw()
|
||||||
love.graphics.push("transform")
|
love.graphics.push("transform")
|
||||||
love.graphics.translate(self.offset.x, self.offset.y)
|
love.graphics.translate(self._offset_.x, self._offset_.y)
|
||||||
if self.child then self.child:draw() end
|
if self.child then self.child:draw() end
|
||||||
love.graphics.pop()
|
love.graphics.pop()
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,18 +4,18 @@ local SingleChildElement = require "lib.simple_ui.core.single_child_element"
|
|||||||
--- @class Center : SingleChildElement
|
--- @class Center : SingleChildElement
|
||||||
local element = setmetatable({}, SingleChildElement)
|
local element = setmetatable({}, SingleChildElement)
|
||||||
element.__index = element
|
element.__index = element
|
||||||
element.__type = "Center"
|
element.type = "Center"
|
||||||
|
|
||||||
function element:layout()
|
function element:layout()
|
||||||
self.size = Vec3 { self.constraints.maxWidth, self.constraints.maxHeight }
|
self._size_ = Vec3 { self._constraints_.maxWidth, self._constraints_.maxHeight }
|
||||||
|
|
||||||
if not self.child then return end
|
if not self.child then return end
|
||||||
self.child.constraints = Constraints(self.constraints)
|
self.child._constraints_ = Constraints(self._constraints_)
|
||||||
self.child:layout()
|
self.child:layout()
|
||||||
|
|
||||||
self.child.offset = Vec3 {
|
self.child._offset_ = Vec3 {
|
||||||
(self.size.x - self.child.size.x) / 2,
|
(self._size_.x - self.child._size_.x) / 2,
|
||||||
(self.size.y - self.child.size.y) / 2,
|
(self._size_.y - self.child._size_.y) / 2,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -17,54 +17,54 @@ function element:layout()
|
|||||||
local crossAxisSize = 0
|
local crossAxisSize = 0
|
||||||
if self.direction == "horizontal" then
|
if self.direction == "horizontal" then
|
||||||
for _, child in ipairs(self.children) do
|
for _, child in ipairs(self.children) do
|
||||||
child.constraints = Constraints { maxHeight = self.constraints.maxHeight }
|
child._constraints_ = Constraints { maxHeight = self._constraints_.maxHeight }
|
||||||
child:layout()
|
child:layout()
|
||||||
if child.size.y > crossAxisSize then crossAxisSize = child.size.y end
|
if child._size_.y > crossAxisSize then crossAxisSize = child._size_.y end
|
||||||
mainAxisSize = mainAxisSize + child.size.x
|
mainAxisSize = mainAxisSize + child._size_.x
|
||||||
end
|
end
|
||||||
|
|
||||||
local start = 0
|
local start = 0
|
||||||
if self.mainAxisAlignment == "center" then
|
if self.mainAxisAlignment == "center" then
|
||||||
start = self.constraints.maxWidth / 2 - mainAxisSize / 2
|
start = self._constraints_.maxWidth / 2 - mainAxisSize / 2
|
||||||
elseif self.mainAxisAlignment == "end" then
|
elseif self.mainAxisAlignment == "end" then
|
||||||
start = self.constraints.maxWidth - mainAxisSize
|
start = self._constraints_.maxWidth - mainAxisSize
|
||||||
end
|
end
|
||||||
local shift = 0
|
local shift = 0
|
||||||
for _, child in ipairs(self.children) do
|
for _, child in ipairs(self.children) do
|
||||||
child.offset = Vec3 { self.offset.x + start + shift, self.offset.y }
|
child._offset_ = Vec3 { self._offset_.x + start + shift, self._offset_.y }
|
||||||
shift = shift + child.size.x
|
shift = shift + child._size_.x
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.mainAxisSize == "max" then
|
if self.mainAxisSize == "max" then
|
||||||
self.size = Vec3 { self.constraints.maxWidth, crossAxisSize }
|
self._size_ = Vec3 { self._constraints_.maxWidth, crossAxisSize }
|
||||||
else
|
else
|
||||||
self.size = Vec3 { mainAxisSize, crossAxisSize }
|
self._size_ = Vec3 { mainAxisSize, crossAxisSize }
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
for _, child in ipairs(self.children) do
|
for _, child in ipairs(self.children) do
|
||||||
child.constraints = Constraints { maxWidth = self.constraints.maxWidth }
|
child._constraints_ = Constraints { maxWidth = self._constraints_.maxWidth }
|
||||||
child:layout()
|
child:layout()
|
||||||
if child.size.x > crossAxisSize then crossAxisSize = child.size.x end
|
if child._size_.x > crossAxisSize then crossAxisSize = child._size_.x end
|
||||||
mainAxisSize = mainAxisSize + child.size.y
|
mainAxisSize = mainAxisSize + child._size_.y
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local start = 0
|
local start = 0
|
||||||
if self.mainAxisAlignment == "center" then
|
if self.mainAxisAlignment == "center" then
|
||||||
start = self.constraints.maxHeight / 2 - mainAxisSize / 2
|
start = self._constraints_.maxHeight / 2 - mainAxisSize / 2
|
||||||
elseif self.mainAxisAlignment == "end" then
|
elseif self.mainAxisAlignment == "end" then
|
||||||
start = self.constraints.maxHeight - mainAxisSize
|
start = self._constraints_.maxHeight - mainAxisSize
|
||||||
end
|
end
|
||||||
local shift = 0
|
local shift = 0
|
||||||
for _, child in ipairs(self.children) do
|
for _, child in ipairs(self.children) do
|
||||||
child.offset = Vec3 { self.offset.x, self.offset.y + start + shift }
|
child._offset_ = Vec3 { self._offset_.x, self._offset_.y + start + shift }
|
||||||
shift = shift + child.size.y
|
shift = shift + child._size_.y
|
||||||
end
|
end
|
||||||
|
|
||||||
if self.mainAxisSize == "max" then
|
if self.mainAxisSize == "max" then
|
||||||
self.size = Vec3 { crossAxisSize, self.constraints.maxHeight }
|
self._size_ = Vec3 { crossAxisSize, self._constraints_.maxHeight }
|
||||||
else
|
else
|
||||||
self.size = Vec3 { crossAxisSize, mainAxisSize }
|
self._size_ = Vec3 { crossAxisSize, mainAxisSize }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -20,16 +20,16 @@ element.bottom = 0
|
|||||||
--- as in https://api.flutter.dev/flutter/widgets/Padding-class.html
|
--- as in https://api.flutter.dev/flutter/widgets/Padding-class.html
|
||||||
function element:layout()
|
function element:layout()
|
||||||
if not self.child then return end
|
if not self.child then return end
|
||||||
local c = Constraints(self.constraints)
|
local c = Constraints(self._constraints_)
|
||||||
c.maxWidth = c.maxWidth - self.left - self.right
|
c.maxWidth = c.maxWidth - self.left - self.right
|
||||||
c.maxHeight = c.maxHeight - self.top - self.bottom
|
c.maxHeight = c.maxHeight - self.top - self.bottom
|
||||||
c.maxWidth = c.maxWidth > 0 and c.maxWidth or 0
|
c.maxWidth = c.maxWidth > 0 and c.maxWidth or 0
|
||||||
c.maxHeight = c.maxHeight > 0 and c.maxHeight or 0
|
c.maxHeight = c.maxHeight > 0 and c.maxHeight or 0
|
||||||
self.child.constraints = c
|
self.child._constraints_ = c
|
||||||
|
|
||||||
self.child:layout()
|
self.child:layout()
|
||||||
self.size = Vec3 { self.child.size.x + self.left + self.right, self.child.size.y + self.top + self.bottom }
|
self._size_ = Vec3 { self.child._size_.x + self.left + self.right, self.child._size_.y + self.top + self.bottom }
|
||||||
self.child.offset = Vec3 { self.left, self.top }
|
self.child._offset_ = Vec3 { self.left, self.top }
|
||||||
end
|
end
|
||||||
|
|
||||||
--- @return Padding
|
--- @return Padding
|
||||||
|
|||||||
@ -7,17 +7,19 @@ element.__index = element
|
|||||||
element.type = "Placeholder"
|
element.type = "Placeholder"
|
||||||
|
|
||||||
function element:layout()
|
function element:layout()
|
||||||
self.size = Vec3 { self.constraints.maxWidth, self.constraints.maxHeight }
|
self._size_ = Vec3 { self._constraints_.maxWidth, self._constraints_.maxHeight }
|
||||||
|
|
||||||
if not self.child then return end
|
if not self.child then return end
|
||||||
self.child.constraints = Constraints(self.constraints)
|
self.child._constraints_ = Constraints(self._constraints_)
|
||||||
self.child:layout()
|
self.child:layout()
|
||||||
end
|
end
|
||||||
|
|
||||||
function element:draw()
|
function element:draw()
|
||||||
love.graphics.rectangle("line", self.offset.x, self.offset.y, self.size.x, self.size.y)
|
love.graphics.rectangle("line", self._offset_.x, self._offset_.y, self._size_.x, self._size_.y)
|
||||||
love.graphics.line(self.offset.x, self.offset.y, self.offset.x + self.size.x, self.offset.y + self.size.y)
|
love.graphics.line(self._offset_.x, self._offset_.y, self._offset_.x + self._size_.x, self._offset_.y + self._size_
|
||||||
love.graphics.line(self.offset.x, self.offset.y + self.size.y, self.offset.x + self.size.x, self.offset.y)
|
.y)
|
||||||
|
love.graphics.line(self._offset_.x, self._offset_.y + self._size_.y, self._offset_.x + self._size_.x, self._offset_
|
||||||
|
.y)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- @return Placeholder
|
--- @return Placeholder
|
||||||
|
|||||||
@ -8,16 +8,16 @@ element.type = "ScreenArea"
|
|||||||
|
|
||||||
function element:layout()
|
function element:layout()
|
||||||
local screenW, screenH = love.graphics.getWidth(), love.graphics.getHeight()
|
local screenW, screenH = love.graphics.getWidth(), love.graphics.getHeight()
|
||||||
self.constraints = Constraints {
|
self._constraints_ = Constraints {
|
||||||
maxWidth = screenW,
|
maxWidth = screenW,
|
||||||
maxHeight = screenH
|
maxHeight = screenH
|
||||||
}
|
}
|
||||||
self.size = Vec3 { screenW, screenH }
|
self._size_ = Vec3 { screenW, screenH }
|
||||||
|
|
||||||
if not self.child then return end
|
if not self.child then return end
|
||||||
self.child.constraints = Constraints(self.constraints)
|
self.child._constraints_ = Constraints(self._constraints_)
|
||||||
self.child:layout()
|
self.child:layout()
|
||||||
self.child.offset = Vec3 {}
|
self.child._offset_ = Vec3 {}
|
||||||
end
|
end
|
||||||
|
|
||||||
--- @return ScreenArea
|
--- @return ScreenArea
|
||||||
|
|||||||
@ -9,15 +9,15 @@ element.width = 0
|
|||||||
element.height = 0
|
element.height = 0
|
||||||
|
|
||||||
function element:layout()
|
function element:layout()
|
||||||
self.size = Vec3 { self.width, self.height }
|
self._size_ = Vec3 { self.width, self.height }
|
||||||
|
|
||||||
if not self.child then return end
|
if not self.child then return end
|
||||||
self.child.constraints = Constraints {
|
self.child._constraints_ = Constraints {
|
||||||
maxWidth = self.width,
|
maxWidth = self.width,
|
||||||
maxHeight = self.height,
|
maxHeight = self.height,
|
||||||
}
|
}
|
||||||
self.child:layout()
|
self.child:layout()
|
||||||
self.child.offset = Vec3 {}
|
self.child._offset_ = Vec3 {}
|
||||||
end
|
end
|
||||||
|
|
||||||
--- @return SizedBox
|
--- @return SizedBox
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user