#include "component_renderer.hpp" #include #include #include #include #include "engine/data/style.hpp" #include "engine/data/vector2.hpp" #include "engine/escape.hpp" #include "engine/graphics/matrix_iterator.hpp" #include "engine/graphics/matrix_iterator_impl.hpp" #include "interfaces/component.hpp" #include "interfaces/cursor.hpp" #include "interfaces/matrix.hpp" #include "util/color.hpp" ComponentRenderer::ComponentRenderer( std::shared_ptr cursor_controller) noexcept : _cursor_controller(std::move(cursor_controller)) { } void ComponentRenderer::render( const std::shared_ptr &component, const Vector2 &position) noexcept { const auto previous_pos = _cursor_controller->where(); _cursor_controller->hide(); _cursor_controller->move_to(position, true); _use_component_colors(component); const auto component_matrix = component->get(); for (const auto &row : *component_matrix) { for (const auto &element : row) { if (element.style.reset_before) { fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); _use_component_colors(component); } const auto opt_element_fg_color = element.style.fg_color; const auto opt_element_bg_color = element.style.bg_color; if (opt_element_fg_color.has_value()) { fmt::print("{}", get_foreground_esc_seq(opt_element_fg_color.value())); } if (opt_element_bg_color.has_value()) { fmt::print("{}", get_background_esc_seq(opt_element_bg_color.value())); } if (element.style.bold) { fmt::print(SET_BOLD_MODE, fmt::arg("esc", ESC)); } std::cout.put(element.value); } const auto current_pos = _cursor_controller->where(); _cursor_controller->move_to( Vector2({.x = previous_pos.get_x(), .y = current_pos.get_y() - 1}), true); } fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); _cursor_controller->move_to(previous_pos); _cursor_controller->show(true); } void ComponentRenderer::_use_component_colors( const std::shared_ptr &component) noexcept { const auto foreground_color = component->get_foreground_color(); const auto background_color = component->get_background_color(); fmt::print( "{}{}", get_background_esc_seq(background_color), get_foreground_esc_seq(foreground_color)); }