From 2894d9100de479139a5f92755750d1b19f363de9 Mon Sep 17 00:00:00 2001 From: Nadia Holmquist Pedersen Date: Sat, 29 Jul 2023 18:09:31 +0200 Subject: [PATCH] hook up most of the stuff --- src/frontend/qt_sdl/Config.cpp | 18 +++ src/frontend/qt_sdl/Config.h | 9 ++ src/frontend/qt_sdl/Input.cpp | 31 +++- src/frontend/qt_sdl/Input.h | 6 + .../qt_sdl/InputConfig/InputConfigDialog.cpp | 136 ++++++++++++++++++ .../qt_sdl/InputConfig/InputConfigDialog.h | 14 ++ .../qt_sdl/InputConfig/InputConfigDialog.ui | 125 ++++++++++++---- src/frontend/qt_sdl/main.cpp | 1 + 8 files changed, 309 insertions(+), 31 deletions(-) diff --git a/src/frontend/qt_sdl/Config.cpp b/src/frontend/qt_sdl/Config.cpp index a32e172a..7e5cefd2 100644 --- a/src/frontend/qt_sdl/Config.cpp +++ b/src/frontend/qt_sdl/Config.cpp @@ -34,6 +34,15 @@ int HKJoyMapping[HK_MAX]; int JoystickID; +int JoyTouchMode; +int JoyTouchMovementStyle; +int JoyTouchStick; +int JoyTouchButton; +int JoyTouchRecenterButton; +std::string JoyTouchCursorColor; +int JoyTouchCursorHideDelay; +int JoyTouchSensitivity; + int WindowWidth; int WindowHeight; bool WindowMaximized; @@ -211,6 +220,15 @@ ConfigEntry ConfigFile[] = {"JoystickID", 0, &JoystickID, 0, true}, + {"JoyTouchMode", 0, &JoyTouchMode, 0, true}, + {"JoyTouchMovementStyle", 0, &JoyTouchMovementStyle, 0, true}, + {"JoyTouchStick", 0, &JoyTouchStick, 0, true}, + {"JoyTouchButton", 0, &JoyTouchButton, -1, true}, + {"JoyTouchRecenterButton", 0, &JoyTouchRecenterButton, -1, true}, + {"JoyTouchSensitivity", 0, &JoyTouchSensitivity, 100, true}, + {"JoyTouchCursorColor", 2, &JoyTouchCursorColor, (std::string)"#FF0000", true}, + {"JoyTouchCursorHideDelay", 0, &JoyTouchCursorHideDelay, 5, true}, + {"WindowWidth", 0, &WindowWidth, 256, true}, {"WindowHeight", 0, &WindowHeight, 384, true}, {"WindowMax", 1, &WindowMaximized, false, true}, diff --git a/src/frontend/qt_sdl/Config.h b/src/frontend/qt_sdl/Config.h index c6fd297f..c7b3870f 100644 --- a/src/frontend/qt_sdl/Config.h +++ b/src/frontend/qt_sdl/Config.h @@ -91,6 +91,15 @@ extern int HKJoyMapping[HK_MAX]; extern int JoystickID; +extern int JoyTouchMode; +extern int JoyTouchMovementStyle; +extern int JoyTouchStick; +extern int JoyTouchButton; +extern int JoyTouchRecenterButton; +extern std::string JoyTouchCursorColor; +extern int JoyTouchCursorHideDelay; +extern int JoyTouchSensitivity; + extern int WindowWidth; extern int WindowHeight; extern bool WindowMaximized; diff --git a/src/frontend/qt_sdl/Input.cpp b/src/frontend/qt_sdl/Input.cpp index 1518bf0a..937c9593 100644 --- a/src/frontend/qt_sdl/Input.cpp +++ b/src/frontend/qt_sdl/Input.cpp @@ -242,12 +242,12 @@ bool JoystickTouchModeAvailable(JoystickTouchMode mode) bool SetJoystickTouchMode(JoystickTouchMode mode) { + JoystickTouch = mode; + if (!JoystickTouchModeAvailable(mode)) return false; SDL_GameControllerSetSensorEnabled(GameController, SDL_SENSOR_GYRO, mode.mode == JoystickTouchMode::gyroscope ? SDL_TRUE : SDL_FALSE); - - JoystickTouch = mode; return true; } @@ -437,6 +437,33 @@ void Process() UpdateJoystickTouch(); } +void SaveJoystickTouchMode() +{ + using Mode = JoystickTouchMode; + Mode mode = JoystickTouch; + + Config::JoyTouchMode = mode.mode; + Config::JoyTouchMovementStyle = mode.style; + Config::JoyTouchStick = mode.stick; + Config::JoyTouchButton = mode.touchButton; + Config::JoyTouchRecenterButton = mode.recenterButton; + Config::JoyTouchSensitivity = (int) std::round(mode.sensitivity * 100.f); +} + +void LoadJoystickTouchMode() +{ + using Mode = JoystickTouchMode; + Mode mode = { + .mode = (Mode::Mode) Config::JoyTouchMode, + .style = (Mode::Style) Config::JoyTouchMovementStyle, + .stick = (Mode::AnalogStick) Config::JoyTouchStick, + .sensitivity = ((float) Config::JoyTouchSensitivity) / 100.f, + .touchButton = Config::JoyTouchButton, + .recenterButton = Config::JoyTouchRecenterButton + }; + + SetJoystickTouchMode(mode); +} bool HotkeyDown(int id) { return HotkeyMask & (1< + #include "types.h" namespace Input @@ -44,6 +46,7 @@ struct JoystickTouchMode { float sensitivity; int touchButton; + int recenterButton; }; extern JoystickTouchMode JoystickTouch; @@ -57,7 +60,10 @@ void CloseJoystick(); void KeyPress(QKeyEvent* event); void KeyRelease(QKeyEvent* event); +bool JoystickTouchModeAvailable(JoystickTouchMode mode); bool SetJoystickTouchMode(JoystickTouchMode mode); +void SaveJoystickTouchMode(); +void LoadJoystickTouchMode(); void Process(); diff --git a/src/frontend/qt_sdl/InputConfig/InputConfigDialog.cpp b/src/frontend/qt_sdl/InputConfig/InputConfigDialog.cpp index b0672a04..58e3fe08 100644 --- a/src/frontend/qt_sdl/InputConfig/InputConfigDialog.cpp +++ b/src/frontend/qt_sdl/InputConfig/InputConfigDialog.cpp @@ -90,6 +90,16 @@ InputConfigDialog::InputConfigDialog(QWidget* parent) : QDialog(parent), ui(new ui->lblInstanceNum->setText(QString("Configuring mappings for instance %1").arg(inst+1)); else ui->lblInstanceNum->hide(); + + QLayout* layout = ui->btnJoyTouchPress->parentWidget()->layout(); + + recenterBtn = new JoyMapButton(&recenterMapping, false); + joyPressBtn = new JoyMapButton(&joyPressMapping, false); + layout->replaceWidget(ui->btnJoyTouchRecenter, recenterBtn); + layout->replaceWidget(ui->btnJoyTouchPress, joyPressBtn); + + joyTouchModeToUI(Input::JoystickTouch); + updateJoyTouchOptions(); } InputConfigDialog::~InputConfigDialog() @@ -196,6 +206,10 @@ void InputConfigDialog::on_InputConfigDialog_accepted() } Config::JoystickID = Input::JoystickID; + + Input::SetJoystickTouchMode(joyTouchModeFromUI()); + Input::SaveJoystickTouchMode(); + Config::Save(); closeDlg(); @@ -226,4 +240,126 @@ void InputConfigDialog::on_cbxJoystick_currentIndexChanged(int id) Input::JoystickID = id; Input::OpenJoystick(); + + updateJoyTouchOptions(); +} + +Input::JoystickTouchMode InputConfigDialog::joyTouchModeFromUI() +{ + using Mode = Input::JoystickTouchMode; + + Mode mode = {}; + + QAbstractButton* uiMode = ui->grpJoyTouchInput->checkedButton(); + QAbstractButton* uiStyle = ui->grpJoyTouchStyle->checkedButton(); + QAbstractButton* uiStick = ui->grpJoyTouchStick->checkedButton(); + + if (uiMode == ui->rbTouchNone) + mode.mode = Mode::none; + else if (uiMode == ui->rbTouchAnalog) + mode.mode = Mode::analogStick; + else if (uiMode == ui->rbTouchTouchpad) + mode.mode = Mode::touchpad; + else if (uiMode == ui->rbTouchGyro) + mode.mode = Mode::gyroscope; + + mode.style = uiStyle == ui->rbTouchAbsolute ? Mode::absolute : Mode::relative; + mode.stick = uiStick == ui->rbStickLeft ? Mode::leftStick : Mode::rightStick; + + mode.touchButton = joyPressMapping; + mode.recenterButton = recenterMapping; + + return mode; +} + +void InputConfigDialog::joyTouchModeToUI(Input::JoystickTouchMode mode) +{ + using Mode = Input::JoystickTouchMode; + + switch (mode.mode) + { + case Mode::none: + ui->rbTouchNone->setChecked(true); + break; + case Mode::analogStick: + ui->rbTouchAnalog->setChecked(true); + break; + case Mode::touchpad: + ui->rbTouchTouchpad->setChecked(true); + break; + case Mode::gyroscope: + ui->rbTouchGyro->setChecked(true); + break; + } + + if (mode.style == Mode::relative) + ui->rbTouchRelative->setChecked(true); + else + ui->rbTouchAbsolute->setChecked(true); + + if (mode.stick == Mode::leftStick) + ui->rbStickLeft->setChecked(true); + else + ui->rbStickRight->setChecked(true); + + recenterMapping = mode.recenterButton; + joyPressMapping = mode.touchButton; +} + +void InputConfigDialog::updateJoyTouchOptions() +{ + using namespace Input; + using Mode = JoystickTouchMode; + + JoystickTouchMode tempMode = {}; + + QAbstractButton* currentMode = ui->grpJoyTouchInput->checkedButton(); + bool relative = ui->grpJoyTouchStyle->checkedButton() == ui->rbTouchRelative; + + JoystickTouchMode analogMode = { .mode = JoystickTouchMode::gyroscope }; + + bool currentAvailable = JoystickTouchModeAvailable(JoystickTouch); + + bool styleEnabled = (currentMode == ui->rbTouchAnalog || currentMode == ui->rbTouchTouchpad) && currentAvailable; + bool stickEnabled = currentMode == ui->rbTouchAnalog && currentAvailable; + bool recenterEnabled = (currentMode == ui->rbTouchGyro || relative) && currentAvailable; + bool pressEnabled = currentMode != ui->rbTouchNone && currentAvailable; + + tempMode.mode = Mode::analogStick; + tempMode.stick = Mode::leftStick; + bool leftAvailable = JoystickTouchModeAvailable(tempMode); + tempMode.stick = Mode::rightStick; + bool rightAvailable = JoystickTouchModeAvailable(tempMode); + tempMode.mode = Mode::touchpad; + bool touchpadAvailable = JoystickTouchModeAvailable(tempMode); + tempMode.mode = Mode::gyroscope; + bool gyroAvailable = JoystickTouchModeAvailable(tempMode); + + ui->rbTouchAnalog->setEnabled(leftAvailable || rightAvailable); + ui->rbTouchTouchpad->setEnabled(touchpadAvailable); + ui->rbTouchGyro->setEnabled(gyroAvailable); + + ui->lblJoyTouchStyle->setEnabled(styleEnabled); + ui->rbTouchRelative->setEnabled(styleEnabled); + ui->rbTouchAbsolute->setEnabled(styleEnabled); + + ui->lblAnalogStick->setEnabled(stickEnabled && (leftAvailable || rightAvailable)); + ui->rbStickLeft->setEnabled(stickEnabled && leftAvailable); + ui->rbStickRight->setEnabled(stickEnabled && rightAvailable); + + ui->lblRecenter->setEnabled(recenterEnabled); + ui->lblJoyTouchPress->setEnabled(pressEnabled); + + recenterBtn->setEnabled(recenterEnabled); + joyPressBtn->setEnabled(pressEnabled); +} + +void InputConfigDialog::on_grpJoyTouchInput_buttonClicked(QAbstractButton* btn) +{ + updateJoyTouchOptions(); +} + +void InputConfigDialog::on_grpJoyTouchStyle_buttonClicked(QAbstractButton* btn) +{ + updateJoyTouchOptions(); } diff --git a/src/frontend/qt_sdl/InputConfig/InputConfigDialog.h b/src/frontend/qt_sdl/InputConfig/InputConfigDialog.h index 53ea8762..0591f92b 100644 --- a/src/frontend/qt_sdl/InputConfig/InputConfigDialog.h +++ b/src/frontend/qt_sdl/InputConfig/InputConfigDialog.h @@ -21,9 +21,12 @@ #include #include +#include #include #include "Config.h" +#include "Input.h" +#include "MapButton.h" static constexpr int keypad_num = 12; @@ -115,17 +118,28 @@ private slots: void on_btnJoyMapSwitch_clicked(); void on_cbxJoystick_currentIndexChanged(int id); + void on_grpJoyTouchInput_buttonClicked(QAbstractButton* btn); + void on_grpJoyTouchStyle_buttonClicked(QAbstractButton* btn); + private: void populatePage(QWidget* page, const std::initializer_list& labels, int* keymap, int* joymap); void setupKeypadPage(); + void updateJoyTouchOptions(); + Input::JoystickTouchMode joyTouchModeFromUI(); + void joyTouchModeToUI(Input::JoystickTouchMode mode); Ui::InputConfigDialog* ui; int keypadKeyMap[12], keypadJoyMap[12]; int addonsKeyMap[hk_addons.size()], addonsJoyMap[hk_addons.size()]; int hkGeneralKeyMap[hk_general.size()], hkGeneralJoyMap[hk_general.size()]; + int recenterMapping; + int joyPressMapping; + + JoyMapButton* recenterBtn; + JoyMapButton* joyPressBtn; }; diff --git a/src/frontend/qt_sdl/InputConfig/InputConfigDialog.ui b/src/frontend/qt_sdl/InputConfig/InputConfigDialog.ui index 7e30401e..cf9da63d 100644 --- a/src/frontend/qt_sdl/InputConfig/InputConfigDialog.ui +++ b/src/frontend/qt_sdl/InputConfig/InputConfigDialog.ui @@ -55,6 +55,9 @@ Joystick: + + cbxJoystick + @@ -2371,10 +2374,13 @@ - + Controller input + + rbTouchNone + @@ -2396,41 +2402,56 @@ 0 - + None (mouse only) + + grpJoyTouchInput + - + Analog stick + + grpJoyTouchInput + - + Touchpad + + grpJoyTouchInput + - + Gyroscope + + grpJoyTouchInput + - + Movement style + + rbTouchRelative + @@ -2458,41 +2479,36 @@ 0 - + Relative + + grpJoyTouchStyle + - + Absolute + + grpJoyTouchStyle + - - - - Recenter - - - - - - - [PLACEHOLDER] - - - - + Analog stick + + rbStickLeft + @@ -2520,22 +2536,62 @@ 0 - + Left + + grpJoyTouchStick + - + Right + + grpJoyTouchStick + + + + + Recenter + + + btnJoyTouchRecenter + + + + + + + [PLACEHOLDER] + + + + + + + Touch + + + btnJoyTouchPress + + + + + + + [PLACEHOLDER] + + + @@ -2568,6 +2624,9 @@ Color + + pushButton + @@ -2582,6 +2641,9 @@ Hide after + + spinBox + @@ -2677,8 +2739,8 @@ accept() - 248 - 254 + 496 + 763 157 @@ -2693,8 +2755,8 @@ reject() - 316 - 260 + 564 + 763 286 @@ -2703,4 +2765,9 @@ + + + + + diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp index 53e11c5e..30ba2175 100644 --- a/src/frontend/qt_sdl/main.cpp +++ b/src/frontend/qt_sdl/main.cpp @@ -3312,6 +3312,7 @@ int main(int argc, char** argv) Input::JoystickID = Config::JoystickID; Input::OpenJoystick(); + Input::LoadJoystickTouchMode(); mainWindow = new MainWindow(); if (options->fullscreen)