From cce42d41def9140d5154230b72b73f22d334d2d5 Mon Sep 17 00:00:00 2001 From: Jupeyy Date: Sun, 13 Mar 2022 19:09:33 +0100 Subject: [PATCH] Auto select search string on hot key --- src/game/client/components/menus.cpp | 4 +++- src/game/client/components/menus_browser.cpp | 7 +++++- src/game/client/components/menus_ingame.cpp | 6 ++++- src/game/client/components/menus_settings.cpp | 20 ++++++++++++---- .../components/menus_settings_assets.cpp | 7 +++++- src/game/client/ui.h | 2 -- src/game/client/ui_ex.cpp | 24 +++++++++++-------- src/game/client/ui_ex.h | 10 ++++++-- 8 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/game/client/components/menus.cpp b/src/game/client/components/menus.cpp index f520329cf..86650cff6 100644 --- a/src/game/client/components/menus.cpp +++ b/src/game/client/components/menus.cpp @@ -2196,7 +2196,9 @@ int CMenus::Render() TextBox.VSplitRight(60.0f, &TextBox, 0); UI()->DoLabel(&Label, Localize("Nickname"), 16.0f, TEXTALIGN_LEFT); static float s_Offset = 0.0f; - UIEx()->DoEditBox(&g_Config.m_PlayerName, &TextBox, g_Config.m_PlayerName, sizeof(g_Config.m_PlayerName), 12.0f, &s_Offset, false, CUI::CORNER_ALL, Client()->PlayerName()); + SUIExEditBoxProperties EditProps; + EditProps.m_pEmptyText = Client()->PlayerName(); + UIEx()->DoEditBox(&g_Config.m_PlayerName, &TextBox, g_Config.m_PlayerName, sizeof(g_Config.m_PlayerName), 12.0f, &s_Offset, false, CUI::CORNER_ALL, EditProps); } else if(m_Popup == POPUP_POINTS) { diff --git a/src/game/client/components/menus_browser.cpp b/src/game/client/components/menus_browser.cpp index fb3501b0a..edcdfda82 100644 --- a/src/game/client/components/menus_browser.cpp +++ b/src/game/client/components/menus_browser.cpp @@ -532,11 +532,16 @@ void CMenus::RenderServerbrowserServerList(CUIRect View) QuickSearch.VSplitLeft(SearchExcludeAddrStrMax, 0, &QuickSearch); QuickSearch.VSplitLeft(5.0f, 0, &QuickSearch); + SUIExEditBoxProperties EditProps; if(Input()->KeyPress(KEY_F) && Input()->ModifierIsPressed()) + { UI()->SetActiveItem(&g_Config.m_BrFilterString); + + EditProps.m_SelectText = true; + } static int s_ClearButton = 0; static float s_Offset = 0.0f; - if(UIEx()->DoClearableEditBox(&g_Config.m_BrFilterString, &s_ClearButton, &QuickSearch, g_Config.m_BrFilterString, sizeof(g_Config.m_BrFilterString), 12.0f, &s_Offset, false, CUI::CORNER_ALL)) + if(UIEx()->DoClearableEditBox(&g_Config.m_BrFilterString, &s_ClearButton, &QuickSearch, g_Config.m_BrFilterString, sizeof(g_Config.m_BrFilterString), 12.0f, &s_Offset, false, CUI::CORNER_ALL, EditProps)) Client()->ServerBrowserUpdate(); } diff --git a/src/game/client/components/menus_ingame.cpp b/src/game/client/components/menus_ingame.cpp index c8262273c..1472c172e 100644 --- a/src/game/client/components/menus_ingame.cpp +++ b/src/game/client/components/menus_ingame.cpp @@ -687,12 +687,16 @@ void CMenus::RenderServerControl(CUIRect MainView) QuickSearch.VSplitLeft(5.0f, 0, &QuickSearch); static int s_ClearButton = 0; static float s_Offset = 0.0f; + + SUIExEditBoxProperties EditProps; if(m_ControlPageOpening || (Input()->KeyPress(KEY_F) && Input()->ModifierIsPressed())) { UI()->SetActiveItem(&m_aFilterString); m_ControlPageOpening = false; + EditProps.m_SelectText = true; } - UIEx()->DoClearableEditBox(&m_aFilterString, &s_ClearButton, &QuickSearch, m_aFilterString, sizeof(m_aFilterString), 14.0f, &s_Offset, false, CUI::CORNER_ALL, Localize("Search")); + EditProps.m_pEmptyText = Localize("Search"); + UIEx()->DoClearableEditBox(&m_aFilterString, &s_ClearButton, &QuickSearch, m_aFilterString, sizeof(m_aFilterString), 14.0f, &s_Offset, false, CUI::CORNER_ALL, EditProps); } Bottom.VSplitRight(120.0f, &Bottom, &Button); diff --git a/src/game/client/components/menus_settings.cpp b/src/game/client/components/menus_settings.cpp index 61fe39e37..7d7cfb984 100644 --- a/src/game/client/components/menus_settings.cpp +++ b/src/game/client/components/menus_settings.cpp @@ -392,7 +392,9 @@ void CMenus::RenderSettingsPlayer(CUIRect MainView) str_format(aBuf, sizeof(aBuf), "%s:", Localize("Name")); UI()->DoLabelScaled(&Label, aBuf, 14.0f, TEXTALIGN_LEFT); static float s_OffsetName = 0.0f; - if(UIEx()->DoEditBox(pName, &Button, pName, sizeof(g_Config.m_PlayerName), 14.0f, &s_OffsetName, false, CUI::CORNER_ALL, pNameFallback)) + SUIExEditBoxProperties EditProps; + EditProps.m_pEmptyText = pNameFallback; + if(UIEx()->DoEditBox(pName, &Button, pName, sizeof(g_Config.m_PlayerName), 14.0f, &s_OffsetName, false, CUI::CORNER_ALL, EditProps)) { SetNeedSendInfo(); } @@ -591,7 +593,9 @@ void CMenus::RenderSettingsTee(CUIRect MainView) static float s_OffsetSkin = 0.0f; static int s_ClearButton = 0; - if(UIEx()->DoClearableEditBox(pSkinName, &s_ClearButton, &Label, pSkinName, sizeof(g_Config.m_ClPlayerSkin), 14.0f, &s_OffsetSkin, false, CUI::CORNER_ALL, "default")) + SUIExEditBoxProperties EditProps; + EditProps.m_pEmptyText = "default"; + if(UIEx()->DoClearableEditBox(pSkinName, &s_ClearButton, &Label, pSkinName, sizeof(g_Config.m_ClPlayerSkin), 14.0f, &s_OffsetSkin, false, CUI::CORNER_ALL, EditProps)) { SetNeedSendInfo(); } @@ -737,9 +741,15 @@ void CMenus::RenderSettingsTee(CUIRect MainView) QuickSearch.VSplitLeft(QuickSearch.w - 15.0f, &QuickSearch, &QuickSearchClearButton); static int s_ClearButton = 0; static float s_Offset = 0.0f; + SUIExEditBoxProperties EditProps; if(Input()->KeyPress(KEY_F) && Input()->ModifierIsPressed()) + { UI()->SetActiveItem(&g_Config.m_ClSkinFilterString); - if(UIEx()->DoClearableEditBox(&g_Config.m_ClSkinFilterString, &s_ClearButton, &QuickSearch, g_Config.m_ClSkinFilterString, sizeof(g_Config.m_ClSkinFilterString), 14.0f, &s_Offset, false, CUI::CORNER_ALL, Localize("Search"))) + + EditProps.m_SelectText = true; + } + EditProps.m_pEmptyText = Localize("Search"); + if(UIEx()->DoClearableEditBox(&g_Config.m_ClSkinFilterString, &s_ClearButton, &QuickSearch, g_Config.m_ClSkinFilterString, sizeof(g_Config.m_ClSkinFilterString), 14.0f, &s_Offset, false, CUI::CORNER_ALL, EditProps)) s_InitSkinlist = true; } @@ -2655,7 +2665,9 @@ void CMenus::RenderSettingsDDNet(CUIRect MainView) UI()->DoLabelScaled(&Label, Localize("Run on join"), 14.0f, TEXTALIGN_LEFT); Right.HSplitTop(20.0f, &Button, &Right); Button.VSplitLeft(5.0f, 0, &Button); - UIEx()->DoEditBox(g_Config.m_ClRunOnJoin, &Button, g_Config.m_ClRunOnJoin, sizeof(g_Config.m_ClRunOnJoin), 14.0f, &s_RunOnJoin, false, CUI::CORNER_ALL, Localize("Chat command (e.g. showall 1)")); + SUIExEditBoxProperties EditProps; + EditProps.m_pEmptyText = Localize("Chat command (e.g. showall 1)"); + UIEx()->DoEditBox(g_Config.m_ClRunOnJoin, &Button, g_Config.m_ClRunOnJoin, sizeof(g_Config.m_ClRunOnJoin), 14.0f, &s_RunOnJoin, false, CUI::CORNER_ALL, EditProps); // Updater #if defined(CONF_AUTOUPDATE) { diff --git a/src/game/client/components/menus_settings_assets.cpp b/src/game/client/components/menus_settings_assets.cpp index 451076c6c..0f2e4a578 100644 --- a/src/game/client/components/menus_settings_assets.cpp +++ b/src/game/client/components/menus_settings_assets.cpp @@ -524,9 +524,14 @@ void CMenus::RenderSettingsCustom(CUIRect MainView) QuickSearch.VSplitLeft(QuickSearch.w - 15.0f, &QuickSearch, &QuickSearchClearButton); static int s_ClearButton = 0; static float s_Offset = 0.0f; + SUIExEditBoxProperties EditProps; if(Input()->KeyPress(KEY_F) && Input()->ModifierIsPressed()) + { UI()->SetActiveItem(&s_aFilterString[s_CurCustomTab]); - if(UIEx()->DoClearableEditBox(&s_aFilterString[s_CurCustomTab], &s_ClearButton, &QuickSearch, s_aFilterString[s_CurCustomTab], sizeof(s_aFilterString[0]), 14.0f, &s_Offset, false, CUI::CORNER_ALL, Localize("Search"))) + EditProps.m_SelectText = true; + } + EditProps.m_pEmptyText = Localize("Search"); + if(UIEx()->DoClearableEditBox(&s_aFilterString[s_CurCustomTab], &s_ClearButton, &QuickSearch, s_aFilterString[s_CurCustomTab], sizeof(s_aFilterString[0]), 14.0f, &s_Offset, false, CUI::CORNER_ALL, EditProps)) s_InitCustomList[s_CurCustomTab] = true; } diff --git a/src/game/client/ui.h b/src/game/client/ui.h index 899dd9228..1e4d3bdab 100644 --- a/src/game/client/ui.h +++ b/src/game/client/ui.h @@ -186,8 +186,6 @@ struct SLabelProperties bool m_StopAtEnd = false; class CTextCursor *m_pSelCursor = nullptr; bool m_EnableWidthCheck = true; - - SLabelProperties() {} }; class CUI diff --git a/src/game/client/ui_ex.cpp b/src/game/client/ui_ex.cpp index 946263ecc..73a036a2a 100644 --- a/src/game/client/ui_ex.cpp +++ b/src/game/client/ui_ex.cpp @@ -212,7 +212,7 @@ float CUIEx::DoScrollbarH(const void *pID, const CUIRect *pRect, float Current, return ReturnValue; } -bool CUIEx::DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden, int Corners, const char *pEmptyText) +bool CUIEx::DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden, int Corners, const SUIExEditBoxProperties &Properties) { int Inside = UI()->MouseInside(pRect); bool ReturnValue = false; @@ -225,6 +225,14 @@ bool CUIEx::DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigne m_pSelItem = m_HasSelection ? pID : nullptr; }; + auto &&SelectAllText = [&]() { + m_CurSelStart = 0; + int StrLen = str_length(pStr); + TextRender()->UTF8OffToDecodedOff(pStr, StrLen, m_CurSelEnd); + SetHasSelection(true); + m_CurCursor = StrLen; + }; + if(UI()->LastActiveItem() == pID) { if(m_HasSelection && m_pSelItem != pID) @@ -322,13 +330,9 @@ bool CUIEx::DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigne Input()->SetClipboardText(pStr); } - if(!IsShiftPressed && IsModPressed && Input()->KeyPress(KEY_A)) + if(Properties.m_SelectText || (!IsShiftPressed && IsModPressed && Input()->KeyPress(KEY_A))) { - m_CurSelStart = 0; - int StrLen = str_length(pStr); - TextRender()->UTF8OffToDecodedOff(pStr, StrLen, m_CurSelEnd); - SetHasSelection(true); - m_CurCursor = StrLen; + SelectAllText(); } if(!IsShiftPressed && IsModPressed && Input()->KeyPress(KEY_U)) @@ -495,7 +499,7 @@ bool CUIEx::DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigne bool IsEmptyText = false; if(pDisplayStr[0] == '\0') { - pDisplayStr = pEmptyText; + pDisplayStr = Properties.m_pEmptyText; IsEmptyText = true; TextRender()->TextColor(1, 1, 1, 0.75f); } @@ -621,12 +625,12 @@ bool CUIEx::DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigne return ReturnValue; } -bool CUIEx::DoClearableEditBox(const void *pID, const void *pClearID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden, int Corners, const char *pEmptyText) +bool CUIEx::DoClearableEditBox(const void *pID, const void *pClearID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden, int Corners, const SUIExEditBoxProperties &Properties) { CUIRect EditBox; CUIRect ClearButton; pRect->VSplitRight(15.0f, &EditBox, &ClearButton); - bool ReturnValue = DoEditBox(pID, &EditBox, pStr, StrSize, FontSize, pOffset, Hidden, Corners & ~CUI::CORNER_R, pEmptyText); + bool ReturnValue = DoEditBox(pID, &EditBox, pStr, StrSize, FontSize, pOffset, Hidden, Corners & ~CUI::CORNER_R, Properties); TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_PIXEL_ALIGMENT); RenderTools()->DrawUIRect(&ClearButton, ColorRGBA(1, 1, 1, 0.33f * UI()->ButtonColorMul(pClearID)), Corners & ~CUI::CORNER_L, 3.0f); diff --git a/src/game/client/ui_ex.h b/src/game/client/ui_ex.h index 882fe7711..092c80a2d 100644 --- a/src/game/client/ui_ex.h +++ b/src/game/client/ui_ex.h @@ -13,6 +13,12 @@ class IGraphics; class CRenderTools; +struct SUIExEditBoxProperties +{ + bool m_SelectText = false; + const char *m_pEmptyText = ""; +}; + class CUIEx { CUI *m_pUI; @@ -58,8 +64,8 @@ public: float DoScrollbarV(const void *pID, const CUIRect *pRect, float Current); float DoScrollbarH(const void *pID, const CUIRect *pRect, float Current, const ColorRGBA *pColorInner = NULL); - bool DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden = false, int Corners = CUI::CORNER_ALL, const char *pEmptyText = ""); - bool DoClearableEditBox(const void *pID, const void *pClearID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden = false, int Corners = CUI::CORNER_ALL, const char *pEmptyText = ""); + bool DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden = false, int Corners = CUI::CORNER_ALL, const SUIExEditBoxProperties &Properties = {}); + bool DoClearableEditBox(const void *pID, const void *pClearID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, float *pOffset, bool Hidden = false, int Corners = CUI::CORNER_ALL, const SUIExEditBoxProperties &Properties = {}); }; #endif