Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IGUIButtonBehavior.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 IGUIButtonBehavior
20 */
21 
22 #include "precompiled.h"
23 
24 #include "GUI.h"
25 
27 
28 //-------------------------------------------------------------------
29 // Constructor / Destructor
30 //-------------------------------------------------------------------
32 {
33 }
34 
36 {
37 }
38 
40 {
41  // TODO Gee: easier access functions
42  switch (Message.type)
43  {
44  case GUIM_MOUSE_ENTER:
45  {
46  bool enabled;
47  GUI<bool>::GetSetting(this, "enabled", enabled);
48  if (enabled)
49  {
50  CStrW soundPath;
51  if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_enter", soundPath) == PSRETURN_OK && !soundPath.empty())
52  g_SoundManager->PlayAsUI(soundPath.c_str(), false);
53  }
54  }
55  break;
56 
57  case GUIM_MOUSE_LEAVE:
58  {
59  bool enabled;
60  GUI<bool>::GetSetting(this, "enabled", enabled);
61  if (enabled)
62  {
63  CStrW soundPath;
64  if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_leave", soundPath) == PSRETURN_OK && !soundPath.empty())
65  g_SoundManager->PlayAsUI(soundPath.c_str(), false);
66  }
67  }
68  break;
69 
71  {
72  bool enabled;
73  GUI<bool>::GetSetting(this, "enabled", enabled);
74 
75  if (!enabled)
76  {
77  CStrW soundPath;
78  if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_disabled", soundPath) == PSRETURN_OK && !soundPath.empty())
79  g_SoundManager->PlayAsUI(soundPath.c_str(), false);
80  break;
81  }
82 
83  CStrW soundPath;
84  if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_pressed", soundPath) == PSRETURN_OK && !soundPath.empty())
85  g_SoundManager->PlayAsUI(soundPath.c_str(), false);
86 
87  m_Pressed = true;
88  }
89  break;
90 
93  {
94  bool enabled;
95  GUI<bool>::GetSetting(this, "enabled", enabled);
96 
97  if (!enabled)
98  break;
99 
100  if (m_PressedRight)
101  {
102  m_PressedRight = false;
103  if (Message.type == GUIM_MOUSE_RELEASE_RIGHT)
104  {
105  // Button was right-clicked
106  SendEvent(GUIM_PRESSED_MOUSE_RIGHT, "pressright");
107  }
108  else
109  {
110  // Button was clicked a second time. We can't tell if the button
111  // expects to receive doublepress events or just a second press
112  // event, so send both of them (and assume the extra unwanted press
113  // is harmless on buttons that expect doublepress)
114  SendEvent(GUIM_PRESSED_MOUSE_RIGHT, "pressright");
115  SendEvent(GUIM_DOUBLE_PRESSED_MOUSE_RIGHT, "doublepressright");
116  }
117 
118  CStrW soundPath;
119  if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_released", soundPath) == PSRETURN_OK && !soundPath.empty())
120  g_SoundManager->PlayAsUI(soundPath.c_str(), false);
121  }
122  }
123  break;
124 
126  {
127  bool enabled;
128  GUI<bool>::GetSetting(this, "enabled", enabled);
129 
130  if (!enabled)
131  {
132  CStrW soundPath;
133  if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_disabled", soundPath) == PSRETURN_OK && !soundPath.empty())
134  g_SoundManager->PlayAsUI(soundPath.c_str(), false);
135  break;
136  }
137 
138  CStrW soundPath;
139  if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_pressed", soundPath) == PSRETURN_OK && !soundPath.empty())
140  g_SoundManager->PlayAsUI(soundPath.c_str(), false);
141 
142  m_PressedRight = true;
143  }
144  break;
145 
148  {
149  bool enabled;
150  GUI<bool>::GetSetting(this, "enabled", enabled);
151 
152  if (!enabled)
153  break;
154 
155  if (m_Pressed)
156  {
157  m_Pressed = false;
158  if (Message.type == GUIM_MOUSE_RELEASE_LEFT)
159  {
160  // Button was clicked
161  SendEvent(GUIM_PRESSED, "press");
162  }
163  else
164  {
165  // Button was clicked a second time. We can't tell if the button
166  // expects to receive doublepress events or just a second press
167  // event, so send both of them (and assume the extra unwanted press
168  // is harmless on buttons that expect doublepress)
169  SendEvent(GUIM_PRESSED, "press");
170  SendEvent(GUIM_DOUBLE_PRESSED, "doublepress");
171  }
172 
173  CStrW soundPath;
174  if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_released", soundPath) == PSRETURN_OK && !soundPath.empty())
175  g_SoundManager->PlayAsUI(soundPath.c_str(), false);
176  }
177  }
178  break;
179 
180  default:
181  break;
182  }
183 }
184 
186 {
187  CColor color, color2;
188 
189  // Yes, the object must possess these settings. They are standard
190  GUI<CColor>::GetSetting(this, "textcolor", color);
191 
192  bool enabled;
193  GUI<bool>::GetSetting(this, "enabled", enabled);
194 
195  if (!enabled)
196  {
197  GUI<CColor>::GetSetting(this, "textcolor_disabled", color2);
198  return GUI<>::FallBackColor(color2, color);
199  }
200  else
201  if (m_MouseHovering)
202  {
203  if (m_Pressed)
204  {
205  GUI<CColor>::GetSetting(this, "textcolor_pressed", color2);
206  return GUI<>::FallBackColor(color2, color);
207  }
208  else
209  {
210  GUI<CColor>::GetSetting(this, "textcolor_over", color2);
211  return GUI<>::FallBackColor(color2, color);
212  }
213  }
214  else return color;
215 }
216 
218  const float &z,
219  CGUISpriteInstance& sprite,
220  CGUISpriteInstance& sprite_over,
221  CGUISpriteInstance& sprite_pressed,
222  CGUISpriteInstance& sprite_disabled,
223  int cell_id)
224 {
225  if (GetGUI())
226  {
227  bool enabled;
228  GUI<bool>::GetSetting(this, "enabled", enabled);
229 
230  if (!enabled)
231  {
232  GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_disabled, sprite), cell_id, z, rect);
233  }
234  else
235  if (m_MouseHovering)
236  {
237  if (m_Pressed)
238  GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_pressed, sprite), cell_id, z, rect);
239  else
240  GetGUI()->DrawSprite(GUI<>::FallBackSprite(sprite_over, sprite), cell_id, z, rect);
241  }
242  else GetGUI()->DrawSprite(sprite, cell_id, z, rect);
243  }
244 }
void DrawSprite(const CGUISpriteInstance &Sprite, int CellID, const float &Z, const CRect &Rect, const CRect &Clipping=CRect())
Draw GUI Sprite.
Definition: CGUI.cpp:467
const PSRETURN PSRETURN_OK
Definition: Errors.h:103
ISoundManager * g_SoundManager
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
virtual void PlayAsUI(const VfsPath &itemPath, bool looping)=0
static CColor FallBackColor(const CColor &prim, const CColor &sec)
Same principle as FallBackSprite.
Definition: GUIutil.h:215
CColor ChooseColor()
Choosing which color of the following according to object enabled/hovered/pressed status: textcolor_d...
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.
Includes static functions that needs one template argument.
Definition: GUIutil.h:103
bool m_Pressed
Everybody knows how a button works, you don&#39;t simply press it, you have to first press the button...
InReaction SendEvent(EGUIMessageType type, const CStr &EventName)
Send event to this GUI object (HandleMessage and ScriptEvent)
Definition: IGUIObject.cpp:466
CGUI * GetGUI()
Definition: IGUIObject.h:388
bool m_MouseHovering
This is an array of true or false, each element is associated with a string representing a setting...
Definition: IGUIObject.h:539
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
virtual void HandleMessage(SGUIMessage &Message)
Rectangle class used for screen rectangles.
Definition: Overlay.h:71