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

#include "client/buttonmenu.h"
#include "client/controlsettingsmenu.h"
#include "client/mainmenu.h"
#include "client/savegamemenu.h"
#include "core/core.h"
#include "core/gameinterface.h"
#include "core/application.h"
#include "filesystem/inifile.h"
#include "ui/paint.h"
#include "ui/ui.h"
#include "ui/label.h"
#include "ui/button.h"

namespace client
{

MainMenu::MainMenu(ui::Widget *parent) : 
	ui::Window(parent), 
	mainmenu_background("bitmaps/banner")
{
	// acive menu
	mainmenu_activemenu = 0;
	
	// default main menus 
	mainmenu_mainmenu  = 0;
	mainmenu_gamemenu  = 0;
	mainmenu_joinmenu  = 0;
	
	// default extra menus
	mainmenu_optionsmenu = 0;
	mainmenu_loadmenu = 0;
	mainmenu_savemenu = 0;
	mainmenu_connectmenu = 0;

	set_border(false);
	set_background(true);
	set_label("mainmenu");
	
	load();
}

MainMenu::~MainMenu()
{
}

void MainMenu::load_definitions()
{
	// load custom menu definitions from ini/menu.ini
	std::string filename("ini/menu");
	filesystem::IniFile ini;
	ini.open(filename);

	if (!ini.is_open()) {
		con_error << "Could not open " << ini.name() << std::endl;
		return;
	}
	
	con_debug << "  Loading menu definitions..." << std::endl;
	
	ui::Label *label = 0;
	ui::Button *button = 0;
	ButtonMenu *menu = 0;
	std::string strval;
	
	while (ini.getline()) {

		if (ini.got_section()) {
		
			if (ini.got_section("menu")) {
				menu = 0;
				continue;
				
			// menu button
			} else 	if (ini.got_section("button")) {
				
				if (!menu) {
					ini.unknown_error("button section without menu defintion");
				} else {
					button = menu->add_button();
				}
				continue;
				
			// menu label
			} else if (ini.got_section("label")) {
				
				if (!menu) {
					ini.unknown_error("label section without menu defintion");
				} else {
					label = menu->add_label();
				}
				continue;
	
			} else {
				ini.unknown_section();
				continue;
			}

		} else if (ini.got_key()) {
			
			if (ini.in_section("menu")) {
				
				if (ini.got_key_label("label", strval)) {
					
					for (ui::Widget::Children::iterator child = children().begin(); child != children().end(); ++child) {
						if ((*child)->label().compare(strval) == 0) {
							// assert child is a menu
							menu = static_cast<ButtonMenu*>((*child));
						}
					}

					if (!menu) {
						menu = new ButtonMenu(this, strval.c_str());
					}
				} else {
					ini.unknown_key();
				}
				
			} else if (menu) {
				
				if (ini.in_section("button")) {
					
					if (ini.got_key_string("text", strval)) {
						aux::strip_quotes(strval);
						button->set_text(strval);

					} else if (ini.got_key_string("command", strval)) {
						for (size_t i = 0; i <= strval.size(); i++) {
							if (strval[i] == ',') strval[i] = ';';
						}
						aux::strip_quotes(strval);
						button->set_command(strval);

					} else if (ini.got_key_string("align", strval)) {
						aux::to_label(strval);
						if (strval.compare("left") == 0) {
							button->set_alignment(ui::AlignLeft | ui::AlignVCenter);
						} else if (strval.compare("center") == 0) {
							button->set_alignment(ui::AlignCenter);
						} else if (strval.compare("right") == 0) {
							button->set_alignment(ui::AlignRight | ui::AlignVCenter);
						} else {
							ini.unknown_value();
						}
					} else {
						ini.unknown_key();
					}
					
				} else if (ini.in_section("label")) {
					
					if (ini.got_key_string("text", strval)) {
						label->set_text(strval);
					} else if (ini.got_key_string("align", strval)) {
						aux::to_label(strval);
						if (strval.compare("left") == 0) {
							label->set_alignment(ui::AlignLeft | ui::AlignHCenter);
						} else if (strval.compare("center") == 0) {
							label->set_alignment(ui::AlignCenter);
						} else if (strval.compare("right") == 0) {
							label->set_alignment(ui::AlignRight | ui::AlignHCenter);
						} else {
							ini.unknown_value();
						}
					} else {
						ini.unknown_key();
					}
				}
			}
		}
	}
		
	ini.close();
}

void MainMenu::load()
{
	remove_children();
	
	// main menu when not connected
	mainmenu_mainmenu = (ui::Widget *) new ButtonMenu(this, "main");
	static_cast<ButtonMenu *>(mainmenu_mainmenu)->set_compact();
	
	// main menu when connected
	mainmenu_gamemenu = (ui::Widget *) new ButtonMenu(this, "game");
	static_cast<ButtonMenu *>(mainmenu_gamemenu)->set_compact();
	
	// menu to join a game
	mainmenu_joinmenu = (ui::Widget *) new ButtonMenu(this, "join");
	static_cast<ButtonMenu *>(mainmenu_joinmenu)->set_compact();
	
	// control settings menu
	new ControlSettingsMenu(this, "controls");
	
	// load custom menus, this needs to be done before the
	// non-buttonmenu child widgets are created	
	load_definitions();
	
	// options menu
	mainmenu_optionsmenu = 0;
	
	// menu to connect to remote servers
	mainmenu_connectmenu = 0;
	
	// load game menu
	mainmenu_loadmenu = (ui::Widget *) new SaveGameMenu(this, "load", SaveGameMenu::Load);
	
	// save game menu
	mainmenu_savemenu = (ui::Widget *) new SaveGameMenu(this, "save", SaveGameMenu::Save);
	
	show_menu();
}

void MainMenu::show_menu()
{
	mainmenu_activemenu = 0;
	
	if (core::application()->connected()) {
	
		// show the main menu on non-interactive modules
		if (!core::game()->interactive()) {
			mainmenu_activemenu = mainmenu_mainmenu;

		// show the join menu if player does not control an entity
		} else if (!core::localcontrol()) {
			mainmenu_activemenu = mainmenu_joinmenu;
			
		} else {
			mainmenu_activemenu = mainmenu_gamemenu;
		}
		
	} else {
		mainmenu_activemenu = mainmenu_mainmenu;
	}

	// show the requested menu and hide all other children
	for (ui::Widget::Children::iterator child = children().begin(); child != children().end(); child++) {
		if ((*child) == mainmenu_activemenu) {
			(*child)->show();
		} else {
			(*child)->hide();
		}
	}
}

void MainMenu::show_default()
{
	if (core::application()->connected() && core::game()->interactive()) {
		show_menu(mainmenu_gamemenu);
	} else {
		show_menu(mainmenu_mainmenu);
	}

}
void MainMenu::show_menu(const Widget *widget)
{
	mainmenu_activemenu = 0;
	
	// show the requested menu and hide all other children
	for (ui::Widget::Children::iterator child = children().begin(); child != children().end(); child++) {
		if ((*child)== widget) {
			(*child)->show();
			mainmenu_activemenu = (*child);
		} else {
			(*child)->hide();
		}
	}
	
	show();
}

void MainMenu::show_menu(const char * label)
{
	mainmenu_activemenu = 0;
	
	for (ui::Widget::Children::iterator child = children().begin(); child != children().end(); ++child) {
		if ((*child)->label().compare(label) == 0) {
			(*child)->show();
			mainmenu_activemenu = (*child);
		} else {
			(*child)->hide();
		}
	}

	show();
}

void MainMenu::show()
{
	if (!mainmenu_activemenu)
		show_menu();
	ui::Window::show();
}

void MainMenu::hide()
{
	ui::Window::hide();
	mainmenu_activemenu = 0;
}

void MainMenu::resize()
{	
	// resize and reposition all child windows
	const float smallmargin = ui::UI::elementsize.height();
	
	for (ui::Widget::Children::iterator child = children().begin(); child != children().end(); ++child) {
		(*child)->set_size(width() - smallmargin * 2, height() -  smallmargin * 4);
		(*child)->set_location(smallmargin, smallmargin * 2);
	}
}

void MainMenu::draw_background()
{
	// we override draw_background instead of adding a ui::Bitmap child
	// this simplifies child window managment
	if (mainmenu_background.size()) {
		ui::Paint::draw_bitmap(global_location(), size(), mainmenu_background, true);
	}
}

void MainMenu::draw()
{
	if (mainmenu_activemenu == mainmenu_mainmenu) {
		if (core::game()->interactive()) {
			hide();
			return;
		}
		
	} else if (mainmenu_activemenu == mainmenu_joinmenu) {
		if (!core::game()->interactive()) {
			hide();
			return;
		}
		if (core::localcontrol()) {
			hide();
			return;
		}
		
	} else if (mainmenu_activemenu == mainmenu_gamemenu) {
		if (!core::game()->interactive()) {
			hide();
			return;
		}
	}
	
	ui::Window::draw();
}

bool MainMenu::on_keypress(const int key, const unsigned int modifier)
{
	if (key == SDLK_ESCAPE) {
		if (mainmenu_activemenu == mainmenu_mainmenu) {
			return true;
			
		} else if (mainmenu_activemenu == mainmenu_joinmenu) {
			show_menu(mainmenu_gamemenu);
			return true;
			
		} else if (mainmenu_activemenu == mainmenu_gamemenu) {
			if (core::localcontrol()) {
				hide();
			} else {
				show_menu(mainmenu_joinmenu);
			}
			return true;
			
		} else {
			show_menu();
			return true;			
		}		
	}

	return false;
}

} // namespace client