Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 9aae10e911521e264d689cc7f3ce43d31d81042c (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
   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:
	/// type definition for child widgets
	typedef std::list<Widget *> Children;


	/// types of custom events a widget can emit
	enum Event {
		EventNone = 0, 
		EventButtonClicked = 1, 
		EventListItemClicked = 2, 
		EventListViewChanged = 3, 
		EventSliderChanged = 4,
		EventWindowShow = 5,
		EventWindowHide = 6
	};
	
	/// 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 -------------------------------------------- */

	/// child widgets
	inline Children &children() {
		return widget_children;
	}



	/// 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 const float left() const {
		return widget_location.x();
	}

	/// x coordinate of the right of the widget
	/**
	 * @see location()
	 * @see size()
	 */
	inline const float right() const {
		return widget_location.x() + widget_size.width();
	}

	/// y coordinate of the top of the widget
	/**
	 *  @see location()
	 */
	inline const float top() const {
		return widget_location.y();
	}

	/// y coordinate of the bottom of the widget
	/**
	 * @see location()
	 * @see size()
	 */
	inline const float bottom() const {
		return widget_location.y() + widget_size.height();
	}

	/// width of the widget in pixels
	/**
	 *  @see size()
	 */
	inline const float width() const {
		return widget_size.width();
	}

	/// height of the widget in pixels
	/**
	 *  @see size()
	 */
	inline const float height() const {
		return widget_size.height();
	}

	/// widget label
	inline const std::string &label() const {
		return widget_label;
	}

	/// true if this widget will draw a background
	inline const bool background() const {
		return widget_background;
	}

	/// true if this widget will draw a border
	inline const bool border() const {
		return widget_border;
	}

	/// true if this widget is visible
	inline const bool visible() const {
		return widget_visible;
	}

	/// true if this widget is not visible
	inline const bool hidden() const {
		return !widget_visible;
	}

	/// true if the widget is enabled
	inline const bool enabled() const {
		return widget_enabled;
	}
	
	/// true if the widget is disabled
	inline const bool disabled() const {
		return !widget_enabled;
	}
	
	/// 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;
	
	/// return true of the widget is a child widget
	bool is_child(const Widget *widget) const;

	/// return the next sibling
	Widget *next_sibling();


	/* -- 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(const bool visible = true);

	/// enable or disable the widget
	virtual void enable();

	/// enable or disable the widget
	virtual void disable();

	///set enabled or disabled state
	void set_enabled(const bool enabled = 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(const std::string &label);

	/// set the widgets label
	void set_label(const char *label);

	/// enable or disable widget border
	void set_border(const bool border = true);

	/// enable or disable widget background
	void set_background(const bool background = true);
	
	/* -- event distributors --------------------------------------- */

	/**
	 * @brief calls the resize event handler and sends the event to all child widgets
	 * @see resize
	 **/
	void event_resize();
	
	/**
	 * @brief calls the draw event handler and sends the event to all child widgets
	 * @see draw
	 **/
	void event_draw();

	/**
	 * @brief calls the key event handlers and sends unhandled keys to the parent widget
	 * @see on_keypress
	 * @see on_keyrelease
	 **/
	bool event_key(const bool pressed, const int key, const unsigned int modifier);

	/**
	 * @brief calls the mouse event handlers and sends unhandled keys to the parent widget
	 * @see on_mousemove
	 * @see on_mouseover
	 **/
	bool event_mouse(const math::Vector2f &cursor);
	
	/**
	 * @brief calls the custom event handler and sends unhandled events to the parent widget
	 * @see on_emit
	 **/
	bool event_emit(Widget *sender, const Event event, void *data = 0);

	/// emit a custom event
	inline void emit(const Event event, void *data=0) {
		event_emit(this, event, data);
	}

protected:
	
	/// 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 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;
	}

	/// 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;
	}

	/* -- 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);
	
	/// called when the widget receives a custom event
	virtual bool on_emit(Widget *sender, const Event event, void *data=0);

	/* -- 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();
	
	/// draw denug state
	virtual void draw_debug();

	/// 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:

	bool			widget_visible;
	bool			widget_background;
	bool			widget_border;
	bool			widget_focus;
	bool			widget_enabled;

	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__