Move bind-search logic to g_input

This commit is contained in:
Antonio Martinez 2024-09-13 22:03:18 -07:00 committed by AJ Martinez
parent 1c50c3510d
commit 0b9c5c7415
3 changed files with 39 additions and 31 deletions

View file

@ -1121,6 +1121,42 @@ INT32 G_CheckDoubleUsage(INT32 keynum, INT32 playernum, boolean modify)
return result; 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) static void setcontrol(UINT8 player)
{ {
INT32 numctrl; INT32 numctrl;

View file

@ -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]); 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_CheckDoubleUsage(INT32 keynum, INT32 playernum, boolean modify);
INT32 G_FindPlayerBindForGameControl(INT32 player, gamecontrols_e control);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif

View file

@ -153,38 +153,8 @@ Draw::TextElement& Draw::TextElement::parse(std::string_view raw)
{ {
// Grab our local controls // Grab our local controls
UINT8 localplayer = stplyr - players; 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 INT32 bind = G_FindPlayerBindForGameControl(localplayer, id->second);
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
if (auto pretty = prettyinputs.find(bind); pretty != prettyinputs.end()) // Gamepad direction or keyboard arrow, use something nice-looking if (auto pretty = prettyinputs.find(bind); pretty != prettyinputs.end()) // Gamepad direction or keyboard arrow, use something nice-looking
{ {