Base for controller touch screen input

* Supports analog stick, touchpad and gryo as input sources
* Relative and absolute input for sticks and touchpad

Still needs work before ready to merge, UI is not hooked up yet, cursor
drawing is a hack and doesn't work with OpenGL and the input code needs
some refining.
This commit is contained in:
Nadia Holmquist Pedersen 2023-06-30 16:52:19 +02:00
parent 7b948e6ec9
commit 95a1c9555d
4 changed files with 599 additions and 52 deletions

View File

@ -28,6 +28,7 @@ namespace Input
int JoystickID;
SDL_Joystick* Joystick = nullptr;
SDL_GameController* GameController = nullptr;
u32 KeyInputMask, JoyInputMask;
u32 KeyHotkeyMask, JoyHotkeyMask;
@ -36,6 +37,16 @@ u32 HotkeyPress, HotkeyRelease;
u32 InputMask;
u8 JoyTouchX, JoyTouchY;
bool JoyTouching;
bool JoyTouchReleased;
JoystickTouchMode TouchMode;
AnalogStick TouchAnalogStick;
JoystickTouchMovementStyle MovementStyle;
bool touchpadTouching;
float touchpadLastX;
float touchpadLastY;
void Init()
{
@ -47,6 +58,18 @@ void Init()
JoyHotkeyMask = 0;
HotkeyMask = 0;
LastHotkeyMask = 0;
JoyTouchX = 0;
JoyTouchY = 0;
JoyTouching = false;
TouchMode = ANALOG;
JoyTouchReleased = false;
MovementStyle = ABSOLUTE;
touchpadLastX = 0;
touchpadLastY = 0;
touchpadTouching = false;
}
@ -65,6 +88,14 @@ void OpenJoystick()
JoystickID = 0;
Joystick = SDL_JoystickOpen(JoystickID);
if (Joystick != nullptr)
{
if (!SDL_IsGameController(JoystickID))
return;
GameController = SDL_GameControllerOpen(JoystickID);
}
}
void CloseJoystick()
@ -184,6 +215,168 @@ bool JoystickButtonDown(int val)
return false;
}
bool JoystickTouchModeAvailable(JoystickTouchMode mode, AnalogStick stick)
{
if (GameController == nullptr)
return false;
switch (mode) {
case ANALOG:
{
SDL_GameControllerAxis xAxis = stick == LEFT
? SDL_CONTROLLER_AXIS_LEFTX
: SDL_CONTROLLER_AXIS_RIGHTX;
SDL_GameControllerAxis yAxis = stick == LEFT
? SDL_CONTROLLER_AXIS_LEFTY
: SDL_CONTROLLER_AXIS_RIGHTY;
return SDL_GameControllerHasAxis(GameController, xAxis)
&& SDL_GameControllerHasAxis(GameController, yAxis);
}
case TOUCHPAD:
return SDL_GameControllerGetNumTouchpads(GameController) != 0;
case GYROSCOPE:
return SDL_GameControllerHasSensor(GameController, SDL_SENSOR_GYRO);
case NONE:
return true;
}
}
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
// so correct it to be more like the DS's aspect ratio
float TouchPadCorrectAspect(float x)
{
SDL_GameControllerType type = SDL_GameControllerGetType(GameController);
if (type != SDL_CONTROLLER_TYPE_PS4 && type != SDL_CONTROLLER_TYPE_PS5)
return x;
float pos = (x - 0.5f) * psTouchpadAspectMul;
pos = std::clamp(pos, -.5f, .5f);
return pos + 0.5f;
}
float JoyTouchXFloat, JoyTouchYFloat;
void HandleRelativeInput(float dx, float dy, float sensitivity)
{
JoyTouchXFloat = std::clamp((float) JoyTouchXFloat + (dx * sensitivity), 0.f, 255.f);
JoyTouchYFloat = std::clamp((float) JoyTouchYFloat + (dy * sensitivity), 0.f, 192.f);
JoyTouchX = (u8) std::round(JoyTouchXFloat);
JoyTouchY = (u8) std::round(JoyTouchYFloat);
}
void UpdateJoystickTouch()
{
bool newTouching = false;
JoyTouchReleased = false;
if (!JoystickTouchModeAvailable(TouchMode, TouchAnalogStick))
{
if (JoyTouching)
{
JoyTouchReleased = true;
JoyTouching = false;
}
return;
}
if (TouchMode == TOUCHPAD)
{
u8 state;
float 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 (MovementStyle == RELATIVE && haveTouchpadButton)
{
if (state == 1)
{
float dx, dy;
if (!touchpadTouching)
{
touchpadTouching = true;
dx = 0;
dy = 0;
}
else
{
dx = (x - touchpadLastX) * psTouchpadAspectMul;
dy = y - touchpadLastY;
}
HandleRelativeInput(dx, dy, 256.f);
newTouching = SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_TOUCHPAD);
touchpadLastX = x;
touchpadLastY = y;
}
else
{
touchpadLastX = 0;
touchpadLastY = 0;
touchpadTouching = false;
}
}
else
{
if (SDL_GameControllerHasButton(GameController, SDL_CONTROLLER_BUTTON_TOUCHPAD))
newTouching = SDL_GameControllerGetButton(GameController, SDL_CONTROLLER_BUTTON_TOUCHPAD);
else
newTouching = state == 1;
JoyTouchX = (u8) round(TouchPadCorrectAspect(x) * 256.f);
JoyTouchY = (u8) round(y * 192.f);
}
}
if (TouchMode == ANALOG)
{
s16 x = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_LEFTX);
s16 y = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_LEFTY);
float fx = ((float) x) / 32768.f;
float fy = ((float) y) / 32768.f;
if (MovementStyle == RELATIVE)
{
HandleRelativeInput(fx, fy, 5.f);
}
else
{
JoyTouchX = (u8) std::round(((fx + 1.0f) / 2) * 256.f);
JoyTouchY = (u8) std::round(((fy + 1.0f) / 2) * 192.f);
}
newTouching = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) > 0.5;
}
if (TouchMode == GYROSCOPE)
{
float gyroPos[3] = {0};
SDL_GameControllerSetSensorEnabled(GameController, SDL_SENSOR_GYRO, SDL_TRUE);
SDL_GameControllerGetSensorData(GameController, SDL_SENSOR_GYRO, (float*) &gyroPos, 3);
HandleRelativeInput(-gyroPos[1], -gyroPos[0], 5.f);
newTouching = SDL_GameControllerGetAxis(GameController, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) > 0.5;
}
if (!newTouching && JoyTouching)
JoyTouchReleased = true;
JoyTouching = newTouching;
}
void Process()
{
SDL_JoystickUpdate();
@ -192,8 +385,14 @@ void Process()
{
if (!SDL_JoystickGetAttached(Joystick))
{
if (GameController != nullptr)
{
SDL_GameControllerClose(GameController);
GameController = nullptr;
}
SDL_JoystickClose(Joystick);
Joystick = NULL;
Joystick = nullptr;
}
}
if (!Joystick && (SDL_NumJoysticks() > 0))
@ -218,6 +417,9 @@ void Process()
HotkeyPress = HotkeyMask & ~LastHotkeyMask;
HotkeyRelease = LastHotkeyMask & ~HotkeyMask;
LastHotkeyMask = HotkeyMask;
if (TouchMode != NONE)
UpdateJoystickTouch();
}

View File

@ -29,6 +29,30 @@ extern SDL_Joystick* Joystick;
extern u32 InputMask;
extern u8 JoyTouchX, JoyTouchY;
extern bool JoyTouching;
extern bool JoyTouchReleased;
enum JoystickTouchMode
{
NONE = 0, ANALOG, TOUCHPAD, GYROSCOPE
};
enum AnalogStick
{
LEFT = 0, RIGHT
};
enum JoystickTouchMovementStyle
{
ABSOLUTE = 0, RELATIVE
};
extern JoystickTouchMode TouchMode;
extern AnalogStick TouchAnalogStick;
extern JoystickTouchMovementStyle MovementStyle;
void Init();
// set joystickID before calling openJoystick()

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>770</width>
<height>678</height>
<height>768</height>
</rect>
</property>
<property name="windowTitle">
@ -30,10 +30,59 @@
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Joystick:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cbxJoystick">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Selects which joystick will be used for joystick input, if any is present.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lblInstanceNum">
<property name="text">
<string>Configuring mappings for instance X</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
<number>3</number>
</property>
<widget class="QWidget" name="tabInput">
<attribute name="title">
@ -2277,55 +2326,312 @@
<string>General hotkeys</string>
</attribute>
</widget>
</widget>
</item>
<item row="7" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Joystick:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cbxJoystick">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Selects which joystick will be used for joystick input, if any is present.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lblInstanceNum">
<property name="text">
<string>Configuring mappings for instance X</string>
</property>
<widget class="QWidget" name="tabTouchScreen">
<attribute name="title">
<string>Touch screen</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QWidget" name="widget_5" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<property name="spacing">
<number>12</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="widget_6" native="true">
<layout class="QVBoxLayout" name="verticalLayout_9">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item alignment="Qt::AlignTop">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Touch screen input</string>
</property>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="label_32">
<property name="text">
<string>Controller input</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QWidget" name="widget_8" native="true">
<layout class="QVBoxLayout" name="verticalLayout_10">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QRadioButton" name="radioButton">
<property name="text">
<string>None (mouse only)</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_2">
<property name="text">
<string>Analog stick</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_3">
<property name="text">
<string>Touchpad</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_4">
<property name="text">
<string>Gyroscope</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_36">
<property name="text">
<string>Movement style</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QWidget" name="widget_10" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<layout class="QVBoxLayout" name="verticalLayout_12">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QRadioButton" name="radioButton_5">
<property name="text">
<string>Relative</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_6">
<property name="text">
<string>Absolute</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_37">
<property name="text">
<string>Recenter</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>[PLACEHOLDER]</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_38">
<property name="text">
<string>Analog stick</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QWidget" name="widget_11" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<layout class="QVBoxLayout" name="verticalLayout_13">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QRadioButton" name="radioButton_7">
<property name="text">
<string>Left</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_8">
<property name="text">
<string>Right</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_7" native="true">
<layout class="QVBoxLayout" name="verticalLayout_11">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item alignment="Qt::AlignTop">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Cursor</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_33">
<property name="text">
<string>Color</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_34">
<property name="text">
<string>Hide after</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QWidget" name="widget_9" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_8">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>6</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<widget class="QSpinBox" name="spinBox"/>
</item>
<item>
<widget class="QLabel" name="label_35">
<property name="text">
<string>seconds of inactivity</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>

View File

@ -469,6 +469,12 @@ void EmuThread::run()
OSD::AddMessage(0, lid ? "Lid closed" : "Lid opened");
}
if (Input::JoyTouching)
NDS::TouchScreen(Input::JoyTouchX, Input::JoyTouchY);
if (Input::JoyTouchReleased)
NDS::ReleaseScreen();
// microphone input
AudioInOut::MicProcess();
@ -1063,6 +1069,15 @@ void ScreenPanelNative::paintEvent(QPaintEvent* event)
{
painter.setTransform(screenTrans[i]);
painter.drawImage(screenrc, screen[screenKind[i]]);
if (i == 1)
{
//QTransform cursorTrans = screenTrans[i].translate(Input::JoyTouchX, Input::JoyTouchY);
//painter.setTransform(cursorTrans);
QRect cursorRect = QRect(Input::JoyTouchX - 3, Input::JoyTouchY - 3, 5, 5);
painter.setPen(QColor::fromRgb(255, 0, 0));
painter.drawRoundedRect(cursorRect, 5, 5);
}
}
}