From eb66598c326862fd9dfc1899be4eac93f81a8023 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 8 Jun 2022 19:23:55 +0200 Subject: fix: prevent A, B, C & D from being interpreted as arrow keys --- src/engine/user/input.cpp | 65 ++++++++++++++++++++++++++++++++++------------- src/engine/user/input.hpp | 13 +++++----- 2 files changed, 54 insertions(+), 24 deletions(-) (limited to 'src/engine/user') diff --git a/src/engine/user/input.cpp b/src/engine/user/input.cpp index cbba143..f0ffbe5 100644 --- a/src/engine/user/input.cpp +++ b/src/engine/user/input.cpp @@ -1,9 +1,16 @@ #include "input.hpp" +#include "engine/keycodes.hpp" + #include #include +#include -#include +const std::unordered_map special_keys_map = { + {'A', keycodes::UP_ARROW}, + {'B', keycodes::DOWN_ARROW}, + {'C', keycodes::RIGHT_ARROW}, + {'D', keycodes::LEFT_ARROW}}; void UserInputObserver::listen() noexcept { @@ -16,38 +23,60 @@ void UserInputObserver::listen() noexcept continue; } - _currently_pressed_mutex.lock(); + _input_buffer_mutex.lock(); - if (character != _currently_pressed) - { - _currently_pressed = character; - } + _input_buffer += character; - _currently_pressed_mutex.unlock(); + _input_buffer_mutex.unlock(); } } -bool UserInputObserver::is_key_pressed(Key key) noexcept +auto UserInputObserver::get_currently_pressed_key() noexcept -> Key { - _currently_pressed_mutex.lock(); + auto input_buffer = _get_input_buffer(); - const auto is_key_pressed = key == _currently_pressed; + if (input_buffer.empty()) + { + return 0; + } + + auto character = input_buffer.front(); - _currently_pressed_mutex.unlock(); + // Special treatment for arrow keys + if (input_buffer.length() == 3 && character == keycodes::ESCAPE) + { + const auto arrow_key_code = input_buffer.at(2); - return is_key_pressed; + if (arrow_key_code >= 'A' && arrow_key_code <= 'D') + { + character = special_keys_map.at(arrow_key_code); + } + + input_buffer = input_buffer.substr(2); + } + + input_buffer = input_buffer.substr(1); + _set_input_buffer(input_buffer); + + return character; } -auto UserInputObserver::get_currently_pressed_key() const noexcept -> Key +auto UserInputObserver::_get_input_buffer() const noexcept -> std::string { - return _currently_pressed; + _input_buffer_mutex.lock(); + + auto input_buffer = _input_buffer; + + _input_buffer_mutex.unlock(); + + return input_buffer; } -void UserInputObserver::clear_currently_pressed() noexcept +void UserInputObserver::_set_input_buffer(const std::string &input_buffer) noexcept { - _currently_pressed_mutex.lock(); + _input_buffer_mutex.lock(); - _currently_pressed = 0; + _input_buffer = input_buffer; - _currently_pressed_mutex.unlock(); + _input_buffer_mutex.unlock(); } diff --git a/src/engine/user/input.hpp b/src/engine/user/input.hpp index 9f565c1..12c27ee 100644 --- a/src/engine/user/input.hpp +++ b/src/engine/user/input.hpp @@ -6,6 +6,7 @@ #include #include +#include class UserInputObserver : public IUserInputObserver, @@ -16,13 +17,13 @@ public: void listen() noexcept override; - auto is_key_pressed(Key key) noexcept -> bool override; + auto get_currently_pressed_key() noexcept -> Key override; - [[nodiscard]] auto get_currently_pressed_key() const noexcept -> Key override; +private: + std::string _input_buffer; + mutable std::mutex _input_buffer_mutex; - void clear_currently_pressed() noexcept override; + auto _get_input_buffer() const noexcept -> std::string; -private: - Key _currently_pressed{}; - std::mutex _currently_pressed_mutex; + void _set_input_buffer(const std::string &input_buffer) noexcept; }; -- cgit v1.2.3-18-g5258