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

#include "ui/ui.h"
#include "ui/button.h"
#include "ui/paint.h"
#include "client/entitymenu.h"

namespace client
{

EntityMenu::EntityMenu(ui::Widget *parent, const char * label) : ui::Window(parent)
{
	set_border(false);
	set_background(false);	
	if (label)
		set_label(label);
	else
		set_label("entitymenu");

	menu_container = new ui::Container(this);
	menu_generated_entity = 0;
	hide();
}

EntityMenu::~EntityMenu()
{
}

void EntityMenu::resize()
{
	set_size(parent()->size());
	menu_container->set_location(ui::UI::elementsize.height(), (height() - menu_container->height()) / 2.0f);
}

void EntityMenu::clear()
{
	remove_children();
}

void EntityMenu::generate(core::Entity *entity, const char *menulabel)
{
	using namespace ui;

	menu_generated_menu.clear();
	menu_generated_entity = entity;

	if (!entity)
		return;

	if (!menulabel) {
		return;
	}

	menu_generated_menu.assign(menulabel);
	if (!menu_generated_menu.size())
		return;
	
	//con_debug << "generating menu " << entity->label() << " " << menulabel << std::endl;
	
	clear();
	menu_container = new Container(this);

	core::MenuDescription *menudescr = 0;
	for (core::Entity::Menus::iterator it = entity->menus().begin(); it != entity->menus().end(); it++) {
		if ((*it)->label().compare(menulabel) == 0) {
			menudescr = (*it);
		}
	}
	
	if (!menudescr) {
		menu_container->event_resize();
		resize();
		return;
	}

	if (menudescr->text().size()) {
		Label *label = new Label(menu_container);
		label->set_text(menudescr->text());
		label->set_alignment(AlignCenter);
		label->set_border(false);
		label->set_font(ui::root()->font_large());
	}

	for (core::MenuDescription::Buttons::iterator it = menudescr->buttons().begin(); it != menudescr->buttons().end(); it++) {
		core::ButtonDescription *buttondescr = (*it);

		// parse the command sequence
		size_t i = 0;
		std::string result;
		std::string current;
		bool quote = false;

		while (i < buttondescr->command().size()) {
			const char c = buttondescr->command()[i];

			if (c == '"') {
				quote = !quote;
				result += c;
	
			} else if (c == ';') {

				if (quote) {
					current += c;
	
				} else if (current.size()) {
					if (buttondescr->command_type() == core::ButtonDescription::CommandGame) {
						if (result.size()) {
							result += ';';
						}
	
						result.append("remote ");
						result.append(current);
	
					} else if (buttondescr->command_type() == core::ButtonDescription::CommandMenu) {
						if (result.size()) {
							result += ';';
						}
	
						result.append("view ");
						result.append(current);
					}
					current.clear();
				}
	
			} else {
				current += c;
			}
	
			i++;
		}

		if (current.size()) {
			if (buttondescr->command_type() == core::ButtonDescription::CommandGame) {
				if (result.size()) {
					result += ';';
				}

				result.append("remote ");
				result.append(current);

			} else if (buttondescr->command_type() == core::ButtonDescription::CommandMenu) {
				if (result.size()) {
					result += ';';
				}

				result.append("view ");
				result.append(current);
			}

		}

		Button *button = new Button(menu_container, buttondescr->text().c_str(), result.c_str());

		switch (buttondescr->alignment()) {
			case core::ButtonDescription::Center:
				button->set_alignment(AlignCenter);
				break;
			case core::ButtonDescription::Left:
				button->set_alignment(AlignLeft | AlignVCenter);
				break;
			case core::ButtonDescription::Right:
				button->set_alignment(AlignRight | AlignVCenter);
				break;
		}
	}

	menu_container->event_resize();
	resize();
}

bool EntityMenu::on_keypress(const int key, const unsigned int modifier)
{
	switch( key ) {
	
	case SDLK_ESCAPE:
		if (visible()) {
			if (menu_generated_menu.compare("main") != 0) {
				generate(menu_generated_entity, "main");
			} else {
				this->hide();
				ui::root()->show_menu("game");
			}
			return true;
		}
		break;
	default:
		break;
	}

	return Window::on_keypress(key, modifier);
}

}