Skip to content

Commit

Permalink
Client: HUD: Menu: Add cooldown to F-keys (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmp64 committed Sep 29, 2024
1 parent 98d50f3 commit d35250b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
88 changes: 51 additions & 37 deletions src/game/client/hud/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "chat.h"

ConVar hud_menu_fkeys("hud_menu_fkeys", "1", FCVAR_BHL_ARCHIVE, "Use keys F1-F10 in the menus");
ConVar hud_menu_fkeys_cooldown("hud_menu_fkeys_cooldown", "1.5", FCVAR_BHL_ARCHIVE, "Cooldown F1-F10 after the menu closes");

#define MAX_MENU_STRING 512
char g_szMenuString[MAX_MENU_STRING];
Expand All @@ -53,6 +54,7 @@ void CHudMenu::InitHudData(void)
m_fMenuDisplayed = 0;
m_bitsValidSlots = 0;
m_iFlags &= ~HUD_ACTIVE;
m_flMenuCloseTime = -1;
Reset();
}

Expand Down Expand Up @@ -134,8 +136,7 @@ void CHudMenu::Draw(float flTime)
{
if (m_flShutoffTime <= gHUD.m_flTime)
{ // times up, shutoff
m_fMenuDisplayed = 0;
m_iFlags &= ~HUD_ACTIVE;
CloseMenu();
return;
}
}
Expand Down Expand Up @@ -252,17 +253,24 @@ bool CHudMenu::OnWeaponSlotSelected(int slotIdx)

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;
if (m_fMenuDisplayed)
{
SelectMenuItem(keynum - K_F1 + 1);
return true;
}
else if (m_flMenuCloseTime != -1 && gEngfuncs.GetAbsoluteTime() - m_flMenuCloseTime < hud_menu_fkeys_cooldown.GetFloat())
{
// In cooldown to prevent miss-presses
return true;
}

return false;
}

// selects an item from the menu
Expand All @@ -276,35 +284,8 @@ void CHudMenu::SelectMenuItem(int menu_item)
EngineClientCmd(szbuf);

// remove the menu
m_fMenuDisplayed = 0;
m_iFlags &= ~HUD_ACTIVE;
}
}

int CHudMenu::GetStartY(int lineCount, int lineHeight)
{
int height = (lineCount + 1) * lineHeight; // +1 to account for the last line missing a \n
int top = (ScreenHeight / 2) - (height / 2); // Centered vertically
int bottom = top + height;

// Make sure menu doesn't occlude the chat
int chatY = CHudChat::Get()->GetYPos();
if (bottom > chatY)
{
int delta = bottom - chatY;
top -= delta;
bottom -= delta;
}

// Make sure the menu doesn't go out of bounds
constexpr int MARGIN_TOP = 10;
if (top < MARGIN_TOP)
{
top += MARGIN_TOP;
bottom += MARGIN_TOP;
CloseMenu();
}

return top;
}

// Message handler for ShowMenu message
Expand Down Expand Up @@ -358,11 +339,44 @@ int CHudMenu::MsgFunc_ShowMenu(const char *pszName, int iSize, void *pbuf)
}
else
{
m_fMenuDisplayed = 0; // no valid slots means that the menu should be turned off
m_iFlags &= ~HUD_ACTIVE;
// no valid slots means that the menu should be turned off
CloseMenu();
}

m_fWaitingForMore = NeedMore;

return 1;
}

int CHudMenu::GetStartY(int lineCount, int lineHeight)
{
int height = (lineCount + 1) * lineHeight; // +1 to account for the last line missing a \n
int top = (ScreenHeight / 2) - (height / 2); // Centered vertically
int bottom = top + height;

// Make sure menu doesn't occlude the chat
int chatY = CHudChat::Get()->GetYPos();
if (bottom > chatY)
{
int delta = bottom - chatY;
top -= delta;
bottom -= delta;
}

// Make sure the menu doesn't go out of bounds
constexpr int MARGIN_TOP = 10;
if (top < MARGIN_TOP)
{
top += MARGIN_TOP;
bottom += MARGIN_TOP;
}

return top;
}

void CHudMenu::CloseMenu()
{
m_fMenuDisplayed = 0;
m_iFlags &= ~HUD_ACTIVE;
m_flMenuCloseTime = gEngfuncs.GetAbsoluteTime();
}
5 changes: 5 additions & 0 deletions src/game/client/hud/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ class CHudMenu : public CHudElemBase<CHudMenu>
int m_fWaitingForMore;

private:
float m_flMenuCloseTime = -1;

//! Calculates the top Y coordinate for menu rendering.
int GetStartY(int lineCount, int lineHeight);

//! Hides the menu and records close time.
void CloseMenu();
};

#endif

0 comments on commit d35250b

Please sign in to comment.