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
parent 92aff0b57c
commit b1d36496b2
5 changed files with 67 additions and 51 deletions

View file

@ -397,10 +397,10 @@ consvar_t cv_kickstartaccel[MAXSPLITSCREENPLAYERS] = {
static CV_PossibleValue_t zerotoone_cons_t[] = {{0, "MIN"}, {FRACUNIT, "MAX"}, {0, NULL}};
consvar_t cv_deadzone[MAXSPLITSCREENPLAYERS] = {
CVAR_INIT ("joy_deadzone", "0.125", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL),
CVAR_INIT ("joy2_deadzone", "0.125", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL),
CVAR_INIT ("joy3_deadzone", "0.125", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL),
CVAR_INIT ("joy4_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 ("deadzone2", "0.25", CV_FLOAT|CV_SAVE, zerotoone_cons_t, NULL),
CVAR_INIT ("deadzone3", "0.25", 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

View file

@ -92,7 +92,6 @@ INT32 G_GetDevicePlayer(INT32 deviceID)
void G_MapEventsToControls(event_t *ev)
{
INT32 i;
boolean alternate = false;
if (ev->device >= 0 && ev->device < MAXDEVICES)
{
@ -181,13 +180,12 @@ void G_MapEventsToControls(event_t *ev)
break;
}
alternate = ev->data1 % 2;
i = (ev->data1 / 2) * 2;
CONS_Printf("AXIS ID IS %d\n", i);
CONS_Printf("AXIS DATA (%d, %d, %d)\n", ev->data1, ev->data2, ev->data3);
i = ev->data1 * 4;
if (ev->data2 != INT32_MAX)
{
if (alternate == true)
if (ev->data2 < 0)
{
// Left
gamekeydown[ev->device][KEY_AXIS1 + i] = abs(ev->data2);
@ -200,12 +198,22 @@ void G_MapEventsToControls(event_t *ev)
gamekeydown[ev->device][KEY_AXIS1 + i + 1] = abs(ev->data2);
}
}
else
{
gamekeydown[ev->device][KEY_AXIS1 + i] = 0;
gamekeydown[ev->device][KEY_AXIS1 + i + 1] = 0;
}
if (ev->data3 != INT32_MAX)
{
if (ev->data3 < 0)
{
// Up
gamekeydown[ev->device][KEY_AXIS1 + i + 2] = abs(ev->data3);
gamekeydown[ev->device][KEY_AXIS1 + i + 3] = 0;
}
else
{
// Down
gamekeydown[ev->device][KEY_AXIS1 + i + 2] = 0;
gamekeydown[ev->device][KEY_AXIS1 + i + 3] = abs(ev->data3);
}
}
break;
default:

View file

@ -1932,13 +1932,6 @@ void M_CharacterSelectInit(INT32 choice)
}
}
for (i = 0; i < MAXSPLITSCREENPLAYERS; i++)
{
// Un-set all devices upon entering this menu.
CV_SetValue(&cv_usejoystick[i], -1);
CONS_Printf("Device for %d set to %d\n", i, -1);
}
PLAY_CharSelectDef.prevMenu = currentMenu;
M_SetupNextMenu(&PLAY_CharSelectDef, false);
}
@ -2034,7 +2027,10 @@ static void M_HandlePressStart(setup_player_t *p, UINT8 num)
}
// Ensure their device is unset
CV_SetValue(&cv_usejoystick[num], -1);
if (cv_usejoystick[num].value != -1)
{
CV_SetValue(&cv_usejoystick[num], -1);
}
if (num != setup_numplayers)
{

View file

@ -1078,7 +1078,7 @@ void I_UpdateJoystickDeviceIndices(UINT8 excludePlayer)
*/
void I_ShutdownJoystick(UINT8 index)
{
INT32 i;
INT32 i, j;
event_t event;
event.device = I_GetJoystickDeviceIndex(JoyInfo[index].dev);
@ -1087,22 +1087,25 @@ void I_ShutdownJoystick(UINT8 index)
event.data3 = 0;
// emulate the up of all joystick buttons
for (i=0;i<JOYBUTTONS;i++)
for (i = 0; i < JOYBUTTONS; i++)
{
event.data1=KEY_JOY1+i;
D_PostEvent(&event);
}
// 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;
D_PostEvent(&event);
for (j = 0; j < 4; j++)
{
event.data1 = KEY_HAT1 + (i * 4) + j;
D_PostEvent(&event);
}
}
// reset joystick position
event.type = ev_joystick;
for (i=0;i<JOYAXISSET; i++)
for (i = 0; i < JOYAXISSET; i++)
{
event.data1 = i;
D_PostEvent(&event);

View file

@ -526,32 +526,29 @@ static inline void SDLJoyRemap(event_t *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
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))
raxis = -1;
else if (raxis > (JOYAXISRANGE/2))
raxis = 1;
else
raxis = 0;
}
// gamepad control type, on or off, live or die
if (raxis < -(JOYAXISRANGE/2))
raxis = -1;
else if (raxis > (JOYAXISRANGE/2))
raxis = 1;
else
{
raxis = JoyInfo[pid].scale!=1?((raxis/JoyInfo[pid].scale)*JoyInfo[pid].scale):raxis;
raxis = 0;
}
else
{
raxis = (JoyInfo[pid].scale != 1) ? ((raxis / JoyInfo[pid].scale) * JoyInfo[pid].scale) : raxis;
#ifdef SDL_JDEADZONE
if (-SDL_JDEADZONE <= raxis && raxis <= SDL_JDEADZONE)
raxis = 0;
if (-SDL_JDEADZONE <= raxis && raxis <= SDL_JDEADZONE)
raxis = 0;
#endif
}
}
return raxis;
@ -777,23 +774,35 @@ static void Impl_HandleJoystickAxisEvent(SDL_JoyAxisEvent evt)
{
event_t event;
event.data1 = event.data2 = event.data3 = INT32_MAX;
event.device = 1 + evt.which;
evt.axis++;
event.type = ev_joystick;
event.device = 1 + evt.which;
if (event.device == INT32_MAX)
{
return;
}
evt.axis++;
event.data1 = event.data2 = event.data3 = INT32_MAX;
//axis
if (evt.axis > JOYAXISSET*2)
{
return;
}
event.data1 = evt.axis;
event.data2 = SDLJoyAxis(evt.value, event.type, event.device);
//vaule[sic]
if (evt.axis % 2)
{
event.data1 = evt.axis / 2;
event.data2 = SDLJoyAxis(evt.value, 0); // TODO: replace 0 with pid
}
else
{
evt.axis--;
event.data1 = evt.axis / 2;
event.data3 = SDLJoyAxis(evt.value, 0); // TODO: replace 0 with pid
}
D_PostEvent(&event);
}