From 102e944fd5b8419b1ac425621309058cca17ce3b Mon Sep 17 00:00:00 2001 From: Indev Date: Mon, 30 Jun 2025 00:28:14 +0300 Subject: [PATCH 1/3] 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 2/3] "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 3/3] 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);