From 486ca3846b46dc229e5807968578809766ec1991 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 23 Mar 2022 19:41:31 +0100 Subject: feat: implement generations & multithreading --- src/game/statusline.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/game/statusline.cpp (limited to 'src/game/statusline.cpp') diff --git a/src/game/statusline.cpp b/src/game/statusline.cpp new file mode 100644 index 0000000..377fa75 --- /dev/null +++ b/src/game/statusline.cpp @@ -0,0 +1,102 @@ +#include "statusline.hpp" + +#include "engine/escape.hpp" +#include "util/color.hpp" + +#include +#include +#include + +StatusLine::StatusLine(std::shared_ptr cursor_controller, + std::shared_ptr window) noexcept + : _cursor_controller(std::move(cursor_controller)), _window(std::move(window)) +{ + constexpr uint32_t SECTION_A_LENGTH = 20; + constexpr uint32_t SECTION_B_LENGTH = 15; + + _sections_lengths[StatusLineSection::A] = SECTION_A_LENGTH; + _sections_lengths[StatusLineSection::B] = SECTION_B_LENGTH; +} + +void StatusLine::initialize_background() noexcept +{ + const auto previous_position = _move_to_statusline(0); + + auto background_color = get_background_esc_seq(STATUSBAR_COLOR); + + fmt::print("{}{}", background_color, std::string(_window->size().get_width(), ' ')); + fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); + + _move_back(previous_position); +} + +void StatusLine::set_status(StatusLineSection section, + const std::string_view &str) noexcept +{ + _clear_section(section); + + int32_t section_start = _get_section_start_x(section); + + const auto previous_position = _move_to_statusline(section_start); + + auto background_color = get_background_esc_seq(STATUSBAR_COLOR); + + fmt::print("{}{}", background_color, str); + fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); + + _move_back(previous_position); +} + +Vector2 StatusLine::_move_to_statusline(int32_t x) noexcept +{ + const auto previous_position = _cursor_controller->where(); + + const auto window_size = _window->size(); + + _cursor_controller->hide(); + + auto window_height = static_cast(window_size.get_height()); + + _cursor_controller->move_to(Vector2({.x = x, .y = window_height}), true); + + return previous_position; +} + +void StatusLine::_move_back(Vector2 previous_position) noexcept +{ + _cursor_controller->move_to(previous_position, true); + _cursor_controller->show(); +} + +int32_t StatusLine::_get_section_start_x(StatusLineSection section) const noexcept +{ + int32_t section_start = 0; + + auto section_index = static_cast(section); + + while (section_index > 0) + { + section_start += static_cast( + _sections_lengths.at(StatusLineSection(section_index - 1))); + + section_index--; + } + + return section_start; +} + +void StatusLine::_clear_section(StatusLineSection section) noexcept +{ + auto section_start = _get_section_start_x(section); + + const auto previous_position = _move_to_statusline(section_start); + + auto background_color = get_background_esc_seq(STATUSBAR_COLOR); + + auto section_length = _sections_lengths.at(section); + + fmt::print("{}{}", background_color, std::string(section_length, ' ')); + fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); + + _move_back(previous_position); +} -- cgit v1.2.3-18-g5258