From a2c9b752e0a3d1d4dac4ceb9830d63281742e63d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 14 Apr 2023 22:39:07 +0200 Subject: [PATCH 1/8] Remove unused `CListBox::FilterMatches` --- src/game/client/ui_listbox.cpp | 6 ------ src/game/client/ui_listbox.h | 1 - 2 files changed, 7 deletions(-) diff --git a/src/game/client/ui_listbox.cpp b/src/game/client/ui_listbox.cpp index c68bc490d..a057b4f01 100644 --- a/src/game/client/ui_listbox.cpp +++ b/src/game/client/ui_listbox.cpp @@ -14,7 +14,6 @@ CListBox::CListBox() { m_ScrollOffset = vec2(0.0f, 0.0f); m_ListBoxUpdateScroll = false; - m_aFilterString[0] = '\0'; m_FilterOffset = 0.0f; m_HasHeader = false; m_AutoSpacing = 0.0f; @@ -215,8 +214,3 @@ int CListBox::DoEnd() } return m_ListBoxNewSelected; } - -bool CListBox::FilterMatches(const char *pNeedle) const -{ - return !m_aFilterString[0] || str_find_nocase(pNeedle, m_aFilterString); -} diff --git a/src/game/client/ui_listbox.h b/src/game/client/ui_listbox.h index f8b26fbd1..176877cec 100644 --- a/src/game/client/ui_listbox.h +++ b/src/game/client/ui_listbox.h @@ -35,7 +35,6 @@ private: float m_AutoSpacing; CScrollRegion m_ScrollRegion; vec2 m_ScrollOffset; - char m_aFilterString[128]; float m_FilterOffset; int m_BackgroundCorners; bool m_HasHeader; From fe40048ca8894055f17d9c5237231a37e35bbe85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 12 Apr 2023 22:34:15 +0200 Subject: [PATCH 2/8] Remove unnecessary `SetRenderFlags` calls Setting the render flags here is unnecessary, as the render flags are already set identically inside `TextEx`. --- src/engine/client/text.cpp | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index d638c9100..66d5da2fb 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -828,11 +828,7 @@ public: CTextCursor Cursor; SetCursor(&Cursor, x, y, Size, TEXTFLAG_RENDER); Cursor.m_LineWidth = LineWidth; - const unsigned OldRenderFlags = m_RenderFlags; - if(LineWidth <= 0) - SetRenderFlags(OldRenderFlags | ETextRenderFlags::TEXT_RENDER_FLAG_NO_FIRST_CHARACTER_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE); TextEx(&Cursor, pText, -1); - SetRenderFlags(OldRenderFlags); } float TextWidth(float Size, const char *pText, int StrLength = -1, float LineWidth = -1.0f, int Flags = 0, float *pHeight = nullptr, float *pAlignedFontSize = nullptr, float *pMaxCharacterHeightInLine = nullptr) override @@ -840,11 +836,7 @@ public: CTextCursor Cursor; SetCursor(&Cursor, 0, 0, Size, Flags); Cursor.m_LineWidth = LineWidth; - const unsigned OldRenderFlags = m_RenderFlags; - if(LineWidth <= 0) - SetRenderFlags(OldRenderFlags | ETextRenderFlags::TEXT_RENDER_FLAG_NO_FIRST_CHARACTER_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE); TextEx(&Cursor, pText, StrLength); - SetRenderFlags(OldRenderFlags); if(pHeight != nullptr) *pHeight = Cursor.Height(); if(pAlignedFontSize != nullptr) @@ -859,11 +851,7 @@ public: CTextCursor Cursor; SetCursor(&Cursor, 0, 0, Size, Flags); Cursor.m_LineWidth = LineWidth; - const unsigned OldRenderFlags = m_RenderFlags; - if(LineWidth <= 0) - SetRenderFlags(OldRenderFlags | ETextRenderFlags::TEXT_RENDER_FLAG_NO_FIRST_CHARACTER_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE); TextEx(&Cursor, pText, StrLength); - SetRenderFlags(OldRenderFlags); return Cursor.BoundingBox(); } @@ -872,11 +860,7 @@ public: CTextCursor Cursor; SetCursor(&Cursor, 0, 0, Size, Flags); Cursor.m_LineWidth = LineWidth; - const unsigned OldRenderFlags = m_RenderFlags; - if(LineWidth <= 0) - SetRenderFlags(OldRenderFlags | ETextRenderFlags::TEXT_RENDER_FLAG_NO_FIRST_CHARACTER_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_LAST_CHARACTER_ADVANCE); TextEx(&Cursor, pText, StrLength); - SetRenderFlags(OldRenderFlags); return vec2(Cursor.m_X, Cursor.m_Y); } From f0c295b2d33d3113c4d7eb28c7ee72da5b9dafad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 12 Apr 2023 22:36:10 +0200 Subject: [PATCH 3/8] Add length check in `AppendTextContainer` Allow `Length` parameter to be longer than maximum string length. --- src/engine/client/text.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index 66d5da2fb..95f56f13d 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -1046,6 +1046,8 @@ public: // string length if(Length < 0) Length = str_length(pText); + else + Length = minimum(Length, str_length(pText)); const float Scale = 1.0f / pSizeData->m_FontSize; From a160364b77e08eed57472c0d4126f0b8574003d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 12 Apr 2023 22:36:52 +0200 Subject: [PATCH 4/8] Add default parameters to `Text` and `TextEx` --- src/engine/client/text.cpp | 4 ++-- src/engine/textrender.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index 95f56f13d..9750f320d 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -823,7 +823,7 @@ public: pCursor->m_Y = y; } - void Text(float x, float y, float Size, const char *pText, float LineWidth) override + void Text(float x, float y, float Size, const char *pText, float LineWidth = -1.0f) override { CTextCursor Cursor; SetCursor(&Cursor, x, y, Size, TEXTFLAG_RENDER); @@ -918,7 +918,7 @@ public: return m_SelectionColor; } - void TextEx(CTextCursor *pCursor, const char *pText, int Length) override + void TextEx(CTextCursor *pCursor, const char *pText, int Length = -1) override { const unsigned OldRenderFlags = m_RenderFlags; m_RenderFlags |= TEXT_RENDER_FLAG_ONE_TIME_USE; diff --git a/src/engine/textrender.h b/src/engine/textrender.h index 523945e2e..dc910b8d2 100644 --- a/src/engine/textrender.h +++ b/src/engine/textrender.h @@ -229,7 +229,7 @@ public: ColorRGBA DefaultSelectionColor() const { return ColorRGBA(0, 0, 1.0f, 1.0f); } // - virtual void TextEx(CTextCursor *pCursor, const char *pText, int Length) = 0; + virtual void TextEx(CTextCursor *pCursor, const char *pText, int Length = -1) = 0; virtual bool CreateTextContainer(int &TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0; virtual void AppendTextContainer(int TextContainerIndex, CTextCursor *pCursor, const char *pText, int Length = -1) = 0; // either creates a new text container or appends to a existing one @@ -262,7 +262,7 @@ public: virtual void TextOutlineColor(ColorRGBA rgb) = 0; virtual void TextSelectionColor(float r, float g, float b, float a) = 0; virtual void TextSelectionColor(ColorRGBA rgb) = 0; - virtual void Text(float x, float y, float Size, const char *pText, float LineWidth) = 0; + virtual void Text(float x, float y, float Size, const char *pText, float LineWidth = -1.0f) = 0; virtual float TextWidth(float Size, const char *pText, int StrLength = -1, float LineWidth = -1.0f, int Flags = 0, float *pHeight = nullptr, float *pAlignedFontSize = nullptr, float *pMaxCharacterHeightInLine = nullptr) = 0; virtual STextBoundingBox TextBoundingBox(float Size, const char *pText, int StrLength = -1, float LineWidth = -1.0f, int Flags = 0) = 0; virtual vec2 CaretPosition(float Size, const char *pText, int StrLength = -1, float LineWidth = -1.0f, int Flags = 0) = 0; From 49a189638986581c7335c84fa1607d3b62cc78f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 12 Apr 2023 22:37:31 +0200 Subject: [PATCH 5/8] Add `STextBoundingBox::Size` convenience function --- src/engine/textrender.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/engine/textrender.h b/src/engine/textrender.h index dc910b8d2..f6efe2d22 100644 --- a/src/engine/textrender.h +++ b/src/engine/textrender.h @@ -153,6 +153,7 @@ struct STextBoundingBox float Right() const { return m_X + m_W; } float Bottom() const { return m_Y + m_H; } + vec2 Size() const { return vec2(m_W, m_H); } }; class CTextCursor From 680de35cabc2ec90a22b099d27f5878c5e740562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Thu, 13 Apr 2023 21:45:12 +0200 Subject: [PATCH 6/8] Add `STextBoundingBox::MoveBy` convenience function --- src/engine/textrender.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/engine/textrender.h b/src/engine/textrender.h index f6efe2d22..4273f667b 100644 --- a/src/engine/textrender.h +++ b/src/engine/textrender.h @@ -154,6 +154,11 @@ struct STextBoundingBox float Right() const { return m_X + m_W; } float Bottom() const { return m_Y + m_H; } vec2 Size() const { return vec2(m_W, m_H); } + void MoveBy(vec2 Offset) + { + m_X += Offset.x; + m_Y += Offset.y; + } }; class CTextCursor From ea058d9d2e43d5ca605213286c975f38e38b8c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Thu, 13 Apr 2023 23:59:06 +0200 Subject: [PATCH 7/8] Replace `CharacterCounter` with existing `pCursor->m_GlyphCount` The separate `CharacterCounter` variable is not necessary, as the same value is already tracked by `pCursor->m_GlyphCount`. --- src/engine/client/text.cpp | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index 9750f320d..51156cda0 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -1084,7 +1084,6 @@ public: } int LineCount = pCursor->m_LineCount; - size_t CharacterCounter = 0; const bool IsRendered = (pCursor->m_Flags & TEXTFLAG_RENDER) != 0; @@ -1112,7 +1111,7 @@ public: { if(CheckInsideChar(CheckOuter, CursorX_, CursorY_, LastCharX, LastCharWidth, CharX, CharWidth, CharY)) { - SelectionChar = CharacterCounter; + SelectionChar = pCursor->m_GlyphCount; SelectionStarted = !SelectionStarted; SelectionUsedCase = true; } @@ -1130,7 +1129,7 @@ public: { if(CheckOutsideChar(CheckOuter, CursorX_, CursorY_, CharX, CharWidth, CharY)) { - SelectionChar = CharacterCounter; + SelectionChar = pCursor->m_GlyphCount; SelectionStarted = !SelectionStarted; SelectionUsedCase = true; } @@ -1235,7 +1234,6 @@ public: if((pCursor->m_Flags & TEXTFLAG_DISALLOW_NEWLINE) == 0) { LastCharGlyphIndex = 0; - ++CharacterCounter; StartNewLine(); if(pCursor->m_MaxLines > 0 && LineCount > pCursor->m_MaxLines) break; @@ -1250,7 +1248,7 @@ public: const SFontSizeChar *pChr = GetChar(TextContainer.m_pFont, pSizeData, Character); if(pChr) { - bool ApplyBearingX = !(((RenderFlags & TEXT_RENDER_FLAG_NO_X_BEARING) != 0) || (CharacterCounter == 0 && (RenderFlags & TEXT_RENDER_FLAG_NO_FIRST_CHARACTER_X_BEARING) != 0)); + bool ApplyBearingX = !(((RenderFlags & TEXT_RENDER_FLAG_NO_X_BEARING) != 0) || (pCursor->m_GlyphCount == 0 && (RenderFlags & TEXT_RENDER_FLAG_NO_FIRST_CHARACTER_X_BEARING) != 0)); float Advance = ((((RenderFlags & TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH) != 0) ? (pChr->m_Width) : (pChr->m_AdvanceX + ((!ApplyBearingX) ? (-pChr->m_OffsetX) : 0.f)))) * Scale * Size; float OutLineRealDiff = (pChr->m_Width - pChr->m_CharWidth) * Scale * Size; @@ -1354,15 +1352,15 @@ public: if(pCursor->m_CursorMode == TEXT_CURSOR_CURSOR_MODE_CALCULATE) { - if(pCursor->m_CursorCharacter == -1 && CheckInsideChar(CharacterCounter == 0, pCursor->m_ReleaseMouseX, pCursor->m_ReleaseMouseY, CharacterCounter == 0 ? std::numeric_limits::lowest() : LastCharX, LastCharWidth, CharX, CharWidth, TmpY)) + if(pCursor->m_CursorCharacter == -1 && CheckInsideChar(pCursor->m_GlyphCount == 0, pCursor->m_ReleaseMouseX, pCursor->m_ReleaseMouseY, pCursor->m_GlyphCount == 0 ? std::numeric_limits::lowest() : LastCharX, LastCharWidth, CharX, CharWidth, TmpY)) { - pCursor->m_CursorCharacter = CharacterCounter; + pCursor->m_CursorCharacter = pCursor->m_GlyphCount; } } if(pCursor->m_CalculateSelectionMode == TEXT_CURSOR_SELECTION_MODE_CALCULATE) { - if(CharacterCounter == 0) + if(pCursor->m_GlyphCount == 0) { CheckSelectionStart(true, pCursor->m_PressMouseX, pCursor->m_PressMouseY, SelectionStartChar, SelectionUsedPress, std::numeric_limits::lowest(), 0, CharX, CharWidth, TmpY); CheckSelectionStart(true, pCursor->m_ReleaseMouseX, pCursor->m_ReleaseMouseY, SelectionEndChar, SelectionUsedRelease, std::numeric_limits::lowest(), 0, CharX, CharWidth, TmpY); @@ -1376,23 +1374,23 @@ public: } if(pCursor->m_CalculateSelectionMode == TEXT_CURSOR_SELECTION_MODE_SET) { - if((int)CharacterCounter == pCursor->m_SelectionStart) + if((int)pCursor->m_GlyphCount == pCursor->m_SelectionStart) { SelectionStarted = !SelectionStarted; - SelectionStartChar = CharacterCounter; + SelectionStartChar = pCursor->m_GlyphCount; SelectionUsedPress = true; } - if((int)CharacterCounter == pCursor->m_SelectionEnd) + if((int)pCursor->m_GlyphCount == pCursor->m_SelectionEnd) { SelectionStarted = !SelectionStarted; - SelectionEndChar = CharacterCounter; + SelectionEndChar = pCursor->m_GlyphCount; SelectionUsedRelease = true; } } if(pCursor->m_CursorMode != TEXT_CURSOR_CURSOR_MODE_NONE) { - if((int)CharacterCounter == pCursor->m_CursorCharacter) + if((int)pCursor->m_GlyphCount == pCursor->m_CursorCharacter) { HasCursor = true; aCursorQuads[0] = IGraphics::CQuadItem(SelX - CursorOuterInnerDiff, DrawY, CursorOuterWidth, Size); @@ -1408,7 +1406,6 @@ public: DrawX += Advance + CharKerning; pCursor->m_GlyphCount++; - ++CharacterCounter; if(SelectionStarted && IsRendered) { @@ -1464,16 +1461,16 @@ public: } else if(pCursor->m_CalculateSelectionMode == TEXT_CURSOR_SELECTION_MODE_SET) { - if((int)CharacterCounter == pCursor->m_SelectionStart) + if((int)pCursor->m_GlyphCount == pCursor->m_SelectionStart) { SelectionStarted = !SelectionStarted; - SelectionStartChar = CharacterCounter; + SelectionStartChar = pCursor->m_GlyphCount; SelectionUsedPress = true; } - if((int)CharacterCounter == pCursor->m_SelectionEnd) + if((int)pCursor->m_GlyphCount == pCursor->m_SelectionEnd) { SelectionStarted = !SelectionStarted; - SelectionEndChar = CharacterCounter; + SelectionEndChar = pCursor->m_GlyphCount; SelectionUsedRelease = true; } } @@ -1482,10 +1479,10 @@ public: { if(pCursor->m_CursorMode == TEXT_CURSOR_CURSOR_MODE_CALCULATE && pCursor->m_CursorCharacter == -1 && CheckOutsideChar(true, pCursor->m_ReleaseMouseX, pCursor->m_ReleaseMouseY, std::numeric_limits::max(), 0, DrawY + Size)) { - pCursor->m_CursorCharacter = CharacterCounter; + pCursor->m_CursorCharacter = pCursor->m_GlyphCount; } - if((int)CharacterCounter == pCursor->m_CursorCharacter) + if((int)pCursor->m_GlyphCount == pCursor->m_CursorCharacter) { HasCursor = true; aCursorQuads[0] = IGraphics::CQuadItem((LastSelX + LastSelWidth) - CursorOuterInnerDiff, DrawY, CursorOuterWidth, Size); From 928d278d0cccb86d1b64d3b568271a6178d02eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 14 Apr 2023 15:03:29 +0200 Subject: [PATCH 8/8] Rename `DefaultSelectionColor` for consistency The other methods also start with `Text`. --- src/engine/client/text.cpp | 2 +- src/engine/textrender.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index 51156cda0..47b0e3b80 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -608,7 +608,7 @@ public: m_Color = DefaultTextColor(); m_OutlineColor = DefaultTextOutlineColor(); - m_SelectionColor = DefaultSelectionColor(); + m_SelectionColor = DefaultTextSelectionColor(); m_pCurFont = nullptr; m_pDefaultFont = nullptr; diff --git a/src/engine/textrender.h b/src/engine/textrender.h index 4273f667b..23ad9d806 100644 --- a/src/engine/textrender.h +++ b/src/engine/textrender.h @@ -232,7 +232,7 @@ public: ColorRGBA DefaultTextColor() const { return ColorRGBA(1, 1, 1, 1); } ColorRGBA DefaultTextOutlineColor() const { return ColorRGBA(0, 0, 0, 0.3f); } - ColorRGBA DefaultSelectionColor() const { return ColorRGBA(0, 0, 1.0f, 1.0f); } + ColorRGBA DefaultTextSelectionColor() const { return ColorRGBA(0, 0, 1.0f, 1.0f); } // virtual void TextEx(CTextCursor *pCursor, const char *pText, int Length = -1) = 0;