Add per-controller settings

This commit is contained in:
Sirius902 2022-04-24 15:05:14 -07:00
commit 506ca1af3b
11 changed files with 133 additions and 46 deletions

View file

@ -3,6 +3,7 @@
#include <memory> #include <memory>
#include <map> #include <map>
#include <string> #include <string>
#include <optional>
#include "stdint.h" #include "stdint.h"
#include "UltraController.h" #include "UltraController.h"
#include "ControllerAttachment.h" #include "ControllerAttachment.h"
@ -25,6 +26,8 @@ namespace Ship {
void SetButtonMapping(const std::string& szButtonName, int32_t dwScancode); void SetButtonMapping(const std::string& szButtonName, int32_t dwScancode);
std::shared_ptr<ControllerAttachment> GetAttachment() { return Attachment; } std::shared_ptr<ControllerAttachment> GetAttachment() { return Attachment; }
int32_t GetControllerNumber() { return dwControllerNumber; } int32_t GetControllerNumber() { return dwControllerNumber; }
virtual bool HasPadConf() = 0;
virtual std::optional<std::string> GetPadConfSection() = 0;
protected: protected:
int32_t dwPressedButtons; int32_t dwPressedButtons;

View file

@ -15,6 +15,8 @@
#include <string> #include <string>
#include "SohHooks.h" #include "SohHooks.h"
#include "Window.h"
#define ABS(var) var < 0 ? -(var) : var #define ABS(var) var < 0 ? -(var) : var
using namespace Ship; using namespace Ship;
@ -93,12 +95,6 @@ namespace Game {
CVar_SetFloat("gFanfareVolume", Settings.audio.fanfare); CVar_SetFloat("gFanfareVolume", Settings.audio.fanfare);
// Controllers // 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"]); Settings.controller.rumble_enabled = Ship::stof(Conf[ControllerSection]["rumble_enabled"]);
CVar_SetS32("gRumbleEnabled", Settings.controller.rumble_enabled); CVar_SetS32("gRumbleEnabled", Settings.controller.rumble_enabled);
@ -157,9 +153,28 @@ namespace Game {
Settings.cheats.freeze_time = stob(Conf[CheatSection]["freeze_time"]); Settings.cheats.freeze_time = stob(Conf[CheatSection]["freeze_time"]);
CVar_SetS32("gFreezeTime", Settings.cheats.freeze_time); CVar_SetS32("gFreezeTime", Settings.cheats.freeze_time);
// Per-Controller
LoadPadSettings();
UpdateAudio(); UpdateAudio();
} }
void LoadPadSettings() {
const std::shared_ptr<ConfigFile> 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() { void SaveSettings() {
const std::shared_ptr<ConfigFile> pConf = GlobalCtx2::GetInstance()->GetConfig(); const std::shared_ptr<ConfigFile> pConf = GlobalCtx2::GetInstance()->GetConfig();
ConfigFile& Conf = *pConf; ConfigFile& Conf = *pConf;
@ -190,8 +205,6 @@ namespace Game {
// Controllers // 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]["rumble_enabled"] = std::to_string(Settings.controller.rumble_enabled);
Conf[ControllerSection]["input_scale"] = std::to_string(Settings.controller.input_scale); Conf[ControllerSection]["input_scale"] = std::to_string(Settings.controller.input_scale);
Conf[ControllerSection]["input_enabled"] = std::to_string(Settings.controller.input_enabled); 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]["moon_jump_on_l"] = std::to_string(Settings.cheats.moon_jump_on_l);
Conf[CheatSection]["super_tunic"] = std::to_string(Settings.cheats.super_tunic); 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(); Conf.Save();
} }

View file

@ -33,13 +33,15 @@ struct SoHConfigType {
} enhancements; } enhancements;
// Controller // Controller
struct {
struct { struct {
float gyro_sensitivity = 1.0f; float gyro_sensitivity = 1.0f;
float rumble_strength = 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; bool rumble_enabled = true;
float input_scale = 1.0f; float input_scale = 1.0f;
float gyroDriftX = 0.0f;
float gyroDriftY = 0.0f;
bool input_enabled = false; bool input_enabled = false;
bool dpad_pause_name = false; bool dpad_pause_name = false;
bool dpad_ocarina_text = false; bool dpad_ocarina_text = false;
@ -87,6 +89,7 @@ namespace Game {
extern SoHConfigType Settings; extern SoHConfigType Settings;
void InitSettings(); void InitSettings();
void LoadSettings(); void LoadSettings();
void LoadPadSettings();
void SaveSettings(); void SaveSettings();
void SetSeqPlayerVolume(SeqPlayers playerId, float volume); void SetSeqPlayerVolume(SeqPlayers playerId, float volume);
} }

View file

@ -16,6 +16,9 @@ namespace Ship {
bool ReleaseButton(int32_t dwScancode); bool ReleaseButton(int32_t dwScancode);
void ReleaseAllButtons(); void ReleaseAllButtons();
std::optional<std::string> GetPadConfSection() { return {}; }
bool HasPadConf() { return false; };
protected: protected:
std::string GetControllerType(); std::string GetControllerType();
std::string GetConfSection(); std::string GetConfSection();

View file

@ -74,15 +74,21 @@ namespace Ship {
Cont = NewCont; Cont = NewCont;
std::string BindingConfSection = GetBindingConfSection(); std::string BindingConfSection = GetBindingConfSection();
std::shared_ptr<ConfigFile> pBindingConf = GlobalCtx2::GetInstance()->GetConfig(); std::string PadConfSection = *GetPadConfSection();
ConfigFile& BindingConf = *pBindingConf.get(); std::shared_ptr<ConfigFile> config = GlobalCtx2::GetInstance()->GetConfig();
if (!BindingConf.has(BindingConfSection)) { if (!config->has(BindingConfSection)) {
CreateDefaultBinding(); CreateDefaultBinding();
} }
if (!config->has(PadConfSection)) {
CreateDefaultPadConf();
}
LoadBinding(); LoadBinding();
LoadAxisThresholds(); LoadAxisThresholds();
// Update per-controller settings in ImGui menu after opening controller.
Game::LoadPadSettings();
break; break;
} }
@ -181,37 +187,41 @@ namespace Ship {
if (SDL_GameControllerHasSensor(Cont, SDL_SENSOR_GYRO)) 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]; float gyroData[3];
SDL_GameControllerGetSensorData(Cont, SDL_SENSOR_GYRO, gyroData, 3); SDL_GameControllerGetSensorData(Cont, SDL_SENSOR_GYRO, gyroData, 3);
const char* contName = SDL_GameControllerName(Cont); const char* contName = SDL_GameControllerName(Cont);
const int isSpecialController = !strcmp("PS5 Controller", contName); const int isSpecialController = !strcmp("PS5 Controller", contName);
const float gyroSensitivity = Game::Settings.controller.gyro_sensitivity;
if (Game::Settings.controller.gyroDriftX == 0) { if (gyro_drift_x == 0) {
Game::Settings.controller.gyroDriftX = gyroData[0]; gyro_drift_x = gyroData[0];
} }
if (Game::Settings.controller.gyroDriftY == 0) { if (gyro_drift_y == 0) {
if (isSpecialController == 1) { if (isSpecialController == 1) {
Game::Settings.controller.gyroDriftY = gyroData[2]; gyro_drift_y = gyroData[2];
} }
else { else {
Game::Settings.controller.gyroDriftY = gyroData[1]; gyro_drift_y = gyroData[1];
} }
} }
if (isSpecialController == 1) { if (isSpecialController == 1) {
wGyroX = gyroData[0] - Game::Settings.controller.gyroDriftX; wGyroX = gyroData[0] - gyro_drift_x;
wGyroY = -gyroData[2] - Game::Settings.controller.gyroDriftY; wGyroY = -gyroData[2] - gyro_drift_y;
} }
else { else {
wGyroX = gyroData[0] - Game::Settings.controller.gyroDriftX; wGyroX = gyroData[0] - gyro_drift_x;
wGyroY = gyroData[1] - Game::Settings.controller.gyroDriftY; wGyroY = gyroData[1] - gyro_drift_y;
} }
wGyroX *= gyroSensitivity; wGyroX *= gyro_sensitivity;
wGyroY *= gyroSensitivity; wGyroY *= gyro_sensitivity;
} }
for (int32_t i = SDL_CONTROLLER_BUTTON_A; i < SDL_CONTROLLER_BUTTON_MAX; i++) { 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 (SDL_GameControllerHasRumble(Cont)) {
if (controller->rumble > 0) { 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 { } else {
SDL_GameControllerRumble(Cont, 0, 0, 0); SDL_GameControllerRumble(Cont, 0, 0, 0);
} }
@ -396,6 +407,19 @@ namespace Ship {
Conf.Save(); Conf.Save();
} }
void SDLController::CreateDefaultPadConf() {
std::string ConfSection = *GetPadConfSection();
std::shared_ptr<ConfigFile> 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) { void SDLController::SetButtonMapping(const std::string& szButtonName, int32_t dwScancode) {
if (guid.compare(INVALID_SDL_CONTROLLER_GUID)) { if (guid.compare(INVALID_SDL_CONTROLLER_GUID)) {
return; return;
@ -415,4 +439,8 @@ namespace Ship {
std::string SDLController::GetBindingConfSection() { std::string SDLController::GetBindingConfSection() {
return GetControllerType() + " CONTROLLER BINDING " + guid; return GetControllerType() + " CONTROLLER BINDING " + guid;
} }
std::optional<std::string> SDLController::GetPadConfSection() {
return GetControllerType() + " CONTROLLER PAD " + guid;
}
} }

View file

@ -16,12 +16,16 @@ namespace Ship {
std::string GetGuid() { return guid; }; std::string GetGuid() { return guid; };
std::optional<std::string> GetPadConfSection();
bool HasPadConf() { return true; };
protected: protected:
std::string GetControllerType(); std::string GetControllerType();
void SetButtonMapping(const std::string& szButtonName, int32_t dwScancode); void SetButtonMapping(const std::string& szButtonName, int32_t dwScancode);
std::string GetConfSection(); std::string GetConfSection();
std::string GetBindingConfSection(); std::string GetBindingConfSection();
void CreateDefaultBinding(); void CreateDefaultBinding();
void CreateDefaultPadConf();
static bool IsGuidInUse(const std::string& guid); static bool IsGuidInUse(const std::string& guid);
private: private:

View file

@ -3,6 +3,9 @@
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <utility> #include <utility>
#include <string>
#include <algorithm>
#include <vector>
#include "Archive.h" #include "Archive.h"
#include "Environment.h" #include "Environment.h"
@ -344,14 +347,34 @@ namespace SohImGui {
} }
if (ImGui::BeginMenu("Controller")) { if (ImGui::BeginMenu("Controller")) {
ImGui::Text("Gyro Sensitivity: %d %%", static_cast<int>(100 * Game::Settings.controller.gyro_sensitivity)); for (const auto& [i, controllers] : Ship::Window::Controllers) {
if (ImGui::SliderFloat("##GYROSCOPE", &Game::Settings.controller.gyro_sensitivity, 0.0f, 1.0f, "")) { bool hasPad = std::find_if(controllers.begin(), controllers.end(), [](const auto& c) {
return c->HasPadConf();
}) != controllers.end();
if (!hasPad) continue;
auto menuLabel = "Controller " + std::to_string(i + 1);
if (ImGui::BeginMenu(menuLabel.c_str())) {
ImGui::Text("Gyro Sensitivity: %d %%", static_cast<int>(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; needs_save = true;
} }
if (ImGui::Button("Recalibrate Gyro")) { if (ImGui::Button("Recalibrate Gyro")) {
Game::Settings.controller.gyroDriftX = 0; Game::Settings.controller.extra[i].gyro_drift_x = 0;
Game::Settings.controller.gyroDriftY = 0; Game::Settings.controller.extra[i].gyro_drift_y = 0;
needs_save = true;
}
ImGui::Separator();
ImGui::Text("Rumble Strength: %d %%", static_cast<int>(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(); ImGui::Separator();
@ -361,11 +384,6 @@ namespace SohImGui {
needs_save = true; needs_save = true;
} }
ImGui::Text("Rumble Strength: %d %%", static_cast<int>(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)) { if (ImGui::Checkbox("Show Inputs", &Game::Settings.controller.input_enabled)) {
needs_save = true; needs_save = true;
} }

View file

@ -3,6 +3,7 @@
#include <locale> #include <locale>
#include <codecvt> #include <codecvt>
#include "GlobalCtx2.h" #include "GlobalCtx2.h"
#include "GameSettings.h"
#include "ResourceMgr.h" #include "ResourceMgr.h"
#include "DisplayList.h" #include "DisplayList.h"
#include "PlayerAnimation.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) { extern "C" int Controller_ShouldRumble(size_t i) {
for (const auto& controller : Ship::Window::Controllers.at(0)) { for (const auto& controller : Ship::Window::Controllers.at(i)) {
if (controller->CanRumble()) { if (controller->CanRumble() && Game::Settings.controller.extra[i].rumble_strength > 0.001f) {
return 1; return 1;
} }
} }

View file

@ -64,5 +64,5 @@ int AudioPlayer_Buffered(void);
int AudioPlayer_GetDesiredBuffered(void); int AudioPlayer_GetDesiredBuffered(void);
void AudioPlayer_Play(const uint8_t* buf, uint32_t len); void AudioPlayer_Play(const uint8_t* buf, uint32_t len);
void AudioMgr_CreateNextAudioBuffer(s16* samples, u32 num_samples); void AudioMgr_CreateNextAudioBuffer(s16* samples, u32 num_samples);
int Controller_CanRumble(void); int Controller_ShouldRumble(size_t i);
#endif #endif

View file

@ -304,7 +304,9 @@ void PadMgr_HandleRetraceMsg(PadMgr* padMgr) {
osRecvMesg(queue, NULL, OS_MESG_BLOCK); osRecvMesg(queue, NULL, OS_MESG_BLOCK);
osContGetReadData(padMgr->pads); 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) { if (padMgr->preNMIShutdown) {
memset(padMgr->pads, 0, sizeof(padMgr->pads)); memset(padMgr->pads, 0, sizeof(padMgr->pads));