#include "input_mapper.h" InputMapper::InputMapper() { // 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::getInstance()->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::getInstance()->exit(); break; case sf::Event::Resized: break; case sf::Event::JoystickMoved: handleJoystickMovement(event.joystickMove); break; case sf::Event::JoystickConnected: addGamepadIdentity(event.joystickConnect.joystickId); break; case sf::Event::JoystickDisconnected: deactivateGamepadIdentity(event.joystickConnect.joystickId); 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::getInstance()->exit(); return; } // Handle directionVector auto direction = Direction::getKeyDirection(event.code); if (direction != HardDirection::NONE) { getInputIdentity(InputDeviceType::KEYBOARD)->direction.add(direction); } } void InputMapper::handleKeyRelease(sf::Event::KeyEvent event) { // Handle directionVector auto direction = Direction::getKeyDirection(event.code); if (direction != HardDirection::NONE) { getInputIdentity(InputDeviceType::KEYBOARD)->direction.remove(direction); } } void InputMapper::handleJoystickMovement(sf::Event::JoystickMoveEvent event) { auto gamepadIdentity = getInputIdentity(InputDeviceType::GAMEPAD, event.joystickId); auto value = event.position / 100.f; auto axis = event.axis; // Handle deadzone and joystick drift if (value > -JOYSTICK_DEADZONE && value < JOYSTICK_DEADZONE) { value = 0.f; } if (axis == sf::Joystick::Axis::X) { gamepadIdentity->direction.setX(value); } else if (axis == sf::Joystick::Axis::Y) { value *= -1; // Is inverted gamepadIdentity->direction.setY(value); } } std::shared_ptr InputMapper::getInputIdentity(InputDeviceType deviceType, unsigned int gamepadId) { switch (deviceType) { case InputDeviceType::KEYBOARD: return keyboardIdentity; case InputDeviceType::GAMEPAD: if (!gamepadIdentities.contains(gamepadId)) { // Create if it does not exist yet addGamepadIdentity(gamepadId); } return gamepadIdentities[gamepadId]; default: return allIdentity; } } void InputMapper::addGamepadIdentity(unsigned int gamepadId) { // Exists already? if (gamepadIdentities.contains(gamepadId)) { gamepadIdentities[gamepadId]->isActive = true; return; } // Does not exist yet, create new auto newIdentity = std::make_shared(InputDeviceType::GAMEPAD, gamepadId); gamepadIdentities[gamepadId] = newIdentity; } void InputMapper::deactivateGamepadIdentity(unsigned int gamepadId) { auto gamepadIdentity = getInputIdentity(InputDeviceType::GAMEPAD, gamepadId); gamepadIdentity->isActive = false; } std::shared_ptr InputMapper::getInstance() { if (singletonInstance == nullptr) { singletonInstance = std::make_shared(); } return singletonInstance; } std::vector> InputMapper::getAllInputIdentities() { std::vector> allIdentities; allIdentities.push_back(allIdentity); allIdentities.push_back(keyboardIdentity); for (auto const &[key, val]: gamepadIdentities) { allIdentities.push_back(val); } return allIdentities; }