Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CButton.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 CButton
20 */
21 
22 #include "precompiled.h"
23 
24 #include "CButton.h"
25 
26 #include "lib/ogl.h"
27 
28 
29 //-------------------------------------------------------------------
30 // Constructor / Destructor
31 //-------------------------------------------------------------------
33 {
34  AddSetting(GUIST_float, "buffer_zone");
35  AddSetting(GUIST_CGUIString, "caption");
36  AddSetting(GUIST_int, "cell_id");
37  AddSetting(GUIST_CStrW, "font");
38  AddSetting(GUIST_CStrW, "sound_disabled");
39  AddSetting(GUIST_CStrW, "sound_enter");
40  AddSetting(GUIST_CStrW, "sound_leave");
41  AddSetting(GUIST_CStrW, "sound_pressed");
42  AddSetting(GUIST_CStrW, "sound_released");
43  AddSetting(GUIST_CGUISpriteInstance, "sprite");
44  AddSetting(GUIST_CGUISpriteInstance, "sprite_over");
45  AddSetting(GUIST_CGUISpriteInstance, "sprite_pressed");
46  AddSetting(GUIST_CGUISpriteInstance, "sprite_disabled");
47  AddSetting(GUIST_EAlign, "text_align");
48  AddSetting(GUIST_EVAlign, "text_valign");
49  AddSetting(GUIST_CColor, "textcolor");
50  AddSetting(GUIST_CColor, "textcolor_over");
51  AddSetting(GUIST_CColor, "textcolor_pressed");
52  AddSetting(GUIST_CColor, "textcolor_disabled");
53  AddSetting(GUIST_CStrW, "tooltip");
54  AddSetting(GUIST_CStr, "tooltip_style");
55 
56  // Add text
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  // Use the default if none is specified
74  // TODO Gee: (2004-08-14) Default should not be hard-coded, but be in styles!
75  font = L"default";
76 
77  CGUIString caption;
78  GUI<CGUIString>::GetSetting(this, "caption", caption);
79 
80  float buffer_zone=0.f;
81  GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
82  *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, m_CachedActualSize.GetWidth(), buffer_zone, this);
83 
85 }
86 
88 {
89  // Important
92 }
93 
95 {
96  float bz = GetBufferedZ();
97 
98  CGUISpriteInstance *sprite, *sprite_over, *sprite_pressed, *sprite_disabled;
99  int cell_id;
100 
101  // Statically initialise some strings, so we don't have to do
102  // lots of allocation every time this function is called
103  static CStr strSprite("sprite");
104  static CStr strSpriteOver("sprite_over");
105  static CStr strSpritePressed("sprite_pressed");
106  static CStr strSpriteDisabled("sprite_disabled");
107  static CStr strCellId("cell_id");
108 
109  GUI<CGUISpriteInstance>::GetSettingPointer(this, strSprite, sprite);
110  GUI<CGUISpriteInstance>::GetSettingPointer(this, strSpriteOver, sprite_over);
111  GUI<CGUISpriteInstance>::GetSettingPointer(this, strSpritePressed, sprite_pressed);
112  GUI<CGUISpriteInstance>::GetSettingPointer(this, strSpriteDisabled, sprite_disabled);
113  GUI<int>::GetSetting(this, strCellId, cell_id);
114 
116  bz,
117  *sprite,
118  *sprite_over,
119  *sprite_pressed,
120  *sprite_disabled,
121  cell_id);
122 
123  CColor color = ChooseColor();
124  DrawText(0, color, m_TextPos, bz+0.1f);
125 }
CButton()
Definition: CButton.cpp:32
virtual void HandleMessage(SGUIMessage &Message)
Definition: CButton.cpp:87
const PSRETURN PSRETURN_OK
Definition: Errors.h:103
Definition: Overlay.h:34
static PSRETURN GetSetting(const IGUIObject *pObject, const CStr &Setting, T &Value)
Retrieves a setting by name from object pointer.
Definition: GUIutil.cpp:344
CColor ChooseColor()
Choosing which color of the following according to object enabled/hovered/pressed status: textcolor_d...
virtual ~CButton()
Definition: CButton.cpp:60
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.
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
void SetupText()
Sets up text, should be called every time changes has been made that can change the visual...
Definition: CButton.cpp:64
virtual void HandleMessage(SGUIMessage &Message)
void AddSetting(const EGUISettingType &Type, const CStr &Name)
Add a setting to m_Settings.
Definition: IGUIObject.cpp:172
CPos m_TextPos
Placement of text.
Definition: CButton.h:90
CRect m_CachedActualSize
Cached size, real size m_Size is actually dependent on resolution and can have different real outcome...
Definition: IGUIObject.h:434
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
virtual void Draw()
Draws the Button.
Definition: CButton.cpp:94
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
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