ddnet/src/game/client/components/tooltips.h
Robert Müller bbe2430be9 Fix tooltips being active behind popups and outside clipping rects
Ensure that tooltips are only activated when the associated UI element is currently the hot item. This prevents tooltips from being activated for UI elements that are currently not enabled due to them being behind a popup or outside of a clipping rect.
2023-05-29 12:29:02 +02:00

62 lines
1.8 KiB
C++

#ifndef GAME_CLIENT_COMPONENTS_TOOLTIPS_H
#define GAME_CLIENT_COMPONENTS_TOOLTIPS_H
#include <game/client/component.h>
#include <game/client/ui_rect.h>
#include <cstdint>
#include <functional>
#include <optional>
#include <unordered_map>
struct CTooltip
{
const void *m_pID;
CUIRect m_Rect;
const char *m_pText;
float m_WidthHint;
bool m_OnScreen; // used to know if the tooltip should be rendered.
};
/**
* A component that manages and renders UI tooltips.
*
* Should be among the last components to render.
*/
class CTooltips : public CComponent
{
std::unordered_map<uintptr_t, CTooltip> m_Tooltips;
std::optional<std::reference_wrapper<CTooltip>> m_ActiveTooltip;
std::optional<std::reference_wrapper<CTooltip>> m_PreviousTooltip;
int64_t m_HoverTime;
/**
* @param Tooltip A reference to the tooltip that should be active.
*/
void SetActiveTooltip(CTooltip &Tooltip);
inline void ClearActiveTooltip();
public:
CTooltips();
virtual int Sizeof() const override { return sizeof(*this); }
/**
* Adds the tooltip to a cache and renders it when active.
*
* 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.
* If multiple tooltips cover the same rect or the rects intersect, then the tooltip that is added later has priority.
*
* @param pID The ID of the tooltip. Usually a reference to some g_Config value.
* @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);
virtual void OnReset() override;
virtual void OnRender() override;
};
#endif