diff --git a/latebit/core/events/EventInput.cpp b/latebit/core/events/EventInput.cpp deleted file mode 100644 index f9989d0..0000000 --- a/latebit/core/events/EventInput.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "EventInput.h" - -namespace lb { - -auto EventInput::getKey() const -> InputKey::InputKey { return this->key; } - -auto EventInput::getAction() const -> InputAction::InputAction { - return this->action; -} - -} // namespace lb diff --git a/latebit/core/events/EventInput.h b/latebit/core/events/EventInput.h index 0abface..5008238 100644 --- a/latebit/core/events/EventInput.h +++ b/latebit/core/events/EventInput.h @@ -16,23 +16,29 @@ class EventInput : public Event { const InputAction::InputAction action = InputAction::UNDEFINED_ACTION; // The event type const string type = INPUT_EVENT; + // If true, the event is a repeat of a previous event. Used to detect long + // pressed, for example + const bool repeat = false; public: - EventInput() - : Event(INPUT_EVENT), - key(InputKey::UNDEFINED_KEY), - action(InputAction::UNDEFINED_ACTION) {}; + EventInput() : Event(INPUT_EVENT){}; - // Create input event with an InputKey representing the control being acted - // on, and an action representing the action on the given control - EventInput(InputKey::InputKey k, InputAction::InputAction a) - : Event(INPUT_EVENT), key(k), action(a) {}; + // Create input event with an InputKey representing the button being pressed, + // whether it's a press or a release, and if it's repeated over time + EventInput(InputKey::InputKey k, InputAction::InputAction a, + bool repeat = false) + : Event(INPUT_EVENT), key(k), action(a), repeat(repeat){}; // Return the key value for this event - [[nodiscard]] auto getKey() const -> InputKey::InputKey; + [[nodiscard]] auto getKey() const -> InputKey::InputKey { return key; } // Return the action for this event - [[nodiscard]] auto getAction() const -> InputAction::InputAction; + [[nodiscard]] auto getAction() const -> InputAction::InputAction { + return action; + } + + // Return true if the event is repeated + [[nodiscard]] auto getRepeat() const -> bool { return repeat; } }; } // namespace lb diff --git a/latebit/core/events/EventInput.test.cpp b/latebit/core/events/EventInput.test.cpp index 3f9bab6..0e34eb0 100644 --- a/latebit/core/events/EventInput.test.cpp +++ b/latebit/core/events/EventInput.test.cpp @@ -12,10 +12,11 @@ auto main() -> int { }); test("constructor with parameters", []() { - EventInput event(InputKey::A, InputAction::PRESSED); + EventInput event(InputKey::A, InputAction::PRESSED, true); assertEq("sets correct key", event.getKey(), InputKey::A); assertEq("sets correct keyboard action", event.getAction(), InputAction::PRESSED); + assertEq("sets correct repeat", event.getRepeat(), true); }); return report(); diff --git a/latebit/core/input/InputManager.cpp b/latebit/core/input/InputManager.cpp index 7456cb6..afff4e6 100644 --- a/latebit/core/input/InputManager.cpp +++ b/latebit/core/input/InputManager.cpp @@ -42,23 +42,26 @@ auto InputManager::isValid(string eventType) const -> bool { void InputManager::getInput() const { SDL_Event event; + auto lastKey = InputKey::UNDEFINED_KEY; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: { const auto key = fromSDLKeyCode(event.key.keysym.sym); - if (key == InputKey::UNDEFINED_KEY) return; + if (key == InputKey::UNDEFINED_KEY) break; - const auto evt = EventInput(key, InputAction::PRESSED); + const auto evt = EventInput(key, InputAction::PRESSED, event.key.repeat != 0 && key == lastKey); broadcast(&evt); + lastKey = key; break; } case SDL_KEYUP: { const auto key = fromSDLKeyCode(event.key.keysym.sym); - if (key == InputKey::UNDEFINED_KEY) return; + if (key == InputKey::UNDEFINED_KEY) break; - const auto evt = EventInput(key, InputAction::RELEASED); + const auto evt = EventInput(key, InputAction::RELEASED, event.key.repeat != 0 && key == lastKey); broadcast(&evt); + lastKey = key; break; } case SDL_QUIT: