diff --git a/include/InputManager.h b/include/InputManager.h index 3e33b0f..ce4a4f4 100644 --- a/include/InputManager.h +++ b/include/InputManager.h @@ -8,15 +8,115 @@ class InputManager { public: + enum class EventType { + KeyDown, + KeyUp + }; + + enum class Key + { + UP, + DOWN, + LEFT, + RIGHT, + SPACE, + ENTER, + ESCAPE, + TAB, + BACKSPACE, + DELETE, + HOME, + END, + PAGE_UP, + PAGE_DOWN, + INSERT, + CAPS_LOCK, + LEFT_SHIFT, + RIGHT_SHIFT, + LEFT_CTRL, + RIGHT_CTRL, + LEFT_ALT, + RIGHT_ALT, + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + NUM_0, + NUM_1, + NUM_2, + NUM_3, + NUM_4, + NUM_5, + NUM_6, + NUM_7, + NUM_8, + NUM_9, + LEFT_BRACKET, + RIGHT_BRACKET, + SEMICOLON, + APOSTROPHE, + COMMA, + PERIOD, + SLASH, + BACKSLASH, + GRAVE + }; + InputManager(); ~InputManager(); - void init(); + void init(); // see if necessary void processEvents(); - void registerAction(const std::string& actionName, std::function callback); + void registerAction(const std::string& actionName, EventType eventType, Key key, std::function callback); + bool isKeyPressed(Key key); private: - std::map> actionBindings; - std::map> actionCallbacks; + std::map keyMap; + std::map eventMap; + + struct ActionBinding { + std::string actionName; // not strictly necessary, but good for debugging purposes + EventType eventType; + Key key; + std::function callback; + }; + std::vector actionBindings; + + void initKeyMap(); + void initEventMap(); void handleEvent(const SDL_Event& event); }; \ No newline at end of file diff --git a/src/InputManager.cpp b/src/InputManager.cpp index e0223b4..2f5cdad 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -1,7 +1,10 @@ #include "InputManager.h" #include -InputManager::InputManager() {} +InputManager::InputManager() { + initKeyMap(); + initEventMap(); +} InputManager::~InputManager() { SDL_Quit(); @@ -14,26 +17,118 @@ void InputManager::init() { } } +void InputManager::initKeyMap() { + keyMap = { + {Key::UP, SDL_SCANCODE_UP}, + {Key::DOWN, SDL_SCANCODE_DOWN}, + {Key::LEFT, SDL_SCANCODE_LEFT}, + {Key::RIGHT, SDL_SCANCODE_RIGHT}, + {Key::SPACE, SDL_SCANCODE_SPACE}, + {Key::ENTER, SDL_SCANCODE_RETURN}, + {Key::ESCAPE, SDL_SCANCODE_ESCAPE}, + {Key::TAB, SDL_SCANCODE_TAB}, + {Key::BACKSPACE, SDL_SCANCODE_BACKSPACE}, + {Key::DELETE, SDL_SCANCODE_DELETE}, + {Key::HOME, SDL_SCANCODE_HOME}, + {Key::END, SDL_SCANCODE_END}, + {Key::PAGE_UP, SDL_SCANCODE_PAGEUP}, + {Key::PAGE_DOWN, SDL_SCANCODE_PAGEDOWN}, + {Key::INSERT, SDL_SCANCODE_INSERT}, + {Key::CAPS_LOCK, SDL_SCANCODE_CAPSLOCK}, + {Key::LEFT_SHIFT, SDL_SCANCODE_LSHIFT}, + {Key::RIGHT_SHIFT, SDL_SCANCODE_RSHIFT}, + {Key::LEFT_CTRL, SDL_SCANCODE_LCTRL}, + {Key::RIGHT_CTRL, SDL_SCANCODE_RCTRL}, + {Key::LEFT_ALT, SDL_SCANCODE_LALT}, + {Key::RIGHT_ALT, SDL_SCANCODE_RALT}, + {Key::F1, SDL_SCANCODE_F1}, + {Key::F2, SDL_SCANCODE_F2}, + {Key::F3, SDL_SCANCODE_F3}, + {Key::F4, SDL_SCANCODE_F4}, + {Key::F5, SDL_SCANCODE_F5}, + {Key::F6, SDL_SCANCODE_F6}, + {Key::F7, SDL_SCANCODE_F7}, + {Key::F8, SDL_SCANCODE_F8}, + {Key::F9, SDL_SCANCODE_F9}, + {Key::F10, SDL_SCANCODE_F10}, + {Key::F11, SDL_SCANCODE_F11}, + {Key::F12, SDL_SCANCODE_F12}, + {Key::A, SDL_SCANCODE_A}, + {Key::B, SDL_SCANCODE_B}, + {Key::C, SDL_SCANCODE_C}, + {Key::D, SDL_SCANCODE_D}, + {Key::E, SDL_SCANCODE_E}, + {Key::F, SDL_SCANCODE_F}, + {Key::G, SDL_SCANCODE_G}, + {Key::H, SDL_SCANCODE_H}, + {Key::I, SDL_SCANCODE_I}, + {Key::J, SDL_SCANCODE_J}, + {Key::K, SDL_SCANCODE_K}, + {Key::L, SDL_SCANCODE_L}, + {Key::M, SDL_SCANCODE_M}, + {Key::N, SDL_SCANCODE_N}, + {Key::O, SDL_SCANCODE_O}, + {Key::P, SDL_SCANCODE_P}, + {Key::Q, SDL_SCANCODE_Q}, + {Key::R, SDL_SCANCODE_R}, + {Key::S, SDL_SCANCODE_S}, + {Key::T, SDL_SCANCODE_T}, + {Key::U, SDL_SCANCODE_U}, + {Key::V, SDL_SCANCODE_V}, + {Key::W, SDL_SCANCODE_W}, + {Key::X, SDL_SCANCODE_X}, + {Key::Y, SDL_SCANCODE_Y}, + {Key::Z, SDL_SCANCODE_Z}, + {Key::NUM_0, SDL_SCANCODE_0}, + {Key::NUM_1, SDL_SCANCODE_1}, + {Key::NUM_2, SDL_SCANCODE_2}, + {Key::NUM_3, SDL_SCANCODE_3}, + {Key::NUM_4, SDL_SCANCODE_4}, + {Key::NUM_5, SDL_SCANCODE_5}, + {Key::NUM_6, SDL_SCANCODE_6}, + {Key::NUM_7, SDL_SCANCODE_7}, + {Key::NUM_8, SDL_SCANCODE_8}, + {Key::NUM_9, SDL_SCANCODE_9}, + {Key::LEFT_BRACKET, SDL_SCANCODE_LEFTBRACKET}, + {Key::RIGHT_BRACKET, SDL_SCANCODE_RIGHTBRACKET}, + {Key::SEMICOLON, SDL_SCANCODE_SEMICOLON}, + {Key::APOSTROPHE, SDL_SCANCODE_APOSTROPHE}, + {Key::COMMA, SDL_SCANCODE_COMMA}, + {Key::PERIOD, SDL_SCANCODE_PERIOD}, + {Key::SLASH, SDL_SCANCODE_SLASH}, + {Key::BACKSLASH, SDL_SCANCODE_BACKSLASH}, + {Key::GRAVE, SDL_SCANCODE_GRAVE} + }; +} + +void InputManager::initEventMap() { + eventMap = { + {EventType::KeyDown, SDL_EVENT_KEY_DOWN}, + {EventType::KeyUp, SDL_EVENT_KEY_UP}, + }; +} + +void InputManager::registerAction(const std::string& actionName, EventType eventType, Key key, std::function callback) { + actionBindings.push_back({actionName, eventType, key, callback}); +} + +bool InputManager::isKeyPressed(Key key) { + const bool* state = SDL_GetKeyboardState(nullptr); + auto sdlKey = keyMap[key]; + return state[sdlKey]; +} + void InputManager::processEvents() { SDL_Event event; - while (SDL_PollEvent(&event)) { handleEvent(event); } } -void InputManager::registerAction(const std::string& actionName, std::function callback) { - actionCallbacks[actionName] = callback; -} - void InputManager::handleEvent(const SDL_Event& event) { - for (const auto& [actionName, bindings] : actionBindings) { - for (const auto& binding : bindings) { - if (event.type == binding) { - if (actionCallbacks.find(actionName) != actionCallbacks.end()) { - actionCallbacks[actionName](); - } - } + for (const auto& binding : actionBindings) { + if (event.type == eventMap[binding.eventType] && event.key.scancode == keyMap[binding.key]) { + binding.callback(); } } } \ No newline at end of file