1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/client/eventsSDL/InputSourceGameController.cpp

331 lines
9.9 KiB
C++
Raw Normal View History

2024-04-03 14:34:22 +02:00
/*
* InputSourceGameController.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "InputSourceGameController.h"
2024-04-30 14:23:19 +02:00
2024-04-03 14:34:22 +02:00
#include "InputHandler.h"
#include "../CGameInfo.h"
#include "../gui/CGuiHandler.h"
2024-04-30 14:23:19 +02:00
#include "../gui/CursorHandler.h"
2024-04-03 14:34:22 +02:00
#include "../gui/EventDispatcher.h"
#include "../gui/ShortcutHandler.h"
2024-11-24 08:06:39 +02:00
#include "../render/IScreenHandler.h"
2024-04-03 14:34:22 +02:00
#include "../../lib/CConfigHandler.h"
2024-04-03 14:34:22 +02:00
void InputSourceGameController::gameControllerDeleter(SDL_GameController * gameController)
{
2024-04-30 10:38:13 +02:00
if(gameController)
SDL_GameControllerClose(gameController);
2024-04-03 14:34:22 +02:00
}
InputSourceGameController::InputSourceGameController():
2024-04-30 10:38:13 +02:00
cursorAxisValueX(0),
cursorAxisValueY(0),
cursorPlanDisX(0.0),
cursorPlanDisY(0.0),
scrollAxisMoved(false),
scrollStart(Point(0,0)),
scrollCurrent(Point(0,0)),
scrollAxisValueX(0),
scrollAxisValueY(0),
scrollPlanDisX(0.0),
scrollPlanDisY(0.0),
configTriggerThreshold(settings["input"]["controllerTriggerThreshold"].Float()),
configAxisDeadZone(settings["input"]["controllerAxisDeadZone"].Float()),
configAxisFullZone(settings["input"]["controllerAxisFullZone"].Float()),
configAxisSpeed(settings["input"]["controllerAxisSpeed"].Float()),
configAxisScale(settings["input"]["controllerAxisScale"].Float())
2024-04-03 14:34:22 +02:00
{
2024-04-30 10:38:13 +02:00
tryOpenAllGameControllers();
2024-04-03 14:34:22 +02:00
}
void InputSourceGameController::tryOpenAllGameControllers()
{
2024-04-30 10:38:13 +02:00
for(int i = 0; i < SDL_NumJoysticks(); ++i)
if(SDL_IsGameController(i))
openGameController(i);
else
logGlobal->warn("Joystick %d is an unsupported game controller!", i);
2024-04-03 14:34:22 +02:00
}
void InputSourceGameController::openGameController(int index)
{
2024-04-30 10:38:13 +02:00
SDL_GameController * controller = SDL_GameControllerOpen(index);
if(!controller)
{
logGlobal->error("Fail to open game controller %d!", index);
return;
}
2024-04-30 15:22:35 +02:00
GameControllerPtr controllerPtr(controller, &gameControllerDeleter);
2024-04-30 10:38:13 +02:00
// Need to save joystick index for event. Joystick index may not be equal to index sometimes.
int joystickIndex = getJoystickIndex(controllerPtr.get());
if(joystickIndex < 0)
{
logGlobal->error("Fail to get joystick index of game controller %d!", index);
return;
}
if(gameControllerMap.find(joystickIndex) != gameControllerMap.end())
{
logGlobal->warn("Game controller with joystick index %d is already opened.", joystickIndex);
return;
}
2024-04-30 15:22:35 +02:00
gameControllerMap.try_emplace(joystickIndex, std::move(controllerPtr));
2024-04-03 14:34:22 +02:00
}
int InputSourceGameController::getJoystickIndex(SDL_GameController * controller)
{
2024-04-30 14:23:19 +02:00
SDL_Joystick * joystick = SDL_GameControllerGetJoystick(controller);
2024-04-30 10:38:13 +02:00
if(!joystick)
return -1;
SDL_JoystickID instanceID = SDL_JoystickInstanceID(joystick);
if(instanceID < 0)
return -1;
2024-04-30 15:22:35 +02:00
return instanceID;
2024-04-03 14:34:22 +02:00
}
void InputSourceGameController::handleEventDeviceAdded(const SDL_ControllerDeviceEvent & device)
{
2024-04-30 10:38:13 +02:00
if(gameControllerMap.find(device.which) != gameControllerMap.end())
{
logGlobal->warn("Game controller %d is already opened.", device.which);
return;
}
openGameController(device.which);
2024-04-03 14:34:22 +02:00
}
void InputSourceGameController::handleEventDeviceRemoved(const SDL_ControllerDeviceEvent & device)
{
2024-04-30 10:38:13 +02:00
if(gameControllerMap.find(device.which) == gameControllerMap.end())
{
logGlobal->warn("Game controller %d is not opened before.", device.which);
return;
}
gameControllerMap.erase(device.which);
2024-04-03 14:34:22 +02:00
}
void InputSourceGameController::handleEventDeviceRemapped(const SDL_ControllerDeviceEvent & device)
{
2024-04-30 10:38:13 +02:00
if(gameControllerMap.find(device.which) == gameControllerMap.end())
{
logGlobal->warn("Game controller %d is not opened.", device.which);
return;
}
gameControllerMap.erase(device.which);
openGameController(device.which);
2024-04-03 14:34:22 +02:00
}
double InputSourceGameController::getRealAxisValue(int value) const
2024-04-03 14:34:22 +02:00
{
double ratio = static_cast<double>(value) / SDL_JOYSTICK_AXIS_MAX;
double greenZone = configAxisFullZone - configAxisDeadZone;
if (std::abs(ratio) < configAxisDeadZone)
2024-04-30 10:38:13 +02:00
return 0;
double scaledValue = (ratio - configAxisDeadZone) / greenZone;
double clampedValue = std::clamp(scaledValue, -1.0, +1.0);
return clampedValue;
2024-04-03 14:34:22 +02:00
}
void InputSourceGameController::dispatchAxisShortcuts(const std::vector<EShortcut> & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue)
2024-04-25 16:23:17 +02:00
{
if(getRealAxisValue(axisValue) > configTriggerThreshold)
{
2024-04-30 14:23:19 +02:00
if(!pressedAxes.count(axisID))
{
GH.events().dispatchShortcutPressed(shortcutsVector);
pressedAxes.insert(axisID);
}
}
2024-04-30 10:38:13 +02:00
else
{
2024-04-30 14:23:19 +02:00
if(pressedAxes.count(axisID))
{
GH.events().dispatchShortcutReleased(shortcutsVector);
pressedAxes.erase(axisID);
}
}
2024-04-25 16:23:17 +02:00
}
2024-04-03 14:34:22 +02:00
void InputSourceGameController::handleEventAxisMotion(const SDL_ControllerAxisEvent & axis)
{
2024-04-30 10:38:13 +02:00
tryToConvertCursor();
SDL_GameControllerAxis axisID = static_cast<SDL_GameControllerAxis>(axis.axis);
2024-04-30 14:23:19 +02:00
std::string axisName = SDL_GameControllerGetStringForAxis(axisID);
auto axisActions = GH.shortcuts().translateJoystickAxis(axisName);
auto buttonActions = GH.shortcuts().translateJoystickButton(axisName);
2024-04-30 14:23:19 +02:00
for(const auto & action : axisActions)
2024-04-30 10:38:13 +02:00
{
2024-04-30 14:23:19 +02:00
switch(action)
{
case EShortcut::MOUSE_CURSOR_X:
cursorAxisValueX = getRealAxisValue(axis.value);
break;
case EShortcut::MOUSE_CURSOR_Y:
cursorAxisValueY = getRealAxisValue(axis.value);
break;
case EShortcut::MOUSE_SWIPE_X:
scrollAxisValueX = getRealAxisValue(axis.value);
break;
case EShortcut::MOUSE_SWIPE_Y:
scrollAxisValueY = getRealAxisValue(axis.value);
break;
}
2024-04-30 10:38:13 +02:00
}
dispatchAxisShortcuts(buttonActions, axisID, axis.value);
2024-04-03 14:34:22 +02:00
}
2024-04-28 14:06:47 +02:00
void InputSourceGameController::tryToConvertCursor()
{
2024-04-30 11:01:03 +02:00
assert(CCS);
assert(CCS->curh);
if(CCS->curh->getShowType() == Cursor::ShowType::HARDWARE)
2024-04-30 10:38:13 +02:00
{
int scalingFactor = GH.screenHandler().getScalingFactor();
2024-04-30 11:01:03 +02:00
const Point & cursorPosition = GH.getCursorPosition();
2024-04-30 15:22:35 +02:00
CCS->curh->changeCursor(Cursor::ShowType::SOFTWARE);
CCS->curh->cursorMove(cursorPosition.x * scalingFactor, cursorPosition.y * scalingFactor);
2024-04-30 10:38:13 +02:00
GH.input().setCursorPosition(cursorPosition);
}
2024-04-28 14:06:47 +02:00
}
2024-04-03 14:34:22 +02:00
void InputSourceGameController::handleEventButtonDown(const SDL_ControllerButtonEvent & button)
{
std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button));
const auto & shortcutsVector = GH.shortcuts().translateJoystickButton(buttonName);
GH.events().dispatchShortcutPressed(shortcutsVector);
2024-04-03 14:34:22 +02:00
}
void InputSourceGameController::handleEventButtonUp(const SDL_ControllerButtonEvent & button)
{
std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button));
const auto & shortcutsVector = GH.shortcuts().translateJoystickButton(buttonName);
GH.events().dispatchShortcutReleased(shortcutsVector);
2024-04-03 14:34:22 +02:00
}
void InputSourceGameController::doCursorMove(int deltaX, int deltaY)
{
2024-04-30 10:38:13 +02:00
if(deltaX == 0 && deltaY == 0)
return;
const Point & screenSize = GH.screenDimensions();
const Point & cursorPosition = GH.getCursorPosition();
int scalingFactor = GH.screenHandler().getScalingFactor();
2024-04-30 10:38:13 +02:00
int newX = std::min(std::max(cursorPosition.x + deltaX, 0), screenSize.x);
int newY = std::min(std::max(cursorPosition.y + deltaY, 0), screenSize.y);
Point targetPosition{newX, newY};
GH.input().setCursorPosition(targetPosition);
if(CCS && CCS->curh)
CCS->curh->cursorMove(GH.getCursorPosition().x * scalingFactor, GH.getCursorPosition().y * scalingFactor);
2024-04-03 14:34:22 +02:00
}
int InputSourceGameController::getMoveDis(float planDis)
{
2024-04-30 10:38:13 +02:00
if(planDis >= 0)
return std::floor(planDis);
else
return std::ceil(planDis);
2024-04-03 14:34:22 +02:00
}
void InputSourceGameController::handleUpdate()
{
2024-04-30 14:31:35 +02:00
std::chrono::steady_clock::time_point nowMs = std::chrono::steady_clock::now();
if(lastCheckTime == std::chrono::steady_clock::time_point())
2024-04-30 14:23:19 +02:00
{
2024-04-30 10:38:13 +02:00
lastCheckTime = nowMs;
return;
}
2024-04-30 15:22:35 +02:00
int32_t deltaTime = std::chrono::duration_cast<std::chrono::milliseconds>(nowMs - lastCheckTime).count();
2024-04-30 10:38:13 +02:00
handleCursorUpdate(deltaTime);
handleScrollUpdate(deltaTime);
lastCheckTime = nowMs;
2024-04-28 14:06:47 +02:00
}
2024-04-03 14:34:22 +02:00
static double scaleAxis(double value, double power)
{
if (value > 0)
return std::pow(value, power);
else
return -std::pow(-value, power);
}
2024-04-30 14:31:35 +02:00
void InputSourceGameController::handleCursorUpdate(int32_t deltaTimeMs)
2024-04-28 14:06:47 +02:00
{
2024-04-30 15:22:35 +02:00
float deltaTimeSeconds = static_cast<float>(deltaTimeMs) / 1000;
if(vstd::isAlmostZero(cursorAxisValueX))
2024-04-30 10:38:13 +02:00
cursorPlanDisX = 0;
else
cursorPlanDisX += deltaTimeSeconds * configAxisSpeed * scaleAxis(cursorAxisValueX, configAxisScale);
2024-04-30 10:38:13 +02:00
if (vstd::isAlmostZero(cursorAxisValueY))
2024-04-30 10:38:13 +02:00
cursorPlanDisY = 0;
else
cursorPlanDisY += deltaTimeSeconds * configAxisSpeed * scaleAxis(cursorAxisValueY, configAxisScale);
2024-04-30 10:38:13 +02:00
int moveDisX = getMoveDis(cursorPlanDisX);
int moveDisY = getMoveDis(cursorPlanDisY);
cursorPlanDisX -= moveDisX;
cursorPlanDisY -= moveDisY;
doCursorMove(moveDisX, moveDisY);
2024-04-28 14:06:47 +02:00
}
2024-04-30 14:31:35 +02:00
void InputSourceGameController::handleScrollUpdate(int32_t deltaTimeMs)
2024-04-28 14:06:47 +02:00
{
2024-04-30 10:38:13 +02:00
if(!scrollAxisMoved && isScrollAxisReleased())
{
return;
}
else if(!scrollAxisMoved && !isScrollAxisReleased())
{
scrollAxisMoved = true;
scrollCurrent = scrollStart = GH.input().getCursorPosition();
GH.events().dispatchGesturePanningStarted(scrollStart);
}
else if(scrollAxisMoved && isScrollAxisReleased())
{
GH.events().dispatchGesturePanningEnded(scrollStart, scrollCurrent);
scrollAxisMoved = false;
scrollPlanDisX = scrollPlanDisY = 0;
return;
}
2024-04-30 15:22:35 +02:00
float deltaTimeSeconds = static_cast<float>(deltaTimeMs) / 1000;
scrollPlanDisX += deltaTimeSeconds * configAxisSpeed * scaleAxis(scrollAxisValueX, configAxisScale);
scrollPlanDisY += deltaTimeSeconds * configAxisSpeed * scaleAxis(scrollAxisValueY, configAxisScale);
2024-04-30 10:38:13 +02:00
int moveDisX = getMoveDis(scrollPlanDisX);
int moveDisY = getMoveDis(scrollPlanDisY);
if(moveDisX != 0 || moveDisY != 0)
{
scrollPlanDisX -= moveDisX;
scrollPlanDisY -= moveDisY;
scrollCurrent.x += moveDisX;
scrollCurrent.y += moveDisY;
Point distance(moveDisX, moveDisY);
GH.events().dispatchGesturePanning(scrollStart, scrollCurrent, distance);
}
2024-04-28 14:06:47 +02:00
}
bool InputSourceGameController::isScrollAxisReleased() const
2024-04-28 14:06:47 +02:00
{
return vstd::isAlmostZero(scrollAxisValueX) && vstd::isAlmostZero(scrollAxisValueY);
2024-04-28 14:06:47 +02:00
}