From e70b329614fd4151680d8d7c6803f9e2982a5d2f Mon Sep 17 00:00:00 2001 From: Jan Date: Tue, 21 Sep 2021 22:08:12 +0200 Subject: [PATCH] Add support for applying the autocomplete for fonticons with the enter key - This also closes the fonticon sequence --- src/Components/Modules/TextRenderer.cpp | 40 +++++++++++++++++++++---- src/Components/Modules/TextRenderer.hpp | 22 ++++++++------ 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/Components/Modules/TextRenderer.cpp b/src/Components/Modules/TextRenderer.cpp index 45edde38..595a1944 100644 --- a/src/Components/Modules/TextRenderer.cpp +++ b/src/Components/Modules/TextRenderer.cpp @@ -443,13 +443,15 @@ namespace Components } } - void TextRenderer::AutocompleteFill(const FontIconAutocompleteContext& context, Game::ScreenPlacement* scrPlace, Game::field_t* edit) + void TextRenderer::AutocompleteFill(const FontIconAutocompleteContext& context, Game::ScreenPlacement* scrPlace, Game::field_t* edit, const bool closeFontIcon) { if (context.selectedOffset >= context.resultOffset + context.resultCount) return; const auto selectedResultIndex = context.selectedOffset - context.resultOffset; std::string remainingFillData = context.results[selectedResultIndex].materialName.substr(context.lastQuery.size()); + if (closeFontIcon) + remainingFillData += ":"; const std::string moveData(&edit->buffer[edit->cursor]); const auto remainingBufferCharacters = std::extent_v - edit->cursor - moveData.size() - 1; @@ -480,8 +482,17 @@ namespace Components AutocompleteDown(context); return true; + case Game::K_ENTER: + case Game::K_KP_ENTER: + if(context.resultCount > 0) + { + AutocompleteFill(context, scrPlace, edit, true); + return true; + } + return false; + case Game::K_TAB: - AutocompleteFill(context, scrPlace, edit); + AutocompleteFill(context, scrPlace, edit, false); return true; case Game::K_ESCAPE: @@ -498,10 +509,28 @@ namespace Components } } + bool TextRenderer::HandleFontIconAutocompleteKey(const int localClientNum, const FontIconAutocompleteInstance autocompleteInstance, const int key) + { + assert(autocompleteInstance < FONT_ICON_ACI_COUNT); + if (autocompleteInstance >= FONT_ICON_ACI_COUNT) + return false; + + auto& autocompleteContext = autocompleteContextArray[autocompleteInstance]; + if (!autocompleteContext.autocompleteActive) + return false; + + if(autocompleteInstance == FONT_ICON_ACI_CONSOLE) + return AutocompleteHandleKeyDown(autocompleteContext, key, Game::scrPlaceFull, Game::g_consoleField); + + if(autocompleteInstance == FONT_ICON_ACI_CHAT) + return AutocompleteHandleKeyDown(autocompleteContext, key, &Game::scrPlaceView[localClientNum], &Game::playerKeys[localClientNum].chatField); + + return false; + } + void TextRenderer::Console_Key_Hk(const int localClientNum, const int key) { - auto& autocompleteContext = autocompleteContextArray[FONT_ICON_ACI_CONSOLE]; - if (autocompleteContext.autocompleteActive && AutocompleteHandleKeyDown(autocompleteContext, key, Game::scrPlaceFull, Game::g_consoleField)) + if (HandleFontIconAutocompleteKey(localClientNum, FONT_ICON_ACI_CONSOLE, key)) return; Utils::Hook::Call(0x4311E0)(localClientNum, key); @@ -509,8 +538,7 @@ namespace Components bool TextRenderer::ChatHandleKeyDown(const int localClientNum, const int key) { - auto& autocompleteContext = autocompleteContextArray[FONT_ICON_ACI_CHAT]; - return autocompleteContext.autocompleteActive && AutocompleteHandleKeyDown(autocompleteContext, key, &Game::scrPlaceView[localClientNum], &Game::playerKeys[localClientNum].chatField); + return HandleFontIconAutocompleteKey(localClientNum, FONT_ICON_ACI_CHAT, key); } constexpr auto Message_Key = 0x5A7E50; diff --git a/src/Components/Modules/TextRenderer.hpp b/src/Components/Modules/TextRenderer.hpp index 3073de43..32676c7d 100644 --- a/src/Components/Modules/TextRenderer.hpp +++ b/src/Components/Modules/TextRenderer.hpp @@ -42,6 +42,16 @@ namespace Components class TextRenderer : public Component { + public: + enum FontIconAutocompleteInstance : unsigned + { + FONT_ICON_ACI_CONSOLE, + FONT_ICON_ACI_CHAT, + + FONT_ICON_ACI_COUNT + }; + + private: struct FontIconTableEntry { std::string iconName; @@ -56,14 +66,6 @@ namespace Components unsigned char v; }; - enum FontIconAutocompleteInstance - { - FONT_ICON_ACI_CONSOLE, - FONT_ICON_ACI_CHAT, - - FONT_ICON_ACI_COUNT - }; - class FontIconAutocompleteResult { public: @@ -237,6 +239,8 @@ namespace Components static float GetNormalizedFontIconWidth(const FontIconInfo& fontIcon); static float GetFontIconWidth(const FontIconInfo& fontIcon, const Game::Font_s* font, float xScale); + static bool HandleFontIconAutocompleteKey(int localClientNum, FontIconAutocompleteInstance autocompleteInstance, int key); + TextRenderer(); private: @@ -253,7 +257,7 @@ namespace Components static void AutocompleteUp(FontIconAutocompleteContext& context); static void AutocompleteDown(FontIconAutocompleteContext& context); - static void AutocompleteFill(const FontIconAutocompleteContext& context, Game::ScreenPlacement* scrPlace, Game::field_t* edit); + static void AutocompleteFill(const FontIconAutocompleteContext& context, Game::ScreenPlacement* scrPlace, Game::field_t* edit, bool closeFontIcon); static bool AutocompleteHandleKeyDown(FontIconAutocompleteContext& context, int key, Game::ScreenPlacement* scrPlace, Game::field_t* edit); static void Console_Key_Hk(int localClientNum, int key); static bool ChatHandleKeyDown(int localClientNum, int key);