aboutsummaryrefslogtreecommitdiff
path: root/src/engine/user
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/user')
-rw-r--r--src/engine/user/input.cpp68
-rw-r--r--src/engine/user/input.hpp37
2 files changed, 34 insertions, 71 deletions
diff --git a/src/engine/user/input.cpp b/src/engine/user/input.cpp
index 7e9fe22..ac6d660 100644
--- a/src/engine/user/input.cpp
+++ b/src/engine/user/input.cpp
@@ -3,66 +3,46 @@
#include <iostream>
#include <unistd.h>
-void InputHandler::listen() const noexcept
-{
- char character = 0;
+#include <iostream>
- while (!std::cin.read(&character, 1).fail())
+void UserInputObserver::listen() noexcept
+{
+ while (true)
{
- notify_subscribers(character, nullptr);
- }
-}
+ char character = 0;
-void InputHandler::subscribe(
- const Event &event,
- const std::shared_ptr<ISubscriber<Context>> &subscriber) noexcept
-{
- auto event_index = _event_as_index(event);
+ if (std::cin.read(&character, 1).fail())
+ {
+ continue;
+ }
- _subscribers.at(event_index).push_back(subscriber);
-}
+ _currently_pressed_mutex.lock();
-void InputHandler::notify_subscribers(const Event &event, const Context &context)
- const noexcept
-{
- auto event_index = _event_as_index(event);
+ if (character != _currently_pressed)
+ {
+ _currently_pressed = character;
+ }
- for (const auto &subscriber : _subscribers.at(event_index))
- {
- subscriber->update(context);
+ _currently_pressed_mutex.unlock();
}
}
-void InputHandler::enter_raw_mode() noexcept
+bool UserInputObserver::is_key_pressed(Key key) noexcept
{
- if (_original_termios == nullptr)
- {
- _original_termios = std::make_shared<termios>();
- }
+ _currently_pressed_mutex.lock();
- tcgetattr(STDIN_FILENO, _original_termios.get());
+ const auto is_key_pressed = key == _currently_pressed;
- auto raw_termios = termios(*_original_termios);
+ _currently_pressed_mutex.unlock();
- raw_termios.c_lflag &= static_cast<uint32_t>(~(ECHO | ICANON | ISIG));
-
- tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_termios);
+ return is_key_pressed;
}
-void InputHandler::leave_raw_mode() noexcept
+void UserInputObserver::clear_currently_pressed() noexcept
{
- if (_original_termios == nullptr)
- {
- return;
- }
-
- tcsetattr(STDIN_FILENO, TCSAFLUSH, _original_termios.get());
+ _currently_pressed_mutex.lock();
- _original_termios = nullptr;
-}
+ _currently_pressed = 0;
-auto InputHandler::_event_as_index(const Event &event) noexcept
- -> InputHandler::SubscribersSizeType
-{
- return static_cast<SubscribersSizeType>(static_cast<uint8_t>(event));
+ _currently_pressed_mutex.unlock();
}
diff --git a/src/engine/user/input.hpp b/src/engine/user/input.hpp
index f17472b..2fb97ac 100644
--- a/src/engine/user/input.hpp
+++ b/src/engine/user/input.hpp
@@ -5,40 +5,23 @@
#include <yacppdic/auto_wirable.hpp>
-#include <array>
-#include <climits>
#include <memory>
-#include <termios.h>
-#include <vector>
+#include <mutex>
-class InputHandler : public IInputHandler,
- public yacppdic::AutoWirable<IInputHandler, InputHandler>
+class UserInputObserver
+ : public IUserInputObserver,
+ public yacppdic::AutoWirable<IUserInputObserver, UserInputObserver>
{
public:
- InputHandler() noexcept = default;
+ UserInputObserver() noexcept = default;
- void listen() const noexcept override;
+ void listen() noexcept override;
- void subscribe(
- const Event &event,
- const std::shared_ptr<ISubscriber<Context>> &subscriber) noexcept override;
+ bool is_key_pressed(Key key) noexcept override;
- void notify_subscribers(const Event &event, const Context &context)
- const noexcept override;
-
- void enter_raw_mode() noexcept override;
-
- void leave_raw_mode() noexcept override;
+ void clear_currently_pressed() noexcept override;
private:
- std::array<
- std::vector<std::shared_ptr<ISubscriber<Context>>>,
- static_cast<std::size_t>(CHAR_MAX * 2U)>
- _subscribers;
-
- std::shared_ptr<termios> _original_termios = nullptr;
-
- using SubscribersSizeType = decltype(_subscribers)::size_type;
-
- static auto _event_as_index(const Event &event) noexcept -> SubscribersSizeType;
+ Key _currently_pressed;
+ std::mutex _currently_pressed_mutex;
};