From cc9a03ce480b34c1730e8857b50f84585d3b388c Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Tue, 2 Apr 2024 04:20:24 +0000 Subject: [PATCH] Add toggle: SDL GameController mode for joysticks Add a toggle for SDL GameController Mode in the game key configuration dialog, default enabled. On check or uncheck, change the option and reinitialize joysticks, not using GameController mode if it is disabled. Signed-off-by: Rafael Kitover --- src/wx/config/internal/option-internal.cpp | 4 ++++ src/wx/config/option-id.h | 1 + src/wx/config/option-proxy.h | 1 + src/wx/dialogs/joypad-config.cpp | 24 +++++++++++++++++++++- src/wx/dialogs/joypad-config.h | 6 ++++++ src/wx/widgets/sdljoy.cpp | 4 +++- src/wx/wxvbam.h | 2 ++ src/wx/xrc/JoypadConfig.xrc | 6 ++++++ 8 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/wx/config/internal/option-internal.cpp b/src/wx/config/internal/option-internal.cpp index cd44e15e..99a9c516 100644 --- a/src/wx/config/internal/option-internal.cpp +++ b/src/wx/config/internal/option-internal.cpp @@ -211,6 +211,7 @@ std::array& Option::All() { /// Joypad uint32_t default_stick = 1; + bool sdl_game_controller_mode = true; /// Geometry bool fullscreen = false; @@ -293,6 +294,7 @@ std::array& Option::All() { Option(OptionID::kJoy), Option(OptionID::kJoyAutofireThrottle, &gopts.autofire_rate, 1, 1000), Option(OptionID::kJoyDefault, &g_owned_opts.default_stick, 1, 4), + Option(OptionID::kSDLGameControllerMode, &g_owned_opts.sdl_game_controller_mode), /// Keyboard Option(OptionID::kKeyboard), @@ -478,6 +480,8 @@ const std::array kAllOptionsData = { _("The autofire toggle period, in frames (1/60 s)")}, OptionData{"Joypad/Default", "", _("The number of the stick to use in single-player mode")}, + OptionData{"Joypad/SDLGameControllerMode", "SDLGameControllerMode", + _("Whether to enable SDL GameController mode")}, /// Keyboard OptionData{"Keyboard/*", "", diff --git a/src/wx/config/option-id.h b/src/wx/config/option-id.h index 90973157..8aa80107 100644 --- a/src/wx/config/option-id.h +++ b/src/wx/config/option-id.h @@ -61,6 +61,7 @@ enum class OptionID { kJoy, kJoyAutofireThrottle, kJoyDefault, + kSDLGameControllerMode, /// Keyboard kKeyboard, diff --git a/src/wx/config/option-proxy.h b/src/wx/config/option-proxy.h index 3b132611..e61b922c 100644 --- a/src/wx/config/option-proxy.h +++ b/src/wx/config/option-proxy.h @@ -65,6 +65,7 @@ static constexpr std::array kOptionsTypes = { /*kJoy*/ Option::Type::kNone, /*kJoyAutofireThrottle*/ Option::Type::kInt, /*kJoyDefault*/ Option::Type::kUnsigned, + /*kSDLGameControllerMode*/ Option::Type::kBool, /// Keyboard /*kKeyboard*/ Option::Type::kNone, diff --git a/src/wx/dialogs/joypad-config.cpp b/src/wx/dialogs/joypad-config.cpp index de781db1..9729fe41 100644 --- a/src/wx/dialogs/joypad-config.cpp +++ b/src/wx/dialogs/joypad-config.cpp @@ -3,9 +3,11 @@ #include #include "wx/dialogs/validated-child.h" -#include "wx/opts.h" +#include "wx/config/option-proxy.h" +#include "wx/config/option.h" #include "wx/widgets/option-validator.h" #include "wx/widgets/user-input-ctrl.h" +#include "wx/wxvbam.h" namespace dialogs { @@ -23,6 +25,11 @@ JoypadConfig::JoypadConfig(wxWindow* parent) : wxDialog(), keep_on_top_styler_(t #endif wxXmlResource::Get()->LoadDialog(this, parent, "JoypadConfig"); + this->Bind(wxEVT_CHECKBOX, std::bind(&JoypadConfig::ToggleSDLGameControllerMode, this), + XRCID("SDLGameControllerMode")); + + GetValidatedChild(this, "SDLGameControllerMode")->SetValue(OPTION(kSDLGameControllerMode)); + for (int joypad = 0; joypad < 4; joypad++) { wxWindow* panel = GetValidatedChild(this, wxString::Format("joy%d", joypad + 1)); @@ -82,4 +89,19 @@ void JoypadConfig::ClearJoypad(wxWindow* panel) { } } +void JoypadConfig::ToggleSDLGameControllerMode() { + OPTION(kSDLGameControllerMode) = GetValidatedChild(this, "SDLGameControllerMode") + ->IsChecked(); + ClearAllJoypads(); + wxGetApp().frame->PollAllJoysticks(); +} + +void JoypadConfig::ClearAllJoypads() { + for (unsigned joypad = 0; joypad < 4; joypad++) { + wxWindow* panel = GetValidatedChild(this, wxString::Format("joy%d", joypad + 1)); + + ClearJoypad(panel); + } +} + } // namespace dialogs diff --git a/src/wx/dialogs/joypad-config.h b/src/wx/dialogs/joypad-config.h index 3cc96f6d..0ba191d3 100644 --- a/src/wx/dialogs/joypad-config.h +++ b/src/wx/dialogs/joypad-config.h @@ -25,6 +25,12 @@ private: // Clears all Joypad controls. void ClearJoypad(wxWindow* panel); + // Clears all Joypad controls for all Joypads. + void ClearAllJoypads(); + + // Toggle SDL GameController mode for all joysticks. + void ToggleSDLGameControllerMode(); + const widgets::KeepOnTopStyler keep_on_top_styler_; }; diff --git a/src/wx/widgets/sdljoy.cpp b/src/wx/widgets/sdljoy.cpp index 9e465cd8..3f655e4a 100644 --- a/src/wx/widgets/sdljoy.cpp +++ b/src/wx/widgets/sdljoy.cpp @@ -3,6 +3,8 @@ #include #include +#include "wx/config/option-proxy.h" +#include "wx/config/option.h" #include "wx/wxvbam.h" namespace { @@ -171,7 +173,7 @@ wxSDLJoyState::wxSDLJoyState(int sdl_index) wxSDLJoyState::wxSDLJoyState(wxJoystick joystick) : wx_joystick_(joystick) { int sdl_index = wx_joystick_.sdl_index_; - if (SDL_IsGameController(sdl_index)) { + if (OPTION(kSDLGameControllerMode) && SDL_IsGameController(sdl_index)) { game_controller_ = SDL_GameControllerOpen(sdl_index); if (game_controller_) sdl_joystick_ = SDL_GameControllerGetJoystick(game_controller_); diff --git a/src/wx/wxvbam.h b/src/wx/wxvbam.h index bd7b5f8c..6bf79922 100644 --- a/src/wx/wxvbam.h +++ b/src/wx/wxvbam.h @@ -314,6 +314,8 @@ public: } void PollJoysticks() { joy.Poll(); } + + void PollAllJoysticks() { joy.PollAllJoysticks(); } // Poll joysticks with timer. void StartJoyPollTimer(); diff --git a/src/wx/xrc/JoypadConfig.xrc b/src/wx/xrc/JoypadConfig.xrc index df7d314b..ed24b5db 100644 --- a/src/wx/xrc/JoypadConfig.xrc +++ b/src/wx/xrc/JoypadConfig.xrc @@ -4,6 +4,12 @@ Joypad Configuration wxVERTICAL + + wxALIGN_CENTRE_HORIZONTAL + + + +