Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCheckBox.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 CCheckBox
20 */
21 
22 #include "precompiled.h"
23 
24 #include "CCheckBox.h"
25 
26 #include "ps/CLogger.h"
27 #include "ps/Font.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, "checked");
39  AddSetting(GUIST_CStrW, "font");
40  AddSetting(GUIST_CStrW, "sound_disabled");
41  AddSetting(GUIST_CStrW, "sound_enter");
42  AddSetting(GUIST_CStrW, "sound_leave");
43  AddSetting(GUIST_CStrW, "sound_pressed");
44  AddSetting(GUIST_CStrW, "sound_released");
45  AddSetting(GUIST_CGUISpriteInstance, "sprite");
46  AddSetting(GUIST_CGUISpriteInstance, "sprite_over");
47  AddSetting(GUIST_CGUISpriteInstance, "sprite_pressed");
48  AddSetting(GUIST_CGUISpriteInstance, "sprite_disabled");
49  AddSetting(GUIST_CGUISpriteInstance, "sprite2");
50  AddSetting(GUIST_CGUISpriteInstance, "sprite2_over");
51  AddSetting(GUIST_CGUISpriteInstance, "sprite2_pressed");
52  AddSetting(GUIST_CGUISpriteInstance, "sprite2_disabled");
53  AddSetting(GUIST_float, "square_side");
54  AddSetting(GUIST_CColor, "textcolor");
55  AddSetting(GUIST_CColor, "textcolor_over");
56  AddSetting(GUIST_CColor, "textcolor_pressed");
57  AddSetting(GUIST_CColor, "textcolor_disabled");
58  AddSetting(GUIST_CStrW, "tooltip");
59  AddSetting(GUIST_CStr, "tooltip_style");
60 
61  // Add text
62  AddText(new SGUIText());
63 }
64 
66 {
67 }
68 
70 {
71  if (!GetGUI())
72  return;
73 
74  ENSURE(m_GeneratedTexts.size()==1);
75 
76  CStrW font;
77  if (GUI<CStrW>::GetSetting(this, "font", font) != PSRETURN_OK || font.empty())
78  // Use the default if none is specified
79  // TODO Gee: (2004-08-14) Default should not be hard-coded, but be in styles!
80  font = L"default";
81 
82  float square_side;
83  GUI<float>::GetSetting(this, "square_side", square_side);
84 
85  CGUIString caption;
86  GUI<CGUIString>::GetSetting(this, "caption", caption);
87 
88  float buffer_zone=0.f;
89  GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
90  *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, m_CachedActualSize.GetWidth()-square_side, 0.f, this);
91 }
92 
94 {
95  // Important
98 
99  switch (Message.type)
100  {
101  case GUIM_PRESSED:
102  {
103  bool checked;
104 
105  GUI<bool>::GetSetting(this, "checked", checked);
106  checked = !checked;
107  GUI<bool>::SetSetting(this, "checked", checked);
108 
109  } break;
110 
111  default:
112  break;
113  }
114 }
115 
117 {
118  ////////// Gee: janwas, this is just temp to see it
119  glDisable(GL_TEXTURE_2D);
120  //////////
121 
122  float square_side, buffer_zone;
123  CStrW font_name;
124  bool checked;
125  int cell_id;
126  GUI<float>::GetSetting(this, "square_side", square_side);
127  GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
128  GUI<CStrW>::GetSetting(this, "font", font_name);
129  GUI<bool>::GetSetting(this, "checked", checked);
130  GUI<int>::GetSetting(this, "cell_id", cell_id);
131 
132  // Get line height
133  CFont font (font_name);
134  float line_height = (float)font.GetHeight();
135 
136  float bz = GetBufferedZ();
137 
138  // Get square
139  CRect rect;
140 
142  rect.right = rect.left + square_side;
143 
144  if (square_side >= line_height)
145  rect.top = m_CachedActualSize.top;
146  else
147  rect.top = m_CachedActualSize.top + line_height/2.f - square_side/2.f;
148 
149  rect.bottom = rect.top + square_side;
150 
151  CGUISpriteInstance *sprite, *sprite_over, *sprite_pressed, *sprite_disabled;
152 
153  if (checked)
154  {
155  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2", sprite);
156  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2_over", sprite_over);
157  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2_pressed", sprite_pressed);
158  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite2_disabled", sprite_disabled);
159  }
160  else
161  {
162  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite", sprite);
163  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite_over", sprite_over);
164  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite_pressed", sprite_pressed);
165  GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite_disabled", sprite_disabled);
166  }
167 
168  DrawButton(rect,
169  bz,
170  *sprite,
171  *sprite_over,
172  *sprite_pressed,
173  *sprite_disabled,
174  cell_id);
175 
176  CColor color = ChooseColor();
177 
178  CPos text_pos(m_CachedActualSize.left + square_side + buffer_zone, m_CachedActualSize.top);
179 
180  if (square_side > line_height)
181  text_pos.y += square_side/2.f - line_height/2.f;
182 
183  DrawText(0, color, text_pos, bz+0.1f, m_CachedActualSize);
184 }
virtual ~CCheckBox()
Definition: CCheckBox.cpp:65
const PSRETURN PSRETURN_OK
Definition: Errors.h:103
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
void SetupText()
Sets up text, should be called every time changes has been made that can change the visual...
Definition: CCheckBox.cpp:69
CColor ChooseColor()
Choosing which color of the following according to object enabled/hovered/pressed status: textcolor_d...
virtual float GetBufferedZ() const
Returns not the Z value, but the actual buffered Z value, i.e.
Definition: IGUIObject.cpp:406
void DrawButton(const CRect &rect, const float &z, CGUISpriteInstance &sprite, CGUISpriteInstance &sprite_over, CGUISpriteInstance &sprite_pressed, CGUISpriteInstance &sprite_disabled, int cell_id)
This is a function that lets a button being drawn, it regards if it&#39;s over, disabled, pressed and such.
float GetWidth() const
Definition: Overlay.cpp:232
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
int GetHeight()
Definition: Font.cpp:60
virtual void HandleMessage(SGUIMessage &Message)
Definition: CCheckBox.cpp:93
Definition: Font.h:28
virtual void HandleMessage(SGUIMessage &Message)
void AddSetting(const EGUISettingType &Type, const CStr &Name)
Add a setting to m_Settings.
Definition: IGUIObject.cpp:172
virtual void Draw()
Draws the control.
Definition: CCheckBox.cpp:116
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
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
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
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
virtual void HandleMessage(SGUIMessage &Message)
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