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);