2023-05-07 12:27:11 +02:00
|
|
|
#include "input_mapper.h"
|
2023-05-17 23:22:45 +02:00
|
|
|
#include "input_device_group.h"
|
2023-05-07 12:27:11 +02:00
|
|
|
|
|
|
|
|
2023-05-17 14:13:39 +02:00
|
|
|
InputMapper::InputMapper()
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
2023-05-16 21:56:55 +02:00
|
|
|
// Initialize identities
|
2023-05-17 23:22:45 +02:00
|
|
|
allIdentity = std::make_shared<InputIdentity>(InputDeviceGroup::ALL);
|
|
|
|
keyboardIdentity = std::make_shared<InputIdentity>(InputDeviceGroup::KEYBOARD);
|
2023-05-16 21:56:55 +02:00
|
|
|
gamepadIdentities = std::map<unsigned int, std::shared_ptr<InputIdentity>>();
|
2023-05-07 12:27:11 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 18:20:55 +02:00
|
|
|
|
2023-05-07 12:27:11 +02:00
|
|
|
void InputMapper::processEvents()
|
|
|
|
{
|
|
|
|
sf::Event event{};
|
2023-05-17 14:13:39 +02:00
|
|
|
while (Game::getInstance()->window->pollEvent(event))
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case sf::Event::KeyPressed:
|
|
|
|
handleKeyPress(event.key);
|
|
|
|
break;
|
|
|
|
case sf::Event::KeyReleased:
|
2023-05-09 20:50:50 +02:00
|
|
|
handleKeyRelease(event.key);
|
2023-05-07 12:27:11 +02:00
|
|
|
break;
|
|
|
|
case sf::Event::Closed:
|
2023-05-17 14:13:39 +02:00
|
|
|
Game::getInstance()->exit();
|
2023-05-07 12:27:11 +02:00
|
|
|
break;
|
|
|
|
case sf::Event::Resized:
|
|
|
|
break;
|
2023-05-16 21:56:55 +02:00
|
|
|
case sf::Event::JoystickMoved:
|
|
|
|
handleJoystickMovement(event.joystickMove);
|
|
|
|
break;
|
2023-05-17 14:13:39 +02:00
|
|
|
case sf::Event::JoystickConnected:
|
|
|
|
addGamepadIdentity(event.joystickConnect.joystickId);
|
|
|
|
break;
|
|
|
|
case sf::Event::JoystickDisconnected:
|
|
|
|
deactivateGamepadIdentity(event.joystickConnect.joystickId);
|
|
|
|
break;
|
2023-05-07 12:27:11 +02:00
|
|
|
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))
|
|
|
|
{
|
2023-05-17 14:13:39 +02:00
|
|
|
Game::getInstance()->exit();
|
2023-05-07 12:27:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:51:09 +02:00
|
|
|
// Handle directionVector
|
|
|
|
auto direction = Direction::getKeyDirection(event.code);
|
|
|
|
if (direction != HardDirection::NONE)
|
2023-05-07 12:27:11 +02:00
|
|
|
{
|
2023-05-17 23:22:45 +02:00
|
|
|
getInputIdentity(InputDeviceGroup::KEYBOARD)->direction.add(direction);
|
2023-05-09 20:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputMapper::handleKeyRelease(sf::Event::KeyEvent event)
|
|
|
|
{
|
2023-05-10 14:51:09 +02:00
|
|
|
// Handle directionVector
|
|
|
|
auto direction = Direction::getKeyDirection(event.code);
|
|
|
|
if (direction != HardDirection::NONE)
|
2023-05-09 20:50:50 +02:00
|
|
|
{
|
2023-05-17 23:22:45 +02:00
|
|
|
getInputIdentity(InputDeviceGroup::KEYBOARD)->direction.remove(direction);
|
2023-05-07 12:27:11 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-16 21:56:55 +02:00
|
|
|
|
|
|
|
void InputMapper::handleJoystickMovement(sf::Event::JoystickMoveEvent event)
|
|
|
|
{
|
2023-05-17 23:22:45 +02:00
|
|
|
auto gamepadIdentity = getInputIdentity(InputDeviceGroup::GAMEPAD, event.joystickId);
|
2023-05-17 14:13:39 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-17 14:41:30 +02:00
|
|
|
if (axis == sf::Joystick::Axis::X)
|
2023-05-17 14:13:39 +02:00
|
|
|
{
|
|
|
|
gamepadIdentity->direction.setX(value);
|
2023-05-17 14:41:30 +02:00
|
|
|
} else if (axis == sf::Joystick::Axis::Y)
|
2023-05-17 14:13:39 +02:00
|
|
|
{
|
2023-05-17 14:41:30 +02:00
|
|
|
value *= -1; // Is inverted
|
2023-05-17 14:13:39 +02:00
|
|
|
gamepadIdentity->direction.setY(value);
|
|
|
|
}
|
2023-05-16 21:56:55 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 23:22:45 +02:00
|
|
|
std::shared_ptr<InputIdentity> InputMapper::getInputIdentity(InputDeviceGroup deviceType, unsigned int gamepadId)
|
2023-05-16 21:56:55 +02:00
|
|
|
{
|
|
|
|
switch (deviceType)
|
|
|
|
{
|
2023-05-17 23:22:45 +02:00
|
|
|
case InputDeviceGroup::KEYBOARD:
|
2023-05-16 21:56:55 +02:00
|
|
|
return keyboardIdentity;
|
2023-05-17 23:22:45 +02:00
|
|
|
case InputDeviceGroup::GAMEPAD:
|
2023-05-17 14:41:30 +02:00
|
|
|
if (!gamepadIdentities.contains(gamepadId))
|
2023-05-16 21:56:55 +02:00
|
|
|
{
|
2023-05-17 14:13:39 +02:00
|
|
|
// Create if it does not exist yet
|
2023-05-17 14:41:30 +02:00
|
|
|
addGamepadIdentity(gamepadId);
|
2023-05-16 21:56:55 +02:00
|
|
|
}
|
2023-05-17 14:41:30 +02:00
|
|
|
return gamepadIdentities[gamepadId];
|
2023-05-16 21:56:55 +02:00
|
|
|
default:
|
2023-05-17 14:41:30 +02:00
|
|
|
return allIdentity;
|
2023-05-17 14:13:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputMapper::addGamepadIdentity(unsigned int gamepadId)
|
|
|
|
{
|
|
|
|
// Exists already?
|
2023-05-17 14:41:30 +02:00
|
|
|
if (gamepadIdentities.contains(gamepadId))
|
2023-05-17 14:13:39 +02:00
|
|
|
{
|
2023-05-17 14:41:30 +02:00
|
|
|
gamepadIdentities[gamepadId]->isActive = true;
|
2023-05-17 14:13:39 +02:00
|
|
|
return;
|
2023-05-16 21:56:55 +02:00
|
|
|
}
|
2023-05-17 14:13:39 +02:00
|
|
|
|
|
|
|
// Does not exist yet, create new
|
2023-05-17 23:22:45 +02:00
|
|
|
auto newIdentity = std::make_shared<InputIdentity>(InputDeviceGroup::GAMEPAD, gamepadId);
|
2023-05-17 14:41:30 +02:00
|
|
|
gamepadIdentities[gamepadId] = newIdentity;
|
2023-05-17 14:13:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputMapper::deactivateGamepadIdentity(unsigned int gamepadId)
|
|
|
|
{
|
2023-05-17 23:22:45 +02:00
|
|
|
auto gamepadIdentity = getInputIdentity(InputDeviceGroup::GAMEPAD, gamepadId);
|
2023-05-17 14:13:39 +02:00
|
|
|
gamepadIdentity->isActive = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<InputMapper> InputMapper::getInstance()
|
|
|
|
{
|
2023-05-17 14:41:30 +02:00
|
|
|
if (singletonInstance == nullptr)
|
|
|
|
{
|
2023-05-17 14:13:39 +02:00
|
|
|
singletonInstance = std::make_shared<InputMapper>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return singletonInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<InputIdentity>> InputMapper::getAllInputIdentities()
|
|
|
|
{
|
|
|
|
std::vector<std::shared_ptr<InputIdentity>> allIdentities;
|
|
|
|
|
|
|
|
allIdentities.push_back(allIdentity);
|
|
|
|
allIdentities.push_back(keyboardIdentity);
|
|
|
|
|
2023-05-17 14:41:30 +02:00
|
|
|
for (auto const &[key, val]: gamepadIdentities)
|
2023-05-17 14:13:39 +02:00
|
|
|
{
|
|
|
|
allIdentities.push_back(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return allIdentities;
|
2023-05-16 21:56:55 +02:00
|
|
|
}
|