From 6d66d5675d0fb78827bc47c49f9d4a1852c7255d Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 8 Jun 2022 18:31:58 +0200 Subject: feat: implement command mode --- src/game/game.hpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'src/game/game.hpp') diff --git a/src/game/game.hpp b/src/game/game.hpp index 8363c4d..a35c0ce 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -14,14 +14,41 @@ #include #include +#include #include #include +#include +#include +#include +#include constexpr auto DEFAULT_MIN_TIME_SINCE_LAST_GEN_MILLIS = 200; constexpr auto MIN_TIME_SINCE_LAST_GEN_INCREMENT = 50; constexpr auto MIN_TIME_SINCE_LAST_GEN_DECREMENT = 50; +constexpr auto CURSOR_FALLBACK_POS_X = 10; +constexpr auto CURSOR_FALLBACK_POS_Y = 10; + +constexpr std::string_view ERASE_ENTIRE_LINE = "{esc}[2K"; +constexpr std::string_view ERASE_LINE_FROM_CURSOR = "{esc}[0K"; + +enum Mode +{ + NORMAL, + COMMAND +}; + +class CommandInfo +{ +public: + using Options = const std::vector &; + using CommandFunction = std::function; + + std::size_t option_cnt; + CommandFunction function; +}; + class Game : public IGame { public: @@ -50,19 +77,38 @@ private: std::shared_ptr _user_input_observer; std::shared_ptr _cell_helper; - using TimePoint = std::chrono::system_clock::time_point; + Mode _current_mode; - TimePoint _last_update_time; - TimePoint _last_gen_update_time; + std::optional _last_pos_before_command_mode; + + std::string _command_mode_input; + + std::unordered_map _commands; + + std::chrono::system_clock::time_point _last_gen_update_time; int32_t _min_time_since_last_gen_millis = DEFAULT_MIN_TIME_SINCE_LAST_GEN_MILLIS; std::list _living_cell_positions; + void _on_normal_mode_update() noexcept; + + void _on_command_mode_update() noexcept; + + void _return_to_normal_mode() noexcept; + + void _run_command(const std::string &command) noexcept; + + void _show_command_error(const std::string_view &error_message) noexcept; + auto _move_cursor(const Vector2 &direction) noexcept -> bool; void _set_space( const std::shared_ptr> &matrix, const Vector2 &position, char character) noexcept; + + static void _erase_entire_line() noexcept; + + static void _erase_line_from_cursor() noexcept; }; -- cgit v1.2.3-18-g5258