diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-07 19:45:17 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:57:01 +0200 |
commit | 8805b1fe27344e8086cebabf869b7a02d2376f05 (patch) | |
tree | a90a2e2dda1bcb98fb4de5cd983138e5441c2222 /src/engine/graphics | |
parent | f778317bae709f397345a2d5e04e23864c6391b3 (diff) |
refactor: decouple statusline from scene & cursor controller
Might be slightly slower than previously though...
Diffstat (limited to 'src/engine/graphics')
-rw-r--r-- | src/engine/graphics/component_renderer.cpp | 55 | ||||
-rw-r--r-- | src/engine/graphics/component_renderer.hpp | 23 | ||||
-rw-r--r-- | src/engine/graphics/scene.cpp | 15 | ||||
-rw-r--r-- | src/engine/graphics/scene.hpp | 14 | ||||
-rw-r--r-- | src/engine/graphics/statusline.cpp | 112 | ||||
-rw-r--r-- | src/engine/graphics/statusline.hpp | 48 |
6 files changed, 106 insertions, 161 deletions
diff --git a/src/engine/graphics/component_renderer.cpp b/src/engine/graphics/component_renderer.cpp new file mode 100644 index 0000000..1ab4c08 --- /dev/null +++ b/src/engine/graphics/component_renderer.cpp @@ -0,0 +1,55 @@ +#include "component_renderer.hpp" + +#include "engine/escape.hpp" +#include "util/color.hpp" + +#include <fmt/core.h> + +#include <iostream> +#include <utility> + +ComponentRenderer::ComponentRenderer( + std::shared_ptr<ICursorController> cursor_controller) noexcept + : _cursor_controller(std::move(cursor_controller)) +{ +} + +void ComponentRenderer::render( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept +{ + const auto previous_pos = _cursor_controller->where(); + + _cursor_controller->hide(); + _cursor_controller->move_to(position); + + const auto component_matrix = component->get(); + + 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)); + + for (const auto &row : *component_matrix) + { + for (const auto &col : row) + { + std::cout.put(col); + } + + const auto current_pos = _cursor_controller->where(); + + _cursor_controller->move_to( + Vector2({.x = previous_pos.get_x(), .y = current_pos.get_y() - 1})); + } + + fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); + + std::cout.flush(); + + _cursor_controller->move_to(previous_pos); + _cursor_controller->show(); +} diff --git a/src/engine/graphics/component_renderer.hpp b/src/engine/graphics/component_renderer.hpp new file mode 100644 index 0000000..4f53e07 --- /dev/null +++ b/src/engine/graphics/component_renderer.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "interfaces/component.hpp" +#include "interfaces/component_renderer.hpp" +#include "interfaces/cursor.hpp" + +#include "engine/data/vector2.hpp" + +#include <memory> + +class ComponentRenderer : public IComponentRenderer +{ +public: + explicit ComponentRenderer( + std::shared_ptr<ICursorController> cursor_controller) noexcept; + + void render( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept override; + +private: + std::shared_ptr<ICursorController> _cursor_controller; +}; diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp index 9f561f0..e0f4da6 100644 --- a/src/engine/graphics/scene.cpp +++ b/src/engine/graphics/scene.cpp @@ -8,7 +8,6 @@ #include <iostream> #include <sys/ioctl.h> -#include <utility> Scene::Scene( IMatrixFactory<MatrixElement> matrix_factory, @@ -84,3 +83,17 @@ auto Scene::get_matrix() const noexcept -> const std::shared_ptr<IMatrix<MatrixE { return _matrix; } + +void Scene::register_component( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept +{ + _components.emplace_back(std::make_pair(component, position)); +} + +auto Scene::get_components() const noexcept + -> std::vector<std::pair<std::shared_ptr<IComponent>, Vector2>> +{ + return _components; +} + diff --git a/src/engine/graphics/scene.hpp b/src/engine/graphics/scene.hpp index 60b541a..c2b11e8 100644 --- a/src/engine/graphics/scene.hpp +++ b/src/engine/graphics/scene.hpp @@ -1,14 +1,19 @@ #pragma once +#include "interfaces/component.hpp" #include "interfaces/cursor.hpp" #include "interfaces/matrix.hpp" #include "interfaces/scene.hpp" +#include "engine/data/vector2.hpp" + #include <fmt/core.h> #include <memory> #include <string_view> #include <termios.h> +#include <utility> +#include <vector> constexpr fmt::string_view ENABLE_ALT_BUFFER = "{esc}[?1049h"; constexpr fmt::string_view DISABLE_ALT_BUFFER = "{esc}[?1049l"; @@ -29,10 +34,19 @@ public: [[nodiscard]] auto get_matrix() const noexcept -> const std::shared_ptr<IMatrix<MatrixElement>> & override; + void register_component( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept override; + + [[nodiscard]] auto get_components() const noexcept + -> std::vector<std::pair<std::shared_ptr<IComponent>, Vector2>> override; + private: std::shared_ptr<IMatrix<MatrixElement>> _matrix; std::shared_ptr<ICursorController> _cursor_controller; bool _is_shown; std::shared_ptr<termios> _original_termios = nullptr; + + std::vector<std::pair<std::shared_ptr<IComponent>, Vector2>> _components; }; diff --git a/src/engine/graphics/statusline.cpp b/src/engine/graphics/statusline.cpp deleted file mode 100644 index bc83993..0000000 --- a/src/engine/graphics/statusline.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#include "statusline.hpp" - -#include "engine/escape.hpp" -#include "util/color.hpp" - -#include <string> -#include <utility> - -StatusLine::StatusLine( - std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IScene> scene) noexcept - : _cursor_controller(std::move(cursor_controller)), _scene(std::move(scene)) -{ -} - -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(_scene->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<int32_t>(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(); - - _cursor_controller->hide(); - - _cursor_controller->move_to(Vector2({.x = x, .y = 0})); - - return previous_position; -} - -void StatusLine::_move_back(Vector2 previous_position) noexcept -{ - _cursor_controller->move_to(previous_position); - _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<int32_t>(section); - - while (section_index > 0) - { - section_start += static_cast<int32_t>( - _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<int32_t>(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<uint32_t>(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 deleted file mode 100644 index 6f0a4c2..0000000 --- a/src/engine/graphics/statusline.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -#include "interfaces/cursor.hpp" -#include "interfaces/scene.hpp" -#include "interfaces/statusline.hpp" - -#include "engine/data/vector2.hpp" - -#include <fmt/core.h> - -#include <memory> -#include <string_view> -#include <unordered_map> - -constexpr uint32_t STATUSBAR_COLOR = 0x1A1A1AU; - -class StatusLine : public IStatusLine -{ -public: - StatusLine( - std::shared_ptr<ICursorController> cursor_controller, - std::shared_ptr<IScene> scene) 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<StatusLineSection, uint32_t> _sections_lengths; - - std::shared_ptr<ICursorController> _cursor_controller; - std::shared_ptr<IScene> _scene; - - 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; -}; |