Implement deadzone setting

This commit is contained in:
James Lambert 2023-09-21 21:21:40 -06:00
parent 7b898c1bbd
commit 89c5f9a543
6 changed files with 40 additions and 4 deletions

View file

@ -51,8 +51,16 @@ void controllerActionApply(enum ControllerAction action) {
#define DEADZONE_SIZE 5
#define MAX_JOYSTICK_RANGE 80
short gDeadzone = DEADZONE_SIZE;
float gDeadzoneScale = 1.0f / (MAX_JOYSTICK_RANGE - DEADZONE_SIZE);
void controllerSetDeadzone(float percent) {
gDeadzone = (short)(percent * MAX_JOYSTICK_RANGE);
gDeadzoneScale = 1.0f / (MAX_JOYSTICK_RANGE - gDeadzone);
}
float controllerCleanupStickInput(s8 input) {
if (input > -DEADZONE_SIZE && input < DEADZONE_SIZE) {
if (input > -gDeadzone && input < gDeadzone) {
return 0.0f;
}
@ -64,7 +72,7 @@ float controllerCleanupStickInput(s8 input) {
return -1.0f;
}
return ((float)input + (input > 0 ? -DEADZONE_SIZE : DEADZONE_SIZE)) * (1.0f / (MAX_JOYSTICK_RANGE - DEADZONE_SIZE));
return ((float)input + (input > 0 ? -gDeadzone : gDeadzone)) * gDeadzoneScale;
}
void controllerActionReadDirection(enum ControllerActionSource source, int controllerIndex, int directionIndex) {

View file

@ -48,6 +48,8 @@ enum ControllerAction {
#define IS_DIRECTION_ACTION(action) ((action) >= ControllerActionMove && (action) <= ControllerActionRotate)
#define IS_VALID_SOURCE(source) ((source) >= 0 && (source) < ControllerActionSourceCount)
#define MAX_DEADZONE 0.25f
struct ControllerSourceWithController {
unsigned char button;
unsigned char controller;
@ -55,6 +57,7 @@ struct ControllerSourceWithController {
void controllerActionRead();
void controllerSetDeadzone(float percent);
struct Vector2 controllerDirectionGet(enum ControllerAction direction);
int controllerActionGet(enum ControllerAction action);

View file

@ -32,11 +32,15 @@ void joystickOptionsInit(struct JoystickOptions* joystickOptions) {
joystickOptions->lookAccelerationText = menuBuildText(&gDejaVuSansFont, "Look Acceleration", JOYSTICK_X + 8, JOYSTICK_Y + 88);
joystickOptions->lookAcceleration = menuBuildSlider(JOYSTICK_X + 120, JOYSTICK_Y + 88, 120, SCROLL_TICKS);
joystickOptions->joystickDeadzoneText = menuBuildText(&gDejaVuSansFont, "Deadzone", JOYSTICK_X + 8, JOYSTICK_Y + 108);
joystickOptions->joystickDeadzone = menuBuildSlider(JOYSTICK_X + 120, JOYSTICK_Y + 108, 120, SCROLL_TICKS);
joystickOptions->invertControls.checked = (gSaveData.controls.flags & ControlSaveFlagsInvert) != 0;
joystickOptions->invertControlsYaw.checked = (gSaveData.controls.flags & ControlSaveFlagsInvertYaw) != 0;
joystickOptions->tankControls.checked = (gSaveData.controls.flags & ControlSaveTankControls) != 0;
joystickOptions->lookSensitivity.value = (float)gSaveData.controls.sensitivity / 0xFFFF;
joystickOptions->lookAcceleration.value = (float)gSaveData.controls.acceleration / 0xFFFF;
joystickOptions->joystickDeadzone.value = (float)gSaveData.controls.deadzone / 0xFFFF;
}
#define FULL_SCROLL_TIME 2.0f
@ -143,10 +147,15 @@ enum MenuDirection joystickOptionsUpdate(struct JoystickOptions* joystickOptions
case JoystickOptionAcceleration:
joystickOptionsHandleSlider(&gSaveData.controls.acceleration, &joystickOptions->lookAcceleration.value);
break;
case JoystickOptionDeadzone:
joystickOptionsHandleSlider(&gSaveData.controls.deadzone, &joystickOptions->joystickDeadzone.value);
controllerSetDeadzone(joystickOptions->joystickDeadzone.value * MAX_DEADZONE);
break;
}
if (joystickOptions->selectedItem == JoystickOptionSensitivity ||
joystickOptions->selectedItem == JoystickOptionAcceleration){
joystickOptions->selectedItem == JoystickOptionAcceleration ||
joystickOptions->selectedItem == JoystickOptionDeadzone){
if ((controllerGetButtonDown(0, L_TRIG) || controllerGetButtonDown(0, Z_TRIG))) {
return MenuDirectionLeft;
}
@ -181,6 +190,9 @@ void joystickOptionsRender(struct JoystickOptions* joystickOptions, struct Rende
gSPDisplayList(renderState->dl++, joystickOptions->lookAcceleration.back);
renderState->dl = menuSliderRender(&joystickOptions->lookAcceleration, renderState->dl);
gSPDisplayList(renderState->dl++, joystickOptions->joystickDeadzone.back);
renderState->dl = menuSliderRender(&joystickOptions->joystickDeadzone, renderState->dl);
gSPDisplayList(renderState->dl++, ui_material_revert_list[SOLID_ENV_INDEX]);
gSPDisplayList(renderState->dl++, ui_material_list[DEJAVU_SANS_INDEX]);
@ -205,5 +217,9 @@ void joystickOptionsRender(struct JoystickOptions* joystickOptions, struct Rende
menuSetRenderColor(renderState, joystickOptions->selectedItem == JoystickOptionAcceleration, &gSelectionGray, &gColorWhite);
gSPDisplayList(renderState->dl++, joystickOptions->lookAccelerationText);
gDPPipeSync(renderState->dl++);
menuSetRenderColor(renderState, joystickOptions->selectedItem == JoystickOptionDeadzone, &gSelectionGray, &gColorWhite);
gSPDisplayList(renderState->dl++, joystickOptions->joystickDeadzoneText);
gSPDisplayList(renderState->dl++, ui_material_revert_list[DEJAVU_SANS_INDEX]);
}

View file

@ -10,6 +10,7 @@ enum JoystickOption {
JoystickOptionTankControls,
JoystickOptionSensitivity,
JoystickOptionAcceleration,
JoystickOptionDeadzone,
JoystickOptionCount,
};
@ -20,8 +21,10 @@ struct JoystickOptions {
struct MenuCheckbox tankControls;
struct MenuSlider lookSensitivity;
struct MenuSlider lookAcceleration;
struct MenuSlider joystickDeadzone;
Gfx* lookSensitivityText;
Gfx* lookAccelerationText;
Gfx* joystickDeadzoneText;
short selectedItem;
};

View file

@ -93,9 +93,12 @@ void savefileNew() {
gSaveData.controls.flags = 0;
gSaveData.controls.sensitivity = 0x7FFF;
gSaveData.controls.acceleration = 0x7FFF;
gSaveData.controls.deadzone = 0x4000;
gSaveData.audio.soundVolume = 0xFF;
gSaveData.audio.musicVolume = 0xFF;
controllerSetDeadzone(gSaveData.controls.deadzone * (1.0f / 0xFFFF) * MAX_DEADZONE);
}
void savefileLoad() {
@ -136,6 +139,8 @@ void savefileLoad() {
if (gSaveData.header.header != SAVEFILE_HEADER) {
savefileNew();
}
controllerSetDeadzone(gSaveData.controls.deadzone * (1.0f / 0xFFFF) * MAX_DEADZONE);
}
void savefileSave() {

View file

@ -20,7 +20,7 @@
#define SCREEN_SHOT_SRAM(slotIndex) (((slotIndex) + 1) * SAVE_SLOT_SIZE + MAX_CHECKPOINT_SIZE + SRAM_START_ADDR)
#define SAVEFILE_HEADER 0xDEEF
#define SAVEFILE_HEADER 0xDEF0
// first save slot is always reserved for auto save
#define MAX_SAVE_SLOTS ((int)(SRAM_SIZE / SAVE_SLOT_SIZE) - 1)
@ -47,6 +47,7 @@ struct ControlSaveState {
unsigned short flags;
unsigned short sensitivity;
unsigned short acceleration;
unsigned short deadzone;
};
struct AudioSettingsSaveState {