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

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 {
		help();
	}
}

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) {

	} 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("ui", func_ui);
	func->set_info("[command] [options] user interface subcommands");

	func = core::Func::add("menu", func_menu);
	func->set_info("[hide|close|menuname] show or hide a menu");
}

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

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

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

void frame()
{
	if (global_ui)
		global_ui->draw_event();
}

UI::UI() : Window(0)
{
	set_palette(new Palette());
	set_label("user interface");
	set_size(1024, 768);
	set_border(false);

	ui_active_window = 0;

	Menu *menu = 0;

	menu = new Menu(this, "main");
	menu->add_label("Main menu");
	menu->add_button("New Game", "connect");
	menu->add_button("Connect to...", "menu connect");
	menu->add_button("Options...", "menu options");
	menu->add_button("Quit", "quit");

	menu = new Menu(this, "options");
	menu->add_label("Options menu");
	
	menu = new Menu(this, "connect");
	menu->add_label("Connect to...");
}	

UI::~UI()
{
	if (palette())
		delete palette();
}

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

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

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

void UI::show_window(char const *label)
{
	Windows::iterator it;
	for (it = window_children.begin(); it != window_children.end(); it++) {
		if ((*it)->label().compare(label) == 0) {
			if (ui_active_window)
				ui_active_window->hide();
			ui_active_window = (*it);
			ui_active_window->resize_event();
			ui_active_window->show();
			return;
		}
	}

	con_warn << "Unknown window '" << label << "'" << std::endl;
}

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

}