Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "CTooltip.h"
21 :
22 : #include "gui/CGUI.h"
23 : #include "gui/CGUIText.h"
24 : #include "gui/SettingTypes/CGUISize.h"
25 : #include "gui/SettingTypes/CGUIString.h"
26 : #include "gui/SettingTypes/EAlign.h"
27 : #include "lib/debug.h"
28 : #include "maths/Rect.h"
29 : #include "maths/Size2D.h"
30 :
31 : #include <vector>
32 :
33 0 : CTooltip::CTooltip(CGUI& pGUI)
34 : : IGUIObject(pGUI),
35 : IGUITextOwner(*static_cast<IGUIObject*>(this)),
36 0 : m_BufferZone(this, "buffer_zone"),
37 0 : m_Caption(this, "caption"),
38 0 : m_Font(this, "font"),
39 0 : m_Sprite(this, "sprite"),
40 0 : m_Delay(this, "delay", 500), // in milliseconds
41 0 : m_TextColor(this, "textcolor"),
42 0 : m_MaxWidth(this, "maxwidth"),
43 0 : m_Offset(this, "offset"),
44 0 : m_Anchor(this, "anchor", EVAlign::BOTTOM),
45 : // This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism
46 0 : m_Independent(this, "independent"),
47 : // Private settings:
48 : // This is set by GUITooltip
49 0 : m_MousePos(this, "_mousepos"),
50 : // If the tooltip is just a reference to another object:
51 0 : m_UseObject(this, "use_object"),
52 0 : m_HideObject(this, "hide_object")
53 : {
54 0 : m_Ghost.GetMutable() = true;
55 : // Set up a blank piece of text, to be replaced with a more
56 : // interesting message later
57 0 : AddText();
58 0 : }
59 :
60 0 : void CTooltip::SetupText()
61 : {
62 0 : ENSURE(m_GeneratedTexts.size() == 1);
63 :
64 0 : m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, m_MaxWidth, m_BufferZone, m_TextAlign, this);
65 :
66 : // Position the tooltip relative to the mouse:
67 :
68 0 : const CVector2D& mousepos = m_Independent ? m_pGUI.GetMousePos() : m_MousePos;
69 :
70 0 : float textwidth = m_GeneratedTexts[0].GetSize().Width;
71 0 : float textheight = m_GeneratedTexts[0].GetSize().Height;
72 :
73 0 : CGUISize size;
74 0 : size.pixel.left = mousepos.X + m_Offset->X;
75 0 : size.pixel.right = size.pixel.left + textwidth;
76 :
77 0 : switch (m_Anchor)
78 : {
79 0 : case EVAlign::TOP:
80 0 : size.pixel.top = mousepos.Y + m_Offset->Y;
81 0 : size.pixel.bottom = size.pixel.top + textheight;
82 0 : break;
83 0 : case EVAlign::BOTTOM:
84 0 : size.pixel.bottom = mousepos.Y + m_Offset->Y;
85 0 : size.pixel.top = size.pixel.bottom - textheight;
86 0 : break;
87 0 : case EVAlign::CENTER:
88 0 : size.pixel.top = mousepos.Y + m_Offset->Y - textheight/2.f;
89 0 : size.pixel.bottom = size.pixel.top + textwidth;
90 0 : break;
91 0 : default:
92 0 : debug_warn(L"Invalid EVAlign!");
93 : }
94 :
95 :
96 : // Reposition the tooltip if it's falling off in the GUI window.
97 0 : const CSize2D windowSize = m_pGUI.GetWindowSize();
98 :
99 0 : if (size.pixel.top < 0.f)
100 : {
101 0 : size.pixel.bottom -= size.pixel.top;
102 0 : size.pixel.top = 0.f;
103 : }
104 0 : else if (size.pixel.bottom > windowSize.Height)
105 : {
106 0 : size.pixel.top -= size.pixel.bottom - windowSize.Height;
107 0 : size.pixel.bottom = windowSize.Height;
108 : }
109 :
110 0 : if (size.pixel.left < 0.f)
111 : {
112 0 : size.pixel.right -= size.pixel.left;
113 0 : size.pixel.left = 0.f;
114 : }
115 0 : else if (size.pixel.right > windowSize.Width)
116 : {
117 0 : size.pixel.left -= size.pixel.right - windowSize.Width;
118 0 : size.pixel.right = windowSize.Width;
119 : }
120 :
121 0 : m_Size.Set(size, true);
122 0 : }
123 :
124 0 : void CTooltip::HandleSizeChanged()
125 : {
126 0 : IGUIObject::HandleSizeChanged();
127 0 : IGUITextOwner::HandleSizeChanged();
128 0 : }
129 :
130 0 : void CTooltip::HandleMessage(SGUIMessage& Message)
131 : {
132 0 : IGUIObject::HandleMessage(Message);
133 0 : IGUITextOwner::HandleMessage(Message);
134 0 : }
135 :
136 0 : void CTooltip::Draw(CCanvas2D& canvas)
137 : {
138 : // Normally IGUITextOwner will handle this updating but since SetupText can modify the position
139 : // we need to call it now *before* we do the rest of the drawing
140 0 : if (!m_GeneratedTextsValid)
141 : {
142 0 : SetupText();
143 0 : m_GeneratedTextsValid = true;
144 : }
145 :
146 0 : m_pGUI.DrawSprite(m_Sprite, canvas, GetActualSize());
147 0 : DrawText(canvas, 0, m_TextColor, GetActualSize().TopLeft());
148 0 : }
149 :
150 0 : float CTooltip::GetBufferedZ() const
151 : {
152 : // TODO: Find a nicer way of putting the tooltip on top of everything else.
153 0 : return 900.f;
154 : }
|