From b1d36496b22976c9484674cb64675f7179e9c88b Mon Sep 17 00:00:00 2001 From: Sally Coolatta Date: Tue, 28 Dec 2021 08:44:20 -0500 Subject: [PATCH] Properly implement joystick axes NOW it's fully navigable with controller --- src/g_game.c | 8 +++---- src/g_input.c | 28 ++++++++++++++--------- src/k_menufunc.c | 12 ++++------ src/sdl/i_system.c | 15 ++++++++----- src/sdl/i_video.c | 55 +++++++++++++++++++++++++++------------------- 5 files changed, 67 insertions(+), 51 deletions(-) diff --git a/src/g_game.c b/src/g_game.c index 59a1a92bd..78dc2f885 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -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 diff --git a/src/g_input.c b/src/g_input.c index ec3ccf846..04f808dc7 100644 --- a/src/g_input.c +++ b/src/g_input.c @@ -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: diff --git a/src/k_menufunc.c b/src/k_menufunc.c index a458945ce..5a2ac0eb6 100644 --- a/src/k_menufunc.c +++ b/src/k_menufunc.c @@ -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) { diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index c716c1dc8..bb2e4ea21 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -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 (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); }