PeaAshMeter 393638bb71 add type hints for EVERYTHING
organize ui into folders
implement element:traverseUp
2026-05-04 02:48:30 +03:00

47 lines
1.7 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local Constraints = require "lib.simple_ui.core.constraints"
local Vec3 = require "lib.utils.vec3"
--- @class UIElement
--- @field type string
--- @field key? any Must be convertible to string
--- @field parent? UIElement
--- @field constraints Constraints
--- @field offset Vec3 Положение левого верхнего угла элемента в экранных координатах {x, y}. Устанавливается родительским элементом.
--- @field size Vec3 Размеры элемента в экранных координатах {x, y}
--- @field build? fun(self): UIElement?
local element = {}
element.__index = element
element.type = "Element"
element.constraints = Constraints {}
element.offset = Vec3 {}
element.size = Vec3 {}
--- "Constraints go down. Sizes go up. Parent sets position."
---
--- Karl Marx, probably.
function element:layout() end
function element:update(dt) end
function element:draw() end
--- @param values {[string]: any}
--- @return UIElement
function element:new(values)
return setmetatable(values, self)
end
--- Рекурсивно обходит дерево элементов вверх, начиная с первого родителя.
---
--- К каждому посещенному элементу применяет функцию visitor.
---
--- Обход заканчивается, если visitor возвращает false, или если родители кончились.
--- @param visitor fun(element: UIElement): boolean
function element:traverseUp(visitor)
if not self.parent then return end
if not visitor(self.parent) then return end
return self.parent:traverseUp(visitor)
end
return element