/
d.vision
/
division_engine_cpp
Обзор
Документация
Войти
/
d.vision
/
division_engine_cpp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
resources/scripts/views/stack.lua
117 строк
4 KB
den163
Improved annotations
27 ноя 2024, 11:06
27 ноя 2024, 11:06
0828538
Код
Авторство
О чём код?
local constraints = require 'constraints' local constrained_size = require 'constrained_size' local multi_child_view = require 'views.multi_child_view' local vec2 = require 'vec2' local rect = require 'rect' ---@class stack.options ---@field children view[] ---@field fit stack.fit_type ---@class stack: multi_child_view, stack.options ---@overload fun(options: stack.options): stack ---@diagnostic disable-next-line: assign-type-mismatch local stack = multi_child_view:inherit 'stack' ---@enum stack.fit_type stack.fit_type = { expand = 0, shrink = 1, } ---@class stack.state: multi_child_view.state stack.state = multi_child_view.state:inherit 'stack.state' ---@param options stack.options ---@return stack function stack:__call(options) local stack = stack:new(options) stack.children = options.children or {} stack.fit = options.fit or stack.fit_type.expand return stack end ---@param constraints constraints ---@param view stack function stack.state:layout(constraints, view) local overall_size = constrained_size.unconstrained() local child_count = #self.child_states for i = 1, child_count do local child_size = self.child_states[i]:layout(constraints, view.children[i]) if not child_size.width_unconstrained then overall_size:set_constrained_width(overall_size.width_unconstrained and child_size.width or (overall_size.width + child_size.width) ) end if not child_size.height_unconstrained then overall_size:set_constrained_height(overall_size.height_unconstrained and child_size.height or (overall_size.height + child_size.height) ) end end return overall_size end ---@param render_rect rect ---@param view stack function stack.state:render(render_rect, view) if view.fit == stack.fit_type.expand then self:render_max_size(render_rect, view) else self:render_min_size(render_rect, view) end end ---@private ---@param render_rect rect ---@param view stack function stack.state:render_max_size(render_rect, view) for i = 1, #self.child_states do self.child_states[i]:render(render_rect, view.children[i]) end end ---@private ---@param render_rect rect ---@param view stack function stack.state:render_min_size(render_rect, view) local max_size = constrained_size() local render_rect_size = render_rect:size() local child_constraints = constraints.new(vec2(0), render_rect_size) for i = 1, #self.child_states do local child_state = self.child_states[i] local child_view = view.children[i] local child_size = child_state:layout(child_constraints, child_view) if not child_size.width_unconstrained then max_size:set_constrained_width(math.max(max_size.width, child_size.width)) end if not child_size.height_unconstrained then max_size:set_constrained_height(math.max(max_size.height, child_size.height)) end end local child_render_rect_size = vec2( max_size.width_unconstrained and render_rect_size.x or max_size.width, max_size.height_unconstrained and render_rect_size.y or max_size.height ) local child_render_rect = rect.from_top_left(render_rect:top_left(), child_render_rect_size) for i = 1, #self.child_states do local child_state = self.child_states[i] local child_view = view.children[i] child_state:render(child_render_rect, child_view) end end return stack