enscapsulate SingleChildElement.child
This commit is contained in:
parent
3d5ac077be
commit
6c8dc6a52b
@ -51,22 +51,31 @@ function builder:_makeKey(element)
|
||||
return element.key
|
||||
end
|
||||
|
||||
--- @param cur UIElement
|
||||
--- @private
|
||||
function builder:build_step(cur)
|
||||
cur = cur --[[@as SingleChildElement | MultiChildElement]]
|
||||
if cur.build then
|
||||
local orphan = cur:build()
|
||||
local child = cur.child
|
||||
child = orphan
|
||||
cur = cur --[[@as SingleChildElement]]
|
||||
|
||||
local child = cur:build()
|
||||
if not child then return end
|
||||
|
||||
child._parent_ = cur
|
||||
cur.child = child
|
||||
-- local oldChild = cur._child_
|
||||
-- if oldChild then
|
||||
-- for key, value in pairs(oldChild) do
|
||||
-- if (not Element[key]) then child[key] = value end
|
||||
-- end
|
||||
-- end
|
||||
|
||||
self:build_step(cur.child)
|
||||
cur._child_ = child
|
||||
child._parent_ = cur
|
||||
|
||||
self:build_step(cur._child_)
|
||||
elseif cur.children then
|
||||
cur = cur --[[@as MultiChildElement]]
|
||||
for _, child in ipairs(cur.children) do
|
||||
child._parent_ = cur
|
||||
|
||||
self:build_step(child)
|
||||
end
|
||||
end
|
||||
|
||||
@ -8,7 +8,6 @@ local Vec3 = require "lib.utils.vec3"
|
||||
--- @field _constraints_ Constraints
|
||||
--- @field _offset_ Vec3 Положение левого верхнего угла элемента в локальных координатах {x, y}. Устанавливается родительским элементом.
|
||||
--- @field _size_ Vec3 Размеры элемента {x, y}
|
||||
--- @field build? fun(self, ctx: UIElement): UIElement
|
||||
local element = {}
|
||||
element.__index = element
|
||||
element.type = "Element"
|
||||
|
||||
@ -3,10 +3,11 @@ local Constraints = require "lib.simple_ui.core.constraints"
|
||||
|
||||
--- @class SingleChildElement : UIElement
|
||||
--- @field child? UIElement
|
||||
--- @field _child_? UIElement
|
||||
local element = setmetatable({}, require "lib.simple_ui.core.element")
|
||||
element.__index = element
|
||||
|
||||
--- дефолтное поведение -- просто возвращать своего ребенка
|
||||
--- дефолтное поведение -- просто возвращать переданного ребенка
|
||||
function element:build()
|
||||
return self.child
|
||||
end
|
||||
@ -15,20 +16,20 @@ function element:layout()
|
||||
--- передать ребенку ограничения
|
||||
--- получить назад размеры
|
||||
--- разместить ребенка
|
||||
if not self.child then return end
|
||||
self.child._constraints_ = Constraints(self._constraints_)
|
||||
self.child:layout()
|
||||
self.child._offset_ = Vec3 {}
|
||||
if not self._child_ then return end
|
||||
self._child_._constraints_ = Constraints(self._constraints_)
|
||||
self._child_:layout()
|
||||
self._child_._offset_ = Vec3 {}
|
||||
end
|
||||
|
||||
function element:update(dt)
|
||||
if self.child then self.child:update(dt) end
|
||||
if self._child_ then self._child_:update(dt) end
|
||||
end
|
||||
|
||||
function element:draw()
|
||||
love.graphics.push("transform")
|
||||
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()
|
||||
end
|
||||
|
||||
|
||||
@ -9,13 +9,13 @@ element.type = "Center"
|
||||
function element:layout()
|
||||
self._size_ = Vec3 { self._constraints_.maxWidth, self._constraints_.maxHeight }
|
||||
|
||||
if not self.child then return end
|
||||
self.child._constraints_ = Constraints(self._constraints_)
|
||||
self.child:layout()
|
||||
if not self._child_ then return end
|
||||
self._child_._constraints_ = Constraints(self._constraints_)
|
||||
self._child_:layout()
|
||||
|
||||
self.child._offset_ = Vec3 {
|
||||
(self._size_.x - self.child._size_.x) / 2,
|
||||
(self._size_.y - self.child._size_.y) / 2,
|
||||
self._child_._offset_ = Vec3 {
|
||||
(self._size_.x - self._child_._size_.x) / 2,
|
||||
(self._size_.y - self._child_._size_.y) / 2,
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ function element:layout()
|
||||
end
|
||||
local shift = 0
|
||||
for _, child in ipairs(self.children) do
|
||||
child._offset_ = Vec3 { self._offset_.x + start + shift, self._offset_.y }
|
||||
child._offset_ = Vec3 { start + shift, 0 }
|
||||
shift = shift + child._size_.x
|
||||
end
|
||||
|
||||
@ -57,7 +57,7 @@ function element:layout()
|
||||
end
|
||||
local shift = 0
|
||||
for _, child in ipairs(self.children) do
|
||||
child._offset_ = Vec3 { self._offset_.x, self._offset_.y + start + shift }
|
||||
child._offset_ = Vec3 { 0, start + shift }
|
||||
shift = shift + child._size_.y
|
||||
end
|
||||
|
||||
|
||||
@ -19,17 +19,17 @@ element.bottom = 0
|
||||
---
|
||||
--- as in https://api.flutter.dev/flutter/widgets/Padding-class.html
|
||||
function element:layout()
|
||||
if not self.child then return end
|
||||
if not self._child_ then return end
|
||||
local c = Constraints(self._constraints_)
|
||||
c.maxWidth = c.maxWidth - self.left - self.right
|
||||
c.maxHeight = c.maxHeight - self.top - self.bottom
|
||||
c.maxWidth = c.maxWidth > 0 and c.maxWidth or 0
|
||||
c.maxHeight = c.maxHeight > 0 and c.maxHeight or 0
|
||||
self.child._constraints_ = c
|
||||
self._child_._constraints_ = c
|
||||
|
||||
self.child:layout()
|
||||
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_:layout()
|
||||
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 }
|
||||
end
|
||||
|
||||
--- @return Padding
|
||||
|
||||
@ -9,17 +9,17 @@ element.type = "Placeholder"
|
||||
function element:layout()
|
||||
self._size_ = Vec3 { self._constraints_.maxWidth, self._constraints_.maxHeight }
|
||||
|
||||
if not self.child then return end
|
||||
self.child._constraints_ = Constraints(self._constraints_)
|
||||
self.child:layout()
|
||||
if not self._child_ then return end
|
||||
self._child_._constraints_ = Constraints(self._constraints_)
|
||||
self._child_:layout()
|
||||
end
|
||||
|
||||
function element:draw()
|
||||
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)
|
||||
.y)
|
||||
love.graphics.line(self._offset_.x, self._offset_.y + self._size_.y, self._offset_.x + self._size_.x, self._offset_
|
||||
.y)
|
||||
.y)
|
||||
end
|
||||
|
||||
--- @return Placeholder
|
||||
|
||||
@ -14,10 +14,10 @@ function element:layout()
|
||||
}
|
||||
self._size_ = Vec3 { screenW, screenH }
|
||||
|
||||
if not self.child then return end
|
||||
self.child._constraints_ = Constraints(self._constraints_)
|
||||
self.child:layout()
|
||||
self.child._offset_ = Vec3 {}
|
||||
if not self._child_ then return end
|
||||
self._child_._constraints_ = Constraints(self._constraints_)
|
||||
self._child_:layout()
|
||||
self._child_._offset_ = Vec3 {}
|
||||
end
|
||||
|
||||
--- @return ScreenArea
|
||||
|
||||
@ -11,13 +11,13 @@ element.height = 0
|
||||
function element:layout()
|
||||
self._size_ = Vec3 { self.width, self.height }
|
||||
|
||||
if not self.child then return end
|
||||
self.child._constraints_ = Constraints {
|
||||
if not self._child_ then return end
|
||||
self._child_._constraints_ = Constraints {
|
||||
maxWidth = self.width,
|
||||
maxHeight = self.height,
|
||||
}
|
||||
self.child:layout()
|
||||
self.child._offset_ = Vec3 {}
|
||||
self._child_:layout()
|
||||
self._child_._offset_ = Vec3 {}
|
||||
end
|
||||
|
||||
--- @return SizedBox
|
||||
|
||||
@ -18,8 +18,8 @@ Canary.type = "Canary"
|
||||
|
||||
local reported = false
|
||||
function Canary:build()
|
||||
-- self.i = self.i and self.i + 1 or 0
|
||||
-- print(self.i)
|
||||
self.i = self.i and self.i + 1 or 0
|
||||
print(self.i)
|
||||
-- if not reported then
|
||||
-- self:traverseUp(function(element)
|
||||
-- print(element.type)
|
||||
@ -60,7 +60,7 @@ function MyWidget:build()
|
||||
width = 100,
|
||||
height = 100,
|
||||
child = Canary:new {
|
||||
i = 10
|
||||
i = 0
|
||||
}
|
||||
},
|
||||
},
|
||||
@ -68,7 +68,7 @@ function MyWidget:build()
|
||||
},
|
||||
Flex:new {
|
||||
key = "inner_flex2",
|
||||
mainAxisAlignment = "center",
|
||||
mainAxisAlignment = "end",
|
||||
children = {
|
||||
SizedBox:new {
|
||||
width = 100,
|
||||
@ -84,6 +84,7 @@ function MyWidget:build()
|
||||
},
|
||||
|
||||
},
|
||||
math.floor(love.timer.getTime()) % 2 == 0 and self.child or nil
|
||||
}
|
||||
}
|
||||
end
|
||||
@ -119,6 +120,14 @@ end
|
||||
|
||||
return Builder {
|
||||
elementTree = ScreenArea:new {
|
||||
child = MyWidget:new {}
|
||||
child = MyWidget:new {
|
||||
child = SizedBox:new {
|
||||
width = 100,
|
||||
height = 100,
|
||||
child = Canary:new {
|
||||
i = 100
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user