Files
FTXUI/src/ftxui/dom/node.cpp

68 lines
1.5 KiB
C++
Raw Normal View History

2021-05-01 20:40:35 +02:00
#include <utility>
#include "ftxui/dom/node.hpp"
2021-05-01 20:40:35 +02:00
#include "ftxui/screen/screen.hpp"
2018-09-18 08:48:40 +02:00
namespace ftxui {
using ftxui::Screen;
2018-09-18 08:48:40 +02:00
Node::Node() {}
Node::Node(Elements children) : children_(std::move(children)) {}
2018-09-18 08:48:40 +02:00
Node::~Node() {}
2020-08-16 02:24:50 +02:00
/// @brief Compute how much space an elements needs.
/// @ingroup dom
void Node::ComputeRequirement() {
for (auto& child : children_)
child->ComputeRequirement();
}
2020-08-16 02:24:50 +02:00
/// @brief Assign a position and a dimension to an element for drawing.
/// @ingroup dom
2018-09-18 08:48:40 +02:00
void Node::SetBox(Box box) {
box_ = box;
}
2020-08-16 02:24:50 +02:00
/// @brief Display an element on a ftxui::Screen.
/// @ingroup dom
2018-09-18 08:48:40 +02:00
void Node::Render(Screen& screen) {
for (auto& child : children_)
2018-09-18 08:48:40 +02:00
child->Render(screen);
}
2020-08-16 02:24:50 +02: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 02:24:50 +02:00
/// @brief Display an element on a ftxui::Screen.
/// @ingroup dom
2018-09-18 08:48:40 +02:00
void Render(Screen& screen, Node* node) {
// Step 1: Find what dimension this elements wants to be.
node->ComputeRequirement();
2020-03-22 22:32:44 +01:00
2018-09-18 08:48:40 +02:00
Box box;
2019-01-19 22:06:05 +01:00
box.x_min = 0;
box.y_min = 0;
box.x_max = screen.dimx() - 1;
box.y_max = screen.dimy() - 1;
2020-03-22 22:32:44 +01:00
2018-09-18 08:48:40 +02:00
// Step 2: Assign a dimension to the element.
node->SetBox(box);
2019-01-19 22:06:05 +01:00
screen.stencil = box;
2018-09-18 08:48:40 +02:00
// Step 3: Draw the element.
node->Render(screen);
2019-01-19 00:20:29 +01:00
// Step 4: Apply shaders
screen.ApplyShader();
2018-09-18 08:48:40 +02:00
}
2020-02-11 21:44:55 +01: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.