From ed09bba45afbfe210703cfafe3e97680333514ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Thu, 18 May 2023 11:18:02 +0200 Subject: [PATCH] Support rendering tooltips with multiple lines of text Render tooltip text correctly with multiple lines when the maximum tooltip width is limited. Add missing padding when rendering multi-line tooltip. Update doxygen documentation. --- src/game/client/components/tooltips.cpp | 19 ++++++------------- src/game/client/components/tooltips.h | 7 +++---- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/game/client/components/tooltips.cpp b/src/game/client/components/tooltips.cpp index 9d9cc1677..423679597 100644 --- a/src/game/client/components/tooltips.cpp +++ b/src/game/client/components/tooltips.cpp @@ -76,19 +76,10 @@ void CTooltips::OnRender() constexpr float Margin = 5.0f; constexpr float Padding = 5.0f; + const STextBoundingBox BoundingBox = TextRender()->TextBoundingBox(FontSize, Tooltip.m_pText, -1, Tooltip.m_WidthHint); CUIRect Rect; - if(Tooltip.m_WidthHint < 0.0f) - { - const STextBoundingBox BoundingBox = TextRender()->TextBoundingBox(FontSize, Tooltip.m_pText); - Rect.w = BoundingBox.m_W + 2 * Padding; - Rect.h = BoundingBox.m_H + 2 * Padding; - } - else - { - const STextBoundingBox BoundingBox = TextRender()->TextBoundingBox(FontSize, Tooltip.m_pText, -1, Tooltip.m_WidthHint); - Rect.w = Tooltip.m_WidthHint; - Rect.h = BoundingBox.m_H; - } + Rect.w = BoundingBox.m_W + 2 * Padding; + Rect.h = BoundingBox.m_H + 2 * Padding; const CUIRect *pScreen = UI()->Screen(); Rect.w = minimum(Rect.w, pScreen->w - 2 * Margin); @@ -121,7 +112,9 @@ void CTooltips::OnRender() Rect.Draw(ColorRGBA(0.2f, 0.2f, 0.2f, 0.8f), IGraphics::CORNER_ALL, Padding); Rect.Margin(Padding, &Rect); - UI()->DoLabel(&Rect, Tooltip.m_pText, FontSize, TEXTALIGN_ML); + SLabelProperties Props; + Props.m_MaxWidth = Tooltip.m_WidthHint; + UI()->DoLabel(&Rect, Tooltip.m_pText, FontSize, TEXTALIGN_ML, Props); Tooltip.m_OnScreen = false; } } diff --git a/src/game/client/components/tooltips.h b/src/game/client/components/tooltips.h index a56ab42d2..84f07defa 100644 --- a/src/game/client/components/tooltips.h +++ b/src/game/client/components/tooltips.h @@ -46,11 +46,10 @@ public: * * On the first call to this function, the data passed is cached, afterwards the calls are used to detect if the tooltip should be activated. * - * For now only works correctly with single line tooltips, since Text width calculation gets broken when there are multiple lines. - * * @param pID The ID of the tooltip. Usually a reference to some g_Config value. - * @param pNearTo Place the tooltip near this rect. - * @param pText The text to display in the tooltip + * @param pNearRect Place the tooltip near this rect. + * @param pText The text to display in the tooltip. + * @param WidthHint The maximum width of the tooltip, or -1.0f for unlimited. */ void DoToolTip(const void *pID, const CUIRect *pNearRect, const char *pText, float WidthHint = -1.0f);