From 7b5129e2fed3f615d24aa0605aafb2d3e008ddb7 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 24 Oct 2025 18:17:17 +0200 Subject: [PATCH] refactor controller dpad menu repeat handling dont hack around manually checking for state checks just use sdl provided stuff and poll if a dpad button currently is being held --- src/d_main.c | 56 ----------------------------------------------- src/g_input.c | 3 --- src/i_video.h | 2 ++ src/m_menu.c | 27 +++-------------------- src/m_menu.h | 10 --------- src/sdl/i_video.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 93 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 9ea10bfa2..3446fa401 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -164,57 +164,6 @@ UINT8 ctrldown = 0; // 0x1 left, 0x2 right UINT8 altdown = 0; // 0x1 left, 0x2 right boolean capslock = 0; // gee i wonder what this does. -static void D_PadMenuScrollInput(UINT8 input) -{ - event_t dpadev; - memset(&dpadev, 0, sizeof(event_t)); - dpadev.type = ev_keydown; - - switch (input) - { - case DPAD_UP: - dpadev.data1 = KEY_UPARROW; - break; - case DPAD_DOWN: - dpadev.data1 = KEY_DOWNARROW; - break; - case DPAD_LEFT: - dpadev.data1 = KEY_LEFTARROW; - break; - case DPAD_RIGHT: - dpadev.data1 = KEY_RIGHTARROW; - break; - } - - D_PostEvent(&dpadev); // put into eventlist -} - -#define SCROLLDELAY 19 - -// Check if any dpad button is held -// and pass it to the eventlist -static void D_GamePadMenuScrollTicker(void) -{ - UINT8 i; - static UINT8 menuInputDelayTimer = 0; - - for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) - { - if (dpadscrollstate[i]) - { - if (menuInputDelayTimer < SCROLLDELAY) - menuInputDelayTimer++; - else if (menuInputDelayTimer == SCROLLDELAY) - D_PadMenuScrollInput(i); - - return; - } - } - - menuInputDelayTimer = 0; -} -#undef SCROLLDELAY - static UINT16 curcolor[MAXSPLITSCREENPLAYERS] = {0}; static void D_DeviceLEDTick(void) @@ -839,11 +788,6 @@ void D_SRB2Loop(void) if (!dedicated) { - if (menuactive) - { - D_GamePadMenuScrollTicker(); - } - D_DeviceLEDTick(); } } diff --git a/src/g_input.c b/src/g_input.c index e8bc8223e..89d887375 100644 --- a/src/g_input.c +++ b/src/g_input.c @@ -262,9 +262,6 @@ void G_ResetControls(void) memset(joyxmove, 0, sizeof(joyxmove)); memset(joyymove, 0, sizeof(joyymove)); - - // reset those just in case the game missed the keyup event - memset(dpadscrollstate, false, sizeof(dpadscrollstate)); } // diff --git a/src/i_video.h b/src/i_video.h index 852645c30..ad29983fa 100644 --- a/src/i_video.h +++ b/src/i_video.h @@ -143,6 +143,8 @@ boolean I_CheckNativeRes(void); void I_SetBorderlessWindow(void); +void I_HandleControllerHatRepeat(void); + #ifdef USE_FBO_OGL void RefreshOGLSDLSurface(void); diff --git a/src/m_menu.c b/src/m_menu.c index 6eaccdce0..b7ea3cc71 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -759,8 +759,6 @@ static void Command_Manual_f(void) itemOn = 0; } -boolean dpadscrollstate[4] = {false, false, false, false}; - // // M_Responder // @@ -807,19 +805,15 @@ boolean M_Responder(event_t *ev) break; case KEY_HAT1: ch = KEY_UPARROW; - dpadscrollstate[DPAD_UP] = true; break; case KEY_HAT1 + 1: ch = KEY_DOWNARROW; - dpadscrollstate[DPAD_DOWN] = true; break; case KEY_HAT1 + 2: ch = KEY_LEFTARROW; - dpadscrollstate[DPAD_LEFT] = true; break; case KEY_HAT1 + 3: ch = KEY_RIGHTARROW; - dpadscrollstate[DPAD_RIGHT] = true; break; } @@ -835,24 +829,6 @@ boolean M_Responder(event_t *ev) } } } - else if (ev->type == ev_keyup) - { - switch (ev->data1) // if you let go of those set those to false - { - case KEY_HAT1: - dpadscrollstate[DPAD_UP] = false; - break; - case KEY_HAT1 + 1: - dpadscrollstate[DPAD_DOWN] = false; - break; - case KEY_HAT1 + 2: - dpadscrollstate[DPAD_LEFT] = false; - break; - case KEY_HAT1 + 3: - dpadscrollstate[DPAD_RIGHT] = false; - break; - } - } else if (menuactive) { tic_t thistime = I_GetTime(); @@ -1731,6 +1707,9 @@ void M_Ticker(void) if (dedicated) return; + if (menuactive) + I_HandleControllerHatRepeat(); + if (--skullAnimCounter <= 0) skullAnimCounter = 8; diff --git a/src/m_menu.h b/src/m_menu.h index de4f3b983..22b768385 100644 --- a/src/m_menu.h +++ b/src/m_menu.h @@ -38,16 +38,6 @@ extern boolean forceshowhud; // Does all the real work of the menu interaction. boolean M_Responder(event_t *ev); -// for scrolling through menus with controllers -extern boolean dpadscrollstate[4]; -enum -{ - DPAD_UP, - DPAD_DOWN, - DPAD_LEFT, - DPAD_RIGHT -}; - // Called by main loop, runs for demo playback. If this returns true, nullify any further user input. boolean M_DemoResponder(event_t *ev); diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index c93e87ef1..28cb9f2a1 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -585,7 +585,9 @@ static void Impl_HandleKeyboardEvent(SDL_KeyboardEvent evt, Uint32 type) } if (event.data1) + { D_PostEvent(&event); + } } static void Impl_HandleMouseMotionEvent(SDL_MouseMotionEvent evt) @@ -723,6 +725,57 @@ static void Impl_HandleMouseWheelEvent(SDL_MouseWheelEvent evt) } } +static Uint32 hatrepeattimer[MAXSPLITSCREENPLAYERS]; +#define HATREPEATDELAY 19 + +void I_HandleControllerHatRepeat(void) +{ + static event_t event = {ev_keydown, 0, 0, 0}; + + // why bother if theres no controllers? + if (numcontrollers == 0) + return; + + static const SDL_GameControllerButton hatbutt[4] = + { + SDL_CONTROLLER_BUTTON_DPAD_UP, + SDL_CONTROLLER_BUTTON_DPAD_DOWN, + SDL_CONTROLLER_BUTTON_DPAD_LEFT, + SDL_CONTROLLER_BUTTON_DPAD_RIGHT + }; + + for (int i = 0; i < MAXSPLITSCREENPLAYERS; i++) + { + if (!cv_usejoystick[i].value) + continue; + + SDL_GameController *controller = JoyInfo[i].dev; + + if (!controller) + continue; + + for (UINT8 h = 0; h < JOYHATS; h++) + { + if (SDL_GameControllerGetButton(controller, hatbutt[h])) + { + if (hatrepeattimer[i] < HATREPEATDELAY) + { + hatrepeattimer[i]++; + } + else if (hatrepeattimer[i] == HATREPEATDELAY) + { + event.data1 = KEY_HAT1 + (i * JOYHATS) + h; + D_PostEvent(&event); + } + + return; + } + } + + hatrepeattimer[i] = 0; + } +} + static void Impl_HandleControllerAxisEvent(SDL_ControllerAxisEvent evt) { event_t event;