Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CText.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2010 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 /*
19 CText
20 */
21 
22 #include "precompiled.h"
23 #include "GUI.h"
24 #include "CText.h"
25 #include "CGUIScrollBarVertical.h"
26 
27 #include "lib/ogl.h"
28 
29 
30 //-------------------------------------------------------------------
31 // Constructor / Destructor
32 //-------------------------------------------------------------------
34 {
35  AddSetting(GUIST_float, "buffer_zone");
36  AddSetting(GUIST_CGUIString, "caption");
37  AddSetting(GUIST_int, "cell_id");
38  AddSetting(GUIST_bool, "clip");
39  AddSetting(GUIST_CStrW, "font");
40  AddSetting(GUIST_bool, "scrollbar");
41  AddSetting(GUIST_CStr, "scrollbar_style");
42  AddSetting(GUIST_bool, "scroll_bottom");
43  AddSetting(GUIST_CGUISpriteInstance, "sprite");
44  AddSetting(GUIST_EAlign, "text_align");
45  AddSetting(GUIST_EVAlign, "text_valign");
46  AddSetting(GUIST_CColor, "textcolor");
47  AddSetting(GUIST_CStrW, "tooltip");
48  AddSetting(GUIST_CStr, "tooltip_style");
49 
50  // Private settings
51  AddSetting(GUIST_CStrW, "_icon_tooltip");
52  AddSetting(GUIST_CStr, "_icon_tooltip_style");
53 
54  //GUI<bool>::SetSetting(this, "ghost", true);
55  GUI<bool>::SetSetting(this, "scrollbar", false);
56  GUI<bool>::SetSetting(this, "clip", true);
57 
58  // Add scroll-bar
60  bar->SetRightAligned(true);
61  bar->SetUseEdgeButtons(true);
62  AddScrollBar(bar);
63 
64  // Add text
65  AddText(new SGUIText());
66 }
67 
69 {
70 }
71 
73 {
74  if (!GetGUI())
75  return;
76 
77  ENSURE(m_GeneratedTexts.size()>=1);
78 
79  CStrW font;
80  if (GUI<CStrW>::GetSetting(this, "font", font) != PSRETURN_OK || font.empty())
81  // Use the default if none is specified
82  // TODO Gee: (2004-08-14) Don't define standard like this. Do it with the default style.
83  font = L"default";
84 
85  CGUIString caption;
86  bool scrollbar;
87  GUI<CGUIString>::GetSetting(this, "caption", caption);
88  GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
89 
90  float width = m_CachedActualSize.GetWidth();
91  // remove scrollbar if applicable
92  if (scrollbar && GetScrollBar(0).GetStyle())
93  width -= GetScrollBar(0).GetStyle()->m_Width;
94 
95 
96  float buffer_zone=0.f;
97  GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
98  *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, width, buffer_zone, this);
99 
100  if (! scrollbar)
102 
103  // Setup scrollbar
104  if (scrollbar)
105  {
106  bool scrollbottom = false;
107  GUI<bool>::GetSetting(this, "scroll_bottom", scrollbottom);
108 
109  // If we are currently scrolled to the bottom of the text,
110  // then add more lines of text, update the scrollbar so we
111  // stick to the bottom.
112  // (Use 1.5px delta so this triggers the first time caption is set)
113  bool bottom = false;
114  if (scrollbottom && GetScrollBar(0).GetPos() > GetScrollBar(0).GetMaxPos() - 1.5f)
115  bottom = true;
116 
117  GetScrollBar(0).SetScrollRange(m_GeneratedTexts[0]->m_Size.cy);
119 
124 
125  if (bottom)
126  GetScrollBar(0).SetPos(GetScrollBar(0).GetMaxPos());
127  }
128 }
129 
131 {
133  //IGUITextOwner::HandleMessage(Message); <== placed it after the switch instead!
134 
135  switch (Message.type)
136  {
138  if (Message.value == "scrollbar")
139  {
140  SetupText();
141  }
142 
143  // Update scrollbar
144  if (Message.value == "scrollbar_style")
145  {
146  CStr scrollbar_style;
147  GUI<CStr>::GetSetting(this, Message.value, scrollbar_style);
148 
149  GetScrollBar(0).SetScrollBarStyle( scrollbar_style );
150 
151  SetupText();
152  }
153 
154  break;
155 
157  {
159  // Since the scroll was changed, let's simulate a mouse movement
160  // to check if scrollbar now is hovered
162  HandleMessage(msg);
163  break;
164  }
165  case GUIM_MOUSE_WHEEL_UP:
166  {
168  // Since the scroll was changed, let's simulate a mouse movement
169  // to check if scrollbar now is hovered
171  HandleMessage(msg);
172  break;
173  }
174  case GUIM_LOAD:
175  {
180 
181  CStr scrollbar_style;
182  GUI<CStr>::GetSetting(this, "scrollbar_style", scrollbar_style);
183  GetScrollBar(0).SetScrollBarStyle( scrollbar_style );
184  }
185  break;
186 
187  default:
188  break;
189  }
190 
192 }
193 
194 void CText::Draw()
195 {
196  float bz = GetBufferedZ();
197 
198  // First call draw on ScrollBarOwner
199  bool scrollbar;
200  GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
201 
202  if (scrollbar)
203  {
204  // Draw scrollbar
206  }
207 
208  if (GetGUI())
209  {
210  CGUISpriteInstance *sprite;
211  int cell_id;
212  bool clip;
213  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite", sprite);
214  GUI<int>::GetSetting(this, "cell_id", cell_id);
215  GUI<bool>::GetSetting(this, "clip", clip);
216 
217  GetGUI()->DrawSprite(*sprite, cell_id, bz, m_CachedActualSize);
218 
219  float scroll=0.f;
220  if (scrollbar)
221  {
222  scroll = GetScrollBar(0).GetPos();
223  }
224 
225  // Clipping area (we'll have to subtract the scrollbar)
226  CRect cliparea;
227  if (clip)
228  {
229  cliparea = m_CachedActualSize;
230 
231  if (scrollbar)
232  {
233  // subtract scrollbar from cliparea
234  if (cliparea.right > GetScrollBar(0).GetOuterRect().left &&
235  cliparea.right <= GetScrollBar(0).GetOuterRect().right)
236  cliparea.right = GetScrollBar(0).GetOuterRect().left;
237 
238  if (cliparea.left >= GetScrollBar(0).GetOuterRect().left &&
239  cliparea.left < GetScrollBar(0).GetOuterRect().right)
240  cliparea.left = GetScrollBar(0).GetOuterRect().right;
241  }
242  }
243 
244  CColor color;
245  GUI<CColor>::GetSetting(this, "textcolor", color);
246 
247  if (scrollbar)
248  DrawText(0, color, m_CachedActualSize.TopLeft() - CPos(0.f, scroll), bz+0.1f, cliparea);
249  else
250  DrawText(0, color, m_TextPos, bz+0.1f, cliparea);
251  }
252 }
253 
255 {
256  std::vector<SGUIText*>::iterator text_it;
257  std::list<SGUIText::SSpriteCall>::iterator sprite_it;
258 
259  for (text_it=m_GeneratedTexts.begin(); text_it!=m_GeneratedTexts.end(); ++text_it)
260  {
261  SGUIText* guitext = *text_it;
262 
263  for (sprite_it=guitext->m_SpriteCalls.begin(); sprite_it!=guitext->m_SpriteCalls.end(); ++sprite_it)
264  {
265  //Check mouse over sprite
266  SGUIText::SSpriteCall spritecall = *sprite_it;
267 
269  {
270  //If tooltip exists, set the property
271  if(!spritecall.m_Tooltip.empty())
272  {
273  SetSetting("_icon_tooltip_style", spritecall.m_TooltipStyle);
274  SetSetting("_icon_tooltip", spritecall.m_Tooltip);
275  }
276 
277  return true;
278  }
279  }
280  }
281 
282  return false;
283 }
CPos m_TextPos
Placement of text.
Definition: CText.h:97
void SetupText()
Sets up text, should be called every time changes has been made that can change the visual...
Definition: CText.cpp:72
void SetScrollRange(float range)
Set content length.
void SetUseEdgeButtons(bool b)
Set use edge buttons.
void DrawSprite(const CGUISpriteInstance &Sprite, int CellID, const float &Z, const CRect &Rect, const CRect &Clipping=CRect())
Draw GUI Sprite.
Definition: CGUI.cpp:467
CStr value
Optional data.
Definition: GUIbase.h:127
const PSRETURN PSRETURN_OK
Definition: Errors.h:103
float top
Definition: Overlay.h:159
void SetZ(float z)
Set Z Position.
Definition: Overlay.h:34
float m_Width
Width of bar, also both sides of the edge buttons.
Definition: IGUIScrollBar.h:67
virtual IGUIScrollBar & GetScrollBar(const int &index)
Get Scroll Bar reference (it should be transparent it&#39;s actually pointers).
float left
Returning CPos representing each corner.
Definition: Overlay.h:159
void SetY(float y)
Set Y Position.
static PSRETURN GetSetting(const IGUIObject *pObject, const CStr &Setting, T &Value)
Retrieves a setting by name from object pointer.
Definition: GUIutil.cpp:344
CStrW m_TooltipStyle
Tooltip style.
Definition: GUItext.h:91
CRect m_Area
Size and position of sprite.
Definition: GUItext.h:74
A sprite call to the CRenderer.
Definition: GUItext.h:67
void SetLength(float length)
Set Length of scroll bar.
virtual void AddScrollBar(IGUIScrollBar *scrollbar)
Add a scroll-bar.
virtual ~CText()
Definition: CText.cpp:68
virtual float GetBufferedZ() const
Returns not the Z value, but the actual buffered Z value, i.e.
Definition: IGUIObject.cpp:406
void CalculateTextPosition(CRect &ObjSize, CPos &TextPos, SGUIText &Text)
Calculate the position for the text, based on the alignment.
float GetWidth() const
Definition: Overlay.cpp:232
virtual void Draw()
Draws the object.
Includes static functions that needs one template argument.
Definition: GUIutil.h:103
void SetX(float x)
Set X Position.
virtual void ScrollPlus()
Increase scroll one step.
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
virtual void ScrollMinus()
Decrease scroll one step.
virtual void HandleMessage(SGUIMessage &Message)
Vertical implementation of IGUIScrollBar.
void AddSetting(const EGUISettingType &Type, const CStr &Name)
Add a setting to m_Settings.
Definition: IGUIObject.cpp:172
void SetScrollBarStyle(const CStr &style)
Set Scroll bar style string.
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
CText()
Definition: CText.cpp:33
void SetScrollSpace(float space)
Set space that is visible in the scrollable control.
float GetPos() const
Get scroll-position.
float right
Definition: Overlay.h:159
CPos GetMousePos() const
Get Mouse from CGUI.
Definition: IGUIObject.cpp:207
virtual void HandleMessage(SGUIMessage &Message)
Definition: CText.cpp:130
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
void SetRightAligned(const bool &align)
Set Right Aligned.
PSRETURN SetSetting(const CStr &Setting, const CStrW &Value, const bool &SkipMessage=false)
Set a setting by string, regardless of what type it is.
Definition: IGUIObject.cpp:260
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
virtual void SetPos(float f)
Set scroll-position by hand.
float GetHeight() const
Definition: Overlay.cpp:237
virtual CRect GetOuterRect() const =0
Get the rectangle of the outline of the scrollbar, every component of the scroll-bar should be inside...
const SGUIScrollBarStyle * GetStyle() const
Get style used by the scrollbar.
std::vector< SGUIText * > m_GeneratedTexts
Texts that are generated and ready to be rendered.
Message send to IGUIObject::HandleMessage() in order to give life to Objects manually with a derived ...
Definition: GUIbase.h:106
EGUIMessageType type
Describes what the message regards.
Definition: GUIbase.h:122
float bottom
Definition: Overlay.h:159
std::list< SSpriteCall > m_SpriteCalls
List of sprites, or &quot;icons&quot; that should be rendered along with the text.
Definition: GUItext.h:155
virtual void HandleMessage(SGUIMessage &Message)
bool PointInside(const CPos &point) const
Evalutates if point is within the rectangle.
Definition: Overlay.cpp:272
void AddText(SGUIText *text)
Adds a text object.
virtual void Draw()
Draws the Text.
Definition: CText.cpp:194
CStrW m_Tooltip
Tooltip text.
Definition: GUItext.h:86
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
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
Rectangle class used for screen rectangles.
Definition: Overlay.h:71
virtual bool MouseOverIcon()
Test if mouse position is over an icon.
Definition: CText.cpp:254