#include "input_mapper.h" void InputMapper::setGame(Game *game) { InputMapper::game = game; // Initialize identities allIdentity = std::make_shared(InputDeviceType::ALL); keyboardIdentity = std::make_shared(InputDeviceType::KEYBOARD); gamepadIdentities = std::map>(); } void InputMapper::processEvents() { sf::Event event{}; while (game->window->pollEvent(event)) { switch (event.type) { case sf::Event::KeyPressed: handleKeyPress(event.key); break; case sf::Event::KeyReleased: handleKeyRelease(event.key); break; case sf::Event::Closed: game->exit(); break; case sf::Event::Resized: break; case sf::Event::JoystickMoved: handleJoystickMovement(event.joystickMove); break; default: break; } } } void InputMapper::handleKeyPress(sf::Event::KeyEvent event) { // Close game on Escape or Q in DEV Mode if (DEVELOPER_MODE && (event.code == sf::Keyboard::Escape || event.code == sf::Keyboard::Q)) { game->exit(); return; } // Handle directionVector auto direction = Direction::getKeyDirection(event.code); if (direction != HardDirection::NONE) { inputDirectionBuffer.push_back(direction); } } Direction InputMapper::getInputDirection() { HardDirection direction = HardDirection::NONE; for (HardDirection directionPart: inputDirectionBuffer) { direction = static_cast(direction | directionPart); } return Direction(direction); } void InputMapper::handleKeyRelease(sf::Event::KeyEvent event) { // Handle directionVector auto direction = Direction::getKeyDirection(event.code); if (direction != HardDirection::NONE) { // Remove directionVector from buffer inputDirectionBuffer.erase(std::remove(inputDirectionBuffer.begin(), inputDirectionBuffer.end(), direction), inputDirectionBuffer.end()); } } void InputMapper::handleJoystickMovement(sf::Event::JoystickMoveEvent event) { event.joystickId; } std::shared_ptr InputMapper::getInputIdentity(InputDeviceType deviceType, unsigned int gamepadId) { switch (deviceType) { case InputDeviceType::KEYBOARD: return keyboardIdentity; case InputDeviceType::GAMEPAD: if (gamepadIdentities.find(gamepadId) == gamepadIdentities.end()) { throw std::invalid_argument("Gamepad with id " + std::to_string(gamepadId) + " not found."); } return gamepadIdentities[gamepadId]; default: return allIdentity; } }