Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IGUITextOwner.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 /*
19 IGUITextOwner
20 */
21 
22 #include "precompiled.h"
23 #include "GUI.h"
24 
25 
26 //-------------------------------------------------------------------
27 // Constructor / Destructor
28 //-------------------------------------------------------------------
29 IGUITextOwner::IGUITextOwner() : m_GeneratedTextsValid(false)
30 {
31 }
32 
34 {
35  // Delete all generated texts.
36  std::vector<SGUIText*>::iterator it;
37  for (it=m_GeneratedTexts.begin(); it!=m_GeneratedTexts.end(); ++it)
38  {
39  delete *it;
40  }
41 }
42 
44 {
45  m_GeneratedTexts.push_back(text);
46 }
47 
49 {
50  switch (Message.type)
51  {
53  // Everything that can change the visual appearance.
54  // it is assumed that the text of the object will be dependent on
55  // these. Although that is not certain, but one will have to manually
56  // change it and disregard this function.
57  // TODO Gee: (2004-09-07) Make sure this is all options that can affect the text.
58  if (Message.value == "size" || Message.value == "z" ||
59  Message.value == "absolute" || Message.value == "caption" ||
60  Message.value == "font" || Message.value == "textcolor" ||
61  Message.value == "buffer_zone")
62  {
63  m_GeneratedTextsValid = false;
64  }
65  break;
66 
67  default:
68  break;
69  }
70 }
71 
73 {
74  // If an ancestor's size changed, this will let us intercept the change and
75  // update our text positions
76 
78  m_GeneratedTextsValid = false;
79 }
80 
81 void IGUITextOwner::DrawText(int index, const CColor& color, const CPos& pos, float z, const CRect& clipping)
82 {
84  {
85  SetupText();
86  m_GeneratedTextsValid = true;
87  }
88 
89  if (index < 0 || index >= (int)m_GeneratedTexts.size())
90  {
91  debug_warn(L"Trying to draw a Text Index within a IGUITextOwner that doesn't exist");
92  return;
93  }
94 
95  if (GetGUI())
96  {
97  GetGUI()->DrawText(*m_GeneratedTexts[index], color, pos, z, clipping);
98  }
99 }
100 
102 {
103  EVAlign valign;
104  GUI<EVAlign>::GetSetting(this, "text_valign", valign);
105 
106  // The horizontal Alignment is now computed in GenerateText in order to not have to
107  // loop through all of the TextCall objects again.
108  TextPos.x = ObjSize.left;
109 
110  switch (valign)
111  {
112  case EVAlign_Top:
113  TextPos.y = ObjSize.top;
114  break;
115  case EVAlign_Center:
116  // Round to integer pixel values, else the fonts look awful
117  TextPos.y = floorf(ObjSize.CenterPoint().y - Text.m_Size.cy/2.f);
118  break;
119  case EVAlign_Bottom:
120  TextPos.y = ObjSize.bottom - Text.m_Size.cy;
121  break;
122  default:
123  debug_warn(L"Broken EVAlign in CButton::SetupText()");
124  break;
125  }
126 }
127 
129 {
130  return false;
131 }
virtual void SetupText()=0
Setup texts.
virtual ~IGUITextOwner()
CStr value
Optional data.
Definition: GUIbase.h:127
CPos CenterPoint() const
Get Position equivalent to the center of the rectangle.
Definition: Overlay.cpp:267
virtual void UpdateCachedSize()
float top
Definition: Overlay.h:159
Definition: Overlay.h:34
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
virtual bool MouseOverIcon()
Test if mouse position is over an icon.
void CalculateTextPosition(CRect &ObjSize, CPos &TextPos, SGUIText &Text)
Calculate the position for the text, based on the alignment.
virtual void UpdateCachedSize()
All sizes are relative to resolution, and the calculation is not wanted in real time, therefore it is cached, update the cached size with this function.
Definition: IGUIObject.cpp:336
CSize m_Size
Width and height of the whole output, used when setting up scrollbars and such.
Definition: GUItext.h:162
virtual void HandleMessage(SGUIMessage &Message)
Made to represent screen positions and delta values.
Definition: Overlay.h:167
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 DrawText(SGUIText &Text, const CColor &DefaultColor, const CPos &pos, const float &z, const CRect &clipping)
Draw a SGUIText object.
Definition: CGUI.cpp:941
float y
Definition: Overlay.h:195
#define debug_warn(expr)
display the error dialog with the given text.
Definition: debug.h:324
float cy
Definition: Overlay.h:234
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
EGUIMessageType type
Describes what the message regards.
Definition: GUIbase.h:122
float bottom
Definition: Overlay.h:159
float x
Position.
Definition: Overlay.h:195
void AddText(SGUIText *text)
Adds a text object.
bool m_GeneratedTextsValid
Whether the cached text is currently valid (if not then SetupText will be called by Draw) ...
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