2022-04-18 07:34:05 +00:00
|
|
|
#include "tooltips.h"
|
|
|
|
|
|
|
|
#include <game/client/render.h>
|
|
|
|
|
|
|
|
CTooltips::CTooltips()
|
|
|
|
{
|
2022-04-18 07:34:21 +00:00
|
|
|
OnReset();
|
2022-04-18 07:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTooltips::OnReset()
|
|
|
|
{
|
2022-04-18 07:34:21 +00:00
|
|
|
HoverTime = -1;
|
|
|
|
m_Tooltips.clear();
|
2022-04-18 07:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTooltips::SetActiveTooltip(CTooltip &Tooltip)
|
|
|
|
{
|
2022-04-19 07:18:46 +00:00
|
|
|
if(m_pActiveTooltip != nullptr)
|
2022-04-18 07:34:21 +00:00
|
|
|
return;
|
|
|
|
|
2022-04-19 07:18:46 +00:00
|
|
|
m_pActiveTooltip = &Tooltip;
|
2022-04-18 07:34:21 +00:00
|
|
|
HoverTime = time_get();
|
2022-04-18 07:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void CTooltips::ClearActiveTooltip()
|
|
|
|
{
|
2022-04-19 07:18:46 +00:00
|
|
|
m_pActiveTooltip = nullptr;
|
2022-04-18 07:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTooltips::DoToolTip(const void *pID, const CUIRect *pNearRect, const char *pText, float WidthHint)
|
|
|
|
{
|
2022-04-18 07:34:21 +00:00
|
|
|
uintptr_t ID = reinterpret_cast<uintptr_t>(pID);
|
|
|
|
|
|
|
|
const auto &it = m_Tooltips.find(ID);
|
|
|
|
|
|
|
|
if(it == m_Tooltips.end())
|
|
|
|
{
|
|
|
|
CTooltip NewTooltip = {
|
2022-04-18 07:44:13 +00:00
|
|
|
*pNearRect,
|
|
|
|
pText,
|
|
|
|
WidthHint,
|
2022-04-18 07:34:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
m_Tooltips[ID] = NewTooltip;
|
|
|
|
|
|
|
|
CTooltip &Tooltip = m_Tooltips[ID];
|
|
|
|
|
|
|
|
if(UI()->MouseInside(&Tooltip.m_Rect))
|
|
|
|
{
|
|
|
|
SetActiveTooltip(Tooltip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(UI()->MouseInside(&it->second.m_Rect))
|
|
|
|
{
|
|
|
|
SetActiveTooltip(it->second);
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 07:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTooltips::OnRender()
|
|
|
|
{
|
2022-04-19 07:18:46 +00:00
|
|
|
if(m_pActiveTooltip != nullptr)
|
2022-04-18 07:34:21 +00:00
|
|
|
{
|
2022-04-19 07:18:46 +00:00
|
|
|
CTooltip &Tooltip = *m_pActiveTooltip;
|
2022-04-18 07:34:21 +00:00
|
|
|
|
|
|
|
if(!UI()->MouseInside(&Tooltip.m_Rect))
|
|
|
|
{
|
|
|
|
ClearActiveTooltip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delay tooltip until 1 second passed.
|
|
|
|
if(HoverTime > time_get() - time_freq())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const float MARGIN = 5.0f;
|
|
|
|
|
|
|
|
CUIRect Rect;
|
|
|
|
Rect.w = Tooltip.m_WidthHint;
|
|
|
|
if(Tooltip.m_WidthHint < 0.0f)
|
|
|
|
Rect.w = TextRender()->TextWidth(0, 14.0f, Tooltip.m_pText, -1, -1.0f) + 4.0f;
|
|
|
|
Rect.h = 30.0f;
|
|
|
|
|
|
|
|
CUIRect *pScreen = UI()->Screen();
|
|
|
|
|
|
|
|
// Try the top side.
|
|
|
|
if(Tooltip.m_Rect.y - Rect.h - MARGIN > pScreen->y)
|
|
|
|
{
|
|
|
|
Rect.x = clamp(UI()->MouseX() - Rect.w / 2.0f, MARGIN, pScreen->w - Rect.w - MARGIN);
|
|
|
|
Rect.y = Tooltip.m_Rect.y - Rect.h - MARGIN;
|
|
|
|
}
|
|
|
|
// Try the bottom side.
|
|
|
|
else if(Tooltip.m_Rect.y + Tooltip.m_Rect.h + MARGIN < pScreen->h)
|
|
|
|
{
|
|
|
|
Rect.x = clamp(UI()->MouseX() - Rect.w / 2.0f, MARGIN, pScreen->w - Rect.w - MARGIN);
|
|
|
|
Rect.y = Tooltip.m_Rect.y + Tooltip.m_Rect.h + MARGIN;
|
|
|
|
}
|
|
|
|
// Try the right side.
|
|
|
|
else if(Tooltip.m_Rect.x + Tooltip.m_Rect.w + MARGIN + Rect.w < pScreen->w)
|
|
|
|
{
|
|
|
|
Rect.x = Tooltip.m_Rect.x + Tooltip.m_Rect.w + MARGIN;
|
|
|
|
Rect.y = clamp(UI()->MouseY() - Rect.h / 2.0f, MARGIN, pScreen->h - Rect.h - MARGIN);
|
|
|
|
}
|
|
|
|
// Try the left side.
|
|
|
|
else if(Tooltip.m_Rect.x - Rect.w - MARGIN > pScreen->x)
|
|
|
|
{
|
|
|
|
Rect.x = Tooltip.m_Rect.x - Rect.w - MARGIN;
|
|
|
|
Rect.y = clamp(UI()->MouseY() - Rect.h / 2.0f, MARGIN, pScreen->h - Rect.h - MARGIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderTools()->DrawUIRect(&Rect, ColorRGBA(0.2, 0.2, 0.2, 0.80f), CUI::CORNER_ALL, 5.0f);
|
|
|
|
Rect.Margin(2.0f, &Rect);
|
|
|
|
UI()->DoLabel(&Rect, Tooltip.m_pText, 14.0f, TEXTALIGN_LEFT);
|
|
|
|
}
|
2022-04-18 07:34:05 +00:00
|
|
|
}
|