1 /* Copyright (C) 2015 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
27 // If the tooltip is an object by itself:
28 AddSetting(GUIST_float
, "buffer_zone");
29 AddSetting(GUIST_CGUIString
, "caption");
30 AddSetting(GUIST_CStrW
, "font");
31 AddSetting(GUIST_CGUISpriteInstance
, "sprite");
32 AddSetting(GUIST_int
, "delay");
33 AddSetting(GUIST_CColor
, "textcolor");
34 AddSetting(GUIST_float
, "maxwidth");
35 AddSetting(GUIST_CPos
, "offset");
36 AddSetting(GUIST_EVAlign
, "anchor");
37 AddSetting(GUIST_EAlign
, "text_align");
38 // This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism
39 AddSetting(GUIST_bool
, "independent");
41 // If the tooltip is just a reference to another object:
42 AddSetting(GUIST_CStr
, "use_object");
43 AddSetting(GUIST_bool
, "hide_object");
46 AddSetting(GUIST_CPos
, "_mousepos");
49 GUI
<int>::SetSetting(this, "delay", 500);
50 GUI
<EVAlign
>::SetSetting(this, "anchor", EVAlign_Bottom
);
51 GUI
<EAlign
>::SetSetting(this, "text_align", EAlign_Left
);
53 // Set up a blank piece of text, to be replaced with a more
54 // interesting message later
55 AddText(new SGUIText());
62 void CTooltip::SetupText()
67 ENSURE(m_GeneratedTexts
.size() == 1);
70 if (GUI
<CStrW
>::GetSetting(this, "font", font
) != PSRETURN_OK
|| font
.empty())
73 float buffer_zone
= 0.f
;
74 GUI
<float>::GetSetting(this, "buffer_zone", buffer_zone
);
77 GUI
<CGUIString
>::GetSetting(this, "caption", caption
);
79 float max_width
= 0.f
;
80 GUI
<float>::GetSetting(this, "maxwidth", max_width
);
82 *m_GeneratedTexts
[0] = GetGUI()->GenerateText(caption
, font
, max_width
, buffer_zone
, this);
85 // Position the tooltip relative to the mouse:
87 CPos mousepos
, offset
;
91 GUI
<bool>::GetSetting(this, "independent", independent
);
93 mousepos
= GetMousePos();
95 GUI
<CPos
>::GetSetting(this, "_mousepos", mousepos
);
97 GUI
<CPos
>::GetSetting(this, "offset", offset
);
98 GUI
<EVAlign
>::GetSetting(this, "anchor", anchor
);
100 // TODO: Calculate the actual width of the wrapped text and use that.
101 // (m_Size.cx is >max_width if the text wraps, which is not helpful)
102 float textwidth
= std::min(m_GeneratedTexts
[0]->m_Size
.cx
, (float)max_width
);
103 float textheight
= m_GeneratedTexts
[0]->m_Size
.cy
;
106 size
.pixel
.left
= mousepos
.x
+ offset
.x
;
107 size
.pixel
.right
= size
.pixel
.left
+ textwidth
;
111 size
.pixel
.top
= mousepos
.y
+ offset
.y
;
112 size
.pixel
.bottom
= size
.pixel
.top
+ textheight
;
115 size
.pixel
.bottom
= mousepos
.y
+ offset
.y
;
116 size
.pixel
.top
= size
.pixel
.bottom
- textheight
;
119 size
.pixel
.top
= mousepos
.y
+ offset
.y
- textheight
/2.f
;
120 size
.pixel
.bottom
= size
.pixel
.top
+ textwidth
;
123 debug_warn(L
"Invalid EVAlign!");
127 // Reposition the tooltip if it's falling off the screen:
129 extern int g_xres
, g_yres
;
130 extern float g_GuiScale
;
131 float screenw
= g_xres
/ g_GuiScale
;
132 float screenh
= g_yres
/ g_GuiScale
;
134 if (size
.pixel
.top
< 0.f
)
135 size
.pixel
.bottom
-= size
.pixel
.top
, size
.pixel
.top
= 0.f
;
136 else if (size
.pixel
.bottom
> screenh
)
137 size
.pixel
.top
-= (size
.pixel
.bottom
-screenh
), size
.pixel
.bottom
= screenh
;
139 if (size
.pixel
.left
< 0.f
)
140 size
.pixel
.right
-= size
.pixel
.left
, size
.pixel
.left
= 0.f
;
141 else if (size
.pixel
.right
> screenw
)
142 size
.pixel
.left
-= (size
.pixel
.right
-screenw
), size
.pixel
.right
= screenw
;
144 GUI
<CClientArea
>::SetSetting(this, "size", size
);
147 void CTooltip::HandleMessage(SGUIMessage
& Message
)
149 IGUITextOwner::HandleMessage(Message
);
152 void CTooltip::Draw()
157 float z
= 900.f
; // TODO: Find a nicer way of putting the tooltip on top of everything else
159 CGUISpriteInstance
* sprite
;
160 GUI
<CGUISpriteInstance
>::GetSettingPointer(this, "sprite", sprite
);
162 // Normally IGUITextOwner will handle this updating but since SetupText can modify the position
163 // we need to call it now *before* we do the rest of the drawing
164 if (!m_GeneratedTextsValid
)
167 m_GeneratedTextsValid
= true;
170 GetGUI()->DrawSprite(*sprite
, 0, z
, m_CachedActualSize
);
173 GUI
<CColor
>::GetSetting(this, "textcolor", color
);
175 DrawText(0, color
, m_CachedActualSize
.TopLeft(), z
+0.1f
);