let's refactor some

This commit is contained in:
Nadia Holmquist Pedersen 2023-07-03 14:57:49 +02:00
parent 95a1c9555d
commit 67879b1821
3 changed files with 67 additions and 57 deletions

View File

@ -40,9 +40,7 @@ u32 InputMask;
u8 JoyTouchX, JoyTouchY; u8 JoyTouchX, JoyTouchY;
bool JoyTouching; bool JoyTouching;
bool JoyTouchReleased; bool JoyTouchReleased;
JoystickTouchMode TouchMode; JoystickTouchMode JoystickTouch;
AnalogStick TouchAnalogStick;
JoystickTouchMovementStyle MovementStyle;
bool touchpadTouching; bool touchpadTouching;
float touchpadLastX; float touchpadLastX;
@ -62,10 +60,7 @@ void Init()
JoyTouchX = 0; JoyTouchX = 0;
JoyTouchY = 0; JoyTouchY = 0;
JoyTouching = false; JoyTouching = false;
TouchMode = ANALOG;
JoyTouchReleased = false; JoyTouchReleased = false;
MovementStyle = ABSOLUTE;
touchpadLastX = 0; touchpadLastX = 0;
touchpadLastY = 0; touchpadLastY = 0;
@ -215,45 +210,58 @@ bool JoystickButtonDown(int val)
return false; return false;
} }
bool JoystickTouchModeAvailable(JoystickTouchMode mode, AnalogStick stick) bool JoystickTouchModeAvailable(JoystickTouchMode mode)
{ {
if (GameController == nullptr) if (GameController == nullptr)
return false; return false;
switch (mode) { switch (mode.mode) {
case ANALOG: case JoystickTouchMode::ANALOG_STICK:
{ {
SDL_GameControllerAxis xAxis = stick == LEFT SDL_GameControllerAxis xAxis = mode.stick == JoystickTouchMode::LEFT_STICK
? SDL_CONTROLLER_AXIS_LEFTX ? SDL_CONTROLLER_AXIS_LEFTX
: SDL_CONTROLLER_AXIS_RIGHTX; : SDL_CONTROLLER_AXIS_RIGHTX;
SDL_GameControllerAxis yAxis = stick == LEFT SDL_GameControllerAxis yAxis = mode.stick == JoystickTouchMode::LEFT_STICK
? SDL_CONTROLLER_AXIS_LEFTY ? SDL_CONTROLLER_AXIS_LEFTY
: SDL_CONTROLLER_AXIS_RIGHTY; : SDL_CONTROLLER_AXIS_RIGHTY;
return SDL_GameControllerHasAxis(GameController, xAxis) return SDL_GameControllerHasAxis(GameController, xAxis)
&& SDL_GameControllerHasAxis(GameController, yAxis); && SDL_GameControllerHasAxis(GameController, yAxis);
} }
case TOUCHPAD: case JoystickTouchMode::TOUCHPAD:
if (mode.style == JoystickTouchMode::RELATIVE && !SDL_GameControllerHasButton(GameController, SDL_CONTROLLER_BUTTON_TOUCHPAD))
return false;
return SDL_GameControllerGetNumTouchpads(GameController) != 0; return SDL_GameControllerGetNumTouchpads(GameController) != 0;
case GYROSCOPE: case JoystickTouchMode::GYROSCOPE:
return SDL_GameControllerHasSensor(GameController, SDL_SENSOR_GYRO); return SDL_GameControllerHasSensor(GameController, SDL_SENSOR_GYRO);
case NONE: case JoystickTouchMode::NONE:
return true; return true;
} }
} }
bool SetJoystickTouchMode(JoystickTouchMode mode)
{
if (!JoystickTouchModeAvailable(mode))
return false;
SDL_GameControllerSetSensorEnabled(GameController, SDL_SENSOR_GYRO, mode.mode == JoystickTouchMode::GYROSCOPE ? SDL_TRUE : SDL_FALSE);
JoystickTouch = mode;
return true;
}
constexpr float psTouchpadAspectMul = ((52.f / 23.f) / (4.f / 3.f)); constexpr float psTouchpadAspectMul = ((52.f / 23.f) / (4.f / 3.f));
// The touchpad is about 52x23 mm on the PS4 controller, and the DualSense looks similar // The touchpad is about 52x23 mm on the PS4 controller, and the DualSense looks similar
// so correct it to be more like the DS's aspect ratio // so correct it to be more like the DS's aspect ratio
float TouchPadCorrectAspect(float x) float TouchpadCorrectAspect(float x)
{ {
SDL_GameControllerType type = SDL_GameControllerGetType(GameController); SDL_GameControllerType type = SDL_GameControllerGetType(GameController);
if (type != SDL_CONTROLLER_TYPE_PS4 && type != SDL_CONTROLLER_TYPE_PS5) if (type != SDL_CONTROLLER_TYPE_PS4 && type != SDL_CONTROLLER_TYPE_PS5)
return x; return x;
float pos = (x - 0.5f) * psTouchpadAspectMul; float pos = (x - 0.5f) * psTouchpadAspectMul;
pos = std::clamp(pos, -.5f, .5f); pos = std::clamp(pos, -.5f, .5f);
return pos + 0.5f; return pos + 0.5f;
@ -263,8 +271,10 @@ float JoyTouchXFloat, JoyTouchYFloat;
void HandleRelativeInput(float dx, float dy, float sensitivity) void HandleRelativeInput(float dx, float dy, float sensitivity)
{ {
// TODO: Screen rotation
JoyTouchXFloat = std::clamp((float) JoyTouchXFloat + (dx * sensitivity), 0.f, 255.f); JoyTouchXFloat = std::clamp((float) JoyTouchXFloat + (dx * sensitivity), 0.f, 255.f);
JoyTouchYFloat = std::clamp((float) JoyTouchYFloat + (dy * sensitivity), 0.f, 192.f); JoyTouchYFloat = std::clamp((float) JoyTouchYFloat + (dy * sensitivity), 0.f, 191.f);
JoyTouchX = (u8) std::round(JoyTouchXFloat); JoyTouchX = (u8) std::round(JoyTouchXFloat);
JoyTouchY = (u8) std::round(JoyTouchYFloat); JoyTouchY = (u8) std::round(JoyTouchYFloat);
@ -276,7 +286,11 @@ void UpdateJoystickTouch()
JoyTouchReleased = false; JoyTouchReleased = false;
if (!JoystickTouchModeAvailable(TouchMode, TouchAnalogStick)) auto mode = JoystickTouch.mode;
auto style = JoystickTouch.style;
auto stick = JoystickTouch.stick;
if (!JoystickTouchModeAvailable(JoystickTouch))
{ {
if (JoyTouching) if (JoyTouching)
{ {
@ -287,16 +301,14 @@ void UpdateJoystickTouch()
return; return;
} }
if (TouchMode == TOUCHPAD) if (mode == JoystickTouchMode::TOUCHPAD)
{ {
u8 state; u8 state;
float x, y, pressure; float x, y, pressure;
SDL_GameControllerGetTouchpadFinger(GameController, 0, 0, &state, &x, &y, &pressure); SDL_GameControllerGetTouchpadFinger(GameController, 0, 0, &state, &x, &y, &pressure);
printf("Touchpad: state: %u, x: %f, y: %f, pressure: %f\n", state, x, y, pressure);
bool haveTouchpadButton = SDL_GameControllerHasButton(GameController, SDL_CONTROLLER_BUTTON_TOUCHPAD); if (style == JoystickTouchMode::RELATIVE)
if (MovementStyle == RELATIVE && haveTouchpadButton)
{ {
if (state == 1) if (state == 1)
{ {
@ -314,7 +326,7 @@ void UpdateJoystickTouch()
dy = y - touchpadLastY; dy = y - touchpadLastY;
} }
HandleRelativeInput(dx, dy, 256.f); HandleRelativeInput(dx, dy, JoystickTouch.sensitivity);
newTouching = SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_TOUCHPAD); newTouching = SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_TOUCHPAD);
touchpadLastX = x; touchpadLastX = x;
@ -334,21 +346,25 @@ void UpdateJoystickTouch()
else else
newTouching = state == 1; newTouching = state == 1;
JoyTouchX = (u8) round(TouchPadCorrectAspect(x) * 256.f); JoyTouchX = (u8) round(TouchpadCorrectAspect(x) * 256.f);
JoyTouchY = (u8) round(y * 192.f); JoyTouchY = (u8) round(y * 192.f);
} }
} }
else if (mode == JoystickTouchMode::ANALOG_STICK)
if (TouchMode == ANALOG)
{ {
s16 x = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_LEFTX); SDL_GameControllerAxis axisX = stick == JoystickTouchMode::LEFT_STICK
s16 y = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_LEFTY); ? SDL_CONTROLLER_AXIS_LEFTX : SDL_CONTROLLER_AXIS_RIGHTX;
SDL_GameControllerAxis axisY = stick == JoystickTouchMode::LEFT_STICK
? SDL_CONTROLLER_AXIS_LEFTY : SDL_CONTROLLER_AXIS_RIGHTY;
s16 x = SDL_GameControllerGetAxis(GameController, axisX);
s16 y = SDL_GameControllerGetAxis(GameController, axisY);
float fx = ((float) x) / 32768.f; float fx = ((float) x) / 32768.f;
float fy = ((float) y) / 32768.f; float fy = ((float) y) / 32768.f;
if (MovementStyle == RELATIVE) if (style == JoystickTouchMode::RELATIVE)
{ {
HandleRelativeInput(fx, fy, 5.f); HandleRelativeInput(fx, fy, JoystickTouch.sensitivity);
} }
else else
{ {
@ -356,19 +372,18 @@ void UpdateJoystickTouch()
JoyTouchY = (u8) std::round(((fy + 1.0f) / 2) * 192.f); JoyTouchY = (u8) std::round(((fy + 1.0f) / 2) * 192.f);
} }
newTouching = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) > 0.5; newTouching = JoystickButtonDown(JoystickTouch.touchButton);
} }
else if (mode == JoystickTouchMode::GYROSCOPE)
if (TouchMode == GYROSCOPE)
{ {
float gyroPos[3] = {0}; float gyroPos[3] = {0};
SDL_GameControllerSetSensorEnabled(GameController, SDL_SENSOR_GYRO, SDL_TRUE); SDL_GameControllerSetSensorEnabled(GameController, SDL_SENSOR_GYRO, SDL_TRUE);
SDL_GameControllerGetSensorData(GameController, SDL_SENSOR_GYRO, (float*) &gyroPos, 3); SDL_GameControllerGetSensorData(GameController, SDL_SENSOR_GYRO, (float*) &gyroPos, 3);
HandleRelativeInput(-gyroPos[1], -gyroPos[0], 5.f); HandleRelativeInput(-gyroPos[1], -gyroPos[0], JoystickTouch.sensitivity);
newTouching = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) > 0.5; newTouching = JoystickButtonDown(JoystickTouch.touchButton);
} }
if (!newTouching && JoyTouching) if (!newTouching && JoyTouching)
@ -418,7 +433,7 @@ void Process()
HotkeyRelease = LastHotkeyMask & ~HotkeyMask; HotkeyRelease = LastHotkeyMask & ~HotkeyMask;
LastHotkeyMask = HotkeyMask; LastHotkeyMask = HotkeyMask;
if (TouchMode != NONE) if (JoystickTouch.mode != JoystickTouchMode::NONE)
UpdateJoystickTouch(); UpdateJoystickTouch();
} }

View File

@ -29,29 +29,24 @@ extern SDL_Joystick* Joystick;
extern u32 InputMask; extern u32 InputMask;
extern u8 JoyTouchX, JoyTouchY; extern u8 JoyTouchX, JoyTouchY;
extern bool JoyTouching; extern bool JoyTouching;
extern bool JoyTouchReleased; extern bool JoyTouchReleased;
enum JoystickTouchMode struct JoystickTouchMode {
{ enum Mode { NONE = 0, ANALOG_STICK, TOUCHPAD, GYROSCOPE };
NONE = 0, ANALOG, TOUCHPAD, GYROSCOPE enum Style { ABSOLUTE = 0, RELATIVE };
enum AnalogStick { LEFT_STICK = 0, RIGHT_STICK };
Mode mode;
Style style;
AnalogStick stick;
float sensitivity;
int touchButton;
}; };
enum AnalogStick extern JoystickTouchMode JoystickTouch;
{
LEFT = 0, RIGHT
};
enum JoystickTouchMovementStyle
{
ABSOLUTE = 0, RELATIVE
};
extern JoystickTouchMode TouchMode;
extern AnalogStick TouchAnalogStick;
extern JoystickTouchMovementStyle MovementStyle;
void Init(); void Init();
@ -62,6 +57,8 @@ void CloseJoystick();
void KeyPress(QKeyEvent* event); void KeyPress(QKeyEvent* event);
void KeyRelease(QKeyEvent* event); void KeyRelease(QKeyEvent* event);
bool SetJoystickTouchMode(JoystickTouchMode mode);
void Process(); void Process();
bool HotkeyDown(int id); bool HotkeyDown(int id);

View File

@ -1070,10 +1070,8 @@ void ScreenPanelNative::paintEvent(QPaintEvent* event)
painter.setTransform(screenTrans[i]); painter.setTransform(screenTrans[i]);
painter.drawImage(screenrc, screen[screenKind[i]]); painter.drawImage(screenrc, screen[screenKind[i]]);
if (i == 1) if (i == 1 && Input::JoystickTouch.mode != Input::JoystickTouchMode::NONE)
{ {
//QTransform cursorTrans = screenTrans[i].translate(Input::JoyTouchX, Input::JoyTouchY);
//painter.setTransform(cursorTrans);
QRect cursorRect = QRect(Input::JoyTouchX - 3, Input::JoyTouchY - 3, 5, 5); QRect cursorRect = QRect(Input::JoyTouchX - 3, Input::JoyTouchY - 3, 5, 5);
painter.setPen(QColor::fromRgb(255, 0, 0)); painter.setPen(QColor::fromRgb(255, 0, 0));
painter.drawRoundedRect(cursorRect, 5, 5); painter.drawRoundedRect(cursorRect, 5, 5);