aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-08 19:23:55 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:57:01 +0200
commiteb66598c326862fd9dfc1899be4eac93f81a8023 (patch)
tree75c74e7d3a180952d9b2efc081aa4d4ef165da60
parent0f9f82c875fef4c8057a994552aa7d7a702b411a (diff)
fix: prevent A, B, C & D from being interpreted as arrow keys
-rw-r--r--src/engine/engine.cpp2
-rw-r--r--src/engine/keycodes.hpp (renamed from src/game/keycodes.hpp)8
-rw-r--r--src/engine/user/input.cpp65
-rw-r--r--src/engine/user/input.hpp13
-rw-r--r--src/game/game.cpp2
-rw-r--r--src/interfaces/input.hpp6
6 files changed, 60 insertions, 36 deletions
diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp
index c569c69..ed75394 100644
--- a/src/engine/engine.cpp
+++ b/src/engine/engine.cpp
@@ -76,8 +76,6 @@ void CLIGameEngine::start() noexcept
}
}
- _user_input_observer->clear_currently_pressed();
-
last_update_time = std::chrono::system_clock::now();
}
}
diff --git a/src/game/keycodes.hpp b/src/engine/keycodes.hpp
index 84ca5a7..163df52 100644
--- a/src/game/keycodes.hpp
+++ b/src/engine/keycodes.hpp
@@ -7,9 +7,9 @@ constexpr auto ENTER = 10;
constexpr auto ESCAPE = 27;
constexpr auto BACKSPACE = 127;
-constexpr auto UP_ARROW = 65;
-constexpr auto DOWN_ARROW = 66;
-constexpr auto RIGHT_ARROW = 67;
-constexpr auto LEFT_ARROW = 68;
+constexpr auto UP_ARROW = -10;
+constexpr auto DOWN_ARROW = -11;
+constexpr auto RIGHT_ARROW = -12;
+constexpr auto LEFT_ARROW = -13;
} // namespace keycodes
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 <iostream>
#include <unistd.h>
+#include <unordered_map>
-#include <iostream>
+const std::unordered_map<char, char> 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 <memory>
#include <mutex>
+#include <string>
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;
};
diff --git a/src/game/game.cpp b/src/game/game.cpp
index 879f096..73ab7f5 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -2,7 +2,7 @@
#include "engine/data/bounds.hpp"
#include "engine/escape.hpp"
-#include "game/keycodes.hpp"
+#include "engine/keycodes.hpp"
#include "util/algorithm.hpp"
#include "util/string.hpp"
diff --git a/src/interfaces/input.hpp b/src/interfaces/input.hpp
index 3558363..a3019ad 100644
--- a/src/interfaces/input.hpp
+++ b/src/interfaces/input.hpp
@@ -13,9 +13,5 @@ public:
virtual void listen() noexcept = 0;
- virtual auto is_key_pressed(Key key) noexcept -> bool = 0;
-
- [[nodiscard]] virtual auto get_currently_pressed_key() const noexcept -> Key = 0;
-
- virtual void clear_currently_pressed() noexcept = 0;
+ virtual auto get_currently_pressed_key() noexcept -> Key = 0;
};