Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 2078134cad0ddcb2f7d05618efd1c93cd5c44500 (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
/*
   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;

	/// create a new widget
	Widget(Widget *parent=0);

	/// destroy a widget
	virtual ~Widget();

	/* -- inspectors -------------------------------------------- */
	inline math::Vector2f &location() { return widget_location; }

	inline math::Vector2f &size() { return widget_size; }

	inline float const left() const { return widget_location.x; }

	inline float const top() const { return widget_location.y; }
	
	inline float const width() const { return widget_size.x; }

	inline float const height() const { return widget_size.y; }


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

	inline bool border() const { return widget_border; }

	inline bool background() const { return widget_background; }

	inline Widget *parent() const { return widget_parent; }

	inline bool visible() const { return widget_visible; }

	Palette const *palette() const;

	Font const *font() const;

	bool has_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
	void show();

	/// hide the widget
	void hide();

	/// set visibility
	void set_visible(bool visible = true);

	/// set location of the top-left corner
	void set_location(float const x, float const y);

	/// set the widgets width and height
	void set_size(float const w, float const h);

	/// set the widgets width and height
	void set_size(math::Vector2f const &size);

	/// set the widgets width
	void set_width(float const w);

	/// set the widgets height
	void set_height(float const h);

	/// set the widgets palette
	void set_palette(Palette *palette);

	/// set the widgets font
 	void set_font(Font *font);

	/// set the widgets label
	void set_label(std::string const &label);
	
	/// set the widgets label
	void set_label(char const *label);

	void set_border(bool border);

	void set_background(bool background);

	/// resize event
	virtual void event_resize();

	/// draw event
	virtual void event_draw();

	/// keyboard event
	virtual bool event_key(bool pressed, unsigned int key, unsigned int modifier);

	/// find the child widget with focus
	/** @param pos local position within the widget
	*/
	Widget *event_focus(math::Vector2f const & pos);

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

protected:
	/// handle keyboard events
	/** returns true if the event was handled by this widget
	 */
	virtual bool keypress(unsigned int key, unsigned int modifier);

	/// handle keyboard events
	/** returns true if the event was handled by this widget
	 */
	virtual bool keyrelease(unsigned int key, unsigned int modifier);

	/// draw the widget
	virtual void draw();

	/// resize the widget
	virtual void resize();

	/// draw the widget background
	virtual void draw_background();

	/// draw the widget border
	virtual void draw_border();

	/// list widget content
	size_t list(size_t indent);

	/// print widget description
	virtual void print(size_t indent);

	/// map local coordinates to global coordinates
	inline math::Vector2f to_global_coords(math::Vector2f const &local)
	{
		math::Vector2f v(local);
		Widget *parent = widget_parent;
		do {
			v -= parent->location();
			parent = parent->widget_parent;
		} while(parent);
		return v;
	}

	/// map global coordinates to local coordinates
	inline math::Vector2f to_local_coords(math::Vector2f const &global) {
		math::Vector2f v(global);
		Widget *parent = this;
		while (parent) {
			v += parent->location();
			parent = parent->widget_parent;
		}
		return v;
	}

	/// map local widget location to global location
	inline math::Vector2f global_location() const {
		math::Vector2f v(widget_location);
		Widget *parent = widget_parent;
		while (parent) {
			v += parent->location();
			parent = parent->widget_parent;
		}
		return v;
	}

	/// remove a child widget
	void remove_child(Widget *child);

private:
	bool			 widget_visible;
	bool			 widget_background;
	bool			 widget_border;
	math::Vector2f		 widget_location;
	math::Vector2f		 widget_size;
	std::string		 widget_label;
	
	Children		 widget_children;

	Palette			*widget_palette;
	Font			*widget_font;
	Widget			*widget_parent;
	
	Children::iterator find_child(Widget *child);

	void add_child(Widget *child);
};

}

#endif // __INCLUDED_UI_WIDGET_H__