Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: e3aeb3128a04aeea1fec56bab51aa75b01da3521 (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
/*
   ui/ui.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <string>
#include <sstream>

#include "audio/audio.h"
#include "auxiliary/functions.h"
#include "core/core.h"
#include "core/application.h"
#include "filesystem/filesystem.h"
#include "render/gl.h"
#include "sys/sys.h"
#include "ui/button.h"
#include "ui/label.h"
#include "ui/paint.h"
#include "ui/ui.h"
#include "ui/widget.h"

namespace ui
{

/* -- static functions --------------------------------------------- */

bool UI::ui_debug = false;

float UI::elementmargin = 16;
math::Vector2f UI::elementsize(256, 48);

UI *global_ui = 0;

UI *root()
{
	return global_ui;
}

void init()
{
	con_print << "^BInitializing user interface..." << std::endl;

	if (!global_ui) {
		global_ui = new UI();
	} else {
		con_warn << "User interface already initialized!" << std::endl;
		return;
	}
}

void shutdown()
{
	con_print << "^BShutting down user interface..." << std::endl;

	if (global_ui) {
		delete global_ui;
		global_ui = 0;
	}
}

/* -- class UI ----------------------------------------------------- */

UI::UI() : Window(0)
{
	set_label("root");
	set_size(1024, 768);
	set_border(false);
	set_background(false);

	// intialize console
	ui_console = new Console(this);

	// default palette
	ui_palette = new Palette();
	set_palette(ui_palette);

	// default fonts
	ui_font_tiny = new Font("gui", 10, 18);
	ui_font_small = new Font("gui", 12, 18);
	ui_font_large = new Font("gui", 14, 24);
	set_font(ui_font_small);

	ui_mouse_focus = this;
	ui_input_focus = this;
	set_focus();

	mouse_pointer_color = Palette::Pointer;
	mouse_pointer_bitmap.assign("pointer");
	
	mouse_buttonleft_pressed = false;
}

UI::~UI()
{
	delete ui_palette;

	delete ui_font_small;
	delete ui_font_large;
}

void UI::load_settings()
{
	ui_mouse_focus = this;
	
	// open ui.ini
	std::string filename("ini/ui");
	filesystem::IniFile ini;
	ini.open(filename);

	if (!ini.is_open()) {
		con_error << "Could not open " << ini.name() << std::endl;
		return;
	}

	std::string strval;
	math::Color color;
	
	float w = elementsize.width();
	float h = elementsize.height();
	float m = elementmargin;


	while (ini.getline()) {

		if (ini.got_section()) {
		
			if (ini.got_section("ui")) {
				continue;

			// section default colors
			} else if (ini.got_section("colors")) {
				continue;

			// section hud configuration
			} else if (ini.got_section("hud")) {
				continue;

			// section text colors
			} else if (ini.got_section("text")) {
				continue;

			} else {
				ini.unknown_section();
				continue;
			}

		} else if (ini.got_key()) {

			if (ini.in_section("ui")) {
				if (ini.got_key_float("elementwidth", w)) {
					elementsize.assign(w, h);
					continue;
				} else if (ini.got_key_float("elementheight", h)) {
					elementsize.assign(w, h);
					continue;
				} else if (ini.got_key_float("elementmargin", m)) {
					elementmargin = m;
					continue;
				} else {
					ini.unknown_key();
					continue;
				}

			} else if (ini.in_section("colors")) {

				if (ini.got_key_color("foreground", color)) {
					ui_palette->set_foreground(color);
					continue;
				} else if (ini.got_key_color("background", color)) {
					ui_palette->set_background(color);
					continue;
				} else if (ini.got_key_color("border", color)) {
					ui_palette->set_border(color);
					continue;
				} else if (ini.got_key_color("text", color)) {
					ui_palette->set_text(color);
					continue;
				} else if (ini.got_key_color("highlight", color)) {
					ui_palette->set_highlight(color);
					continue;
				} else if (ini.got_key_color("disabled", color)) {
					ui_palette->set_disabled(color);
					continue;
				} else if (ini.got_key_color("pointer", color)) {
					ui_palette->set_pointer(color);
					continue;
				} else if (ini.got_key_color("active", color)) {
					ui_palette->set_active(color);
					continue;
				} else if (ini.got_key_color("debug", color)) {
					ui_palette->set_debug(color);
					continue;
				} else {
					ini.unknown_key();
					continue;
				}

			} else if (ini.in_section("hud")) {

				if (ini.got_key_color("mission", color)) {
					ui_palette->set_mission(color);
					continue;
				} else {
					ini.unknown_key();
					continue;
				}

			} else if (ini.in_section("text")) {

				if (ini.got_key_color("bold", color)) {
					ui_palette->set_bold(color);

				} else if (ini.got_key_color("fancy", color)) {
					ui_palette->set_fancy(color);

				} else if (ini.got_key_color("warning", color)) {
					ui_palette->set_warning(color);

				} else if (ini.got_key_color("error", color)) {
					ui_palette->set_error(color);
				}
			
			}
		}
	}
		
	// apply palette colors to the render subsystem
	Paint::assign_system_color('N', palette()->text());
	Paint::assign_system_color('D', palette()->debug());
	Paint::assign_system_color('B', palette()->bold());
	Paint::assign_system_color('F', palette()->fancy());
	Paint::assign_system_color('W', palette()->warning());
	Paint::assign_system_color('E', palette()->error());
		
	ini.close();
}

void UI::list() const
{
	size_t n = Widget::list(0);
	con_print << n << " user interface widgets" << std::endl;
}

void UI::list_visible() const
{
	size_t n = Widget::list(0, true);
	con_print << n << " visible user interface widgets" << std::endl;
}

void UI::frame()
{
	ui_input_focus = find_input_focus();
	
	Widget *f = 0;
	if (!mouse_buttonleft_pressed) {
		f = find_mouse_focus(mouse_cursor);
	} else {
		f = find_visible_child(ui_mouse_focus);
	}
	if (f) {
		f->event_mouse(mouse_cursor);
	}
	ui_mouse_focus = f;
	
	// reset mouse pointer
	ui::root()->set_pointer("pointer");
	
	// draw the widget stack
	event_draw();

	// draw the mouse pointer
	if (visible())
		draw_pointer();
}

/* -- global event handlers ---------------------------------------- */
/*
	These functions receive input events from the client input
	subsystem and distributes them to the widget hierarchy
*/
void UI::input_mouse(const float x, const float y)
{
	mouse_cursor.assign(x, y);
}

bool UI::input_mouse_button(const bool pressed, unsigned int button)
{
	bool handled = false;
	
	if (button == SDL_BUTTON_LEFT)
	{
		mouse_buttonleft_pressed = pressed;
	}
	
	// set mouse focus
	Widget *f = find_mouse_focus(mouse_cursor);
	if (f)
	{
		f->event_mouse(mouse_cursor);
	}
	ui_mouse_focus = f;

	// send mouse button events
	if (ui_mouse_focus)
	{
		handled = ui_mouse_focus->event_mouse_button(pressed, button);
	}
	return handled;
}

bool UI::input_mouse_wheel(const math::Vector2f & direction)
{
	bool handled = false;
	
	// set mouse focus
	Widget *f = find_mouse_focus(mouse_cursor);
	if (f)
	{
		f->event_mouse(mouse_cursor);
	}
	ui_mouse_focus = f;

	// send mouse wheel events
	if (ui_mouse_focus)
	{
		handled = ui_mouse_focus->event_mouse_wheel(direction);
	}
	return handled;
}

bool UI::input_key(const bool pressed, const int key, const unsigned int modifier)
{
	bool handled = false;

	// send keyboard events
	Widget *f = find_input_focus();
	if (f)
	{
		handled = f->event_key(pressed, key, modifier);
	}
	ui_input_focus = f;

	return handled;
}

bool UI::on_keypress(const int key, const unsigned int modifier)
{
	return false;
}

bool UI::on_keyrelease(const int key, const unsigned int modifier)
{
	return false;
}

void UI::set_pointer(const char *pointerbitmap, const Palette::Color color, const bool animated)
{
	if (!pointerbitmap) {
		mouse_pointer_bitmap.clear();
	} else {
		mouse_pointer_bitmap.assign(pointerbitmap);
	}
	mouse_pointer_animated = animated;
	mouse_pointer_color = color;
}

void UI::draw_pointer()
{
	if (!mouse_pointer_bitmap.size())
		return;

	math::Color c(palette()->color(mouse_pointer_color));
	if (mouse_pointer_animated) {
		c.a = 1;
	} else {
		c.a = 0.5f;
	}
	
	math::Vector2f pos(mouse_cursor.x() - pointer_size * 0.5f, mouse_cursor.y() - pointer_size * 0.5f);
	math::Vector2f s(pointer_size, pointer_size);

	std::string texture("bitmaps/pointers/");
	texture.append(mouse_pointer_bitmap);

	if (mouse_pointer_animated) {
		gl::push();
		gl::translate(mouse_cursor.x(), mouse_cursor.y(), 0);

		float angle = core::application()->time() * 0.75f - floorf(core::application()->time() * 0.75f);
		angle *= 360.0f;
		gl::rotate(angle, math::Vector3f(0, 0, 1.0f));
		gl::translate(-mouse_cursor.x(), -mouse_cursor.y(), 0);
	}

	Paint::draw_bitmap(pos, s, c, texture);

	if (mouse_pointer_animated) {
		gl::pop();
	}
}

}