Skip to content

Commit

Permalink
port: add controller axis settings
Browse files Browse the repository at this point in the history
also make sliders look nicer and save some input settings on quit
  • Loading branch information
fgsfdsfgs committed Oct 28, 2023
1 parent 2813ece commit cea9527
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 9 deletions.
6 changes: 6 additions & 0 deletions port/include/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ void inputControllerSetSticksSwapped(s32 swapped);
s32 inputControllerGetDualAnalog(void);
void inputControllerSetDualAnalog(s32 enable);

f32 inputControllerGetAxisScale(s32 stick, s32 axis);
void inputControllerSetAxisScale(s32 stick, s32 axis, f32 value);

f32 inputControllerGetAxisDeadzone(s32 stick, s32 axis);
void inputControllerSetAxisDeadzone(s32 stick, s32 axis, f32 value);

// vk is a value from the virtkey enum above
s32 inputKeyPressed(u32 vk);

Expand Down
37 changes: 37 additions & 0 deletions port/src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,25 @@ void inputSaveConfig(void)
{
inputSaveBinds();

configSetInt("Input.MouseEnabled", mouseEnabled);
configSetFloat("Input.MouseSpeedX", mouseSensX);
configSetFloat("Input.MouseSpeedY", mouseSensY);

configSetInt("Input.LStickDeadzoneX", deadzone[0]);
configSetInt("Input.LStickDeadzoneY", deadzone[1]);
configSetInt("Input.RStickDeadzoneX", deadzone[2]);
configSetInt("Input.RStickDeadzoneY", deadzone[3]);

configSetFloat("Input.LStickScaleX", stickSens[0]);
configSetFloat("Input.LStickScaleY", stickSens[1]);
configSetFloat("Input.RStickScaleX", stickSens[2]);
configSetFloat("Input.RStickScaleY", stickSens[3]);

configSetFloat("Input.RumbleScale", rumbleScale);

configSetInt("Input.StickCButtons", stickCButtons);

configGetInt("Input.SwapSticks", axisMap[0][0] == SDL_CONTROLLER_AXIS_RIGHTX);
}

s32 inputInit(void)
Expand Down Expand Up @@ -694,6 +711,26 @@ void inputControllerSetDualAnalog(s32 enable)
stickCButtons = !enable;
}

f32 inputControllerGetAxisScale(s32 stick, s32 axis)
{
return stickSens[stick * 2 + axis];
}

void inputControllerSetAxisScale(s32 stick, s32 axis, f32 value)
{
stickSens[stick * 2 + axis] = value;
}

f32 inputControllerGetAxisDeadzone(s32 stick, s32 axis)
{
return (f32)deadzone[stick * 2 + axis] / 32767.f;
}

void inputControllerSetAxisDeadzone(s32 stick, s32 axis, f32 value)
{
deadzone[stick * 2 + axis] = value * 32767.f;
}

void inputKeyBind(s32 idx, u32 ck, s32 bind, u32 vk)
{
if (idx < 0 || idx >= INPUT_MAX_CONTROLLERS || bind >= MAX_BINDS || ck >= CK_TOTAL_COUNT) {
Expand Down
174 changes: 165 additions & 9 deletions port/src/optionsmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,34 +142,42 @@ struct menuitem g_ExtendedMouseMenuItems[] = {
0,
menuhandlerMouseAimLock,
},
{
MENUITEMTYPE_SEPARATOR,
0,
0,
0,
0,
NULL,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_ALTSIZE,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"Mouse Speed X",
100,
menuhandlerMouseSpeedX,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_ALTSIZE,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"Mouse Speed Y",
100,
menuhandlerMouseSpeedY,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_ALTSIZE,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"Crosshair Speed X",
100,
menuhandlerMouseAimSpeedX,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_ALTSIZE,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"Crosshair Speed Y",
100,
menuhandlerMouseAimSpeedY,
Expand Down Expand Up @@ -202,14 +210,154 @@ struct menudialogdef g_ExtendedMouseMenuDialog = {
NULL,
};

static MenuItemHandlerResult menuhandlerStickSpeed(s32 operation, struct menuitem *item, union handlerdata *data);
static MenuItemHandlerResult menuhandlerStickDeadzone(s32 operation, struct menuitem *item, union handlerdata *data);

struct menuitem g_ExtendedStickMenuItems[] = {
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"LStick Scale X",
20,
menuhandlerStickSpeed,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"LStick Scale Y",
20,
menuhandlerStickSpeed,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"RStick Scale X",
20,
menuhandlerStickSpeed,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"RStick Scale Y",
20,
menuhandlerStickSpeed,
},
{
MENUITEMTYPE_SEPARATOR,
0,
0,
0,
0,
NULL,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"LStick Deadzone X",
32,
menuhandlerStickDeadzone,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"LStick Deadzone Y",
32,
menuhandlerStickDeadzone,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"RStick Deadzone X",
32,
menuhandlerStickDeadzone,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"RStick Deadzone Y",
32,
menuhandlerStickDeadzone,
},
{
MENUITEMTYPE_SEPARATOR,
0,
0,
0,
0,
NULL,
},
{
MENUITEMTYPE_SELECTABLE,
0,
MENUITEMFLAG_SELECTABLE_CLOSESDIALOG,
L_OPTIONS_213, // "Back"
0,
NULL,
},
{ MENUITEMTYPE_END },
};

static MenuItemHandlerResult menuhandlerStickSpeed(s32 operation, struct menuitem *item, union handlerdata *data)
{
const s32 idx = item - g_ExtendedStickMenuItems;
const s32 stick = idx / 2;
const s32 axis = idx % 2;

switch (operation) {
case MENUOP_GETSLIDER:
data->slider.value = inputControllerGetAxisScale(stick, axis) * 10.f + 0.5f;
break;
case MENUOP_SET:
inputControllerSetAxisScale(stick, axis, (f32)data->slider.value / 10.f);
break;
}

return 0;
}

static MenuItemHandlerResult menuhandlerStickDeadzone(s32 operation, struct menuitem *item, union handlerdata *data)
{
const s32 idx = item - (g_ExtendedStickMenuItems + 5);
const s32 stick = idx / 2;
const s32 axis = idx % 2;

switch (operation) {
case MENUOP_GETSLIDER:
data->slider.value = inputControllerGetAxisDeadzone(stick, axis) * 32.f + 0.5f;
break;
case MENUOP_SET:
inputControllerSetAxisDeadzone(stick, axis, (f32)data->slider.value / 32.f);
break;
}

return 0;
}

struct menudialogdef g_ExtendedStickMenuDialog = {
MENUDIALOGTYPE_DEFAULT,
(uintptr_t)"Analog Stick Settings",
g_ExtendedStickMenuItems,
NULL,
MENUDIALOGFLAG_LITERAL_TEXT,
NULL,
};

static MenuItemHandlerResult menuhandlerVibration(s32 operation, struct menuitem *item, union handlerdata *data)
{
switch (operation) {
case MENUOP_GETSLIDER:
data->slider.value = inputRumbleGetStrength() * 100.f + 0.5f;
data->slider.value = inputRumbleGetStrength() * 10.f + 0.5f;
break;
case MENUOP_SET:
inputRumbleSetStrength((f32)data->slider.value / 100.f);
inputRumbleSetStrength((f32)data->slider.value / 10.f);
break;
}

Expand Down Expand Up @@ -259,12 +407,20 @@ struct menuitem g_ExtendedControllerMenuItems[] = {
0,
menuhandlerSwapSticks,
},
{
MENUITEMTYPE_SELECTABLE,
0,
MENUITEMFLAG_SELECTABLE_OPENSDIALOG | MENUITEMFLAG_LITERAL_TEXT,
(uintptr_t)"Stick Settings...\n",
0,
(void *)&g_ExtendedStickMenuDialog,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT,
(uintptr_t)"Vibration",
100,
10,
menuhandlerVibration,
},
{
Expand Down Expand Up @@ -494,15 +650,15 @@ struct menuitem g_ExtendedGameMenuItems[] = {
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_ALTSIZE,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"Crosshair Sway",
20,
menuhandlerCrosshairSway,
},
{
MENUITEMTYPE_SLIDER,
0,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_ALTSIZE,
MENUITEMFLAG_LITERAL_TEXT | MENUITEMFLAG_SLIDER_WIDE,
(uintptr_t)"Explosion Shake",
20,
menuhandlerScreenShake,
Expand Down
3 changes: 3 additions & 0 deletions src/game/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ void menuCalculateItemSize(struct menuitem *item, s16 *width, s16 *height, struc
if (item->flags & MENUITEMFLAG_SLIDER_ALTSIZE) {
*height = 22;
*width = 120;
} else if (item->flags & MENUITEMFLAG_SLIDER_WIDE) {
*width = 200;
*height = VERSION == VERSION_JPN_FINAL ? 14 : 12;
}
break;
case MENUITEMTYPE_CHECKBOX:
Expand Down
1 change: 1 addition & 0 deletions src/include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,7 @@
#define MENUITEMFLAG_LESSHEIGHT 0x02000000
#define MENUITEMFLAG_CAROUSEL_04000000 0x04000000
#define MENUITEMFLAG_LITERAL_TEXT 0x08000000
#define MENUITEMFLAG_SLIDER_WIDE 0x10000000

#define MENUITEMTYPE_LABEL 0x01
#define MENUITEMTYPE_LIST 0x02
Expand Down

0 comments on commit cea9527

Please sign in to comment.