| 1 | #include "tooltips.h" |
| 2 | |
| 3 | #include <base/time.h> |
| 4 | |
| 5 | #include <engine/textrender.h> |
| 6 | |
| 7 | #include <game/client/ui.h> |
| 8 | |
| 9 | #include <algorithm> |
| 10 | |
| 11 | void CTooltips::Init(CUi *pUi, ITextRender *pTextRender) |
| 12 | { |
| 13 | m_pUi = pUi; |
| 14 | m_pTextRender = pTextRender; |
| 15 | CTooltips::OnReset(); |
| 16 | } |
| 17 | |
| 18 | void CTooltips::OnReset() |
| 19 | { |
| 20 | m_HoverTime = -1; |
| 21 | m_Tooltips.clear(); |
| 22 | ClearActiveTooltip(); |
| 23 | } |
| 24 | |
| 25 | void CTooltips::SetActiveTooltip(CTooltip &Tooltip) |
| 26 | { |
| 27 | m_ActiveTooltip.emplace(args&: Tooltip); |
| 28 | } |
| 29 | |
| 30 | inline void CTooltips::ClearActiveTooltip() |
| 31 | { |
| 32 | m_ActiveTooltip.reset(); |
| 33 | m_PreviousTooltip.reset(); |
| 34 | } |
| 35 | |
| 36 | void CTooltips::DoToolTip(const CTooltip &Tooltip) |
| 37 | { |
| 38 | uintptr_t Id = reinterpret_cast<uintptr_t>(Tooltip.m_pId); |
| 39 | const auto &[Entry, WasInserted] = m_Tooltips.emplace(args&: Id, args: Tooltip); |
| 40 | CTooltip &StoredTooltip = Entry->second; |
| 41 | if(!WasInserted) |
| 42 | { |
| 43 | // update in case of window resize, language change or changed settings |
| 44 | StoredTooltip.m_Rect = Tooltip.m_Rect; |
| 45 | StoredTooltip.m_pText = Tooltip.m_pText; |
| 46 | } |
| 47 | |
| 48 | StoredTooltip.m_OnScreen = true; |
| 49 | |
| 50 | if(Ui()->HotItem() == StoredTooltip.m_pId) |
| 51 | { |
| 52 | SetActiveTooltip(StoredTooltip); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void CTooltips::OnRender() |
| 57 | { |
| 58 | if(m_ActiveTooltip.has_value()) |
| 59 | { |
| 60 | CTooltip &Tooltip = m_ActiveTooltip.value(); |
| 61 | |
| 62 | if(Ui()->HotItem() != Tooltip.m_pId || !Tooltip.m_Rect.Inside(Point: Ui()->MousePos())) |
| 63 | { |
| 64 | Tooltip.m_OnScreen = false; |
| 65 | ClearActiveTooltip(); |
| 66 | return; |
| 67 | } |
| 68 | if(!Tooltip.m_OnScreen) |
| 69 | return; |
| 70 | |
| 71 | // Reset hover time if a different tooltip is active. |
| 72 | // Only reset hover time when rendering, because multiple tooltips can be |
| 73 | // activated in the same frame, but only the last one should be rendered. |
| 74 | if(!m_PreviousTooltip.has_value() || m_PreviousTooltip.value().get().m_pText != Tooltip.m_pText) |
| 75 | m_HoverTime = time_get(); |
| 76 | m_PreviousTooltip.emplace(args&: Tooltip); |
| 77 | |
| 78 | // Delay tooltip until 1 second passed. Start fade-in in the last 0.25 seconds. |
| 79 | constexpr float SecondsBeforeFadeIn = 0.75f; |
| 80 | const float SecondsSinceActivation = (time_get() - m_HoverTime) / (float)time_freq(); |
| 81 | if(SecondsSinceActivation < SecondsBeforeFadeIn) |
| 82 | return; |
| 83 | constexpr float SecondsFadeIn = 0.25f; |
| 84 | const float AlphaFactor = SecondsSinceActivation < SecondsBeforeFadeIn + SecondsFadeIn ? (SecondsSinceActivation - SecondsBeforeFadeIn) / SecondsFadeIn : 1.0f; |
| 85 | |
| 86 | constexpr float Margin = 5.0f; |
| 87 | constexpr float Padding = 5.0f; |
| 88 | |
| 89 | const CUIRect *pScreen = Ui()->Screen(); |
| 90 | // The x-bearing might not be applied without a width, causing 1 px offset |
| 91 | const float LineWidth = Tooltip.m_Truncated && Tooltip.m_WidthHint <= 0.0f ? pScreen->w - 4 * Margin : Tooltip.m_WidthHint; |
| 92 | |
| 93 | const STextBoundingBox BoundingBox = TextRender()->TextBoundingBox(Size: Tooltip.m_FontSize, pText: Tooltip.m_pText, StrLength: -1, LineWidth); |
| 94 | CUIRect Rect; |
| 95 | Rect.w = BoundingBox.m_W + 2 * Padding; |
| 96 | Rect.h = BoundingBox.m_H + 2 * Padding; |
| 97 | |
| 98 | Rect.w = std::min(a: Rect.w, b: pScreen->w - 2 * Margin); |
| 99 | Rect.h = std::min(a: Rect.h, b: pScreen->h - 2 * Margin); |
| 100 | |
| 101 | // On top of the current Rect |
| 102 | if(Tooltip.m_Truncated && Tooltip.m_Rect.x + Rect.w < pScreen->w && Tooltip.m_Rect.y + Rect.h < pScreen->h) |
| 103 | { |
| 104 | Rect.x = Tooltip.m_Rect.x; |
| 105 | Rect.y = Tooltip.m_Rect.y; |
| 106 | |
| 107 | // try to overlay perfectly |
| 108 | Rect.y += (Tooltip.m_Rect.h - Rect.h) / 2.0f; |
| 109 | Rect.x -= Margin; |
| 110 | } |
| 111 | else if(Tooltip.m_Truncated) |
| 112 | { |
| 113 | // the tooltip wants to go offscreen, move it to the left until it matches |
| 114 | float OffscreenRight = std::max(a: Tooltip.m_Rect.x + Rect.w - pScreen->w, b: 0.0f); |
| 115 | float OffscreenBottom = std::max(a: Tooltip.m_Rect.y + Rect.h - pScreen->h, b: 0.0f); |
| 116 | |
| 117 | Rect.x = Tooltip.m_Rect.x; |
| 118 | Rect.y = Tooltip.m_Rect.y; |
| 119 | |
| 120 | Rect.y += (Tooltip.m_Rect.h - Rect.h) / 2.0f; |
| 121 | Rect.x -= Margin; |
| 122 | |
| 123 | Rect.x = std::max(a: 0.0f, b: Rect.x - OffscreenRight); |
| 124 | Rect.y = std::max(a: 0.0f, b: Rect.y - OffscreenBottom); |
| 125 | } |
| 126 | // Try the top side. |
| 127 | else if(Tooltip.m_Rect.y - Rect.h - Margin > pScreen->y) |
| 128 | { |
| 129 | Rect.x = std::clamp(val: Ui()->MouseX() - Rect.w / 2.0f, lo: Margin, hi: pScreen->w - Rect.w - Margin); |
| 130 | Rect.y = Tooltip.m_Rect.y - Rect.h - Margin; |
| 131 | } |
| 132 | // Try the bottom side. |
| 133 | else if(Tooltip.m_Rect.y + Tooltip.m_Rect.h + Margin < pScreen->h) |
| 134 | { |
| 135 | Rect.x = std::clamp(val: Ui()->MouseX() - Rect.w / 2.0f, lo: Margin, hi: pScreen->w - Rect.w - Margin); |
| 136 | Rect.y = Tooltip.m_Rect.y + Tooltip.m_Rect.h + Margin; |
| 137 | } |
| 138 | // Try the right side. |
| 139 | else if(Tooltip.m_Rect.x + Tooltip.m_Rect.w + Margin + Rect.w < pScreen->w) |
| 140 | { |
| 141 | Rect.x = Tooltip.m_Rect.x + Tooltip.m_Rect.w + Margin; |
| 142 | Rect.y = std::clamp(val: Ui()->MouseY() - Rect.h / 2.0f, lo: Margin, hi: pScreen->h - Rect.h - Margin); |
| 143 | } |
| 144 | // Try the left side. |
| 145 | else if(Tooltip.m_Rect.x - Rect.w - Margin > pScreen->x) |
| 146 | { |
| 147 | Rect.x = Tooltip.m_Rect.x - Rect.w - Margin; |
| 148 | Rect.y = std::clamp(val: Ui()->MouseY() - Rect.h / 2.0f, lo: Margin, hi: pScreen->h - Rect.h - Margin); |
| 149 | } |
| 150 | |
| 151 | Rect.Draw(Color: ColorRGBA(0.2f, 0.2f, 0.2f, 0.8f * AlphaFactor), Corners: IGraphics::CORNER_ALL, Rounding: Padding); |
| 152 | Rect.Margin(Cut: Padding, pOtherRect: &Rect); |
| 153 | |
| 154 | CTextCursor Cursor; |
| 155 | Cursor.SetPosition(Rect.TopLeft()); |
| 156 | Cursor.m_FontSize = Tooltip.m_FontSize; |
| 157 | Cursor.m_LineWidth = LineWidth; |
| 158 | |
| 159 | STextContainerIndex TextContainerIndex; |
| 160 | const unsigned OldRenderFlags = TextRender()->GetRenderFlags(); |
| 161 | TextRender()->SetRenderFlags(OldRenderFlags | TEXT_RENDER_FLAG_ONE_TIME_USE); |
| 162 | TextRender()->CreateTextContainer(TextContainerIndex, pCursor: &Cursor, pText: Tooltip.m_pText); |
| 163 | TextRender()->SetRenderFlags(OldRenderFlags); |
| 164 | |
| 165 | if(TextContainerIndex.Valid()) |
| 166 | { |
| 167 | ColorRGBA TextColor = TextRender()->DefaultTextColor(); |
| 168 | TextColor.a *= AlphaFactor; |
| 169 | ColorRGBA OutlineColor = TextRender()->DefaultTextOutlineColor(); |
| 170 | OutlineColor.a *= AlphaFactor; |
| 171 | TextRender()->RenderTextContainer(TextContainerIndex, TextColor, TextOutlineColor: OutlineColor); |
| 172 | } |
| 173 | |
| 174 | TextRender()->DeleteTextContainer(TextContainerIndex); |
| 175 | |
| 176 | Tooltip.m_OnScreen = false; |
| 177 | } |
| 178 | } |
| 179 | |