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 | |
parent | f778317bae709f397345a2d5e04e23864c6391b3 (diff) |
refactor: decouple statusline from scene & cursor controller
Might be slightly slower than previously though...
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/bootstrap.cpp | 24 | ||||
-rw-r--r-- | src/engine/components/statusline.cpp | 99 | ||||
-rw-r--r-- | src/engine/components/statusline.hpp | 45 | ||||
-rw-r--r-- | src/engine/engine.cpp | 15 | ||||
-rw-r--r-- | src/engine/engine.hpp | 4 | ||||
-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 | ||||
-rw-r--r-- | src/game/game.cpp | 2 | ||||
-rw-r--r-- | src/game/status_manager.cpp | 27 | ||||
-rw-r--r-- | src/game/status_manager.hpp | 4 | ||||
-rw-r--r-- | src/interfaces/component.hpp | 25 | ||||
-rw-r--r-- | src/interfaces/component_renderer.hpp | 24 | ||||
-rw-r--r-- | src/interfaces/matrix.hpp | 4 | ||||
-rw-r--r-- | src/interfaces/scene.hpp | 11 | ||||
-rw-r--r-- | src/interfaces/status_manager.hpp | 3 | ||||
-rw-r--r-- | src/interfaces/statusline.hpp | 23 |
21 files changed, 383 insertions, 197 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 85ef2b9..4b470a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,7 +20,8 @@ file(GLOB SOURCES engine/data/bounds.cpp engine/graphics/scene.cpp engine/graphics/string_matrix.cpp - engine/graphics/statusline.cpp + engine/graphics/component_renderer.cpp + engine/components/statusline.cpp engine/user/input.cpp engine/user/cursor.cpp) diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 1effa96..44ef211 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -2,6 +2,8 @@ // Interfaces #include "interfaces/cell_helper.hpp" +#include "interfaces/component.hpp" +#include "interfaces/component_renderer.hpp" #include "interfaces/cursor.hpp" #include "interfaces/engine.hpp" #include "interfaces/game.hpp" @@ -13,12 +15,13 @@ #include "interfaces/statusline.hpp" // Implementations +#include "engine/components/statusline.hpp" #include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" #include "engine/engine.hpp" +#include "engine/graphics/component_renderer.hpp" #include "engine/graphics/matrix.hpp" #include "engine/graphics/scene.hpp" -#include "engine/graphics/statusline.hpp" #include "engine/user/cursor.hpp" #include "engine/user/input.hpp" #include "game/cell_helper.hpp" @@ -29,7 +32,6 @@ #include <fmt/core.h> #include <memory> -#include <random> #include <string> #include <string_view> #include <vector> @@ -48,8 +50,8 @@ auto bootstrap() noexcept -> yacppdic::Container const std::shared_ptr<ICursorController> &cursor_controller, const std::shared_ptr<IUserInputObserver> &user_input_observer) { - std::shared_ptr<IStatusLine> statusline = - container.get<IStatusLineFactory>()(cursor_controller, scene); + std::shared_ptr<IStatusLine> statusline = container.get<IStatusLineFactory>()( + Bounds({.width = scene->size().get_width(), .height = 1})); std::shared_ptr<IStatusManager> status_manager = container.get<IStatusManagerFactory>()(statusline); @@ -83,10 +85,12 @@ auto bootstrap() noexcept -> yacppdic::Container }); container.bind<IStatusLineFactory>().to_factory( - [](const std::shared_ptr<ICursorController> &cursor_controller, - const std::shared_ptr<IScene> &scene) + [&container](const Bounds &size) { - return std::make_unique<StatusLine>(cursor_controller, scene); + const auto matrix_factory = + container.get<IMatrixFactory<IComponent::ComponentMatrix::Element>>(); + + return std::make_unique<StatusLine>(matrix_factory(size)); }); container.bind<IStatusManagerFactory>().to_factory( @@ -107,5 +111,11 @@ auto bootstrap() noexcept -> yacppdic::Container return std::make_unique<CellHelper<char>>(matrix); }); + container.bind<IComponentRendererFactory>().to_factory( + [](const std::shared_ptr<ICursorController> &cursor_controller) + { + return std::make_unique<ComponentRenderer>(cursor_controller); + }); + return container; } diff --git a/src/engine/components/statusline.cpp b/src/engine/components/statusline.cpp new file mode 100644 index 0000000..ae0f3fa --- /dev/null +++ b/src/engine/components/statusline.cpp @@ -0,0 +1,99 @@ +#include "statusline.hpp" + +#include "engine/data/vector2.hpp" +#include "util/color.hpp" + +#include <fmt/color.h> + +#include <utility> + +StatusLine::StatusLine(std::shared_ptr<ComponentMatrix> component_matrix) noexcept + : _component_matrix(std::move(component_matrix)), _need_render(false) +{ + _component_matrix->fill(' '); +} + +auto StatusLine::get() const noexcept -> const std::shared_ptr<ComponentMatrix> & +{ + return _component_matrix; +} + +auto StatusLine::get_need_render() const noexcept -> bool +{ + return _need_render; +} + +void StatusLine::set_need_render(bool need_render) noexcept +{ + _need_render = need_render; +} + +auto StatusLine::get_foreground_color() const noexcept -> uint32_t +{ + return static_cast<uint32_t>(fmt::color::white); +} + +auto StatusLine::get_background_color() const noexcept -> uint32_t +{ + return STATUSLINE_COLOR; +} + +void StatusLine::set_status( + StatusLineSection section, + const std::string_view &status, + int32_t start) noexcept +{ + _clear_section(section, start); + + auto section_start = _get_section_start_x(section); + + auto pos = Vector2({.x = section_start + start, .y = 0}); + + for (const auto &character : status) + { + _component_matrix->set(pos, character); + + pos += Vector2::right(); + } + + set_need_render(true); +} + +void StatusLine::set_section_length(StatusLineSection section, int32_t length) noexcept +{ + _section_lengths[section] = length; +} + +auto StatusLine::_get_section_start_x(StatusLineSection section) const noexcept -> int32_t +{ + int32_t section_start = 0; + + auto section_index = static_cast<int32_t>(section); + + while (section_index > 0) + { + const auto prev_section = static_cast<StatusLineSection>(section_index - 1); + + section_start += static_cast<int32_t>(_section_lengths.at(prev_section)); + + section_index--; + } + + return section_start; +} + +void StatusLine::_clear_section(StatusLineSection section, int32_t start) noexcept +{ + auto section_start = _get_section_start_x(section); + + auto pos = Vector2({.x = section_start + start, .y = 0}); + + auto section_length = _section_lengths[section]; + + for (auto index = 0; index < section_length - start; index++) + { + _component_matrix->set(pos, ' '); + + pos += Vector2::right(); + } +} diff --git a/src/engine/components/statusline.hpp b/src/engine/components/statusline.hpp new file mode 100644 index 0000000..3fdb8d3 --- /dev/null +++ b/src/engine/components/statusline.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include "interfaces/matrix.hpp" +#include "interfaces/statusline.hpp" + +#include "engine/data/vector2.hpp" + +#include <memory> +#include <string_view> +#include <unordered_map> + +constexpr uint32_t STATUSLINE_COLOR = 0x1A1A1AU; + +class StatusLine : public IStatusLine +{ +public: + explicit StatusLine(std::shared_ptr<ComponentMatrix> component_matrix) noexcept; + + auto get() const noexcept -> const std::shared_ptr<ComponentMatrix> & override; + + [[nodiscard]] auto get_need_render() const noexcept -> bool override; + + void set_need_render(bool need_render) noexcept override; + + auto get_foreground_color() const noexcept -> uint32_t override; + + auto get_background_color() const noexcept -> uint32_t override; + + void set_status( + StatusLineSection section, + const std::string_view &status, + int32_t start) noexcept override; + + void set_section_length(StatusLineSection section, int32_t length) noexcept override; + +private: + std::shared_ptr<ComponentMatrix> _component_matrix; + std::unordered_map<StatusLineSection, int32_t> _section_lengths; + bool _need_render; + + [[nodiscard]] auto _get_section_start_x(StatusLineSection section) const noexcept + -> int32_t; + + void _clear_section(StatusLineSection section, int32_t start) noexcept; +}; diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 4775fe4..f947a45 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -9,10 +9,12 @@ CLIGameEngine::CLIGameEngine( IGameFactory game_factory, ISceneFactory scene_factory, + IComponentRendererFactory component_renderer_factory, std::shared_ptr<IUserInputObserver> user_input_observer, std::shared_ptr<ICursorController> cursor_controller) noexcept : _game_factory(std::move(game_factory)), _scene_factory(std::move(scene_factory)), + _component_renderer_factory(std::move(component_renderer_factory)), _user_input_observer(std::move(user_input_observer)), _cursor_controller(std::move(cursor_controller)) { @@ -22,6 +24,8 @@ void CLIGameEngine::start() noexcept { std::shared_ptr<IScene> scene = _scene_factory(_cursor_controller); + auto component_renderer = _component_renderer_factory(_cursor_controller); + scene->enter(); _cursor_controller->set_bounds(scene->size()); @@ -65,8 +69,17 @@ void CLIGameEngine::start() noexcept game->on_update(); - last_update_time = std::chrono::system_clock::now(); + for (auto [component, position] : scene->get_components()) + { + if (component->get_need_render()) + { + component_renderer->render(component, position); + component->set_need_render(false); + } + } _user_input_observer->clear_currently_pressed(); + + last_update_time = std::chrono::system_clock::now(); } } diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index 1562bbb..a274ed4 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -1,5 +1,6 @@ #pragma once +#include "interfaces/component_renderer.hpp" #include "interfaces/cursor.hpp" #include "interfaces/engine.hpp" #include "interfaces/game.hpp" @@ -19,6 +20,7 @@ class CLIGameEngine : public ICLIGameEngine, CLIGameEngine, IGameFactory, ISceneFactory, + IComponentRendererFactory, IUserInputObserver, ICursorController> { @@ -26,6 +28,7 @@ public: CLIGameEngine( IGameFactory game_factory, ISceneFactory scene_factory, + IComponentRendererFactory component_renderer_factory, std::shared_ptr<IUserInputObserver> user_input_observer, std::shared_ptr<ICursorController> cursor_controller) noexcept; @@ -34,6 +37,7 @@ public: private: IGameFactory _game_factory; ISceneFactory _scene_factory; + IComponentRendererFactory _component_renderer_factory; std::shared_ptr<IUserInputObserver> _user_input_observer; std::shared_ptr<ICursorController> _cursor_controller; 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; -}; diff --git a/src/game/game.cpp b/src/game/game.cpp index 064b643..8d72324 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -27,6 +27,8 @@ Game::Game( void Game::on_start() noexcept { + _scene->register_component(_status_manager->get_statusline(), Vector2({0, 0})); + _status_manager->initialize(); _status_manager->set_section_title(StatusLineSection::A, ""); diff --git a/src/game/status_manager.cpp b/src/game/status_manager.cpp index 3f2df01..c7d80e9 100644 --- a/src/game/status_manager.cpp +++ b/src/game/status_manager.cpp @@ -12,23 +12,21 @@ StatusManager::StatusManager(std::shared_ptr<IStatusLine> statusline) noexcept void StatusManager::initialize() noexcept { - _statusline->set_section_length(StatusLineSection::A, 5U); - _statusline->set_section_length(StatusLineSection::B, 15U); - _statusline->set_section_length(StatusLineSection::C, 15U); - _statusline->set_section_length(StatusLineSection::D, 20U); - _statusline->set_section_length(StatusLineSection::E, 25U); - _statusline->set_section_length(StatusLineSection::F, 60U); - _statusline->set_section_length(StatusLineSection::G, 30U); - _statusline->set_section_length(StatusLineSection::H, 30U); - - _statusline->initialize_background(); + _statusline->set_section_length(StatusLineSection::A, 5); + _statusline->set_section_length(StatusLineSection::B, 15); + _statusline->set_section_length(StatusLineSection::C, 15); + _statusline->set_section_length(StatusLineSection::D, 20); + _statusline->set_section_length(StatusLineSection::E, 25); + _statusline->set_section_length(StatusLineSection::F, 60); + _statusline->set_section_length(StatusLineSection::G, 30); + _statusline->set_section_length(StatusLineSection::H, 30); } void StatusManager::set_section_title( const StatusLineSection §ion, const std::string_view &title) noexcept { - if (_title_lengths.count(section) != 0) + if (_title_lengths.contains(section)) { fmt::print(stderr, "Error: can't set statusbar section title more than once"); return; @@ -36,7 +34,7 @@ void StatusManager::set_section_title( _statusline->set_status(section, title); - _title_lengths[section] = title.length(); + _title_lengths[section] = static_cast<int>(title.length()); } void StatusManager::set_section_body( @@ -47,3 +45,8 @@ void StatusManager::set_section_body( _statusline->set_status(section, body, section_title_length + 1); } + +auto StatusManager::get_statusline() const noexcept -> std::shared_ptr<IStatusLine> +{ + return _statusline; +} diff --git a/src/game/status_manager.hpp b/src/game/status_manager.hpp index 7bb4f2f..245e888 100644 --- a/src/game/status_manager.hpp +++ b/src/game/status_manager.hpp @@ -24,8 +24,10 @@ public: const StatusLineSection §ion, const std::string_view &body) noexcept override; + auto get_statusline() const noexcept -> std::shared_ptr<IStatusLine> override; + private: std::shared_ptr<IStatusLine> _statusline; - std::unordered_map<StatusLineSection, std::size_t> _title_lengths; + std::unordered_map<StatusLineSection, int32_t> _title_lengths; }; diff --git a/src/interfaces/component.hpp b/src/interfaces/component.hpp new file mode 100644 index 0000000..7e1b132 --- /dev/null +++ b/src/interfaces/component.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "interfaces/matrix.hpp" + +#include <memory> + +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) +class IComponent +{ +public: + using ComponentMatrix = IMatrix<char>; + + virtual ~IComponent() = default; + + [[nodiscard]] virtual auto get() const noexcept + -> const std::shared_ptr<ComponentMatrix> & = 0; + + [[nodiscard]] virtual auto get_need_render() const noexcept -> bool = 0; + + virtual void set_need_render(bool need_render) noexcept = 0; + + [[nodiscard]] virtual auto get_foreground_color() const noexcept -> uint32_t = 0; + + [[nodiscard]] virtual auto get_background_color() const noexcept -> uint32_t = 0; +}; diff --git a/src/interfaces/component_renderer.hpp b/src/interfaces/component_renderer.hpp new file mode 100644 index 0000000..fa8bc18 --- /dev/null +++ b/src/interfaces/component_renderer.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "interfaces/component.hpp" +#include "interfaces/cursor.hpp" + +#include "engine/data/vector2.hpp" + +#include <yacppdic/factory.hpp> + +#include <memory> + +// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) +class IComponentRenderer +{ +public: + virtual ~IComponentRenderer() = default; + + virtual void render( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept = 0; +}; + +using IComponentRendererFactory = yacppdic::Factory<std::unique_ptr<IComponentRenderer>( + const std::shared_ptr<ICursorController> &)>; diff --git a/src/interfaces/matrix.hpp b/src/interfaces/matrix.hpp index 010138f..3ab102c 100644 --- a/src/interfaces/matrix.hpp +++ b/src/interfaces/matrix.hpp @@ -8,11 +8,13 @@ #include <memory> -template <typename Element> +template <typename ElementType> // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) class IMatrix { public: + using Element = ElementType; + virtual ~IMatrix() noexcept = default; virtual void fill(Element element) noexcept = 0; diff --git a/src/interfaces/scene.hpp b/src/interfaces/scene.hpp index 903a299..daabb10 100644 --- a/src/interfaces/scene.hpp +++ b/src/interfaces/scene.hpp @@ -1,14 +1,18 @@ #pragma once +#include "interfaces/component.hpp" #include "interfaces/cursor.hpp" #include "interfaces/matrix.hpp" #include "engine/data/bounds.hpp" +#include "engine/data/vector2.hpp" #include <yacppdic/factory.hpp> #include <memory> #include <string_view> +#include <utility> +#include <vector> // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) class IScene @@ -26,6 +30,13 @@ public: [[nodiscard]] virtual auto get_matrix() const noexcept -> const std::shared_ptr<IMatrix<MatrixElement>> & = 0; + + virtual void register_component( + const std::shared_ptr<IComponent> &component, + const Vector2 &position) noexcept = 0; + + [[nodiscard]] virtual auto get_components() const noexcept + -> std::vector<std::pair<std::shared_ptr<IComponent>, Vector2>> = 0; }; using ISceneFactory = yacppdic::Factory<std::unique_ptr<IScene>( diff --git a/src/interfaces/status_manager.hpp b/src/interfaces/status_manager.hpp index 18d6928..9c7234e 100644 --- a/src/interfaces/status_manager.hpp +++ b/src/interfaces/status_manager.hpp @@ -22,6 +22,9 @@ public: virtual void set_section_body( const StatusLineSection §ion, const std::string_view &body) noexcept = 0; + + [[nodiscard]] virtual auto get_statusline() const noexcept + -> std::shared_ptr<IStatusLine> = 0; }; using IStatusManagerFactory = yacppdic::Factory<std::unique_ptr<IStatusManager>( diff --git a/src/interfaces/statusline.hpp b/src/interfaces/statusline.hpp index a71699e..ae25fd9 100644 --- a/src/interfaces/statusline.hpp +++ b/src/interfaces/statusline.hpp @@ -1,7 +1,8 @@ #pragma once -#include "interfaces/cursor.hpp" -#include "interfaces/scene.hpp" +#include "interfaces/component.hpp" + +#include "engine/data/bounds.hpp" #include <yacppdic/factory.hpp> @@ -20,22 +21,18 @@ enum StatusLineSection }; // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions) -class IStatusLine +class IStatusLine : public IComponent { public: - virtual ~IStatusLine() noexcept = default; - - virtual void initialize_background() noexcept = 0; - + // NOLINTNEXTLINE(google-default-arguments) virtual void set_status( - const StatusLineSection §ion, + StatusLineSection section, const std::string_view &status, - std::size_t start = 1UL) noexcept = 0; + int32_t start = 1) noexcept = 0; virtual void - set_section_length(const StatusLineSection §ion, uint32_t length) noexcept = 0; + set_section_length(StatusLineSection section, int32_t length) noexcept = 0; }; -using IStatusLineFactory = yacppdic::Factory<std::unique_ptr<IStatusLine>( - const std::shared_ptr<ICursorController> &cursor_controller, - const std::shared_ptr<IScene> &scene)>; +using IStatusLineFactory = + yacppdic::Factory<std::unique_ptr<IStatusLine>(const Bounds &size)>; |