From 0b9c5c7415eeccc17c58252fcdedb16d98e8f9d5 Mon Sep 17 00:00:00 2001 From: Antonio Martinez Date: Fri, 13 Sep 2024 22:03:18 -0700 Subject: [PATCH] Move bind-search logic to g_input --- src/g_input.c | 36 ++++++++++++++++++++++++++++++++++++ src/g_input.h | 2 ++ src/v_draw.cpp | 32 +------------------------------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/g_input.c b/src/g_input.c index 8a209c12f..5a4b6ded5 100644 --- a/src/g_input.c +++ b/src/g_input.c @@ -1121,6 +1121,42 @@ INT32 G_CheckDoubleUsage(INT32 keynum, INT32 playernum, boolean modify) return result; } +INT32 G_FindPlayerBindForGameControl(INT32 player, gamecontrols_e control) +{ + profile_t *ourProfile = PR_GetLocalPlayerProfile(player); + if (ourProfile == NULL) + ourProfile = PR_GetLocalPlayerProfile(0); + + INT32 device = G_GetDeviceForPlayer(player); // TODO: Respond to what device player is CURRENTLY using + if (device == -1) // No registered device = you can't possibly be using a gamepad + device = KEYBOARD_MOUSE_DEVICE; + + INT32 bestbind = -1; // Bind that matches our input device + INT32 anybind = -1; // Bind that doesn't match, but is at least for this control + + INT32 bindindex = 3; + + while (bindindex >= 0) // Prefer earlier binds + { + INT32 possiblecontrol = ourProfile->controls[control][bindindex]; + + // if (device is gamepad) == (bound control is in gamepad range) - e.g. if bind matches device + if ((device != KEYBOARD_MOUSE_DEVICE) == (possiblecontrol >= KEY_JOY1 && possiblecontrol < JOYINPUTEND)) + { + bestbind = possiblecontrol; + anybind = possiblecontrol; + } + else + { + anybind = possiblecontrol; + } + + bindindex--; + } + + return (bestbind != -1) ? bestbind : anybind; // If we couldn't find a device-appropriate bind, try to at least use something +} + static void setcontrol(UINT8 player) { INT32 numctrl; diff --git a/src/g_input.h b/src/g_input.h index cc04f0047..485ba2618 100644 --- a/src/g_input.h +++ b/src/g_input.h @@ -205,6 +205,8 @@ void G_ApplyControlScheme(UINT8 splitplayer, INT32 (*fromcontrols)[MAXINPUTMAPPI void G_SaveKeySetting(FILE *f, INT32 (*fromcontrolsa)[MAXINPUTMAPPING], INT32 (*fromcontrolsb)[MAXINPUTMAPPING], INT32 (*fromcontrolsc)[MAXINPUTMAPPING], INT32 (*fromcontrolsd)[MAXINPUTMAPPING]); INT32 G_CheckDoubleUsage(INT32 keynum, INT32 playernum, boolean modify); +INT32 G_FindPlayerBindForGameControl(INT32 player, gamecontrols_e control); + #ifdef __cplusplus } // extern "C" #endif diff --git a/src/v_draw.cpp b/src/v_draw.cpp index 5b5725d0e..a33b89f83 100644 --- a/src/v_draw.cpp +++ b/src/v_draw.cpp @@ -153,38 +153,8 @@ Draw::TextElement& Draw::TextElement::parse(std::string_view raw) { // Grab our local controls UINT8 localplayer = stplyr - players; - profile_t *ourProfile = PR_GetLocalPlayerProfile(localplayer); - if (ourProfile == NULL) - ourProfile = PR_GetLocalPlayerProfile(0); - INT32 device = G_GetDeviceForPlayer(localplayer); // TODO: Respond to what device player is CURRENTLY using - if (device == -1) // No registered device = you can't possibly be using a gamepad - device = KEYBOARD_MOUSE_DEVICE; - - INT32 bestbind = -1; // Bind that matches our input device - INT32 anybind = -1; // Bind that doesn't match, but is at least for this control - - INT32 control = 3; - - while (control >= 0) // Prefer earlier binds - { - INT32 possiblecontrol = ourProfile->controls[(INT32)id->second][control]; - - // if (device is gamepad) == (bound control is in gamepad range) - e.g. if bind matches device - if ((device != KEYBOARD_MOUSE_DEVICE) == (possiblecontrol >= KEY_JOY1 && possiblecontrol < JOYINPUTEND)) - { - bestbind = possiblecontrol; - anybind = possiblecontrol; - } - else - { - anybind = possiblecontrol; - } - - control--; - } - - INT32 bind = (bestbind != -1) ? bestbind : anybind; // If we couldn't find a device-appropriate bind, try to at least use something + INT32 bind = G_FindPlayerBindForGameControl(localplayer, id->second); if (auto pretty = prettyinputs.find(bind); pretty != prettyinputs.end()) // Gamepad direction or keyboard arrow, use something nice-looking {