Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rumble cvar and fixes #184

Merged
merged 15 commits into from
Apr 26, 2022
6 changes: 6 additions & 0 deletions libultraship/libultraship/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <map>
#include <string>
#include <optional>
#include "stdint.h"
#include "UltraController.h"
#include "ControllerAttachment.h"
Expand All @@ -19,12 +20,17 @@ namespace Ship {
void Read(OSContPad* pad);
virtual void ReadFromSource() = 0;
virtual void WriteToSource(ControllerCallback* controller) = 0;
virtual bool Connected() const = 0;
virtual bool CanRumble() const = 0;
bool isRumbling;

void SetButtonMapping(const std::string& szButtonName, int32_t dwScancode);
std::shared_ptr<ControllerAttachment> GetAttachment() { return Attachment; }
int32_t GetControllerNumber() { return dwControllerNumber; }

virtual bool HasPadConf() const = 0;
virtual std::optional<std::string> GetPadConfSection() = 0;

protected:
int32_t dwPressedButtons;
std::map<int32_t, int32_t> ButtonMapping;
Expand Down
112 changes: 70 additions & 42 deletions libultraship/libultraship/GameSettings.cpp

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions libultraship/libultraship/GameSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +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;
Expand Down Expand Up @@ -128,6 +131,7 @@ namespace Game {
extern SoHConfigType Settings;
void InitSettings();
void LoadSettings();
void LoadPadSettings();
void SaveSettings();
void SetSeqPlayerVolume(SeqPlayers playerId, float volume);
}
2 changes: 1 addition & 1 deletion libultraship/libultraship/KeyboardController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ namespace Ship {
std::string KeyboardController::GetBindingConfSection() {
return GetControllerType() + " CONTROLLER BINDING " + std::to_string(GetControllerNumber() + 1);
}
}
}
5 changes: 5 additions & 0 deletions libultraship/libultraship/KeyboardController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ namespace Ship {

void ReadFromSource();
void WriteToSource(ControllerCallback* controller);
bool Connected() const { return true; }
bool CanRumble() const { return false; }

bool PressButton(int32_t dwScancode);
bool ReleaseButton(int32_t dwScancode);
void ReleaseAllButtons();

bool HasPadConf() const { return false; }
std::optional<std::string> GetPadConfSection() { return {}; }

protected:
std::string GetControllerType();
std::string GetConfSection();
Expand Down
65 changes: 49 additions & 16 deletions libultraship/libultraship/SDLController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -93,6 +99,9 @@ namespace Ship {
}

bool SDLController::Close() {
if (SDL_GameControllerHasRumble(Cont)) {
SDL_GameControllerRumble(Cont, 0, 0, 0);
}
if (Cont != nullptr) {
SDL_GameControllerClose(Cont);
}
Expand Down Expand Up @@ -178,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++) {
Expand Down Expand Up @@ -331,7 +344,10 @@ 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, 1);
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);
}
}

Expand Down Expand Up @@ -391,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;
Expand All @@ -410,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;
}
}
6 changes: 6 additions & 0 deletions libultraship/libultraship/SDLController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ namespace Ship {

void ReadFromSource();
void WriteToSource(ControllerCallback* controller);
bool Connected() const { return Cont != nullptr; }
bool CanRumble() const { return SDL_GameControllerHasRumble(Cont); }

std::string GetGuid() { return guid; };

bool HasPadConf() const { return true; }
std::optional<std::string> GetPadConfSection();

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:
Expand Down
Loading