Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CTooltip.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 // CTooltip: GUI object used for tooltips
19 
20 #include "precompiled.h"
21 
22 #include "CTooltip.h"
23 #include "CGUI.h"
24 
25 #include <algorithm>
26 
28 {
29  // If the tooltip is an object by itself:
30  AddSetting(GUIST_float, "buffer_zone");
31  AddSetting(GUIST_CGUIString, "caption");
32  AddSetting(GUIST_CStrW, "font");
33  AddSetting(GUIST_CGUISpriteInstance, "sprite");
34  AddSetting(GUIST_int, "delay");
35  AddSetting(GUIST_CColor, "textcolor");
36  AddSetting(GUIST_float, "maxwidth");
37  AddSetting(GUIST_CPos, "offset");
38  AddSetting(GUIST_EVAlign, "anchor");
39  AddSetting(GUIST_EAlign, "text_align");
40  // This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism
41  AddSetting(GUIST_bool, "independent");
42 
43  // If the tooltip is just a reference to another object:
44  AddSetting(GUIST_CStr, "use_object");
45  AddSetting(GUIST_bool, "hide_object");
46 
47  // Private settings:
48  AddSetting(GUIST_CPos, "_mousepos");
49 
50  // Defaults
51  GUI<int>::SetSetting(this, "delay", 500);
53  GUI<EAlign>::SetSetting(this, "text_align", EAlign_Left);
54 
55  // Set up a blank piece of text, to be replaced with a more
56  // interesting message later
57  AddText(new SGUIText());
58 }
59 
61 {
62 }
63 
65 {
66  if (!GetGUI())
67  return;
68 
69  ENSURE(m_GeneratedTexts.size()==1);
70 
71  CStrW font;
72  if (GUI<CStrW>::GetSetting(this, "font", font) != PSRETURN_OK || font.empty())
73  font = L"default";
74 
75  float buffer_zone = 0.f;
76  GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
77 
78  CGUIString caption;
79  GUI<CGUIString>::GetSetting(this, "caption", caption);
80 
81  float max_width = 0.f;
82  GUI<float>::GetSetting(this, "maxwidth", max_width);
83 
84  *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, max_width, buffer_zone, this);
85 
86 
87  // Position the tooltip relative to the mouse:
88 
89  CPos mousepos, offset;
90  EVAlign anchor;
91  bool independent;
92 
93  GUI<bool>::GetSetting(this, "independent", independent);
94  if (independent)
95  mousepos = GetMousePos();
96  else
97  GUI<CPos>::GetSetting(this, "_mousepos", mousepos);
98 
99  GUI<CPos>::GetSetting(this, "offset", offset);
100  GUI<EVAlign>::GetSetting(this, "anchor", anchor);
101 
102  // TODO: Calculate the actual width of the wrapped text and use that.
103  // (m_Size.cx is >max_width if the text wraps, which is not helpful)
104  float textwidth = std::min(m_GeneratedTexts[0]->m_Size.cx, (float)max_width);
105  float textheight = m_GeneratedTexts[0]->m_Size.cy;
106 
107  CClientArea size;
108  size.pixel.left = mousepos.x + offset.x;
109  size.pixel.right = size.pixel.left + textwidth;
110  switch (anchor)
111  {
112  case EVAlign_Top:
113  size.pixel.top = mousepos.y + offset.y;
114  size.pixel.bottom = size.pixel.top + textheight;
115  break;
116  case EVAlign_Bottom:
117  size.pixel.bottom = mousepos.y + offset.y;
118  size.pixel.top = size.pixel.bottom - textheight;
119  break;
120  case EVAlign_Center:
121  size.pixel.top = mousepos.y + offset.y - textheight/2.f;
122  size.pixel.bottom = size.pixel.top + textwidth;
123  break;
124  default:
125  debug_warn(L"Invalid EVAlign!");
126  }
127 
128 
129  // Reposition the tooltip if it's falling off the screen:
130 
131  extern int g_xres, g_yres;
132  float screenw = (float)g_xres, screenh = (float)g_yres;
133 
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;
138 
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;
143 
144  GUI<CClientArea>::SetSetting(this, "size", size);
145 }
146 
148 {
150 }
151 
153 {
154  float z = 900.f; // TODO: Find a nicer way of putting the tooltip on top of everything else
155 
156  if (GetGUI())
157  {
158  CGUISpriteInstance *sprite;
159  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite", sprite);
160 
161  // Normally IGUITextOwner will handle this updating but since SetupText can modify the position
162  // we need to call it now *before* we do the rest of the drawing
164  {
165  SetupText();
166  m_GeneratedTextsValid = true;
167  }
168 
169  GetGUI()->DrawSprite(*sprite, 0, z, m_CachedActualSize);
170 
171  CColor color;
172  GUI<CColor>::GetSetting(this, "textcolor", color);
173 
174  DrawText(0, color, m_CachedActualSize.TopLeft(), z+0.1f);
175  }
176 }
void DrawSprite(const CGUISpriteInstance &Sprite, int CellID, const float &Z, const CRect &Rect, const CRect &Clipping=CRect())
Draw GUI Sprite.
Definition: CGUI.cpp:467
virtual void Draw()
Draws the object.
Definition: CTooltip.cpp:152
const PSRETURN PSRETURN_OK
Definition: Errors.h:103
float top
Definition: Overlay.h:159
Definition: Overlay.h:34
CTooltip()
Definition: CTooltip.cpp:27
int g_xres
Definition: Config.cpp:58
float left
Returning CPos representing each corner.
Definition: Overlay.h:159
static PSRETURN GetSetting(const IGUIObject *pObject, const CStr &Setting, T &Value)
Retrieves a setting by name from object pointer.
Definition: GUIutil.cpp:344
CRect pixel
Pixel modifiers.
Definition: GUIbase.h:187
void SetupText()
Setup texts.
Definition: CTooltip.cpp:64
virtual void HandleMessage(SGUIMessage &Message)
Definition: CTooltip.cpp:147
virtual ~CTooltip()
Definition: CTooltip.cpp:60
Includes static functions that needs one template argument.
Definition: GUIutil.h:103
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
virtual void HandleMessage(SGUIMessage &Message)
int g_yres
Definition: Config.cpp:58
void AddSetting(const EGUISettingType &Type, const CStr &Name)
Add a setting to m_Settings.
Definition: IGUIObject.cpp:172
CRect m_CachedActualSize
Cached size, real size m_Size is actually dependent on resolution and can have different real outcome...
Definition: IGUIObject.h:434
Made to represent screen positions and delta values.
Definition: Overlay.h:167
float right
Definition: Overlay.h:159
CPos GetMousePos() const
Get Mouse from CGUI.
Definition: IGUIObject.cpp:207
virtual void DrawText(int index, const CColor &color, const CPos &pos, float z, const CRect &clipping=CRect())
Draws the Text.
CGUI * GetGUI()
Definition: IGUIObject.h:388
static PSRETURN SetSetting(IGUIObject *pObject, const CStr &Setting, const T &Value, const bool &SkipMessage=false)
Sets a value by name using a real datatype as input.
Definition: GUIutil.cpp:366
float y
Definition: Overlay.h:195
#define debug_warn(expr)
display the error dialog with the given text.
Definition: debug.h:324
std::vector< SGUIText * > m_GeneratedTexts
Texts that are generated and ready to be rendered.
EVAlign
Definition: GUIbase.h:151
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: GUIbase.h:106
float bottom
Definition: Overlay.h:159
float x
Position.
Definition: Overlay.h:195
void AddText(SGUIText *text)
Adds a text object.
String class, substitute for CStr, but that parses the tags and builds up a list of all text that wil...
Definition: GUItext.h:176
SGUIText GenerateText(const CGUIString &Text, const CStrW &Font, const float &Width, const float &BufferZone, const IGUIObject *pObject=NULL)
Generate a SGUIText object from the inputted string.
Definition: CGUI.cpp:644
bool m_GeneratedTextsValid
Whether the cached text is currently valid (if not then SetupText will be called by Draw) ...
CPos TopLeft() const
Get Position equivalent to top/left corner.
Definition: Overlay.cpp:247
static PSRETURN GetSettingPointer(const IGUIObject *pObject, const CStr &Setting, T *&Value)
Definition: GUIutil.cpp:317
An SGUIText object is a parsed string, divided into text-rendering components.
Definition: GUItext.h:62
Client Area is a rectangle relative to a parent rectangle.
Definition: GUIbase.h:179