Skip to content

Commit

Permalink
Client: HUD: Menu: Use F-keys instead if weapon slots (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmp64 committed Aug 25, 2024
1 parent 6b50c97 commit 07f0599
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/game/client/hud/ammo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,9 @@ HSPRITE *WeaponsResource::GetAmmoPicFromWeapon(int iAmmoId, wrect_t &rect)

void WeaponsResource::SelectSlot(int iSlot, int fAdvance, int iDirection)
{
if (CHudMenu::Get()->m_fMenuDisplayed && (fAdvance == FALSE) && (iDirection == 1))
{ // menu is overriding slot use commands
CHudMenu::Get()->SelectMenuItem(iSlot + 1); // slots are one off the key numbers
if ((fAdvance == FALSE) && (iDirection == 1) && CHudMenu::Get()->OnWeaponSlotSelected(iSlot))
{
// menu is overriding slot use commands
return;
}

Expand Down
64 changes: 63 additions & 1 deletion src/game/client/hud/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//
#include <string.h>
#include <stdio.h>
#include <keydefs.h>

#include "hud.h"
#include "cl_util.h"
Expand All @@ -28,6 +29,8 @@
#include "text_message.h"
#include "chat.h"

ConVar hud_menu_fkeys("hud_menu_fkeys", "1", FCVAR_BHL_ARCHIVE, "Use keys F1-F10 in the menus");

#define MAX_MENU_STRING 512
char g_szMenuString[MAX_MENU_STRING];
char g_szPrelocalisedMenuString[MAX_MENU_STRING];
Expand Down Expand Up @@ -158,7 +161,7 @@ void CHudMenu::Draw(float flTime)
menu_r = 255;
menu_g = 255;
menu_b = 255;
menu_x = 20;
menu_x = SPR_RES_SCALED(20);
menu_ralign = FALSE;

const char *sptr = g_szMenuString;
Expand Down Expand Up @@ -188,6 +191,38 @@ void CHudMenu::Draw(float flTime)
strncpy(menubuf, ptr, min((sptr - ptr), (int)sizeof(menubuf)));
menubuf[min((sptr - ptr), (int)(sizeof(menubuf) - 1))] = '\0';

if (hud_menu_fkeys.GetBool() && !menu_ralign)
{
// Prepend menu items with 'F'
// Only check first 16 chars to reduce false number detection
std::string_view menubufView(menubuf, std::min(strlen(menubuf), size_t(16)));
size_t firstNonSpace = menubufView.find_first_not_of(" \t");
if (firstNonSpace != std::string_view::npos && firstNonSpace <= sizeof(menubuf) - 2)
{
// First char is a digit and next one isn't
if (menubuf[firstNonSpace] >= '0' && menubuf[firstNonSpace] <= '9' &&
!(menubuf[firstNonSpace + 1] >= '0' && menubuf[firstNonSpace + 1] <= '9'))
{
int digit = menubuf[firstNonSpace] - '0';
int shift = digit == 0 ? 2 : 1;

// Shift the string by 1/2 chars
memmove(menubuf + firstNonSpace + shift, menubuf + firstNonSpace, sizeof(menubuf) - firstNonSpace - shift);

// Set the char
if (digit == 0)
{
menubuf[firstNonSpace] = 'F';
menubuf[firstNonSpace + 1] = '1';
}
else
{
menubuf[firstNonSpace] = 'F';
}
}
}
}

if (menu_ralign)
{
// IMPORTANT: Right-to-left rendered text does not parse escape tokens!
Expand All @@ -203,6 +238,33 @@ void CHudMenu::Draw(float flTime)
return;
}

bool CHudMenu::OnWeaponSlotSelected(int slotIdx)
{
if (!m_fMenuDisplayed)
return false;

if (hud_menu_fkeys.GetBool())
return false;

SelectMenuItem(slotIdx + 1); // slots are one off the key numbers
return true;
}

bool CHudMenu::OnKeyPressed(int keynum)
{
if (!m_fMenuDisplayed)
return false;

if (!hud_menu_fkeys.GetBool())
return false;

if (!(keynum >= K_F1 && keynum <= K_F10))
return false;

SelectMenuItem(keynum - K_F1 + 1);
return true;
}

// selects an item from the menu
void CHudMenu::SelectMenuItem(int menu_item)
{
Expand Down
2 changes: 2 additions & 0 deletions src/game/client/hud/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class CHudMenu : public CHudElemBase<CHudMenu>
void Draw(float flTime);
int MsgFunc_ShowMenu(const char *pszName, int iSize, void *pbuf);

bool OnWeaponSlotSelected(int slotIdx);
bool OnKeyPressed(int keynum);
void SelectMenuItem(int menu_item);

int m_fMenuDisplayed;
Expand Down
8 changes: 6 additions & 2 deletions src/game/client/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Exports.h"
#include "cl_voice_status.h"
#include "hud/health.h"
#include "hud/menu.h"
#include "hud/spectator.h"

#include "vgui/client_viewport.h"
Expand Down Expand Up @@ -432,8 +433,11 @@ int CL_DLLEXPORT HUD_Key_Event(int down, int keynum, const char *pszCurrentBindi
{
// RecClKeyEvent(down, keynum, pszCurrentBinding);

if (g_pViewport)
return g_pViewport->KeyInput(down, keynum, pszCurrentBinding);
if (g_pViewport && !g_pViewport->KeyInput(down, keynum, pszCurrentBinding))
return 0;

if (down && CHudMenu::Get()->OnKeyPressed(keynum))
return 0;

return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/game/server/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4690,7 +4690,7 @@ Vector CBasePlayer ::GetAutoaimVector(float flDelta)
// m_vecAutoAim = m_vecAutoAim * 0.99;

// Don't send across network if sv_aim is 0
if (g_psv_aim->value != 0 && g_psv_allow_autoaim->value != 0)
if (g_psv_aim->value != 0 && (!g_psv_allow_autoaim || g_psv_allow_autoaim->value != 0))
{
if (m_vecAutoAim.x != m_lastx || m_vecAutoAim.y != m_lasty)
{
Expand All @@ -4716,7 +4716,7 @@ Vector CBasePlayer ::AutoaimDeflection(Vector &vecSrc, float flDist, float flDel
edict_t *bestent;
TraceResult tr;

if (g_psv_aim->value == 0 && g_psv_allow_autoaim->value != 0)
if (g_psv_aim->value == 0 && (!g_psv_allow_autoaim || g_psv_allow_autoaim->value != 0))
{
m_fOnTarget = FALSE;
return g_vecZero;
Expand Down

0 comments on commit 07f0599

Please sign in to comment.