From ea8840291cdf66784c6a2cb465b63ccfb5483c38 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 24 Sep 2014 01:38:30 +0200 Subject: New event-driven goodness --- src/control/InputHandler.hpp | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'src/control/InputHandler.hpp') diff --git a/src/control/InputHandler.hpp b/src/control/InputHandler.hpp index eba94d1..1a34982 100644 --- a/src/control/InputHandler.hpp +++ b/src/control/InputHandler.hpp @@ -26,10 +26,10 @@ #pragma once -#include - #include +#include #include +#include namespace RPGEdit { @@ -37,34 +37,35 @@ namespace RPGEdit { namespace Control { class InputHandler { +public: + typedef std::function Listener; + private: + std::vector listeners; + std::unordered_set pressedKeys; - std::unordered_set unhandledKeys; public: - void keyPressed(SDL_Scancode key) { - pressedKeys.insert(key); - unhandledKeys.insert(key); + void registerListener(const Listener &listener) { + listeners.push_back(listener); } - void keyReleased(SDL_Scancode key) { - pressedKeys.erase(key); - } + void keyPressed(uint16_t key, uint64_t time) { + pressedKeys.insert(key); - void keyHandled(SDL_Scancode key) { - unhandledKeys.erase(key); + for (auto &listener : listeners) + listener(key, true, time); } - void resetHandled() { - unhandledKeys.clear(); - } + void keyReleased(uint16_t key, uint64_t time) { + pressedKeys.erase(key); - bool isKeyPressed(SDL_Scancode key) { - return pressedKeys.count(key); + for (auto &listener : listeners) + listener(key, false, time); } - bool isKeyUnhandled(SDL_Scancode key) { - return unhandledKeys.count(key); + bool isKeyPressed(uint16_t key) { + return pressedKeys.count(key); } }; -- cgit v1.2.3