FTXUI/src/ftxui/dom/node.cpp

85 lines
2.0 KiB
C++
Raw Normal View History

#include <utility> // for move
2021-05-02 02:40:35 +08:00
#include "ftxui/dom/node.hpp"
#include "ftxui/screen/screen.hpp" // for Screen
2018-09-18 14:48:40 +08:00
namespace ftxui {
2022-03-31 08:17:43 +08:00
Node::Node() = default;
Node::Node(Elements children) : children_(std::move(children)) {}
2022-03-31 08:17:43 +08:00
Node::~Node() = default;
2018-09-18 14:48:40 +08:00
2020-08-16 08:24:50 +08:00
/// @brief Compute how much space an elements needs.
/// @ingroup dom
void Node::ComputeRequirement() {
2022-03-31 08:17:43 +08:00
for (auto& child : children_) {
child->ComputeRequirement();
2022-03-31 08:17:43 +08:00
}
}
2020-08-16 08:24:50 +08:00
/// @brief Assign a position and a dimension to an element for drawing.
/// @ingroup dom
2018-09-18 14:48:40 +08:00
void Node::SetBox(Box box) {
box_ = box;
}
2020-08-16 08:24:50 +08:00
/// @brief Display an element on a ftxui::Screen.
/// @ingroup dom
2018-09-18 14:48:40 +08:00
void Node::Render(Screen& screen) {
2022-03-31 08:17:43 +08:00
for (auto& child : children_) {
2018-09-18 14:48:40 +08:00
child->Render(screen);
2022-03-31 08:17:43 +08:00
}
2018-09-18 14:48:40 +08:00
}
void Node::Check(Status* status) {
2022-03-31 08:17:43 +08:00
for (auto& child : children_) {
child->Check(status);
2022-03-31 08:17:43 +08:00
}
status->need_iteration |= (status->iteration == 0);
}
2020-08-16 08:24:50 +08:00
/// @brief Display an element on a ftxui::Screen.
/// @ingroup dom
void Render(Screen& screen, const Element& element) {
Render(screen, element.get());
}
2020-08-16 08:24:50 +08:00
/// @brief Display an element on a ftxui::Screen.
/// @ingroup dom
2018-09-18 14:48:40 +08:00
void Render(Screen& screen, Node* node) {
Box box;
2019-01-20 05:06:05 +08:00
box.x_min = 0;
box.y_min = 0;
box.x_max = screen.dimx() - 1;
box.y_max = screen.dimy() - 1;
2020-03-23 05:32:44 +08:00
Node::Status status;
node->Check(&status);
2022-03-31 08:17:43 +08:00
const int max_iterations = 20;
while (status.need_iteration && status.iteration < max_iterations) {
// Step 1: Find what dimension this elements wants to be.
node->ComputeRequirement();
// Step 2: Assign a dimension to the element.
node->SetBox(box);
// Check if the element needs another iteration of the layout algorithm.
status.need_iteration = false;
status.iteration++;
node->Check(&status);
}
2018-09-18 14:48:40 +08:00
// Step 3: Draw the element.
screen.stencil = box;
2018-09-18 14:48:40 +08:00
node->Render(screen);
2019-01-19 07:20:29 +08:00
// Step 4: Apply shaders
screen.ApplyShader();
2018-09-18 14:48:40 +08:00
}
2020-02-12 04:44:55 +08:00
} // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.