#ifndef INPUT_SERVICE_H #define INPUT_SERVICE_H #include "Service.h" #include "raylib.h" #include #include #include /** * Tracks all user input, which is then retrieved by the screens */ class InputService : public Service { public: /** * Setting up the keys to watch, so that only the keys that might get pressed are being watched */ void setKeysToWatch(std::vector keysToWatch, std::vector mouseButtonsToWatch) { this->watchedKeys.clear(); this->keysPressed.clear(); this->keysDown.clear(); for (int i = 0; i < keysToWatch.size(); i++) { this->watchedKeys.insert(keysToWatch[i]); this->keysPressed[keysToWatch[i]] = false; this->keysDown[keysToWatch[i]] = false; } setMouseButtonsToWatch(mouseButtonsToWatch); } /** * Checks if a button has been pressed * The flow: * 1. Screen constructor: Announce which keys to watch * 2. StateService loop: Check keys, store the result * 3. Screen logic: If pressed, execute logic */ void checkKeys() { for (auto key : watchedKeys) { keysPressed[key] = false; keysDown[key] = false; if (IsKeyPressed(key)) { keysPressed[key] = true; } if (IsKeyDown(key)) { keysDown[key] = true; } } for (auto button : watchedMouseButtons) { mouseButtonsPressed[button] = false; if (IsMouseButtonPressed(button)) { mouseButtonsPressed[button] = true; } } } bool isKeyPressed(KeyboardKey key) { return keysPressed[key]; } bool isKeyDown(KeyboardKey key) { return keysDown[key]; } bool isMouseButtonPressed(MouseButton button) { return mouseButtonsPressed[button]; } private: std::unordered_set watchedKeys; std::unordered_map keysPressed; std::unordered_map keysDown; std::unordered_set watchedMouseButtons; std::unordered_map mouseButtonsPressed; void setMouseButtonsToWatch(std::vector mouseButtonsToWatch) { this->watchedMouseButtons.clear(); this->mouseButtonsPressed.clear(); for (int i = 0; i < mouseButtonsToWatch.size(); i++) { this->watchedMouseButtons.insert(mouseButtonsToWatch[i]); this->mouseButtonsPressed[mouseButtonsToWatch[i]] = false; } } }; #endif