diff options
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/graphics/scene.cpp | 5 | ||||
-rw-r--r-- | src/engine/user/cursor.cpp | 36 | ||||
-rw-r--r-- | src/engine/user/cursor.hpp | 9 |
3 files changed, 46 insertions, 4 deletions
diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp index 476b966..4e382fe 100644 --- a/src/engine/graphics/scene.cpp +++ b/src/engine/graphics/scene.cpp @@ -52,7 +52,8 @@ void Scene::write_status(const std::string_view &str) noexcept const auto window_size = _window->size(); _cursor_controller->move_to( - Vector2({.x = 2, .y = static_cast<Vector2::Value>(window_size.get_height())})); + Vector2({.x = 2, .y = static_cast<Vector2::Value>(window_size.get_height())}), + true); auto background_color = get_background_esc_seq(STATUSBAR_COLOR); @@ -61,7 +62,7 @@ void Scene::write_status(const std::string_view &str) noexcept fmt::print(fmt::runtime(str.data())); fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC)); - _cursor_controller->move_to(previous_position); + _cursor_controller->move_to(previous_position, true); } const std::shared_ptr<IMatrix<std::string_view>> &Scene::get_matrix() const noexcept diff --git a/src/engine/user/cursor.cpp b/src/engine/user/cursor.cpp index b02a9bc..2a72663 100644 --- a/src/engine/user/cursor.cpp +++ b/src/engine/user/cursor.cpp @@ -16,15 +16,22 @@ void CursorController::move(const Vector2 &direction, const uint32_t &amount) no std::cout.flush(); _position = _position.to_direction(direction, static_cast<Vector2::Value>(amount)); + + notify_subscribers(CursorEvent::POSITION_CHANGE, _position); } -void CursorController::move_to(const Vector2 &position) noexcept +void CursorController::move_to(const Vector2 &position, bool silent) noexcept { fmt::print(MOVE_CURSOR_TO, fmt::arg("esc", ESC), fmt::arg("row", position.get_y()), fmt::arg("column", position.get_x())); std::cout.flush(); _position = position; + + if (!silent) + { + notify_subscribers(CursorEvent::POSITION_CHANGE, position); + } } Vector2 CursorController::where() const noexcept @@ -43,6 +50,8 @@ void CursorController::ensure_position() noexcept scanf("\033[%u;%uR", &vector2_options.y, &vector2_options.x); _position = Vector2(vector2_options); + + notify_subscribers(CursorEvent::POSITION_CHANGE, _position); } void CursorController::hide() noexcept @@ -56,3 +65,28 @@ void CursorController::show() noexcept fmt::print(CURSOR_VISIBLE, fmt::arg("esc", ESC)); std::cout.flush(); } + +void CursorController::subscribe(const Event &event, + const Subscriber &subscriber) noexcept +{ + if (_subscribers.count(event) == 0) + { + _subscribers.insert({event, std::vector<Subscriber>()}); + } + + _subscribers.at(event).push_back(subscriber); +} + +void CursorController::notify_subscribers(const Event &event, + const Context &context) const noexcept +{ + if (_subscribers.count(event) == 0) + { + return; + } + + for (const auto &subscriber : _subscribers.at(event)) + { + subscriber->update(context); + } +} diff --git a/src/engine/user/cursor.hpp b/src/engine/user/cursor.hpp index 713ee31..3ba8a40 100644 --- a/src/engine/user/cursor.hpp +++ b/src/engine/user/cursor.hpp @@ -37,7 +37,7 @@ public: void move(const Vector2 &direction, const uint32_t &amount) noexcept override; - void move_to(const Vector2 &position) noexcept override; + void move_to(const Vector2 &position, bool silent) noexcept override; [[nodiscard]] Vector2 where() const noexcept override; @@ -47,6 +47,13 @@ public: static void show() noexcept; + void subscribe(const Event &event, const Subscriber &subscriber) noexcept override; + + void notify_subscribers(const Event &event, + const Context &context) const noexcept override; + private: Vector2 _position; + + std::unordered_map<CursorEvent, std::vector<Subscriber>> _subscribers; }; |