Properly implement joystick axes

NOW it's fully navigable with controller
This commit is contained in:
Sally Coolatta 2021-12-28 08:44:20 -05:00 committed by GenericHeroGuy
parent 9b3067a7a2
commit a934e09b0c
3 changed files with 31 additions and 32 deletions

View file

@ -438,10 +438,10 @@ consvar_t cv_shrinkme[MAXSPLITSCREENPLAYERS] = {
static CV_PossibleValue_t zerotoone_cons_t[] = {{0, "MIN"}, {FRACUNIT, "MAX"}, {0, NULL}}; static CV_PossibleValue_t zerotoone_cons_t[] = {{0, "MIN"}, {FRACUNIT, "MAX"}, {0, NULL}};
consvar_t cv_deadzone[MAXSPLITSCREENPLAYERS] = { consvar_t cv_deadzone[MAXSPLITSCREENPLAYERS] = {
CVAR_INIT ("joy_deadzone", "0.125", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL), CVAR_INIT ("deadzone", "0.25", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL),
CVAR_INIT ("joy2_deadzone", "0.125", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL), CVAR_INIT ("deadzone2", "0.25", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL),
CVAR_INIT ("joy3_deadzone", "0.125", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL), CVAR_INIT ("deadzone3", "0.25", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL),
CVAR_INIT ("joy4_deadzone", "0.125", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL) CVAR_INIT ("deadzone4", "0.25", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL)
}; };
// now automatically allocated in D_RegisterClientCommands // now automatically allocated in D_RegisterClientCommands

View file

@ -1177,7 +1177,7 @@ static UINT64 lastjoyhats[MAXSPLITSCREENPLAYERS] = {0,0,0,0};
*/ */
void I_ShutdownJoystick(UINT8 index) void I_ShutdownJoystick(UINT8 index)
{ {
INT32 i; INT32 i, j;
event_t event; event_t event;
event.type=ev_keyup; event.type=ev_keyup;
event.data2 = 0; event.data2 = 0;
@ -1186,22 +1186,25 @@ void I_ShutdownJoystick(UINT8 index)
lastjoybuttons[index] = lastjoyhats[index] = 0; lastjoybuttons[index] = lastjoyhats[index] = 0;
// emulate the up of all joystick buttons // emulate the up of all joystick buttons
for (i=0;i<JOYBUTTONS;i++) for (i = 0; i < JOYBUTTONS; i++)
{ {
event.data1=KEY_JOY1+i; event.data1=KEY_JOY1+i;
D_PostEvent(&event); D_PostEvent(&event);
} }
// emulate the up of all joystick hats // emulate the up of all joystick hats
for (i=0;i<JOYHATS*4;i++) for (i = 0; i < JOYHATS*4; i++)
{ {
event.data1=KEY_HAT1+i; for (j = 0; j < 4; j++)
D_PostEvent(&event); {
event.data1 = KEY_HAT1 + (i * 4) + j;
D_PostEvent(&event);
}
} }
// reset joystick position // reset joystick position
event.type = ev_joystick; event.type = ev_joystick;
for (i=0;i<JOYAXISSET; i++) for (i = 0; i < JOYAXISSET; i++)
{ {
event.data1 = i; event.data1 = i;
D_PostEvent(&event); D_PostEvent(&event);

View file

@ -541,32 +541,29 @@ static inline void SDLJoyRemap(event_t *event)
(void)event; (void)event;
} }
static INT32 SDLJoyAxis(const Sint16 axis, evtype_t which, UINT8 pid) static INT32 SDLJoyAxis(const Sint16 axis, UINT8 pid)
{ {
// -32768 to 32767 // -32768 to 32767
INT32 raxis = axis/32; INT32 raxis = axis / 32;
if (which == ev_joystick) if (Joystick[pid].bGamepadStyle)
{ {
if (Joystick[pid].bGamepadStyle) // gamepad control type, on or off, live or die
{ if (raxis < -(JOYAXISRANGE/2))
// gamepad control type, on or off, live or die raxis = -1;
if (raxis < -(JOYAXISRANGE/2)) else if (raxis > (JOYAXISRANGE/2))
raxis = -1; raxis = 1;
else if (raxis > (JOYAXISRANGE/2))
raxis = 1;
else
raxis = 0;
}
else else
{ raxis = 0;
raxis = JoyInfo[pid].scale!=1?((raxis/JoyInfo[pid].scale)*JoyInfo[pid].scale):raxis; }
else
{
raxis = (JoyInfo[pid].scale != 1) ? ((raxis / JoyInfo[pid].scale) * JoyInfo[pid].scale) : raxis;
#ifdef SDL_JDEADZONE #ifdef SDL_JDEADZONE
if (-SDL_JDEADZONE <= raxis && raxis <= SDL_JDEADZONE) if (-SDL_JDEADZONE <= raxis && raxis <= SDL_JDEADZONE)
raxis = 0; raxis = 0;
#endif #endif
}
} }
return raxis; return raxis;
@ -802,15 +799,14 @@ static void Impl_HandleControllerAxisEvent(SDL_ControllerAxisEvent evt)
event.type = ev_joystick; event.type = ev_joystick;
event.data1 = event.data2 = event.data3 = INT32_MAX;
event.device = 1 + evt.which; event.device = 1 + evt.which;
if (event.device == INT32_MAX) if (event.device == INT32_MAX)
{ {
return; return;
} }
event.data1 = event.data2 = event.data3 = INT32_MAX;
//axis //axis
switch (evt.axis) switch (evt.axis)
{ {
@ -837,7 +833,7 @@ static void Impl_HandleControllerAxisEvent(SDL_ControllerAxisEvent evt)
} }
//value //value
event.data2 = SDLJoyAxis(evt.value, event.type, event.device); event.data2 = SDLJoyAxis(evt.value, 0); // TODO: replace 0 with pid
D_PostEvent(&event); D_PostEvent(&event);
} }