From 741a6008e806abb8e5aaa8f8fb91827fa97ece9d Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 2 May 2022 22:57:51 +0200 Subject: refactor: move statusline to engine graphics folder --- src/CMakeLists.txt | 2 +- src/bootstrap.cpp | 2 +- src/engine/graphics/statusline.cpp | 121 +++++++++++++++++++++++++++++++++++++ src/engine/graphics/statusline.hpp | 50 +++++++++++++++ src/game/statusline.cpp | 121 ------------------------------------- src/game/statusline.hpp | 50 --------------- 6 files changed, 173 insertions(+), 173 deletions(-) create mode 100644 src/engine/graphics/statusline.cpp create mode 100644 src/engine/graphics/statusline.hpp delete mode 100644 src/game/statusline.cpp delete mode 100644 src/game/statusline.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3a75269..32118e2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,13 +17,13 @@ file(GLOB SOURCES game/game.cpp game/status_manager.cpp game/generation_tracker.cpp - game/statusline.cpp engine/engine.cpp engine/data/vector2.cpp engine/data/bounds.cpp engine/graphics/scene.cpp engine/graphics/window.cpp engine/graphics/string_matrix.cpp + engine/graphics/statusline.cpp engine/user/input.cpp engine/user/cursor.cpp randomization/generator.cpp diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 70407a2..7962910 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -22,13 +22,13 @@ #include "engine/engine.hpp" #include "engine/graphics/matrix.hpp" #include "engine/graphics/scene.hpp" +#include "engine/graphics/statusline.hpp" #include "engine/graphics/window.hpp" #include "engine/user/cursor.hpp" #include "engine/user/input.hpp" #include "game/game.hpp" #include "game/generation_tracker.hpp" #include "game/status_manager.hpp" -#include "game/statusline.hpp" #include "game/statusline_subscriber_adapter.hpp" #include "randomization/generator.hpp" #include "randomization/seed_generator.hpp" diff --git a/src/engine/graphics/statusline.cpp b/src/engine/graphics/statusline.cpp new file mode 100644 index 0000000..1abb87b --- /dev/null +++ b/src/engine/graphics/statusline.cpp @@ -0,0 +1,121 @@ +#include "statusline.hpp" + +#include "engine/escape.hpp" +#include "util/color.hpp" + +#include +#include + +StatusLine::StatusLine( + std::shared_ptr cursor_controller, + std::shared_ptr window +) noexcept + : _cursor_controller(std::move(cursor_controller)), _window(std::move(window)) +{ +} + +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( + const StatusLineSection §ion, + const std::string_view &status, + std::size_t start +) noexcept +{ + _clear_section(section, start); + + auto section_start = _get_section_start_x(section); + + const auto previous_position = + _move_to_statusline(section_start + static_cast(start)); + + auto background_color = get_background_esc_seq(STATUSBAR_COLOR); + + auto section_length = _sections_lengths[section]; + + fmt::print("{}{}", background_color, status); + fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); + + _move_back(previous_position); +} + +void StatusLine::set_section_length( + const StatusLineSection §ion, + uint32_t length +) noexcept +{ + _sections_lengths[section] = length; +} + +auto StatusLine::_move_to_statusline(int32_t x) noexcept -> Vector2 +{ + 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(); +} + +auto StatusLine::_get_section_start_x(const StatusLineSection §ion) const noexcept + -> int32_t +{ + 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( + const StatusLineSection §ion, + std::size_t start +) noexcept +{ + auto section_start = _get_section_start_x(section); + + auto start_int32 = static_cast(start); + + const auto previous_position = _move_to_statusline(section_start + start_int32); + + auto background_color = get_background_esc_seq(STATUSBAR_COLOR); + + auto section_length = _sections_lengths[section]; + + auto start_uint32 = static_cast(start); + + fmt::print("{}{}", background_color, std::string(section_length - start_uint32, ' ')); + fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); + + _move_back(previous_position); +} diff --git a/src/engine/graphics/statusline.hpp b/src/engine/graphics/statusline.hpp new file mode 100644 index 0000000..181090d --- /dev/null +++ b/src/engine/graphics/statusline.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include "interfaces/cursor.hpp" +#include "interfaces/statusline.hpp" +#include "interfaces/window.hpp" + +#include "engine/data/vector2.hpp" + +#include + +#include +#include +#include + +constexpr uint32_t STATUSBAR_COLOR = 0x1A1A1AU; + +class StatusLine : public IStatusLine +{ +public: + StatusLine( + std::shared_ptr cursor_controller, + std::shared_ptr window + ) noexcept; + + void initialize_background() noexcept override; + + void set_status( + const StatusLineSection §ion, + const std::string_view &status, + std::size_t start + ) noexcept override; + + void set_section_length(const StatusLineSection §ion, uint32_t length) noexcept + override; + +private: + std::unordered_map _sections_lengths; + + std::shared_ptr _cursor_controller; + std::shared_ptr _window; + + auto _move_to_statusline(int32_t x) noexcept -> Vector2; + + void _move_back(Vector2 previous_position) noexcept; + + [[nodiscard]] auto _get_section_start_x(const StatusLineSection §ion + ) const noexcept -> int32_t; + + void _clear_section(const StatusLineSection §ion, std::size_t start) noexcept; +}; diff --git a/src/game/statusline.cpp b/src/game/statusline.cpp deleted file mode 100644 index 1abb87b..0000000 --- a/src/game/statusline.cpp +++ /dev/null @@ -1,121 +0,0 @@ -#include "statusline.hpp" - -#include "engine/escape.hpp" -#include "util/color.hpp" - -#include -#include - -StatusLine::StatusLine( - std::shared_ptr cursor_controller, - std::shared_ptr window -) noexcept - : _cursor_controller(std::move(cursor_controller)), _window(std::move(window)) -{ -} - -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( - const StatusLineSection §ion, - const std::string_view &status, - std::size_t start -) noexcept -{ - _clear_section(section, start); - - auto section_start = _get_section_start_x(section); - - const auto previous_position = - _move_to_statusline(section_start + static_cast(start)); - - auto background_color = get_background_esc_seq(STATUSBAR_COLOR); - - auto section_length = _sections_lengths[section]; - - fmt::print("{}{}", background_color, status); - fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); - - _move_back(previous_position); -} - -void StatusLine::set_section_length( - const StatusLineSection §ion, - uint32_t length -) noexcept -{ - _sections_lengths[section] = length; -} - -auto StatusLine::_move_to_statusline(int32_t x) noexcept -> Vector2 -{ - 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(); -} - -auto StatusLine::_get_section_start_x(const StatusLineSection §ion) const noexcept - -> int32_t -{ - 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( - const StatusLineSection §ion, - std::size_t start -) noexcept -{ - auto section_start = _get_section_start_x(section); - - auto start_int32 = static_cast(start); - - const auto previous_position = _move_to_statusline(section_start + start_int32); - - auto background_color = get_background_esc_seq(STATUSBAR_COLOR); - - auto section_length = _sections_lengths[section]; - - auto start_uint32 = static_cast(start); - - fmt::print("{}{}", background_color, std::string(section_length - start_uint32, ' ')); - fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); - - _move_back(previous_position); -} diff --git a/src/game/statusline.hpp b/src/game/statusline.hpp deleted file mode 100644 index 181090d..0000000 --- a/src/game/statusline.hpp +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include "interfaces/cursor.hpp" -#include "interfaces/statusline.hpp" -#include "interfaces/window.hpp" - -#include "engine/data/vector2.hpp" - -#include - -#include -#include -#include - -constexpr uint32_t STATUSBAR_COLOR = 0x1A1A1AU; - -class StatusLine : public IStatusLine -{ -public: - StatusLine( - std::shared_ptr cursor_controller, - std::shared_ptr window - ) noexcept; - - void initialize_background() noexcept override; - - void set_status( - const StatusLineSection §ion, - const std::string_view &status, - std::size_t start - ) noexcept override; - - void set_section_length(const StatusLineSection §ion, uint32_t length) noexcept - override; - -private: - std::unordered_map _sections_lengths; - - std::shared_ptr _cursor_controller; - std::shared_ptr _window; - - auto _move_to_statusline(int32_t x) noexcept -> Vector2; - - void _move_back(Vector2 previous_position) noexcept; - - [[nodiscard]] auto _get_section_start_x(const StatusLineSection §ion - ) const noexcept -> int32_t; - - void _clear_section(const StatusLineSection §ion, std::size_t start) noexcept; -}; -- cgit v1.2.3-18-g5258