Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 2b099383b696f9b6d38211583d9665c0be1170f7 (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
/*
   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 "auxiliary/functions.h"
#include "core/core.h"
#include "filesystem/filesystem.h"
#include "sys/sys.h"
#include "ui/button.h"
#include "ui/label.h"
#include "ui/menu.h"
#include "ui/ui.h"
#include "ui/widget.h"
#include "ui/window.h"

namespace ui {

UI *global_ui = 0;

void func_list_ui(std::string const &args)
{
	if (global_ui) {
		global_ui->list();
	}
}

void func_ui_restart(std::string const &args)
{
	if (global_ui) {
		global_ui->load();
	}
}

void func_list_menu(std::string const &args)
{
	if (global_ui) {
		global_ui->list_menus();
	}
}

void help()
{
	con_print << "^BUser interface functions" << std::endl;
	con_print << "  ui help          show this help" << std::endl;
	con_print << "  ui list          list widgets" << std::endl;
	con_print << "  ui show          show user interface" << std::endl;
	con_print << "  ui hide          hide user interface" << std::endl;
	con_print << "  ui restart       reload user interface files" << std::endl;
}

void func_ui(std::string const &args)
{
	if (!global_ui) {
		con_warn << "User Interface not available!" << std::endl;
		return;
	}

	if (!args.size()) {
		help();
		return;
	}
	std::stringstream argstr(args);
	std::string command;
	argstr >> command;
	aux::to_label(command);

	if (command.compare("help") == 0) {
		help();
	} else if (command.compare("list") == 0) {
		global_ui->list();
	} else if (command.compare("show") == 0) {
		global_ui->show();
	} else if (command.compare("hide") == 0) {
		global_ui->hide();
	} else if (command.compare("restart") == 0) {
		global_ui->load();
	} else {
		help();
	}
}

void help_menu()
{
	con_print << "^Bmenu functions" << std::endl;
	con_print << "  menu help          show this help" << std::endl;
	con_print << "  menu list          list available menus" << std::endl;
	con_print << "  menu hide          hide the current menu" << std::endl;
	con_print << "  menu close         close the current menu" << std::endl;
	con_print << "  menu [name]        show a menu" << std::endl;
	root()->list_menus();
}

void func_menu(std::string const &args)
{
	if (!global_ui) {
		con_warn << "User Interface not available!" << std::endl;
		return;
	}

	if (!args.size()) {
		return;
	}
	std::stringstream argstr(args);
	std::string command;
	argstr >> command;

	aux::to_label(command);

	if (command.compare("hide") == 0) {
		root()->hide_window();

	} else if (command.compare("close") == 0) {
		root()->hide_window();

	} else if (command.compare("list") == 0) {
		root()->list_menus();
	} else {
		root()->show_window(command.c_str());
	}
}

UI *root()
{
	return global_ui;
}

void init()
{
	con_print << "^BInitializing user interface..." << std::endl;
	if (!global_ui)
		global_ui = new UI();

	core::Func *func = core::Func::add("list_ui", func_list_ui);
	func->set_info("list user interface widgets");

	func = core::Func::add("list_menu", func_list_menu);
	func->set_info("list available menus");

	func = core::Func::add("ui", func_ui);
	func->set_info("[command] user interface functions");

	func = core::Func::add("ui_restart", func_ui_restart);
	func->set_info("[command] [options] reload user interface files");

	func = core::Func::add("menu", func_menu);
	func->set_info("[command] menu functions");
}

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

	core::Func::remove("list_ui");
	core::Func::remove("list_menu");
	core::Func::remove("menu");
	core::Func::remove("ui");

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

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

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

	// default fonts
	ui_font_small = new Font("gui", 12, 18);
	ui_font_medium = new Font("gui", 14, 24);
	ui_font_large = new Font("gui", 16, 30);
	set_font(ui_font_small);

	load();
}

UI::~UI()
{
	delete ui_palette;

	delete ui_font_small;
	delete ui_font_medium;
	delete ui_font_large;
}

void UI::load()
{
	Windows::iterator it;
	for (it = window_children.begin(); it != window_children.end(); it++) {
		Window *window = (*it);
		remove_child(window);
	}
	window_children.clear();
	ui_focus = this;
	ui_active_window = 0;

	std::string filename("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;
	Menu *menu = 0;
	
	while (ini.getline()) {

		if (ini.got_section()) {

			//con_debug << "  " << ini.name() << " [" << ini.section() << "]" << std::endl;

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

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

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

		} else if (ini.got_key()) {

			//con_debug << "  " << ini.name() << " " << ini.key() << "=" << ini.value() << std::endl;

			if (ini.in_section("ui")) {

				if (ini.got_key_string("menu", strval)) {
					aux::to_label(strval);
					menu = new Menu(this, strval.c_str());
					menu->load();
					continue;
				} else {
					ini.unkown_key();
				}

			} 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("highlight", color)) {
					ui_palette->set_highlight(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 {
					ini.unkown_key();
				}
			}
		}
	}
	
	con_debug << "  " << ini.name() << " " << window_children.size() << " menus" << std::endl;
	ini.close();

	// fallback main menu
	if (!find_window("main")) {
		con_warn << "menu 'main' not found, using default" << std::endl;
		Menu *menu = new Menu(this, "main");
		menu->add_label("Main Menu");
		menu->add_button("Connect", "connect");
		menu->add_button("Quit", "quit");
	}

	// fallback game menu
	if (!find_window("game")) {
		con_warn << "menu 'game' not found, using default" << std::endl;
		Menu *menu = new Menu(this, "game");
		menu->add_label("Game Menu");
		menu->add_button("Disconnect", "disconnect");
		menu->add_button("Quit", "quit");
	}
}

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

void UI::list_menus()
{
	Windows::iterator it;
	for (it = window_children.begin(); it != window_children.end(); it++) {
		Window *window = (*it);
		con_print << "  " <<  window->label() << std::endl;
	}
	con_print << window_children.size()  << " menus" << std::endl;
}

void UI::add_window(Window *window)
{
	Window::add_window(window);
	window->hide();
	window->set_font(ui_font_medium);
}

void UI::remove_window(Window *window)
{
	if (ui_active_window == window) {
		ui_active_window = 0;
		ui_focus = this;
	}

	Window::remove_window(window);
}

Window *UI::find_window(const char *label)
{
	for (Windows::iterator it = window_children.begin(); it != window_children.end(); it++) {
		if ((*it)->label().compare(label) == 0) {
			return (*it);
		}
	}
	return 0;
}

void UI::show_window(const char *label)
{
	Window *window = find_window(label);

	if (window) {
			if (ui_active_window)
				ui_active_window->hide();
			ui_active_window = window;
			ui_active_window->event_resize();
			ui_active_window->show();
			ui_focus = window;
	} else {
		con_warn << "Unknown window '" << label << "'" << std::endl;
	}
}

void UI::hide_window()
{
	if (ui_active_window) {
		ui_active_window->hide();
		ui_active_window = 0;
	}
}

void UI::frame()
{
	ui_focus = find_focus(mouse_cursor);
	event_draw();
}

void UI::event_mousemove(float x, float y)
{
	mouse_cursor.assign(x, y);
}

void UI::event_keypress(unsigned int key, unsigned int modifier)
{
	ui_focus->keypress(key, modifier);
}

void UI::event_keyrelease(unsigned int key, unsigned int modifier)
{
	ui_focus->keyrelease(key, modifier);
}

}