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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
/*
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 "filesystem/filesystem.h"
#include "render/gl.h"
#include "sys/sys.h"
#include "ui/button.h"
#include "ui/label.h"
#include "ui/menu.h"
#include "ui/paint.h"
#include "ui/ui.h"
#include "ui/widget.h"
namespace ui
{
/* -- static functions --------------------------------------------- */
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 [name] show a menu" << std::endl;
con_print << " menu back return to the previous menu" << std::endl;
con_print << " menu previous return to the previous menu" << std::endl;
con_print << " menu close close the current menu" << std::endl;
con_print << " menu hide hide the current 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_menu();
} else if (command.compare("close") == 0) {
root()->hide_menu();
} else if (command.compare("back") == 0) {
root()->previous_menu();
} else if (command.compare("previous") == 0) {
root()->previous_menu();
} else if (command.compare("list") == 0) {
root()->list_menus();
} else {
root()->show_menu(command.c_str());
}
}
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;
}
global_ui->load();
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;
}
}
/* -- class UI ----------------------------------------------------- */
UI::UI() : Window(0)
{
set_label("root");
set_size(1024, 768);
set_border(false);
set_background(false);
// default palette
ui_palette = new Palette();
set_palette(ui_palette);
// default fonts
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");
}
UI::~UI()
{
delete ui_palette;
delete ui_font_small;
delete ui_font_large;
}
/*
remove all existing child windows and load ini/ui.ini
*/
void UI::load()
{
Menus::iterator it;
for (it = ui_menus.begin(); it != ui_menus.end(); it++) {
Window *menu = (*it);
remove_child(menu);
}
ui_menus.clear();
ui_mouse_focus = this;
ui_active_menu = 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());
add_menu(menu);
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 if (ini.got_key_color("pointer", color)) {
ui_palette->set_pointer(color);
} else if (ini.got_key_color("active", color)) {
ui_palette->set_active(color);
} else {
ini.unkown_key();
}
}
}
}
con_debug << " " << ini.name() << " " << ui_menus.size() << " menus" << std::endl;
ini.close();
// fallback main menu
if (!find_menu("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_menu("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() const
{
size_t n = Widget::list(0);
con_print << n << " user interface widgets" << std::endl;
}
UI::Menus::iterator UI::find_menu(Window *menu)
{
Menus::iterator it;
for (it = ui_menus.begin(); it != ui_menus.end(); it++) {
if ((*it) == menu)
return it;
}
return it;
}
Window *UI::find_menu(const char *label)
{
for (Menus::const_iterator it = ui_menus.begin(); it != ui_menus.end(); it++) {
if ((*it)->label().compare(label) == 0) {
return (*it);
}
}
return 0;
}
void UI::list_menus() const
{
for (Menus::const_iterator it = ui_menus.begin(); it != ui_menus.end(); it++) {
const Window *menu = (*it);
con_print << " " << menu->label() << std::endl;
}
con_print << ui_menus.size() << " menus" << std::endl;
}
void UI::add_menu(Window *menu)
{
Menus::iterator it = find_menu(menu);
if (it == ui_menus.end()) {
ui_menus.push_back(menu);
}
}
void UI::show_menu(const char *label)
{
Window *menu = find_menu(label);
if (menu) {
if (ui_active_menu) {
ui_active_menu->hide();
menu->set_previous(ui_active_menu);
} else {
menu->clear_previous();
}
ui_mouse_focus = this;
ui_input_focus = this;
ui_active_menu = menu;
ui_active_menu->event_resize();
ui_active_menu->show();
set_pointer("pointer");
} else {
con_warn << "Unknown window '" << label << "'" << std::endl;
}
}
void UI::hide_menu()
{
if (ui_active_menu) {
ui_active_menu->hide();
ui_active_menu = 0;
}
}
void UI::previous_menu()
{
if (ui_active_menu) {
if (ui_active_menu->previous().size()) {
show_menu(ui_active_menu->previous().c_str());
} else {
hide_menu();
}
}
}
void UI::frame()
{
if (ui_active_menu && !ui_active_menu->visible()) {
ui_active_menu = 0;
}
event_draw();
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);
Widget *f = find_mouse_focus(mouse_cursor);
if (f) {
f->event_mouse(mouse_cursor);
}
ui_mouse_focus = f;
}
bool UI::input_key(const bool pressed, const int key, const unsigned int modifier)
{
bool handled = false;
if (key < 512) {
// keyboard keys
Widget *f = find_input_focus();
if (f) {
handled = f->event_key(pressed, key, modifier);
}
ui_input_focus = f;
} else {
// mosue buttons
if (ui_mouse_focus)
handled = ui_mouse_focus->event_key(pressed, key, modifier);
}
return handled;
}
bool UI::on_keypress(const int key, const unsigned int modifier)
{
switch( key ) {
case SDLK_ESCAPE:
if (active()) {
hide_menu();
audio::play("ui/menu");
} else {
if (core::application()->connected()) {
show_menu("game");
audio::play("ui/menu");
}
}
return true;
break;
default:
break;
}
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;
}
paint::color(c);
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("pointers/");
texture.append(mouse_pointer_bitmap);
if (mouse_pointer_animated) {
render::gl::push();
render::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;
render::gl::rotate(angle, math::Vector3f(0, 0, 1.0f));
render::gl::translate(-mouse_cursor.x, -mouse_cursor.y, 0);
}
paint::bitmap(pos, s, texture);
if (mouse_pointer_animated) {
render::gl::pop();
}
}
}
|