mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-08-22 06:13:45 -07:00
Add per-controller settings
This commit is contained in:
parent
0e1e62713d
commit
506ca1af3b
11 changed files with 133 additions and 46 deletions
|
@ -3,6 +3,7 @@
|
|||
#include <memory>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#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<ControllerAttachment> GetAttachment() { return Attachment; }
|
||||
int32_t GetControllerNumber() { return dwControllerNumber; }
|
||||
virtual bool HasPadConf() = 0;
|
||||
virtual std::optional<std::string> GetPadConfSection() = 0;
|
||||
|
||||
protected:
|
||||
int32_t dwPressedButtons;
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
#include <string>
|
||||
#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<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() {
|
||||
const std::shared_ptr<ConfigFile> 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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -16,6 +16,9 @@ namespace Ship {
|
|||
bool ReleaseButton(int32_t dwScancode);
|
||||
void ReleaseAllButtons();
|
||||
|
||||
std::optional<std::string> GetPadConfSection() { return {}; }
|
||||
bool HasPadConf() { return false; };
|
||||
|
||||
protected:
|
||||
std::string GetControllerType();
|
||||
std::string GetConfSection();
|
||||
|
|
|
@ -74,15 +74,21 @@ namespace Ship {
|
|||
Cont = NewCont;
|
||||
|
||||
std::string BindingConfSection = GetBindingConfSection();
|
||||
std::shared_ptr<ConfigFile> pBindingConf = GlobalCtx2::GetInstance()->GetConfig();
|
||||
ConfigFile& BindingConf = *pBindingConf.get();
|
||||
std::string PadConfSection = *GetPadConfSection();
|
||||
std::shared_ptr<ConfigFile> 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<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) {
|
||||
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<std::string> SDLController::GetPadConfSection() {
|
||||
return GetControllerType() + " CONTROLLER PAD " + guid;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,12 +16,16 @@ namespace Ship {
|
|||
|
||||
std::string GetGuid() { return guid; };
|
||||
|
||||
std::optional<std::string> 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:
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
#include <iostream>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include "Archive.h"
|
||||
#include "Environment.h"
|
||||
|
@ -344,14 +347,34 @@ namespace SohImGui {
|
|||
}
|
||||
|
||||
if (ImGui::BeginMenu("Controller")) {
|
||||
ImGui::Text("Gyro Sensitivity: %d %%", static_cast<int>(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<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;
|
||||
}
|
||||
|
||||
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<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();
|
||||
|
@ -361,11 +384,6 @@ namespace SohImGui {
|
|||
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)) {
|
||||
needs_save = true;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <locale>
|
||||
#include <codecvt>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue