xoreos  0.0.5
widget.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/common/system.h"
28 
30 #include "src/engines/aurora/gui.h"
31 
32 namespace Engines {
33 
34 Widget::Widget(GUI &gui, const Common::UString &tag) : _gui(&gui), _tag(tag),
35  _parent(0), _owner(0),
36  _active(false), _visible(false), _disabled(false), _invisible(false),
37  _x(0.0f), _y(0.0f), _z(0.0f),
38  _lastClickButton(0), _lastClickTime(0), _lastClickX(0.0f), _lastClickY(0.0f) {
39 
40 }
41 
43 }
44 
46  return _tag;
47 }
48 
49 void Widget::setTag(const Common::UString &tag) {
50  assert(_tag.empty());
51 
52  _tag = tag;
53 }
54 
55 bool Widget::isActive() const {
56  return _active;
57 }
58 
59 bool Widget::isVisible() const {
60  return _visible;
61 }
62 
63 bool Widget::isDisabled() const {
64  return _disabled;
65 }
66 
67 bool Widget::isInvisible() const {
68  return _invisible;
69 }
70 
71 void Widget::show() {
72  if (_visible)
73  // Already shown, nothing to do
74  return;
75 
76  // Reset the double-click info
77  _lastClickButton = 0;
78  _lastClickTime = 0;
79  _lastClickX = 0.0f;
80  _lastClickY = 0.0f;
81 
82  if (!_invisible)
83  _visible = true;
84 
85  // Show children
86  for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it)
87  (*it)->show();
88 }
89 
90 void Widget::hide() {
91  if (!_visible)
92  // Already hidden, nothing to do
93  return;
94 
95  _visible = false;
96 
97  // Hide children
98  for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it)
99  (*it)->hide();
100 }
101 
103  return _parent;
104 }
105 
106 const Widget *Widget::getParent() const {
107  return _parent;
108 }
109 
111  for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it) {
112  if ((*it)->getTag() == childTag)
113  return *it;
114  }
115 
116  return 0;
117 }
118 
119 void Widget::setPosition(float x, float y, float z) {
120  for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it) {
121  float sX, sY, sZ;
122  (*it)->getPosition(sX, sY, sZ);
123 
124  sX -= _x;
125  sY -= _y;
126  sZ -= _z;
127 
128  (*it)->setPosition(sX + x, sY + y, sZ + z);
129  }
130 
131  _x = x;
132  _y = y;
133  _z = z;
134 }
135 
136 void Widget::movePosition(float x, float y, float z) {
137  setPosition(_x + x, _y + y, _z + z);
138 }
139 
140 void Widget::getPosition(float &x, float &y, float &z) const {
141  x = _x;
142  y = _y;
143  z = _z;
144 }
145 
146 float Widget::getWidth() const {
147  return 0.0f;
148 }
149 
150 float Widget::getHeight() const {
151  return 0.0f;
152 }
153 
154 void Widget::setDisabled(bool disabled) {
155  if (_disabled == disabled)
156  // State won't change, nothing to do
157  return;
158 
159  _disabled = disabled;
160 
161  // Disable/Enable children
162  for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it)
163  (*it)->setDisabled(disabled);
164  for (std::list<Widget *>::iterator it = _subWidgets.begin(); it != _subWidgets.end(); ++it)
165  (*it)->setDisabled(disabled);
166 }
167 
168 void Widget::setInvisible(bool invisible) {
169  if (_invisible == invisible)
170  // State won't change, nothing to do
171  return;
172 
173  _invisible = invisible;
174 
175  // Invisible the children
176  for (std::list<Widget *>::iterator it = _children.begin(); it != _children.end(); ++it)
177  (*it)->setInvisible(invisible);
178  for (std::list<Widget *>::iterator it = _subWidgets.begin(); it != _subWidgets.end(); ++it)
179  (*it)->setInvisible(invisible);
180 }
181 
183 }
184 
186 }
187 
188 void Widget::mouseMove(uint8 UNUSED(state), float UNUSED(x), float UNUSED(y)) {
189 }
190 
191 void Widget::mouseDown(uint8 UNUSED(state), float UNUSED(x), float UNUSED(y)) {
192 }
193 
194 void Widget::mouseUp(uint8 UNUSED(state), float UNUSED(x), float UNUSED(y)) {
195 }
196 
197 void Widget::mouseWheel(uint8 UNUSED(state), int UNUSED(x), int UNUSED(y)) {
198 }
199 
200 void Widget::mouseDblClick(uint8 UNUSED(state), float UNUSED(x), float UNUSED(y)) {
201 }
202 
204 }
205 
206 void Widget::addSub(Widget &widget) {
207  _subWidgets.push_back(&widget);
208 
209  widget._owner = this;
210 
211  if (!_gui->hasWidget(widget.getTag()))
212  _gui->addWidget(&widget);
213 }
214 
215 void Widget::addChild(Widget &widget) {
216  if (&widget != this)
217  _children.push_back(&widget);
218 
219  widget._parent = this;
220 }
221 
223  if (&widget != this)
224  _groupMembers.push_back(&widget);
225 }
226 
227 void Widget::removeSub(Widget &widget) {
228  for (std::list<Widget *>::iterator i = _subWidgets.begin(); i != _subWidgets.end(); ++i) {
229  if (*i == &widget) {
230  _subWidgets.erase(i);
231  break;
232  }
233  }
234 
235  widget._owner = 0;
236 }
237 
239  for (std::list<Widget *>::iterator i = _children.begin(); i != _children.end(); ++i) {
240  if (*i == &widget) {
241  _children.erase(i);
242  break;
243  }
244  }
245 
246  widget._parent = 0;
247 }
248 
250  for (std::list<Widget *>::iterator i = _groupMembers.begin(); i != _groupMembers.end(); ++i) {
251  if (*i == &widget) {
252  _groupMembers.erase(i);
253  break;
254  }
255  }
256 }
257 
259  hide();
260 
261  _gui->removeWidget(this);
262 }
263 
265  _active = false;
266 }
267 
268 void Widget::setActive(bool active) {
269  if (_active == active)
270  // State won't change, nothing to do
271  return;
272 
273  _active = active;
274 
275  // Signal our group members that we're active now
276  if (_active)
277  for (std::list<Widget *>::iterator it = _groupMembers.begin(); it != _groupMembers.end(); ++it)
278  (*it)->signalGroupMemberActive();
279 }
280 
282  _gui->callbackActive(widget);
283 }
284 
285 } // End of namespace Engines
virtual void mouseDblClick(uint8 state, float x, float y)
A mouse button was double-clicked on the widget.
Definition: widget.cpp:200
uint8 _lastClickButton
Definition: widget.h:135
float _y
The widget Y position.
Definition: widget.h:132
virtual void setInvisible(bool invisible)
Make the widget invisible.
Definition: widget.cpp:168
Widget * _owner
The widget&#39;s owner, if any.
Definition: widget.h:113
virtual void setTag(const Common::UString &tag)
Set the widget&#39;s tag.
Definition: widget.cpp:49
A class holding an UTF-8 string.
Definition: ustring.h:48
virtual void callbackActive(Widget &widget)
Callback that&#39;s triggered when a widget was activated.
Definition: gui.cpp:231
float _lastClickX
Definition: widget.h:137
bool isActive() const
Was the widget activated?
Definition: widget.cpp:55
uint8_t uint8
Definition: types.h:200
virtual void subActive(Widget &widget)
A sub-widget was activated.
Definition: widget.cpp:203
virtual void setPosition(float x, float y, float z)
Set the widget&#39;s position.
Definition: widget.cpp:119
virtual void enter()
The mouse entered the widget.
Definition: widget.cpp:182
void setActive(bool active)
The widget&#39;s active state.
Definition: widget.cpp:268
virtual void setDisabled(bool disabled)
Disable/Enable the widget.
Definition: widget.cpp:154
virtual float getWidth() const
Get the widget&#39;s width.
Definition: widget.cpp:146
virtual void addGroupMember(Widget &widget)
Add a fellow group member to the widget.
Definition: widget.cpp:222
virtual void getPosition(float &x, float &y, float &z) const
Get the widget&#39;s position.
Definition: widget.cpp:140
void remove()
Remove the widget from the GUI.
Definition: widget.cpp:258
virtual void mouseUp(uint8 state, float x, float y)
A mouse button was released on the widget.
Definition: widget.cpp:194
bool isVisible() const
Is the widget visible?
Definition: widget.cpp:59
Widget(GUI &gui, const Common::UString &tag)
Definition: widget.cpp:34
bool _visible
Is the widget visible?
Definition: widget.h:127
virtual void signalGroupMemberActive()
A fellow group member signaled that it is now active.
Definition: widget.cpp:264
GUI * _gui
The GUI the widget belongs to.
Definition: widget.h:108
virtual float getHeight() const
Get the widget&#39;s height.
Definition: widget.cpp:150
A GUI.
A GUI.
Definition: gui.h:43
bool isInvisible() const
Is the widget invisible (never visible)?
Definition: widget.cpp:67
virtual void mouseDown(uint8 state, float x, float y)
A mouse button was pressed on the widget.
Definition: widget.cpp:191
void raiseCallbackActive(Widget &widget)
Definition: widget.cpp:281
Common::UString _tag
The widget&#39;s tag.
Definition: widget.h:110
virtual ~Widget()
Definition: widget.cpp:42
Widget * getParent()
Definition: widget.cpp:102
#define UNUSED(x)
Definition: system.h:170
const Common::UString & getTag() const
Get the widget&#39;s tag.
Definition: widget.cpp:45
bool _active
Was the widget activated?
Definition: widget.h:126
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
float _lastClickY
Definition: widget.h:138
virtual void movePosition(float x, float y, float z)
Move the widget, relative to its current position.
Definition: widget.cpp:136
virtual void addChild(Widget &widget)
Add a child to the widget.
Definition: widget.cpp:215
virtual void removeChild(Widget &widget)
Remove a child from the widget.
Definition: widget.cpp:238
A widget in a GUI.
bool _disabled
Is the widget disabled?
Definition: widget.h:128
virtual void hide()
Hide the widget.
Definition: widget.cpp:90
virtual void leave()
The mouse left the widget.
Definition: widget.cpp:185
virtual void mouseWheel(uint8 state, int x, int y)
A mouse wheel was used on the widget.
Definition: widget.cpp:197
virtual void removeGroupMember(Widget &widget)
Remove fellow group member from the widget.
Definition: widget.cpp:249
virtual void removeSub(Widget &widget)
Remove a sub-widget from the widget.
Definition: widget.cpp:227
A widget in a GUI.
Definition: widget.h:40
virtual void show()
Show the widget.
Definition: widget.cpp:71
Widget * getChild(const Common::UString &childTag)
Get the widget&#39;s child by tag.
Definition: widget.cpp:110
bool _invisible
Is the widget invisible (never visible)?
Definition: widget.h:129
Low-level detection of architecture/system properties.
float _z
The widget Z position.
Definition: widget.h:133
bool isDisabled() const
Is the widget disabled?
Definition: widget.cpp:63
std::list< Widget * > _groupMembers
The widget&#39;s fellow group members.
Definition: widget.h:117
uint32 _lastClickTime
Definition: widget.h:136
void addWidget(Widget *widget)
Add a widget.
Definition: gui.cpp:250
Widget * _parent
The widget&#39;s parent, if any.
Definition: widget.h:112
virtual void addSub(Widget &widget)
Add a sub-widget to the widget.
Definition: widget.cpp:206
std::list< Widget * > _subWidgets
The widget&#39;s sub-widgets.
Definition: widget.h:115
virtual void mouseMove(uint8 state, float x, float y)
The mouse was moved over the widget.
Definition: widget.cpp:188
bool hasWidget(const Common::UString &tag) const
Does this specific widget exist within the GUI?
Definition: gui.cpp:310
std::list< Widget * > _children
The widget&#39;s children.
Definition: widget.h:116
void removeWidget(Widget *widget)
Remove a widget.
Definition: gui.cpp:267
float _x
The widget X position.
Definition: widget.h:131