| 1 | #ifndef GAME_CLIENT_COMPONENTS_TOOLTIPS_H |
| 2 | #define GAME_CLIENT_COMPONENTS_TOOLTIPS_H |
| 3 | |
| 4 | #include <game/client/component.h> |
| 5 | #include <game/client/ui_rect.h> |
| 6 | |
| 7 | #include <cstdint> |
| 8 | #include <functional> |
| 9 | #include <optional> |
| 10 | #include <unordered_map> |
| 11 | |
| 12 | struct CTooltip |
| 13 | { |
| 14 | const void *m_pId; |
| 15 | CUIRect m_Rect; |
| 16 | const char *m_pText; |
| 17 | float m_WidthHint; |
| 18 | bool m_OnScreen; // used to know if the tooltip should be rendered. |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * A component that manages and renders UI tooltips. |
| 23 | * |
| 24 | * Should be among the last components to render. |
| 25 | */ |
| 26 | class CTooltips : public CComponent |
| 27 | { |
| 28 | std::unordered_map<uintptr_t, CTooltip> m_Tooltips; |
| 29 | std::optional<std::reference_wrapper<CTooltip>> m_ActiveTooltip; |
| 30 | std::optional<std::reference_wrapper<CTooltip>> m_PreviousTooltip; |
| 31 | int64_t m_HoverTime; |
| 32 | |
| 33 | /** |
| 34 | * @param Tooltip A reference to the tooltip that should be active. |
| 35 | */ |
| 36 | void SetActiveTooltip(CTooltip &Tooltip); |
| 37 | |
| 38 | inline void ClearActiveTooltip(); |
| 39 | |
| 40 | public: |
| 41 | CTooltips(); |
| 42 | int Sizeof() const override { return sizeof(*this); } |
| 43 | |
| 44 | /** |
| 45 | * Adds the tooltip to a cache and renders it when active. |
| 46 | * |
| 47 | * 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. |
| 48 | * If multiple tooltips cover the same rect or the rects intersect, then the tooltip that is added later has priority. |
| 49 | * |
| 50 | * @param pId The ID of the tooltip. Usually a reference to some g_Config value. |
| 51 | * @param pNearRect Place the tooltip near this rect. |
| 52 | * @param pText The text to display in the tooltip. |
| 53 | * @param WidthHint The maximum width of the tooltip, or -1.0f for unlimited. |
| 54 | */ |
| 55 | void DoToolTip(const void *pId, const CUIRect *pNearRect, const char *pText, float WidthHint = -1.0f); |
| 56 | |
| 57 | void OnReset() override; |
| 58 | void OnRender() override; |
| 59 | }; |
| 60 | |
| 61 | #endif |
| 62 | |