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

#include "filesystem/filesystem.h"
#include "ui/label.h"
#include "ui/button.h"
#include "ui/menu.h"
#include "ui/ui.h"

namespace ui {

Menu::Menu(Window *parent, const char *label) : Window(parent)
{
	set_label(label);
	set_border(false);

	menu_element_width = 256.0f;
	menu_element_height = 48.0f;
	menu_element_margin = 24.0f;

	menu_background = new Bitmap(this);
	menu_container = new Window(this);
	menu_container->set_border(true);
	menu_container->set_background(true);

	menu_container->set_label("container");
}

Menu::~Menu()
{
	// menu_container and menu_background are deleted by Widget::~Widget()
}

void Menu::load()
{
	std::string filename("menus/");
	filename.append(label());

	filesystem::IniFile ini;

	ini.open(filename);

	if (!ini.is_open()) {
		con_error << "Could not open " << ini.name() << std::endl;
		return;
	}

	std::string strval;
	Button *button = 0;
	Label *label = 0;
	
	while (ini.getline()) {
		if (ini.got_section()) {

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

			if (ini.got_section("menu")) {
			
			} else if (ini.got_section("button")) {
				button = add_button();
	
			} else if (ini.got_section("label")) {
				label = add_label();
	
			} else if (ini.got_section()) {
				ini.unknown_section();
			}

		} else if (ini.got_key()) {

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

			if (ini.in_section("menu")) {
				if (ini.got_key_string("background", strval)) {
					set_background(strval.c_str());
				} else if (ini.got_key_float("elementwidth", menu_element_width)) {
					continue;
				} else if (ini.got_key_float("elementheight", menu_element_height)) {
					continue;
				} else if (ini.got_key_float("elementmargin", menu_element_margin)) {
					continue;
				} else {
					ini.unkown_key();
				}
			} else if (ini.in_section("button")) {
				if (ini.got_key_string("text", strval)) {
					button->set_text(strval);

				} else if (ini.got_key_string("command", 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(AlignLeft | AlignVCenter);
					} else if (strval.compare("center") == 0) {
						button->set_alignment(AlignCenter);
					} else if (strval.compare("right") == 0) {
 						button->set_alignment(AlignRight | AlignVCenter);
					} else {
						ini.unknown_value();
					}
				} else {
					ini.unkown_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(AlignLeft | AlignHCenter);
					} else if (strval.compare("center") == 0) {
						label->set_alignment(AlignCenter);
					} else if (strval.compare("right") == 0) {
						label->set_alignment(AlignRight | AlignHCenter);
					} else {
						ini.unknown_value();
					}
				} else {
					ini.unkown_key();
				}
			}

		}
	}

	con_debug << "  " << ini.name() << " " << children().size() << " widgets" << std::endl;
	ini.close();
}

void Menu::set_background(const char *texture)
{
	menu_background->set_texture(texture);
}

Label *Menu::add_label(char const * text)
{
	Label *label = new Label(menu_container, text);
	label->set_alignment(AlignCenter);
	label->set_border(false);
	label->set_font(ui::root()->font_large());
	return label;
}

Button *Menu::add_button(char const *text, char const *command)
{
	return new Button(menu_container, text, command);
}

void Menu::resize()
{
	size().assign(parent()->size());
	menu_background->size().assign(size());

	float n = (float) menu_container->children().size();
	menu_container->set_size(1.5f * menu_element_width, n * (menu_element_height + menu_element_margin) + menu_element_height);
	menu_container->set_location(menu_element_width * 0.25, (height() - menu_container->height()) / 2.0f);
	
	// reposition all children within the container
	size_t i = 0;
	for (Children::iterator it = menu_container->children().begin(); it != menu_container->children().end(); it++) {
		Widget *w = (*it);
		w->set_size(menu_element_width, menu_element_height);
		w->set_location(menu_element_width * 0.25f, menu_element_height * 0.5f + i * (menu_element_height + menu_element_margin));
		i++;
	}
}

}