From 6e04e4171e3ea1cf4f58f933f3b28ac1629252c0 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 6 Jun 2025 00:19:23 +0200 Subject: [PATCH 01/12] fix the goddamn boolean --- src/doomtype.h | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/doomtype.h b/src/doomtype.h index 174bba5b1..88e9ce5a4 100644 --- a/src/doomtype.h +++ b/src/doomtype.h @@ -156,22 +156,19 @@ size_t strlcpy(char *dst, const char *src, size_t siz); /* Boolean type definition */ -// \note __BYTEBOOL__ used to be set above if "macintosh" was defined, -// if macintosh's version of boolean type isn't needed anymore, then isn't this macro pointless now? -#ifndef __BYTEBOOL__ - #define __BYTEBOOL__ - - //faB: clean that up !! - #if defined( _MSC_VER) && (_MSC_VER >= 1800) // MSVC 2013 and forward - #include "stdbool.h" - #elif defined (_WIN32) - #define false FALSE // use windows types - #define true TRUE - #define boolean BOOL - #else - typedef enum {false, true} boolean; - #endif -#endif // __BYTEBOOL__ +#ifndef _WIN32 +#include +// dont use stdbools _BOOL type +// its smaller (1 byte) than the old interger bool (4 bytes) +// which results in packed struct sizes being mismatched between vanilla and this +// however we gotta still include stdbool cause since c23 true and false are keywords +// which we cant use as enumeration constants +typedef int32_t boolean; +#else +#define false FALSE +#define true TRUE +#define boolean BOOL +#endif /* 7.18.2.1 Limits of exact-width integer types */ #ifndef INT8_MIN From 3a55972997efad4956dded445602e3945427be5a Mon Sep 17 00:00:00 2001 From: Indev Date: Sat, 28 Jun 2025 21:03:15 +0300 Subject: [PATCH 02/12] Initial emote support --- src/Sourcefile | 1 + src/d_netcmd.c | 3 + src/doomtype.h | 2 + src/hu_stuff.c | 73 +++++++++++++++- src/m_emotes.cpp | 220 +++++++++++++++++++++++++++++++++++++++++++++++ src/m_emotes.h | 38 ++++++++ src/p_setup.c | 6 ++ 7 files changed, 341 insertions(+), 2 deletions(-) create mode 100644 src/m_emotes.cpp create mode 100644 src/m_emotes.h diff --git a/src/Sourcefile b/src/Sourcefile index 227e6c95a..23572d293 100644 --- a/src/Sourcefile +++ b/src/Sourcefile @@ -28,6 +28,7 @@ m_cond.c m_fixed.c m_menu.c m_textinput.c +m_emotes.cpp m_misc.c m_perfstats.c m_queue.c diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 91ea8e945..ff7a2031a 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -21,6 +21,7 @@ #include "i_system.h" #include "g_game.h" #include "hu_stuff.h" +#include "m_emotes.h" #include "g_input.h" #include "m_menu.h" #include "r_local.h" @@ -917,6 +918,8 @@ void D_RegisterClientCommands(void) CV_RegisterVar(&cv_keyboardlayout); + CV_RegisterVar(&cv_emotes); + // time attack ghost options are also saved to config CV_RegisterVar(&cv_ghost_besttime); CV_RegisterVar(&cv_ghost_bestlap); diff --git a/src/doomtype.h b/src/doomtype.h index 88e9ce5a4..c598e86ee 100644 --- a/src/doomtype.h +++ b/src/doomtype.h @@ -100,8 +100,10 @@ typedef long ssize_t; #define strnicmp(x,y,n) strncasecmp(x,y,n) #endif +#ifndef __cplusplus char *strcasestr(const char *in, const char *what); #define stristr strcasestr +#endif #if defined (macintosh) //|| defined (__APPLE__) //skip all boolean/Boolean crap #define true 1 diff --git a/src/hu_stuff.c b/src/hu_stuff.c index f8f0524b2..b1d0cd64a 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -17,6 +17,7 @@ #include "m_menu.h" // gametype_cons_t #include "m_cond.h" // emblems +#include "m_emotes.h" #include "d_clisrv.h" @@ -93,6 +94,7 @@ static textinput_t w_chat; static boolean headsupactive = false; boolean hu_showscores; // draw rankings static char hu_tick; +static tic_t hu_emoteanim = 0; static huddrawlist_h luahuddrawlist_scores; @@ -1102,6 +1104,7 @@ void HU_Ticker(void) return; hu_tick++; + hu_emoteanim++; hu_tick &= 7; // currently only to blink chat input cursor if (PLAYER1INPUTDOWN(gc_scores)) @@ -1406,6 +1409,40 @@ boolean HU_Responder(event_t *ev) #ifndef NONET +static INT32 HU_DrawEmote(INT32 x, INT32 y, emote_t *emote, INT32 flags) +{ + if (emote->numframes == 0) + return 4; // charwidth + + const char *lumpname = emote->frames[(hu_emoteanim/emote->timeperframe) % emote->numframes]; + patch_t *emotepatch = W_CachePatchName(lumpname, PU_CACHE); + + int emotewidth = min(6, emotepatch->width); + int charheight = 6; + + fixed_t scale = (FRACUNIT/emotepatch->width)*emotewidth; + + x *= FRACUNIT; + y *= FRACUNIT; + + y -= (scale*emotepatch->height-charheight*FRACUNIT)/2; + + V_DrawFixedPatch(x, y, scale, flags, emotepatch, NULL); + + return emotewidth; +} + +static INT32 HU_EmoteWidth(emote_t *emote) +{ + if (emote->numframes == 0) + return 4; // charwidth + + const char *lumpname = emote->frames[(hu_emoteanim/emote->timeperframe) % emote->numframes]; + patch_t *emotepatch = W_CachePatchName(lumpname, PU_CACHE); + + return min(8, emotepatch->width); +} + // Precompile a wordwrapped string to any given width. // This is a muuuch better method than V_WORDWRAP. // again stolen and modified a bit from video.c, don't mind me, will need to rearrange this one day. @@ -1417,6 +1454,8 @@ static char *CHAT_WordWrap(INT32 x, INT32 w, INT32 option, const char *string) size_t slen; char *newstring = Z_StrDup(string); INT32 spacewidth = (vid.width < 640) ? 8 : 4, charwidth = (vid.width < 640) ? 8 : 4; + emote_t *emote = NULL; + int emotelen = 0; slen = strlen(string); x = 0; @@ -1438,7 +1477,12 @@ static char *CHAT_WordWrap(INT32 x, INT32 w, INT32 option, const char *string) c = toupper(c); c -= HU_FONTSTART; - if (c < 0 || c >= HU_FONTSIZE || !hu_font[c]) + if (string[i] == ':' && (emote = M_VerifyEmote(string+i, &emotelen))) + { + chw = HU_EmoteWidth(emote); + i += emotelen-1; + } + else if (c < 0 || c >= HU_FONTSIZE || !hu_font[c]) { chw = spacewidth; lastusablespace = i; @@ -1460,7 +1504,6 @@ static char *CHAT_WordWrap(INT32 x, INT32 w, INT32 option, const char *string) return newstring; } - // 30/7/18: chaty is now the distance at which the lowest point of the chat will be drawn if that makes any sense. INT16 chatx = 13, chaty = 169; // let's use this as our coordinates, shh @@ -1493,6 +1536,8 @@ static void HU_drawMiniChat(void) char *msg = CHAT_WordWrap(x+2, boxw-(charwidth*2), V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_ALLOWLOWERCASE, chat_mini[i-1]); size_t j = 0; INT32 linescount = 0; + emote_t *emote = NULL; + int emotelen = 0; while(msg[j]) // iterate through msg { @@ -1517,6 +1562,11 @@ static void HU_drawMiniChat(void) ++j; } + else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) + { + dx += HU_EmoteWidth(emote) - charwidth; + j += emotelen; + } else { j++; @@ -1562,6 +1612,8 @@ static void HU_drawMiniChat(void) size_t j = 0; char *msg = CHAT_WordWrap(x+2, boxw-(charwidth*2), V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_ALLOWLOWERCASE, chat_mini[i]); // get the current message, and word wrap it. UINT8 *colormap = NULL; + emote_t *emote = NULL; + int emotelen = 0; while(msg[j]) // iterate through msg { @@ -1588,6 +1640,15 @@ static void HU_drawMiniChat(void) ++j; } + else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) + { + if (cv_chatbacktint.value) // on request of wolfy + V_DrawFillConsoleMap(x + dx + 2, y+dy, charwidth, charheight, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); + + dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT|transflag) - charwidth; + + j += emotelen; + } else { if (cv_chatbacktint.value) // on request of wolfy @@ -1665,6 +1726,9 @@ static void HU_drawChatLog(INT32 offset) INT32 j = 0; char *msg = CHAT_WordWrap(x+2, boxw-(charwidth*2), V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_ALLOWLOWERCASE, chat_log[i]); // get the current message, and word wrap it. UINT8 *colormap = NULL; + emote_t *emote = NULL; + int emotelen = 0; + while(msg[j]) // iterate through msg { if (msg[j] < HU_FONTSTART) // don't draw @@ -1686,6 +1750,11 @@ static void HU_drawChatLog(INT32 offset) ++j; } + else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) + { + dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT) - charwidth; + j += emotelen; + } else { if ((y+dy+2 >= chat_topy) && (y+dy < (chat_bottomy))) diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp new file mode 100644 index 000000000..25d3a4ebc --- /dev/null +++ b/src/m_emotes.cpp @@ -0,0 +1,220 @@ +#include +#include +#include +#include + +#include "m_emotes.h" + +extern "C" { +#include "w_wad.h" +} + +#include "z_zone.h" + +consvar_t cv_emotes = {"emotes", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL};; + +static std::map emotes; + +// TODO - Would be really nice to generalize soc-like file parsing and not reimplement it 100 times :))))) +void M_LoadEmotes(UINT16 wadnum) +{ + UINT16 lumpnum = W_CheckNumForNamePwad("EMOTES", wadnum, 0); + + if (lumpnum == INT16_MAX) + return; + + char *lump = static_cast(W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE)); + size_t size = W_LumpLengthPwad(wadnum, lumpnum); + + std::istringstream file(std::string(lump, size)); + std::string line; + + emote_t *emote = nullptr; + std::string emote_name; + + // Just smol lambda to remove all the trailing chars we might not want + auto trim = [](std::string &str) { + str.erase(0, str.find_first_not_of(" \t\n\r\f\v")); + str.erase(str.find_last_not_of(" \t\n\r\f\v")+1); + }; + + int numemotes = 0; + int linenum = 0; + + while (std::getline(file, line)) + { + ++linenum; + + trim(line); + + if (line.empty()) + continue; + + size_t split = line.find('='); + + // Didn't find the =, means we're about to parse `Emote ` + if (split == line.npos) + { + split = line.find(' '); + + if (split == line.npos) + { + CONS_Alert(CONS_WARNING, "EMOTES: Unrecognized line. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + continue; + } + + if (strnicmp(line.c_str(), "emote", 5)) + { + CONS_Alert(CONS_WARNING, "EMOTES: 'Emote' expected. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + continue; + } + + emote_name = line.substr(split+1); + + if (emote_name.find_first_of(" \t\n\r\f\v") != emote_name.npos) + { + CONS_Alert(CONS_WARNING, "EMOTES: Emote name cannot contain spaces. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + continue; + } + + // This first->second looks kind of weird i know + emote = &emotes[emote_name]; + + emote->timeperframe = 1; + emote->numframes = 0; + std::memset(emote->frames, 0, sizeof(emote->frames)); + + ++numemotes; + } + else if (emote) + { + std::string field = line.substr(0, split); + std::string value = line.substr(split+1); + + trim(field); + trim(value); + + if (stricmp(field.c_str(), "frames") == 0) + { + UINT8 numframes = 0; + + auto copy_frame = [&] { + if (numframes == MAXEMOTEFRAMES) + { + CONS_Alert(CONS_WARNING, "EMOTES: Too many frames. (file %s, line %d)", wadfiles[wadnum]->filename, linenum); + return false; + } + + size_t list_split = value.find(','); + std::string framelumpname = value.substr(0, list_split); + trim(framelumpname); + + if (list_split == value.npos) + value = ""; + else + value = value.substr(list_split+1, value.npos); + + // End of list of frames + if (framelumpname.size() == 0) + return false; + + if (framelumpname.size() > 8) + { + CONS_Alert(CONS_WARNING, "EMOTES: Frame %d name is too long. (file %s, line %d)", numframes, wadfiles[wadnum]->filename, linenum); + framelumpname = framelumpname.substr(0, 8); + } + + std::strncpy(emote->frames[numframes], framelumpname.c_str(), framelumpname.size()+1); + + ++numframes; + + return true; + }; + + while (copy_frame()) {} + + if (numframes == 0) + { + CONS_Alert(CONS_WARNING, "EMOTES: Expected list of frames. (file %s, line %d)", wadfiles[wadnum]->filename, linenum); + } + + emote->numframes = numframes; + } + else if (stricmp(field.c_str(), "timeperframe") == 0) + { + emote->timeperframe = std::stoi(value); + + if (emote->timeperframe <= 0) + { + emote->timeperframe = 1; + CONS_Alert(CONS_WARNING, "EMOTES: Bad value for 'timeperframe'. (file %s, line %d)", wadfiles[wadnum]->filename, linenum); + } + } + else + { + CONS_Alert(CONS_WARNING, "EMOTES: Unrecognized field '%s'. (file %s, line %d)", field.c_str(), wadfiles[wadnum]->filename, linenum); + } + } + else + { + CONS_Alert(CONS_WARNING, "EMOTES: Unrecognized line. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + } + } + + CONS_Printf("Added %d emotes\n", numemotes); +} + +emote_t *M_FindEmote(const char *name, int skip) +{ + if (!cv_emotes.value) + return nullptr; + + char query[MAXEMOTENAME+1] = {0}; + std::strncpy(query, name, MAXEMOTENAME); + + for (auto &pair: emotes) + { + if (pair.first.rfind(query, 0, MAXEMOTENAME) != 0) + continue; + + if (skip > 0) + { + --skip; + continue; + } + + return &pair.second; + } + + return nullptr; +} + +emote_t *M_VerifyEmote(const char *name, int *emotelen) +{ + if (!cv_emotes.value) + return nullptr; + + const char *p = name+1; // skip ':' + emote_t *match = nullptr; + + while (*p && (p - name) < MAXEMOTENAME) + { + if (*p == ':') + { + std::string checkname = std::string(name+1, (p-name)-1); + auto it = emotes.find(checkname); + + if (it != emotes.end()) + match = &it->second; + + break; + } + + ++p; + } + + if (match && emotelen) + *emotelen = p-name+1; + + return match; +} diff --git a/src/m_emotes.h b/src/m_emotes.h new file mode 100644 index 000000000..f3e1875ce --- /dev/null +++ b/src/m_emotes.h @@ -0,0 +1,38 @@ +#ifndef __M_EMOTES__ +#define __M_EMOTES__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "doomdef.h" +#include "command.h" + +extern consvar_t cv_emotes; + +#define MAXEMOTENAME 32 +#define MAXEMOTEFRAMES 32 + +typedef struct emote_s { + char frames[MAXEMOTEFRAMES][9]; + UINT8 numframes; + tic_t timeperframe; +} emote_t; + +// Reads and adds emotes from EMOTES lump, with syntax similar to soc +void M_LoadEmotes(UINT16 wadnum); + +// Finds first matching emote, ignoring first few matches +emote_t *M_FindEmote(const char *name, int skips); + +// Starts from ':' character, goes until it finds ':' or end of string +// If end of string is found, or closing ':' is found but resulting emote name doesn't exist, +// it returns null, otherwise it returns emote and stores into *emotelen how much characters +// needs to be skipped to get past emote name +emote_t *M_VerifyEmote(const char *name, int *emotelen); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // __M_EMOTES__ diff --git a/src/p_setup.c b/src/p_setup.c index c56e8a2be..f5d92e7ca 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -34,6 +34,7 @@ #include "r_fps.h" // R_ResetViewInterpolation in level load #include "s_sound.h" +#include "m_emotes.h" #include "st_stuff.h" #include "w_wad.h" #include "z_zone.h" @@ -3310,6 +3311,11 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, boolean local) // S_LoadMusicDefs(wadnum); + // + // look for emotes + // + M_LoadEmotes(wadnum); + // TODO: Experimental SPRTINFO support, test first R_LoadSpriteInfoLumps(wadnum, wadfiles[wadnum]->numlumps); From 4b129314d3a5ca2ab8087a73b4f7b0299f8b2f7b Mon Sep 17 00:00:00 2001 From: Indev Date: Sat, 28 Jun 2025 21:04:09 +0300 Subject: [PATCH 03/12] Right that got removed in the end :chonkbunckle: --- src/m_emotes.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp index 25d3a4ebc..50718b79b 100644 --- a/src/m_emotes.cpp +++ b/src/m_emotes.cpp @@ -77,7 +77,6 @@ void M_LoadEmotes(UINT16 wadnum) continue; } - // This first->second looks kind of weird i know emote = &emotes[emote_name]; emote->timeperframe = 1; From 74bdabc636afa93283ccfa98904f2ba33d97b39a Mon Sep 17 00:00:00 2001 From: Indev Date: Sat, 28 Jun 2025 22:31:33 +0300 Subject: [PATCH 04/12] Load emotes from base wads too at boot (so it loads ones from -file wads) --- src/d_main.c | 3 +++ src/m_emotes.cpp | 7 +++++++ src/m_emotes.h | 3 +++ 3 files changed, 13 insertions(+) diff --git a/src/d_main.c b/src/d_main.c index ecd7e6247..fbc2c6c03 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -40,6 +40,7 @@ #include "g_game.h" #include "g_input.h" #include "hu_stuff.h" +#include "m_emotes.h" #include "i_sound.h" #include "i_system.h" #include "i_time.h" @@ -1997,6 +1998,8 @@ void D_SRB2Main(void) S_InitMusicDefs(); + M_InitEmotes(); + CONS_Printf("ST_Init(): Init status bar.\n"); ST_Init(); diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp index 50718b79b..4c28f72b4 100644 --- a/src/m_emotes.cpp +++ b/src/m_emotes.cpp @@ -163,6 +163,13 @@ void M_LoadEmotes(UINT16 wadnum) CONS_Printf("Added %d emotes\n", numemotes); } +void M_InitEmotes(void) +{ + UINT16 i; + for (i = 0; i < numwadfiles; i++) + M_LoadEmotes(i); +} + emote_t *M_FindEmote(const char *name, int skip) { if (!cv_emotes.value) diff --git a/src/m_emotes.h b/src/m_emotes.h index f3e1875ce..0290e3909 100644 --- a/src/m_emotes.h +++ b/src/m_emotes.h @@ -22,6 +22,9 @@ typedef struct emote_s { // Reads and adds emotes from EMOTES lump, with syntax similar to soc void M_LoadEmotes(UINT16 wadnum); +// Load emotes from all base wads +void M_InitEmotes(void); + // Finds first matching emote, ignoring first few matches emote_t *M_FindEmote(const char *name, int skips); From da24ba20c7ff30ff3c61897701d4ffdf893ccf4a Mon Sep 17 00:00:00 2001 From: Indev Date: Sat, 28 Jun 2025 22:39:44 +0300 Subject: [PATCH 05/12] Fix emotes having not enough 'background' rendered :chonkbunckle: --- src/hu_stuff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hu_stuff.c b/src/hu_stuff.c index b1d0cd64a..aea2a2310 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -1643,7 +1643,7 @@ static void HU_drawMiniChat(void) else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) { if (cv_chatbacktint.value) // on request of wolfy - V_DrawFillConsoleMap(x + dx + 2, y+dy, charwidth, charheight, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); + V_DrawFillConsoleMap(x + dx + 2, y+dy, HU_EmoteWidth(emote), charheight, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT|transflag) - charwidth; From e2336357836da65ad26f7062349f16bedf7bf317 Mon Sep 17 00:00:00 2001 From: Indev Date: Sun, 29 Jun 2025 00:05:25 +0300 Subject: [PATCH 06/12] huh? --- src/hu_stuff.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hu_stuff.c b/src/hu_stuff.c index aea2a2310..e125a7b7e 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -1643,7 +1643,7 @@ static void HU_drawMiniChat(void) else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) { if (cv_chatbacktint.value) // on request of wolfy - V_DrawFillConsoleMap(x + dx + 2, y+dy, HU_EmoteWidth(emote), charheight, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); + V_DrawFillConsoleMap(x + dx + 2, y+dy, HU_EmoteWidth(emote)-charwidth/2, charheight, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT|transflag) - charwidth; From 9bd4a933cf26dc1731022bf067961af67d5918b0 Mon Sep 17 00:00:00 2001 From: Indev Date: Sun, 29 Jun 2025 00:30:24 +0300 Subject: [PATCH 07/12] Check if text is inside chat log box for emotes --- src/hu_stuff.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hu_stuff.c b/src/hu_stuff.c index e125a7b7e..6e3064098 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -1752,7 +1752,8 @@ static void HU_drawChatLog(INT32 offset) } else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) { - dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT) - charwidth; + if ((y+dy+2 >= chat_topy) && (y+dy < (chat_bottomy))) + dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT) - charwidth; j += emotelen; } else From 102e944fd5b8419b1ac425621309058cca17ce3b Mon Sep 17 00:00:00 2001 From: Indev Date: Mon, 30 Jun 2025 00:28:14 +0300 Subject: [PATCH 08/12] Preview emotes in chat input --- src/hu_stuff.c | 96 ++++++++++++++++++------------------------- src/m_emotes.cpp | 36 +++++++++++++++- src/m_emotes.h | 6 +++ src/m_textinput.c | 102 +++++++++++++++++++++++++++++++++++++++++++--- src/m_textinput.h | 3 ++ 5 files changed, 179 insertions(+), 64 deletions(-) diff --git a/src/hu_stuff.c b/src/hu_stuff.c index 6e3064098..d2b15e9e4 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -1369,7 +1369,7 @@ boolean HU_Responder(event_t *ev) && ev->data1 != gamecontrol[0][gc_talkkey][1])) return false; - M_TextInputHandle(&w_chat, c); + M_TextInputHandleEmotes(&w_chat, c); if (c == KEY_ENTER) { @@ -1409,39 +1409,7 @@ boolean HU_Responder(event_t *ev) #ifndef NONET -static INT32 HU_DrawEmote(INT32 x, INT32 y, emote_t *emote, INT32 flags) -{ - if (emote->numframes == 0) - return 4; // charwidth - - const char *lumpname = emote->frames[(hu_emoteanim/emote->timeperframe) % emote->numframes]; - patch_t *emotepatch = W_CachePatchName(lumpname, PU_CACHE); - - int emotewidth = min(6, emotepatch->width); - int charheight = 6; - - fixed_t scale = (FRACUNIT/emotepatch->width)*emotewidth; - - x *= FRACUNIT; - y *= FRACUNIT; - - y -= (scale*emotepatch->height-charheight*FRACUNIT)/2; - - V_DrawFixedPatch(x, y, scale, flags, emotepatch, NULL); - - return emotewidth; -} - -static INT32 HU_EmoteWidth(emote_t *emote) -{ - if (emote->numframes == 0) - return 4; // charwidth - - const char *lumpname = emote->frames[(hu_emoteanim/emote->timeperframe) % emote->numframes]; - patch_t *emotepatch = W_CachePatchName(lumpname, PU_CACHE); - - return min(8, emotepatch->width); -} +#define HU_DrawEmote(x, y, emote, flags) M_DrawEmote((x), (y), (emote), hu_emoteanim, (flags)) // Precompile a wordwrapped string to any given width. // This is a muuuch better method than V_WORDWRAP. @@ -1477,10 +1445,10 @@ static char *CHAT_WordWrap(INT32 x, INT32 w, INT32 option, const char *string) c = toupper(c); c -= HU_FONTSTART; - if (string[i] == ':' && (emote = M_VerifyEmote(string+i, &emotelen))) + if ((emote = M_VerifyEmote(string+i, &emotelen))) { - chw = HU_EmoteWidth(emote); - i += emotelen-1; + chw = EMOTEWIDTH; + i += emotelen-1; // Will be incremented on next loop iteration, hence -1 } else if (c < 0 || c >= HU_FONTSIZE || !hu_font[c]) { @@ -1562,9 +1530,9 @@ static void HU_drawMiniChat(void) ++j; } - else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) + else if ((emote = M_VerifyEmote(msg+j, &emotelen))) { - dx += HU_EmoteWidth(emote) - charwidth; + dx += EMOTEWIDTH - charwidth; j += emotelen; } else @@ -1640,12 +1608,13 @@ static void HU_drawMiniChat(void) ++j; } - else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) + else if ((emote = M_VerifyEmote(msg+j, &emotelen))) { if (cv_chatbacktint.value) // on request of wolfy - V_DrawFillConsoleMap(x + dx + 2, y+dy, HU_EmoteWidth(emote)-charwidth/2, charheight, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); + V_DrawFillConsoleMap(x + dx + 2, y+dy, EMOTEWIDTH, charheight, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); - dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT|transflag) - charwidth; + HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT|transflag); + dx += EMOTEWIDTH - charwidth; j += emotelen; } @@ -1750,10 +1719,13 @@ static void HU_drawChatLog(INT32 offset) ++j; } - else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) + else if ((emote = M_VerifyEmote(msg+j, &emotelen))) { if ((y+dy+2 >= chat_topy) && (y+dy < (chat_bottomy))) - dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT) - charwidth; + { + HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT); + dx += EMOTEWIDTH - charwidth; + } j += emotelen; } else @@ -1881,7 +1853,7 @@ static void HU_DrawChat(void) typelines = 1; if ((w_chat.cursor == 0 || w_chat.length == 0) && hu_tick < 4) - V_DrawChatCharacter(chatx+2+c+charwidth*w_chat.cursor, y+1, '_'|V_SNAPTOBOTTOM|V_SNAPTOLEFT|t, !cv_allcaps.value, NULL); + V_DrawChatCharacter(chatx+2+c, y+1, '_'|V_SNAPTOBOTTOM|V_SNAPTOLEFT|t, !cv_allcaps.value, NULL); if (w_chat.select != w_chat.cursor) { @@ -1892,30 +1864,40 @@ static void HU_DrawChat(void) while (w_chat_buf[i]) { boolean skippedline = false; - if (w_chat.cursor == (i+1)) + emote_t *emote = NULL; + int emotelen = 0; + int drawwidth = charwidth; // if we've drawn emote, selection needs to account for that + + if ((emote = M_VerifyEmote(w_chat_buf+i, &emotelen))) + { + HU_DrawEmote(chatx + c + 2, y-1, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT|t); + c += EMOTEWIDTH - charwidth; + i += emotelen-1; + drawwidth = EMOTEWIDTH; + } + else if (w_chat_buf[i] >= HU_FONTSTART) //Hurdler: isn't it better like that? + V_DrawChatCharacter(chatx + c + 2, y, w_chat_buf[i] | V_SNAPTOBOTTOM|V_SNAPTOLEFT | t, !cv_allcaps.value, NULL); + + // Draw selection + if (i >= select_start && i < select_end) + V_DrawFill(chatx + c + 2 - (drawwidth-charwidth), y-1, drawwidth, charheight, 103|V_TRANSLUCENT|V_SNAPTOBOTTOM|V_SNAPTOLEFT|t); + + ++i; + + if (w_chat.cursor == i) { INT32 cursorx = (c+charwidth < boxw-charwidth) ? (chatx + 2 + c+charwidth) : (chatx+1); // we may have to go down. INT32 cursory = (cursorx != chatx+1) ? (y) : (y+charheight); if (hu_tick < 4) V_DrawChatCharacter(cursorx, cursory+1, '_' |V_SNAPTOBOTTOM|V_SNAPTOLEFT|t, !cv_allcaps.value, NULL); - if (cursorx == chatx+1 && saylen == i) // a weirdo hack + if (cursorx == chatx+1 && saylen == i-1) // a weirdo hack { typelines += 1; skippedline = true; } } - //Hurdler: isn't it better like that? - if (w_chat_buf[i] >= HU_FONTSTART) - V_DrawChatCharacter(chatx + c + 2, y, w_chat_buf[i] | V_SNAPTOBOTTOM|V_SNAPTOLEFT | t, !cv_allcaps.value, NULL); - - // Draw selection - if (i >= select_start && i < select_end) - V_DrawFill(chatx + c + 2, y-1, charwidth, charheight, 103|V_TRANSLUCENT|V_SNAPTOBOTTOM|V_SNAPTOLEFT|t); - - ++i; - c += charwidth; if (c > boxw-(charwidth*2) && !skippedline) { diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp index 4c28f72b4..9cfb1b953 100644 --- a/src/m_emotes.cpp +++ b/src/m_emotes.cpp @@ -7,9 +7,13 @@ extern "C" { #include "w_wad.h" -} - #include "z_zone.h" +#include "hu_stuff.h" + +// Goddammit +#define restrict +#include "v_video.h" +} consvar_t cv_emotes = {"emotes", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL};; @@ -197,6 +201,9 @@ emote_t *M_FindEmote(const char *name, int skip) emote_t *M_VerifyEmote(const char *name, int *emotelen) { + if (*name != ':') + return nullptr; + if (!cv_emotes.value) return nullptr; @@ -224,3 +231,28 @@ emote_t *M_VerifyEmote(const char *name, int *emotelen) return match; } + +void M_DrawEmote(INT32 x, INT32 y, emote_t *emote, tic_t anim, INT32 flags) +{ + if (emote->numframes == 0) + return; + + const char *lumpname = emote->frames[(anim/emote->timeperframe) % emote->numframes]; + patch_t *emotepatch = (patch_t*)W_CachePatchName(lumpname, PU_CACHE); + + const int CHARHEIGHT = 6; + + fixed_t scale = FRACUNIT; + + x *= FRACUNIT; + y *= FRACUNIT; + + if (emotepatch->width > EMOTEWIDTH) + scale = (FRACUNIT/emotepatch->width)*EMOTEWIDTH; + else if (emotepatch->width < EMOTEWIDTH) + x += (EMOTEWIDTH-emotepatch->width)*FRACUNIT/2; + + y -= (scale*emotepatch->height-CHARHEIGHT*FRACUNIT)/2; + + V_DrawFixedPatch(x, y, scale, flags, emotepatch, NULL); +} diff --git a/src/m_emotes.h b/src/m_emotes.h index 0290e3909..bfce68142 100644 --- a/src/m_emotes.h +++ b/src/m_emotes.h @@ -12,6 +12,7 @@ extern consvar_t cv_emotes; #define MAXEMOTENAME 32 #define MAXEMOTEFRAMES 32 +#define EMOTEWIDTH 6 typedef struct emote_s { char frames[MAXEMOTEFRAMES][9]; @@ -28,12 +29,17 @@ void M_InitEmotes(void); // Finds first matching emote, ignoring first few matches emote_t *M_FindEmote(const char *name, int skips); +// If first character is not ':', instantly returns null // Starts from ':' character, goes until it finds ':' or end of string // If end of string is found, or closing ':' is found but resulting emote name doesn't exist, // it returns null, otherwise it returns emote and stores into *emotelen how much characters // needs to be skipped to get past emote name emote_t *M_VerifyEmote(const char *name, int *emotelen); +// Draw the emote, anim should be some kind of timer ticking every game tic +// for animated emotes +void M_DrawEmote(INT32 x, INT32 y, emote_t *emote, tic_t anim, INT32 flags); + #ifdef __cplusplus } // extern "C" #endif diff --git a/src/m_textinput.c b/src/m_textinput.c index dfe9f5a64..18801eec2 100644 --- a/src/m_textinput.c +++ b/src/m_textinput.c @@ -1,5 +1,6 @@ #include "m_textinput.h" #include "m_menu.h" // MAXSTRINGLENGTH +#include "m_emotes.h" // M_EmoteV_SubStringWidth #include "v_video.h" #include "r_main.h" // renderisnewtic #include "i_system.h" @@ -130,6 +131,82 @@ static void M_TextInputPaste(textinput_t *input) M_TextInputAddString(input, paste); } +static void M_TextInputLeft(textinput_t *input, boolean emotes) +{ + // Just do it simple way if possible + if (!emotes || input->cursor < 2 || input->buffer[input->cursor-1] != ':') + { + if (input->cursor != 0) + --input->cursor; + return; + } + + // Otherwise we need to check if we have to skip emote + size_t start = input->cursor-2; + + while (input->buffer[start] != ':') + { + // We reached start of string and didn't find ':' symbol, thats definitely not an emote + // so just do it simple way + if (start == 0) + { + M_TextInputLeft(input, false); + return; + } + + --start; + } + + int emotelen = 0; + if (M_VerifyEmote(input->buffer+start, &emotelen)) + input->cursor -= emotelen; // also skip the : + else + M_TextInputLeft(input, false); // Not a valid emote, do the simple thing +} + +static void M_TextInputRight(textinput_t *input, boolean emotes) +{ + // Just do it simple way if possible + if (!emotes || input->cursor == input->length || input->buffer[input->cursor] != ':') + { + if (input->cursor < input->length) + ++input->cursor; + return; + } + + int emotelen = 0; + if (M_VerifyEmote(input->buffer+input->cursor, &emotelen)) + input->cursor += emotelen; // Also skip the : + else + M_TextInputRight(input, false); // Not a valid emote, do the simple thing +} + +// Check if we're inside a valid emote +static boolean M_TextInputCheckEmote(textinput_t *input) +{ + int start = input->cursor; + + // Definitely not an emote + if (start == 0) + return false; + + // Might be closing :, need to skip it just in case + if (input->buffer[start] == ':') + --start; + + while (input->buffer[start] != ':') + { + // No ':' found, definitely not inside emote + if (start == 0) + return false; + + --start; + } + + // Found what may be emote start, now we can check + return M_VerifyEmote(input->buffer+start, NULL) != NULL; +} + void M_TextInputInit(textinput_t *input, char *buffer, size_t buffer_size) { input->buffer = buffer; @@ -156,7 +233,7 @@ void M_TextInputSetString(textinput_t *input, const char *c) input->cursor = input->select = input->length = strlen(c); } -boolean M_TextInputHandle(textinput_t *input, INT32 key) +static boolean M_TextInputHandleBase(textinput_t *input, INT32 key, boolean emotes) { if (key == KEY_LSHIFT || key == KEY_RSHIFT || key == KEY_LCTRL || key == KEY_RCTRL @@ -241,16 +318,14 @@ boolean M_TextInputHandle(textinput_t *input, INT32 key) if (key == KEY_LEFTARROW) { - if (input->cursor != 0) - --input->cursor; + M_TextInputLeft(input, emotes); if (!shiftdown) input->select = input->cursor; return true; } else if (key == KEY_RIGHTARROW) { - if (input->cursor < input->length) - ++input->cursor; + M_TextInputRight(input, emotes); if (!shiftdown) input->select = input->cursor; return true; @@ -320,6 +395,23 @@ boolean M_TextInputHandle(textinput_t *input, INT32 key) return true; } +// Just an alias, more or less +boolean M_TextInputHandle(textinput_t *input, INT32 key) +{ + return M_TextInputHandleBase(input, key, false); +} + +boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key) +{ + boolean ret = M_TextInputHandleBase(input, key, true); + + // After this handled key we ended up inside an emote, lets fix that + if (M_TextInputCheckEmote(input)) + M_TextInputToWordEnd(input, !shiftdown); + + return ret; +} + void M_DrawTextInputScroll(INT32 x, INT32 y, textinput_t *input, INT32 flags, INT32 MAXINPUTWIDTH) { if (renderisnewtic) diff --git a/src/m_textinput.h b/src/m_textinput.h index 649288ec4..20321cc3b 100644 --- a/src/m_textinput.h +++ b/src/m_textinput.h @@ -22,6 +22,9 @@ void M_TextInputSetString(textinput_t *input, const char *c); boolean M_TextInputHandle(textinput_t *input, INT32 key); +// Does everything same as M_TextInputHandle, but also handles emotes in input string +boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key); + #define M_DrawTextInput(x, y, input, flags) M_DrawTextInputScroll((x), (y), (input), (flags), ((input)->buffer_size-1)*8) void M_DrawTextInputScroll(INT32 x, INT32 y, textinput_t *input, INT32 flags, INT32 MAXINPUTWIDTH); From 78ab90c1a685a5d567b497b651f6ada3915afe90 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 30 Jun 2025 08:25:34 +0200 Subject: [PATCH 09/12] "restrict" macro for c++ since it does not have it, use __restrict instead --- src/doomdef.h | 8 ++++++++ src/m_emotes.cpp | 1 - src/r_main.h | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/doomdef.h b/src/doomdef.h index f9467199b..15993b4bb 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -560,6 +560,14 @@ UINT32 quickncasehash (const char *p, size_t n) #define UNLIKELY(x) (x) #endif +#ifdef __cplusplus +#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER) + #define restrict __restrict +#else + #define restrict +#endif +#endif + // An assert-type mechanism. #ifdef PARANOIA #define I_Assert(e) ((e) ? (void)0 : I_Error("assert failed: %s, file %s, line %d", #e, __FILE__, __LINE__)) diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp index 9cfb1b953..90c7e22f3 100644 --- a/src/m_emotes.cpp +++ b/src/m_emotes.cpp @@ -11,7 +11,6 @@ extern "C" { #include "hu_stuff.h" // Goddammit -#define restrict #include "v_video.h" } diff --git a/src/r_main.h b/src/r_main.h index d3284764d..e1ca0c5fc 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -84,7 +84,7 @@ extern lighttable_t *zlight[LIGHTLEVELS][MAXLIGHTZ]; // // killough 5/2/98: reformatted // -FUNCINLINE static ATTRINLINE PUREFUNC INT32 R_PointOnSide(fixed_t x, fixed_t y, const node_t *restrict node) +FUNCINLINE static ATTRINLINE PUREFUNC INT32 R_PointOnSide(fixed_t x, fixed_t y, const node_t * restrict node) { if (!node->dx) return x <= node->x ? node->dy > 0 : node->dy < 0; From d2bab80b29a252904e5de7d98255988292a26a73 Mon Sep 17 00:00:00 2001 From: Indev Date: Mon, 30 Jun 2025 16:43:14 +0300 Subject: [PATCH 10/12] Add emote suggestions and autocompletion --- src/hu_stuff.c | 34 ++++++++++++++++++++- src/m_emotes.cpp | 17 +++++++---- src/m_emotes.h | 3 +- src/m_textinput.c | 76 +++++++++++++++++++++++++++++++++++++++++++++-- src/m_textinput.h | 4 ++- 5 files changed, 124 insertions(+), 10 deletions(-) diff --git a/src/hu_stuff.c b/src/hu_stuff.c index d2b15e9e4..0a4727b1e 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -95,6 +95,8 @@ static boolean headsupactive = false; boolean hu_showscores; // draw rankings static char hu_tick; static tic_t hu_emoteanim = 0; +#define MAXEMOTESUGGESTIONS 8 +static emote_t *emote_suggestions[MAXEMOTESUGGESTIONS] = {0}; static huddrawlist_h luahuddrawlist_scores; @@ -1369,7 +1371,7 @@ boolean HU_Responder(event_t *ev) && ev->data1 != gamecontrol[0][gc_talkkey][1])) return false; - M_TextInputHandleEmotes(&w_chat, c); + M_TextInputHandleEmotes(&w_chat, c, emote_suggestions, MAXEMOTESUGGESTIONS); if (c == KEY_ENTER) { @@ -1907,6 +1909,36 @@ static void HU_DrawChat(void) } } + if (emote_suggestions[0]) + { + // A bit of copy-paste from /pm code :p + INT32 suggesty = chaty - charheight - 1; + size_t longest_suggestion_length = 0; + + for (i = 0; i < MAXEMOTESUGGESTIONS && emote_suggestions[i]; ++i) + { + longest_suggestion_length = max(longest_suggestion_length, strlen(emote_suggestions[i]->name)); + } + +#ifdef NETSPLITSCREEN + if (splitscreen) + { + suggesty -= BASEVIDHEIGHT/2; + if (splitscreen > 1) + suggesty += 16; + } + else +#endif + suggesty -= (cv_kartspeedometer.value ? 16 : 0); + + for (i = 0; i < MAXEMOTESUGGESTIONS && emote_suggestions[i]; ++i) + { + V_DrawFillConsoleMap(chatx + boxw + 2, suggesty - (7*i), (longest_suggestion_length+2)*4 + EMOTEWIDTH, 6, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); + V_DrawSmallString(chatx + boxw + 4 + EMOTEWIDTH, suggesty - (7*i), V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_ALLOWLOWERCASE, va(":%s:", emote_suggestions[i]->name)); + HU_DrawEmote(chatx + boxw + 2, suggesty - i*7, emote_suggestions[i], V_SNAPTOBOTTOM|V_SNAPTOLEFT); + } + } + // handle /pm list. It's messy, horrible and I don't care. if (strnicmp(w_chat_buf, "/pm", 3) == 0 && vid.width >= 400 && !teamtalk) // 320x200 unsupported kthxbai { diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp index 90c7e22f3..27c66be14 100644 --- a/src/m_emotes.cpp +++ b/src/m_emotes.cpp @@ -74,13 +74,20 @@ void M_LoadEmotes(UINT16 wadnum) emote_name = line.substr(split+1); - if (emote_name.find_first_of(" \t\n\r\f\v") != emote_name.npos) + if (emote_name.find_first_of(" \t\n\r\f\v:") != emote_name.npos) { - CONS_Alert(CONS_WARNING, "EMOTES: Emote name cannot contain spaces. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + CONS_Alert(CONS_WARNING, "EMOTES: Emote name cannot contain spaces or ':' symbols. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); continue; } + if (emote_name.size() > MAXEMOTENAME) + { + CONS_Alert(CONS_WARNING, "EMOTES: Emote name is too long, truncating. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + emote_name = emote_name.substr(0, MAXEMOTENAME); + } + emote = &emotes[emote_name]; + strlcpy(emote->name, emote_name.c_str(), MAXEMOTENAME); emote->timeperframe = 1; emote->numframes = 0; @@ -173,17 +180,17 @@ void M_InitEmotes(void) M_LoadEmotes(i); } -emote_t *M_FindEmote(const char *name, int skip) +emote_t *M_FindEmote(const char *name, int len, int skip) { if (!cv_emotes.value) return nullptr; char query[MAXEMOTENAME+1] = {0}; - std::strncpy(query, name, MAXEMOTENAME); + std::strncpy(query, name, min(len, MAXEMOTENAME)); for (auto &pair: emotes) { - if (pair.first.rfind(query, 0, MAXEMOTENAME) != 0) + if (pair.first.rfind(query, 0, min(len, MAXEMOTENAME)) != 0) continue; if (skip > 0) diff --git a/src/m_emotes.h b/src/m_emotes.h index bfce68142..783c6ec17 100644 --- a/src/m_emotes.h +++ b/src/m_emotes.h @@ -15,6 +15,7 @@ extern consvar_t cv_emotes; #define EMOTEWIDTH 6 typedef struct emote_s { + char name[MAXEMOTENAME+1]; char frames[MAXEMOTEFRAMES][9]; UINT8 numframes; tic_t timeperframe; @@ -27,7 +28,7 @@ void M_LoadEmotes(UINT16 wadnum); void M_InitEmotes(void); // Finds first matching emote, ignoring first few matches -emote_t *M_FindEmote(const char *name, int skips); +emote_t *M_FindEmote(const char *name, int len, int skips); // If first character is not ':', instantly returns null // Starts from ':' character, goes until it finds ':' or end of string diff --git a/src/m_textinput.c b/src/m_textinput.c index 18801eec2..353c8122b 100644 --- a/src/m_textinput.c +++ b/src/m_textinput.c @@ -1,6 +1,5 @@ #include "m_textinput.h" #include "m_menu.h" // MAXSTRINGLENGTH -#include "m_emotes.h" // M_EmoteV_SubStringWidth #include "v_video.h" #include "r_main.h" // renderisnewtic #include "i_system.h" @@ -401,7 +400,7 @@ boolean M_TextInputHandle(textinput_t *input, INT32 key) return M_TextInputHandleBase(input, key, false); } -boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key) +boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key, emote_t *suggestions[], int maxsuggestions) { boolean ret = M_TextInputHandleBase(input, key, true); @@ -409,6 +408,79 @@ boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key) if (M_TextInputCheckEmote(input)) M_TextInputToWordEnd(input, !shiftdown); + // Always clear the first entry + suggestions[0] = NULL; + + // For autocomplete + int emotestart = 0; + + // Try find suggestions for emote names + for (int i = input->cursor-1; i >= 0 && input->cursor-i <= MAXEMOTENAME; --i) + { + // Space can't be part of emote name + if (isspace(input->buffer[i])) + break; + + // Found a :, try suggest emotes + if (input->buffer[i] == ':') + { + emotestart = i+1; + // ...But only if we typed at least something + if ((int)input->cursor-(i-1) < 3) + break; + + for (int skip = 0; skip < maxsuggestions; ++skip) + { + suggestions[skip] = M_FindEmote(input->buffer+i+1, input->cursor-i-1, skip); + + // No more suggestions + if (!suggestions[skip]) + break; + } + + // In any case, we found what we wanted, can exit the loop now + break; + } + } + + // If we suggest emotes, try autocomplete + if (key == '\t' && suggestions[0]) + { + int pos = (input->cursor-emotestart); + boolean autocomplete = true; + + while (autocomplete) + { + // Check if current character matches for all suggestions + char c = suggestions[0]->name[pos]; + + // End of string reached + if (!c) + break; + + for (int i = 1; i < maxsuggestions && suggestions[i]; ++i) + { + if (suggestions[i]->name[pos] != c) + { + autocomplete = false; + break; + } + } + + ++pos; + + if (autocomplete) + M_TextInputAddChar(input, c); + } + + // This was the only suggestion, finish autocomplete with a ':' and clear suggestions + if (maxsuggestions == 1 || !suggestions[1]) + { + M_TextInputAddChar(input, ':'); + suggestions[0] = NULL; + } + } + return ret; } diff --git a/src/m_textinput.h b/src/m_textinput.h index 20321cc3b..1cc1c5005 100644 --- a/src/m_textinput.h +++ b/src/m_textinput.h @@ -2,6 +2,7 @@ #define __M_TEXTINPUT__ #include "doomtype.h" +#include "m_emotes.h" // M_VerifyEmote, M_FindEmote typedef struct textinput_s { size_t cursor; @@ -23,7 +24,8 @@ void M_TextInputSetString(textinput_t *input, const char *c); boolean M_TextInputHandle(textinput_t *input, INT32 key); // Does everything same as M_TextInputHandle, but also handles emotes in input string -boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key); +// If currently typing something that looks like an existing emote, return suggestions for it +boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key, emote_t *suggestions[], int maxsuggestions); #define M_DrawTextInput(x, y, input, flags) M_DrawTextInputScroll((x), (y), (input), (flags), ((input)->buffer_size-1)*8) void M_DrawTextInputScroll(INT32 x, INT32 y, textinput_t *input, INT32 flags, INT32 MAXINPUTWIDTH); From fca7b4a837b80ee62924dc6e4cc10e42596dac40 Mon Sep 17 00:00:00 2001 From: Indev Date: Tue, 1 Jul 2025 15:15:36 +0300 Subject: [PATCH 11/12] Use std::min --- src/m_emotes.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp index 27c66be14..052fbfec4 100644 --- a/src/m_emotes.cpp +++ b/src/m_emotes.cpp @@ -9,11 +9,13 @@ extern "C" { #include "w_wad.h" #include "z_zone.h" #include "hu_stuff.h" - -// Goddammit #include "v_video.h" } +// No thanks +#undef min +#undef max + consvar_t cv_emotes = {"emotes", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL};; static std::map emotes; @@ -186,11 +188,11 @@ emote_t *M_FindEmote(const char *name, int len, int skip) return nullptr; char query[MAXEMOTENAME+1] = {0}; - std::strncpy(query, name, min(len, MAXEMOTENAME)); + std::strncpy(query, name, std::min(len, MAXEMOTENAME)); for (auto &pair: emotes) { - if (pair.first.rfind(query, 0, min(len, MAXEMOTENAME)) != 0) + if (pair.first.rfind(query, 0, std::min(len, MAXEMOTENAME)) != 0) continue; if (skip > 0) From 5d8bb3223f5d861d0245ee10e84a693707095ef4 Mon Sep 17 00:00:00 2001 From: Indev Date: Tue, 1 Jul 2025 15:25:48 +0300 Subject: [PATCH 12/12] No need for this now --- src/m_emotes.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp index 052fbfec4..facc7fb54 100644 --- a/src/m_emotes.cpp +++ b/src/m_emotes.cpp @@ -12,10 +12,6 @@ extern "C" { #include "v_video.h" } -// No thanks -#undef min -#undef max - consvar_t cv_emotes = {"emotes", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL};; static std::map emotes;