From 506ca1af3b7e6715d83f90503ba2f856ed9535c9 Mon Sep 17 00:00:00 2001 From: Sirius902 <3645979-Sirius902@users.noreply.gitlab.com> Date: Sun, 24 Apr 2022 15:05:14 -0700 Subject: [PATCH] Add per-controller settings --- libultraship/libultraship/Controller.h | 3 + libultraship/libultraship/GameSettings.cpp | 41 ++++++++++--- libultraship/libultraship/GameSettings.h | 11 ++-- .../libultraship/KeyboardController.cpp | 2 +- .../libultraship/KeyboardController.h | 3 + libultraship/libultraship/SDLController.cpp | 60 ++++++++++++++----- libultraship/libultraship/SDLController.h | 4 ++ libultraship/libultraship/SohImGuiImpl.cpp | 42 +++++++++---- soh/soh/OTRGlobals.cpp | 7 ++- soh/soh/OTRGlobals.h | 2 +- soh/src/code/padmgr.c | 4 +- 11 files changed, 133 insertions(+), 46 deletions(-) diff --git a/libultraship/libultraship/Controller.h b/libultraship/libultraship/Controller.h index 9deb912dc..19ec7c6dd 100644 --- a/libultraship/libultraship/Controller.h +++ b/libultraship/libultraship/Controller.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "stdint.h" #include "UltraController.h" #include "ControllerAttachment.h" @@ -25,6 +26,8 @@ namespace Ship { void SetButtonMapping(const std::string& szButtonName, int32_t dwScancode); std::shared_ptr GetAttachment() { return Attachment; } int32_t GetControllerNumber() { return dwControllerNumber; } + virtual bool HasPadConf() = 0; + virtual std::optional GetPadConfSection() = 0; protected: int32_t dwPressedButtons; diff --git a/libultraship/libultraship/GameSettings.cpp b/libultraship/libultraship/GameSettings.cpp index 56db78a18..637d9d084 100644 --- a/libultraship/libultraship/GameSettings.cpp +++ b/libultraship/libultraship/GameSettings.cpp @@ -15,6 +15,8 @@ #include #include "SohHooks.h" +#include "Window.h" + #define ABS(var) var < 0 ? -(var) : var using namespace Ship; @@ -93,12 +95,6 @@ namespace Game { CVar_SetFloat("gFanfareVolume", Settings.audio.fanfare); // Controllers - Settings.controller.gyro_sensitivity = Ship::stof(Conf[ControllerSection]["gyro_sensitivity"]); - CVar_SetFloat("gGyroSensitivity", Settings.controller.gyro_sensitivity); - - Settings.controller.rumble_strength = Ship::stof(Conf[ControllerSection]["rumble_strength"]); - CVar_SetFloat("gRumbleStrength", Settings.controller.rumble_strength); - Settings.controller.rumble_enabled = Ship::stof(Conf[ControllerSection]["rumble_enabled"]); CVar_SetS32("gRumbleEnabled", Settings.controller.rumble_enabled); @@ -157,9 +153,28 @@ namespace Game { Settings.cheats.freeze_time = stob(Conf[CheatSection]["freeze_time"]); CVar_SetS32("gFreezeTime", Settings.cheats.freeze_time); + // Per-Controller + LoadPadSettings(); + UpdateAudio(); } + void LoadPadSettings() { + const std::shared_ptr pConf = GlobalCtx2::GetInstance()->GetConfig(); + ConfigFile& Conf = *pConf; + + for (const auto& [i, controllers] : Ship::Window::Controllers) { + for (const auto& controller : controllers) { + if (auto padConfSection = controller->GetPadConfSection()) { + Settings.controller.extra[i].gyro_sensitivity = Ship::stof(Conf[*padConfSection]["gyro_sensitivity"]); + Settings.controller.extra[i].rumble_strength = Ship::stof(Conf[*padConfSection]["rumble_strength"]); + Settings.controller.extra[i].gyro_drift_x = Ship::stof(Conf[*padConfSection]["gyro_drift_x"], 0.0f); + Settings.controller.extra[i].gyro_drift_y = Ship::stof(Conf[*padConfSection]["gyro_drift_y"], 0.0f); + } + } + } + } + void SaveSettings() { const std::shared_ptr pConf = GlobalCtx2::GetInstance()->GetConfig(); ConfigFile& Conf = *pConf; @@ -190,8 +205,6 @@ namespace Game { // Controllers - Conf[ControllerSection]["gyro_sensitivity"] = std::to_string(Settings.controller.gyro_sensitivity); - Conf[ControllerSection]["rumble_strength"] = std::to_string(Settings.controller.rumble_strength); Conf[ControllerSection]["rumble_enabled"] = std::to_string(Settings.controller.rumble_enabled); Conf[ControllerSection]["input_scale"] = std::to_string(Settings.controller.input_scale); Conf[ControllerSection]["input_enabled"] = std::to_string(Settings.controller.input_enabled); @@ -210,6 +223,18 @@ namespace Game { Conf[CheatSection]["moon_jump_on_l"] = std::to_string(Settings.cheats.moon_jump_on_l); Conf[CheatSection]["super_tunic"] = std::to_string(Settings.cheats.super_tunic); + // Per-Controller + for (const auto& [i, controllers] : Ship::Window::Controllers) { + for (const auto& controller : controllers) { + if (auto padConfSection = controller->GetPadConfSection()) { + Conf[*padConfSection]["gyro_sensitivity"] = std::to_string(Settings.controller.extra[i].gyro_sensitivity); + Conf[*padConfSection]["rumble_strength"] = std::to_string(Settings.controller.extra[i].rumble_strength); + Conf[*padConfSection]["gyro_drift_x"] = std::to_string(Settings.controller.extra[i].gyro_drift_x); + Conf[*padConfSection]["gyro_drift_y"] = std::to_string(Settings.controller.extra[i].gyro_drift_y); + } + } + } + Conf.Save(); } diff --git a/libultraship/libultraship/GameSettings.h b/libultraship/libultraship/GameSettings.h index 4a553431a..f4d75938d 100644 --- a/libultraship/libultraship/GameSettings.h +++ b/libultraship/libultraship/GameSettings.h @@ -34,12 +34,14 @@ struct SoHConfigType { // Controller struct { - float gyro_sensitivity = 1.0f; - float rumble_strength = 1.0f; + struct { + float gyro_sensitivity = 1.0f; + float rumble_strength = 1.0f; + float gyro_drift_x = 0.0f; + float gyro_drift_y = 0.0f; + } extra[4]; bool rumble_enabled = true; float input_scale = 1.0f; - float gyroDriftX = 0.0f; - float gyroDriftY = 0.0f; bool input_enabled = false; bool dpad_pause_name = false; bool dpad_ocarina_text = false; @@ -87,6 +89,7 @@ namespace Game { extern SoHConfigType Settings; void InitSettings(); void LoadSettings(); + void LoadPadSettings(); void SaveSettings(); void SetSeqPlayerVolume(SeqPlayers playerId, float volume); } diff --git a/libultraship/libultraship/KeyboardController.cpp b/libultraship/libultraship/KeyboardController.cpp index 6f78a1678..13014c6a7 100644 --- a/libultraship/libultraship/KeyboardController.cpp +++ b/libultraship/libultraship/KeyboardController.cpp @@ -53,4 +53,4 @@ namespace Ship { std::string KeyboardController::GetBindingConfSection() { return GetControllerType() + " CONTROLLER BINDING " + std::to_string(GetControllerNumber() + 1); } -} \ No newline at end of file +} diff --git a/libultraship/libultraship/KeyboardController.h b/libultraship/libultraship/KeyboardController.h index 1a97065d5..97e2235ff 100644 --- a/libultraship/libultraship/KeyboardController.h +++ b/libultraship/libultraship/KeyboardController.h @@ -16,6 +16,9 @@ namespace Ship { bool ReleaseButton(int32_t dwScancode); void ReleaseAllButtons(); + std::optional GetPadConfSection() { return {}; } + bool HasPadConf() { return false; }; + protected: std::string GetControllerType(); std::string GetConfSection(); diff --git a/libultraship/libultraship/SDLController.cpp b/libultraship/libultraship/SDLController.cpp index 78b221880..267607048 100644 --- a/libultraship/libultraship/SDLController.cpp +++ b/libultraship/libultraship/SDLController.cpp @@ -74,15 +74,21 @@ namespace Ship { Cont = NewCont; std::string BindingConfSection = GetBindingConfSection(); - std::shared_ptr pBindingConf = GlobalCtx2::GetInstance()->GetConfig(); - ConfigFile& BindingConf = *pBindingConf.get(); + std::string PadConfSection = *GetPadConfSection(); + std::shared_ptr config = GlobalCtx2::GetInstance()->GetConfig(); - if (!BindingConf.has(BindingConfSection)) { + if (!config->has(BindingConfSection)) { CreateDefaultBinding(); } + if (!config->has(PadConfSection)) { + CreateDefaultPadConf(); + } + LoadBinding(); LoadAxisThresholds(); + // Update per-controller settings in ImGui menu after opening controller. + Game::LoadPadSettings(); break; } @@ -181,37 +187,41 @@ namespace Ship { if (SDL_GameControllerHasSensor(Cont, SDL_SENSOR_GYRO)) { + size_t contNumber = GetControllerNumber(); + float& gyro_drift_x = Game::Settings.controller.extra[contNumber].gyro_drift_x; + float& gyro_drift_y = Game::Settings.controller.extra[contNumber].gyro_drift_y; + const float gyro_sensitivity = Game::Settings.controller.extra[contNumber].gyro_sensitivity; + float gyroData[3]; SDL_GameControllerGetSensorData(Cont, SDL_SENSOR_GYRO, gyroData, 3); const char* contName = SDL_GameControllerName(Cont); const int isSpecialController = !strcmp("PS5 Controller", contName); - const float gyroSensitivity = Game::Settings.controller.gyro_sensitivity; - if (Game::Settings.controller.gyroDriftX == 0) { - Game::Settings.controller.gyroDriftX = gyroData[0]; + if (gyro_drift_x == 0) { + gyro_drift_x = gyroData[0]; } - if (Game::Settings.controller.gyroDriftY == 0) { + if (gyro_drift_y == 0) { if (isSpecialController == 1) { - Game::Settings.controller.gyroDriftY = gyroData[2]; + gyro_drift_y = gyroData[2]; } else { - Game::Settings.controller.gyroDriftY = gyroData[1]; + gyro_drift_y = gyroData[1]; } } if (isSpecialController == 1) { - wGyroX = gyroData[0] - Game::Settings.controller.gyroDriftX; - wGyroY = -gyroData[2] - Game::Settings.controller.gyroDriftY; + wGyroX = gyroData[0] - gyro_drift_x; + wGyroY = -gyroData[2] - gyro_drift_y; } else { - wGyroX = gyroData[0] - Game::Settings.controller.gyroDriftX; - wGyroY = gyroData[1] - Game::Settings.controller.gyroDriftY; + wGyroX = gyroData[0] - gyro_drift_x; + wGyroY = gyroData[1] - gyro_drift_y; } - wGyroX *= gyroSensitivity; - wGyroY *= gyroSensitivity; + wGyroX *= gyro_sensitivity; + wGyroY *= gyro_sensitivity; } for (int32_t i = SDL_CONTROLLER_BUTTON_A; i < SDL_CONTROLLER_BUTTON_MAX; i++) { @@ -334,7 +344,8 @@ namespace Ship { { if (SDL_GameControllerHasRumble(Cont)) { if (controller->rumble > 0) { - SDL_GameControllerRumble(Cont, 0xFFFF * Game::Settings.controller.rumble_strength, 0xFFFF * Game::Settings.controller.rumble_strength, 0); + float rumble_strength = Game::Settings.controller.extra[GetControllerNumber()].rumble_strength; + SDL_GameControllerRumble(Cont, 0xFFFF * rumble_strength, 0xFFFF * rumble_strength, 0); } else { SDL_GameControllerRumble(Cont, 0, 0, 0); } @@ -396,6 +407,19 @@ namespace Ship { Conf.Save(); } + void SDLController::CreateDefaultPadConf() { + std::string ConfSection = *GetPadConfSection(); + std::shared_ptr pConf = GlobalCtx2::GetInstance()->GetConfig(); + ConfigFile& Conf = *pConf.get(); + + Conf[ConfSection]["gyro_sensitivity"] = std::to_string(1.0f); + Conf[ConfSection]["rumble_strength"] = std::to_string(1.0f); + Conf[ConfSection]["gyro_drift_x"] = std::to_string(0.0f); + Conf[ConfSection]["gyro_drift_y"] = std::to_string(0.0f); + + Conf.Save(); + } + void SDLController::SetButtonMapping(const std::string& szButtonName, int32_t dwScancode) { if (guid.compare(INVALID_SDL_CONTROLLER_GUID)) { return; @@ -415,4 +439,8 @@ namespace Ship { std::string SDLController::GetBindingConfSection() { return GetControllerType() + " CONTROLLER BINDING " + guid; } + + std::optional SDLController::GetPadConfSection() { + return GetControllerType() + " CONTROLLER PAD " + guid; + } } diff --git a/libultraship/libultraship/SDLController.h b/libultraship/libultraship/SDLController.h index f7b538cba..f4c84822d 100644 --- a/libultraship/libultraship/SDLController.h +++ b/libultraship/libultraship/SDLController.h @@ -16,12 +16,16 @@ namespace Ship { std::string GetGuid() { return guid; }; + std::optional GetPadConfSection(); + bool HasPadConf() { return true; }; + protected: std::string GetControllerType(); void SetButtonMapping(const std::string& szButtonName, int32_t dwScancode); std::string GetConfSection(); std::string GetBindingConfSection(); void CreateDefaultBinding(); + void CreateDefaultPadConf(); static bool IsGuidInUse(const std::string& guid); private: diff --git a/libultraship/libultraship/SohImGuiImpl.cpp b/libultraship/libultraship/SohImGuiImpl.cpp index e6ebd5fb4..83de91939 100644 --- a/libultraship/libultraship/SohImGuiImpl.cpp +++ b/libultraship/libultraship/SohImGuiImpl.cpp @@ -3,6 +3,9 @@ #include #include #include +#include +#include +#include #include "Archive.h" #include "Environment.h" @@ -344,14 +347,34 @@ namespace SohImGui { } if (ImGui::BeginMenu("Controller")) { - ImGui::Text("Gyro Sensitivity: %d %%", static_cast(100 * Game::Settings.controller.gyro_sensitivity)); - if (ImGui::SliderFloat("##GYROSCOPE", &Game::Settings.controller.gyro_sensitivity, 0.0f, 1.0f, "")) { - needs_save = true; - } + for (const auto& [i, controllers] : Ship::Window::Controllers) { + bool hasPad = std::find_if(controllers.begin(), controllers.end(), [](const auto& c) { + return c->HasPadConf(); + }) != controllers.end(); + if (!hasPad) continue; - if (ImGui::Button("Recalibrate Gyro")) { - Game::Settings.controller.gyroDriftX = 0; - Game::Settings.controller.gyroDriftY = 0; + auto menuLabel = "Controller " + std::to_string(i + 1); + if (ImGui::BeginMenu(menuLabel.c_str())) { + ImGui::Text("Gyro Sensitivity: %d %%", static_cast(100 * Game::Settings.controller.extra[i].gyro_sensitivity)); + if (ImGui::SliderFloat("##GYROSCOPE", &Game::Settings.controller.extra[i].gyro_sensitivity, 0.0f, 1.0f, "")) { + needs_save = true; + } + + if (ImGui::Button("Recalibrate Gyro")) { + Game::Settings.controller.extra[i].gyro_drift_x = 0; + Game::Settings.controller.extra[i].gyro_drift_y = 0; + needs_save = true; + } + + ImGui::Separator(); + + ImGui::Text("Rumble Strength: %d %%", static_cast(100 * Game::Settings.controller.extra[i].rumble_strength)); + if (ImGui::SliderFloat("##RUMBLE", &Game::Settings.controller.extra[i].rumble_strength, 0.0f, 1.0f, "")) { + needs_save = true; + } + + ImGui::EndMenu(); + } } ImGui::Separator(); @@ -361,11 +384,6 @@ namespace SohImGui { needs_save = true; } - ImGui::Text("Rumble Strength: %d %%", static_cast(100 * Game::Settings.controller.rumble_strength)); - if (ImGui::SliderFloat("##RUMBLE", &Game::Settings.controller.rumble_strength, 0.0f, 1.0f, "")) { - needs_save = true; - } - if (ImGui::Checkbox("Show Inputs", &Game::Settings.controller.input_enabled)) { needs_save = true; } diff --git a/soh/soh/OTRGlobals.cpp b/soh/soh/OTRGlobals.cpp index 0ad86ac63..f125437ab 100644 --- a/soh/soh/OTRGlobals.cpp +++ b/soh/soh/OTRGlobals.cpp @@ -3,6 +3,7 @@ #include #include #include "GlobalCtx2.h" +#include "GameSettings.h" #include "ResourceMgr.h" #include "DisplayList.h" #include "PlayerAnimation.h" @@ -886,9 +887,9 @@ extern "C" void AudioPlayer_Play(const uint8_t* buf, uint32_t len) { } } -extern "C" int Controller_CanRumble(void) { - for (const auto& controller : Ship::Window::Controllers.at(0)) { - if (controller->CanRumble()) { +extern "C" int Controller_ShouldRumble(size_t i) { + for (const auto& controller : Ship::Window::Controllers.at(i)) { + if (controller->CanRumble() && Game::Settings.controller.extra[i].rumble_strength > 0.001f) { return 1; } } diff --git a/soh/soh/OTRGlobals.h b/soh/soh/OTRGlobals.h index 6a4fe8c86..dfb235712 100644 --- a/soh/soh/OTRGlobals.h +++ b/soh/soh/OTRGlobals.h @@ -64,5 +64,5 @@ int AudioPlayer_Buffered(void); int AudioPlayer_GetDesiredBuffered(void); void AudioPlayer_Play(const uint8_t* buf, uint32_t len); void AudioMgr_CreateNextAudioBuffer(s16* samples, u32 num_samples); -int Controller_CanRumble(void); +int Controller_ShouldRumble(size_t i); #endif diff --git a/soh/src/code/padmgr.c b/soh/src/code/padmgr.c index 2ae3abaa3..08c89869f 100644 --- a/soh/src/code/padmgr.c +++ b/soh/src/code/padmgr.c @@ -304,7 +304,9 @@ void PadMgr_HandleRetraceMsg(PadMgr* padMgr) { osRecvMesg(queue, NULL, OS_MESG_BLOCK); osContGetReadData(padMgr->pads); - padMgr->padStatus[0].status = CVar_GetS32("gRumbleEnabled", 0) && Controller_CanRumble(); + for (i = 0; i < __osMaxControllers; i++) { + padMgr->padStatus[i].status = CVar_GetS32("gRumbleEnabled", 0) && Controller_ShouldRumble(i); + } if (padMgr->preNMIShutdown) { memset(padMgr->pads, 0, sizeof(padMgr->pads));