Skip to content

Commit

Permalink
chat and text menu updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wootguy committed Dec 18, 2024
1 parent 1d5bad5 commit 659ef0a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 47 deletions.
14 changes: 13 additions & 1 deletion dlls/hooks/client_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,19 @@ void Host_Say(edict_t* pEntity, int teamonly)

player->m_flNextChatTime = gpGlobals->time + CHAT_INTERVAL;

UTIL_ClientSay(player, p, NULL, NULL, teamonly);
UTIL_ClientSay(player, p, NULL, teamonly, NULL);

// echo to server console for listen servers, dedicated servers should have logs enabled
if (!IS_DEDICATED_SERVER())
g_engfuncs.pfnServerPrint(p);

const char* temp;
if (teamonly)
temp = "say_team";
else
temp = "say";

UTIL_LogPlayerEvent(player->edict(), "%s \"%s\"\n", temp, p);
}

#define ABORT_IF_CHEATS_DISABLED(cheatName) \
Expand Down
72 changes: 54 additions & 18 deletions dlls/util/TextMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ void TextMenu::initCommon() {
}
TextMenuItem exitItem = { "Exit", "exit" };
options[MAX_MENU_OPTIONS - 1] = exitItem;
backText = "Back";
moreText = "More";
}

void TextMenu::initAnon(TextMenuCallback newCallback) {
Expand Down Expand Up @@ -117,22 +119,22 @@ void TextMenu::handleMenuselectCmd(CBasePlayer* pPlayer, int selection) {
int playerbit = PLRBIT(pPlayer->edict());

if (viewers & playerbit) {
if (selection == g_exitOptionNum-1) {
if (!noexit && selection == g_exitOptionNum-1) {
// exit menu
viewers &= ~playerbit;
}
else if (isPaginated() && selection == BACK_OPTION_IDX) {
else if (isPaginated() && selection == BackOptionIdx()) {
Open(lastDuration, lastPage - 1, pPlayer);
}
else if (isPaginated() && selection == MORE_OPTION_IDX) {
else if (isPaginated() && selection == MoreOptionIdx()) {
Open(lastDuration, lastPage + 1, pPlayer);
}
else if (selection < numOptions && IsValidPlayer(pPlayer->edict())) {
if (anonCallback)
anonCallback(this, pPlayer, selection, options[lastPage*ITEMS_PER_PAGE + selection]);
anonCallback(this, pPlayer, selection, options[lastPage*ItemsPerPage() + selection]);
if (entCallback) {
if (h_ent)
(((CTriggerVote*)h_ent.GetEntity())->*entCallback)(this, pPlayer, selection, options[lastPage * ITEMS_PER_PAGE + selection]);
(((CTriggerVote*)h_ent.GetEntity())->*entCallback)(this, pPlayer, selection, options[lastPage * ItemsPerPage() + selection]);
}

viewers &= ~playerbit;
Expand All @@ -144,13 +146,38 @@ void TextMenu::handleMenuselectCmd(CBasePlayer* pPlayer, int selection) {
}

bool TextMenu::isPaginated() {
return numOptions > MAX_ITEMS_NO_PAGES;
return numOptions > MaxItemsNoPages();
}

void TextMenu::SetTitle(std::string newTitle) {
this->title = newTitle;
}

void TextMenu::SetPaginationText(std::string backText, std::string moreText) {
this->backText = backText;
this->moreText = moreText;
}

void TextMenu::RemoveExit() {
noexit = true;
}

int TextMenu::ItemsPerPage() {
return noexit ? MAX_PAGE_OPTIONS - 2 : MAX_PAGE_OPTIONS - 3;
}

int TextMenu::BackOptionIdx() {
return ItemsPerPage();
}

int TextMenu::MoreOptionIdx() {
return ItemsPerPage() + 1;
}

int TextMenu::MaxItemsNoPages() {
return ItemsPerPage() + 2;
}

void TextMenu::AddItem(std::string displayText, std::string optionData) {
if (numOptions >= MAX_MENU_OPTIONS) {
ALERT(at_console, "Maximum menu options reached! Failed to add: %s\n", optionData.c_str());
Expand All @@ -164,16 +191,24 @@ void TextMenu::AddItem(std::string displayText, std::string optionData) {
}

void TextMenu::Open(uint8_t duration, uint8_t page, CBasePlayer* player) {
std::string menuText = title + "\n\n";

uint16_t validSlots = (1 << (g_exitOptionNum-1)); // exit option always valid
uint16_t validSlots = 0;

if (!noexit)
validSlots = (1 << (g_exitOptionNum - 1)); // exit option always valid

lastPage = page;
lastDuration = duration;

int limitPerPage = isPaginated() ? ITEMS_PER_PAGE : MAX_ITEMS_NO_PAGES;
int itemOffset = page * ITEMS_PER_PAGE;
int totalPages = (numOptions+(ITEMS_PER_PAGE-1)) / ITEMS_PER_PAGE;
int limitPerPage = isPaginated() ? ItemsPerPage() : ItemsPerPage();
int itemOffset = page * ItemsPerPage();
int totalPages = (numOptions+(ItemsPerPage() -1)) / ItemsPerPage();

std::string pageSuffix = "";
if (isPaginated()) {
pageSuffix = UTIL_VarArgs(" (%d/%d)", page + 1, totalPages);
}

std::string menuText = title + pageSuffix + "\n\n";

int addedOptions = 0;
for (int i = itemOffset, k = 0; i < itemOffset+limitPerPage && i < numOptions; i++, k++) {
Expand All @@ -182,7 +217,7 @@ void TextMenu::Open(uint8_t duration, uint8_t page, CBasePlayer* player) {
addedOptions++;
}

while (isPaginated() && addedOptions < ITEMS_PER_PAGE) {
while (isPaginated() && addedOptions < ItemsPerPage()) {
menuText += "\n";
addedOptions++;
}
Expand All @@ -191,22 +226,23 @@ void TextMenu::Open(uint8_t duration, uint8_t page, CBasePlayer* player) {

if (isPaginated()) {
if (page > 0) {
menuText += std::to_string(BACK_OPTION_IDX+1) + ": Back\n";
validSlots |= (1 << (ITEMS_PER_PAGE));
menuText += std::to_string(BackOptionIdx()+ 1) + ": " + backText + "\n";
validSlots |= (1 << (ItemsPerPage()));
}
else {
menuText += "\n";
}
if (page < totalPages - 1) {
menuText += std::to_string(MORE_OPTION_IDX+1) + ": More\n";
validSlots |= (1 << (ITEMS_PER_PAGE + 1));
menuText += std::to_string(MoreOptionIdx() + 1) + ": " + moreText + "\n";
validSlots |= (1 << (ItemsPerPage() + 1));
}
else {
menuText += "\n";
}
}

menuText += std::to_string(g_exitOptionNum % 10) + ": Exit";
if (!noexit)
menuText += std::to_string(g_exitOptionNum % 10) + ": Exit";

if (player && IsValidPlayer(player->edict())) {
MESSAGE_BEGIN(MSG_ONE, gmsgShowMenu, NULL, player->edict());
Expand Down
17 changes: 13 additions & 4 deletions dlls/util/TextMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

#define MAX_MENU_OPTIONS 128
#define MAX_PAGE_OPTIONS 5
#define ITEMS_PER_PAGE (MAX_PAGE_OPTIONS-3) // leave room for next, more, and exit options
#define BACK_OPTION_IDX (ITEMS_PER_PAGE)
#define MORE_OPTION_IDX (ITEMS_PER_PAGE+1)
#define MAX_ITEMS_NO_PAGES (ITEMS_PER_PAGE+2)
#define MAX_PLAYERS 32

class CTriggerVote;
Expand Down Expand Up @@ -40,6 +36,11 @@ class TextMenu {

EXPORT void SetTitle(std::string title);

EXPORT void SetPaginationText(std::string backText, std::string moreText);

// remove exit as a menu option
EXPORT void RemoveExit();

EXPORT void AddItem(std::string displayText, std::string optionData);

// set player to NULL to send to all players.
Expand All @@ -58,6 +59,11 @@ class TextMenu {
void initEnt(EntityTextMenuCallback callback, CBaseEntity* ent);
void initCommon();

int ItemsPerPage();
int BackOptionIdx();
int MoreOptionIdx();
int MaxItemsNoPages();

private:
EntityTextMenuCallback entCallback = NULL;
TextMenuCallback anonCallback = NULL;
Expand All @@ -69,6 +75,9 @@ class TextMenu {
int numOptions = 0;
int8_t lastPage = 0;
int8_t lastDuration = 0;
bool noexit;
std::string backText;
std::string moreText;

bool isActive = false;

Expand Down
34 changes: 13 additions & 21 deletions dlls/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ void UTIL_ClientPrint(CBaseEntity* client, PRINT_TYPE print_type, const char * m
}
}

void UTIL_ClientSay(CBasePlayer* plr, const char* text, const char* customPrefix, const char* customLogPrefix, bool teamMessage) {
void UTIL_ClientSay(CBasePlayer* plr, const char* text, const char* customPrefix, bool teamMessage, edict_t* target) {
if (!plr || !text) {
return;
}
Expand All @@ -1514,31 +1514,23 @@ void UTIL_ClientSay(CBasePlayer* plr, const char* text, const char* customPrefix

const char* msg = UTIL_VarArgs("%c%s%s\n", 2, prefix.c_str(), textTmp.c_str());

// print to the sending client
MESSAGE_BEGIN(MSG_ALL, gmsgSayText);
WRITE_BYTE(plr->entindex());
WRITE_STRING(msg);
MESSAGE_END();
if (target) {
MESSAGE_BEGIN(MSG_ONE, gmsgSayText, NULL, target);
WRITE_BYTE(plr->entindex());
WRITE_STRING(msg);
MESSAGE_END();
}
else {
MESSAGE_BEGIN(MSG_ALL, gmsgSayText);
WRITE_BYTE(plr->entindex());
WRITE_STRING(msg);
MESSAGE_END();
}

if (plr->tempNameActive) {
plr->Rename(plr->m_tempName, false, MSG_ONE, plr->edict());
plr->UpdateTeamInfo(plr->m_tempTeam, MSG_ONE, plr->edict());
}

// echo to server console for listen servers, dedicated servers should have logs enabled
if (!IS_DEDICATED_SERVER())
g_engfuncs.pfnServerPrint(text);

const char* temp;
if (customLogPrefix)
temp = customLogPrefix;
else if (teamMessage)
temp = "say_team";
else
temp = "say";

// team match?
UTIL_LogPlayerEvent(plr->edict(), "%s \"%s\"\n", temp, text);
}

char *UTIL_dtos1( int d )
Expand Down
6 changes: 3 additions & 3 deletions dlls/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@ EXPORT void UTIL_PrecacheOther( const char *szClassname, std::unordered_map<st
EXPORT void UTIL_ClientPrint(CBaseEntity* client, PRINT_TYPE print_type, const char* msg);
EXPORT void UTIL_ClientPrintAll(PRINT_TYPE print_type, const char *msg);

// handles coloring and logs to the server console and file
// don't include the newline
EXPORT void UTIL_ClientSay(CBasePlayer* plr, const char* text, const char* customPrefix=NULL, const char* customLogPrefix=NULL, bool teamMessage=false);
// handles coloring and name prefix. Don't include the newline
EXPORT void UTIL_ClientSay(CBasePlayer* plr, const char* text, const char* customPrefix=NULL,
bool teamMessage=false, edict_t* target=NULL);

class CBasePlayerItem;
class CBasePlayer;
Expand Down

0 comments on commit 659ef0a

Please sign in to comment.