1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/*
ui/widget.h
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#ifndef __INCLUDED_UI_WIDGET_H__
#define __INCLUDED_UI_WIDGET_H__
#include "SDL/SDL.h"
#include <string>
#include <list>
#include "auxiliary/functions.h"
#include "math/color.h"
#include "math/vector2f.h"
#include "ui/font.h"
#include "ui/palette.h"
#include "ui/definitions.h"
#include "sys/sys.h"
namespace ui
{
class Widget
{
public:
/// create a new widget
Widget(Widget *parent=0);
/// destroy a widget
virtual ~Widget();
/// parent widget this widget belongs to
inline Widget *parent() const {
return widget_parent;
}
/* -- inspectors -------------------------------------------- */
/// pixel coordinates of the top-left corner of this widget within its parent
inline const math::Vector2f &location() const {
return widget_location;
}
/// size of this widget in pixels
inline const math::Vector2f &size() const {
return widget_size;
}
/// x coordinate of the left of the widget
/**
* @see location()
*/
inline float left() const {
return widget_location.x;
}
/// x coordinate of the right of the widget
/**
* @see location()
* @see size()
*/
inline float right() const {
return widget_location.x + widget_size.x;
}
/// y coordinate of the top of the widget
/**
* @see location()
*/
inline float top() const {
return widget_location.y;
}
/// y coordinate of the bottom of the widget
/**
* @see location()
* @see size()
*/
inline float bottom() const {
return widget_location.y + widget_size.y;
}
/// width of the widget in pixels
/**
* @see size()
*/
inline float width() const {
return widget_size.x;
}
/// height of the widget in pixels
/**
* @see size()
*/
inline float height() const {
return widget_size.y;
}
/// widget label
inline const std::string &label() const {
return widget_label;
}
/// true if this widget will draw a background
inline bool background() const {
return widget_background;
}
/// true if this widget will draw a border
inline bool border() const {
return widget_border;
}
/// true if this widget is visible
inline bool visible() const {
return widget_visible;
}
/// true if this widget is not visible
inline bool hidden() const {
return !widget_visible;
}
/// the palette used to draw this widget
const Palette *palette() const;
/// the font used to draw this widget
const Font *font() const;
/// return true if the widget has input focus
bool has_input_focus() const;
/// returns true if the widget has mouse focus
bool has_mouse_focus() const;
/* -- mutators --------------------------------------------- */
/// raise the widget to the top of the widget stack
void raise();
/// lower the widget to the bottom of the widget stack
void lower();
/// show the widget
virtual void show();
/// hide the widget
virtual void hide();
/// set visibility
void set_visible(bool visible = true);
/// set input focus
void set_focus();
/// set the widget geometry
void set_geometry(const float x, const float y, const float w, const float h);
/// set the widget geometry
void set_geometry(const math::Vector2f &location, const math::Vector2f &size);
/// set location of the top-left corner, relative to the parent
void set_location(const float x, const float y);
/// set location of the top-left corner, relative to the parent
void set_location(const math::Vector2f &location);
/// set the widgets width and height
void set_size(const float w, const float h);
/// set the widgets width and height
void set_size(const math::Vector2f &size);
/// set the widgets width
void set_width(const float w);
/// set the widgets height
void set_height(const float h);
/// set the widgets palette
void set_palette(const Palette *palette);
/// set the widgets font
void set_font(const Font *font);
/// set the widgets label
void set_label(std::string const &label);
/// set the widgets label
void set_label(const char *label);
/// enable or disable widget border
void set_border(bool border = true);
///enable or disable widget background
void set_background(bool background = true);
/* -- event distributors ----------------------------------- */
/// distribute resize event
virtual void event_resize();
/// distribute draw event
virtual void event_draw();
/// distribute keyboard events
virtual bool event_key(const bool pressed, const int key, const unsigned int modifier);
/// distribute mouse movement events
virtual bool event_mouse(const math::Vector2f &cursor);
protected:
/// type definition for child widgets
typedef std::list<Widget *> Children;
/// child widgets
inline Children &children() {
return widget_children;
}
/// find the widget that has input focus
virtual Widget *find_input_focus();
/// find widget that has mosue focus
/** @param cursor mouse cursor position relative to this widget's location
*/
Widget *find_mouse_focus(const math::Vector2f & cursor);
/// list widget content
size_t list(const size_t indent, const bool visible_only = false) const;
/// print widget description
virtual void print(const size_t indent) const;
/// true of this sibling has local focus
inline bool focus() const { return widget_focus; }
/* -- coordinate mapping ----------------------------------- */
/// map local coordinates to global coordinates
inline math::Vector2f to_global_coords(const math::Vector2f &local) {
math::Vector2f v(local);
Widget *parent = widget_parent;
do {
v -= parent->location();
parent = parent->parent();
} while (parent);
return v;
}
/// map global coordinates to local coordinates
inline math::Vector2f to_local_coords(const math::Vector2f &global) {
math::Vector2f v(global);
Widget *parent = this;
while (parent) {
v += parent->location();
parent = parent->parent();
}
return v;
}
/// map local widget location to global location
inline math::Vector2f global_location() {
math::Vector2f v(widget_location);
Widget *parent = widget_parent;
while (parent) {
v += parent->location();
parent = parent->parent();
}
return v;
}
/* -- event handlers --------------------------------------- */
/// called when the mouse receives mouse movement
virtual void on_mousemove(const math::Vector2f &cursor);
/// called when the mouse enters the widget
virtual void on_mouseover(const math::Vector2f &cursor);
/// called when the widget receives a key press
virtual bool on_keypress(const int key, const unsigned int modifier);
/// called when the widget receives a key release
virtual bool on_keyrelease(const int key, const unsigned int modifier);
/* -- draw functions --------------------------------------- */
/// resize event
virtual void resize();
/// draw the widget
virtual void draw();
/// draw the widget background
virtual void draw_background();
/// draw the widget border
virtual void draw_border();
/// add a child widget
virtual void add_child(Widget *child);
/// remove a child widget
virtual void remove_child(Widget *child);
/// remove all child widgets
virtual void remove_children();
private:
void draw_debug_border();
bool widget_visible;
bool widget_background;
bool widget_border;
bool widget_focus;
math::Vector2f widget_location;
math::Vector2f widget_size;
std::string widget_label;
Children widget_children;
const Palette *widget_palette;
const Font *widget_font;
Widget *widget_parent;
Children::iterator find_child(Widget *child);
};
}
#endif // __INCLUDED_UI_WIDGET_H__
|