Add alpha fade-in for tooltips

After 0.75 seconds of hovering the tooltip rect without rendering the tooltip, start fading in the tooltip for the remaining 0.25 seconds.
This commit is contained in:
Robert Müller 2023-05-18 12:04:00 +02:00
parent 31304c4573
commit 47fca7683d

View file

@ -67,15 +67,19 @@ void CTooltips::OnRender()
return;
// Reset hover time if a different tooltip is active.
// Only reset hover time when rendering, because multiple tooltips can
// Only reset hover time when rendering, because multiple tooltips can be
// activated in the same frame, but only the last one should be rendered.
if(!m_PreviousTooltip.has_value() || m_PreviousTooltip.value().get().m_pText != Tooltip.m_pText)
m_HoverTime = time_get();
m_PreviousTooltip.emplace(Tooltip);
// Delay tooltip until 1 second passed.
if(m_HoverTime > time_get() - time_freq())
// Delay tooltip until 1 second passed. Start fade-in in the last 0.25 seconds.
constexpr float SecondsBeforeFadeIn = 0.75f;
const float SecondsSinceActivation = (time_get() - m_HoverTime) / (float)time_freq();
if(SecondsSinceActivation < SecondsBeforeFadeIn)
return;
constexpr float SecondsFadeIn = 0.25f;
const float AlphaFactor = SecondsSinceActivation < SecondsBeforeFadeIn + SecondsFadeIn ? (SecondsSinceActivation - SecondsBeforeFadeIn) / SecondsFadeIn : 1.0f;
constexpr float FontSize = 14.0f;
constexpr float Margin = 5.0f;
@ -115,11 +119,30 @@ void CTooltips::OnRender()
Rect.y = clamp(UI()->MouseY() - Rect.h / 2.0f, Margin, pScreen->h - Rect.h - Margin);
}
Rect.Draw(ColorRGBA(0.2f, 0.2f, 0.2f, 0.8f), IGraphics::CORNER_ALL, Padding);
Rect.Draw(ColorRGBA(0.2f, 0.2f, 0.2f, 0.8f * AlphaFactor), IGraphics::CORNER_ALL, Padding);
Rect.Margin(Padding, &Rect);
SLabelProperties Props;
Props.m_MaxWidth = Tooltip.m_WidthHint;
UI()->DoLabel(&Rect, Tooltip.m_pText, FontSize, TEXTALIGN_ML, Props);
CTextCursor Cursor;
TextRender()->SetCursor(&Cursor, Rect.x, Rect.y, FontSize, TEXTFLAG_RENDER);
Cursor.m_LineWidth = Tooltip.m_WidthHint;
STextContainerIndex TextContainerIndex;
const unsigned OldRenderFlags = TextRender()->GetRenderFlags();
TextRender()->SetRenderFlags(OldRenderFlags | TEXT_RENDER_FLAG_ONE_TIME_USE);
TextRender()->CreateTextContainer(TextContainerIndex, &Cursor, Tooltip.m_pText);
TextRender()->SetRenderFlags(OldRenderFlags);
if(TextContainerIndex.Valid())
{
ColorRGBA TextColor = TextRender()->DefaultTextColor();
TextColor.a *= AlphaFactor;
ColorRGBA OutlineColor = TextRender()->DefaultTextOutlineColor();
OutlineColor.a *= AlphaFactor;
TextRender()->RenderTextContainer(TextContainerIndex, TextColor, OutlineColor);
}
TextRender()->DeleteTextContainer(TextContainerIndex);
Tooltip.m_OnScreen = false;
}
}