From a14d80f83aebe75241bf63b4f3ffca3a5d952577 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 6 Oct 2008 18:22:32 +0000 Subject: libui updates, support menu .ini files --- src/ui/menu.cc | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 11 deletions(-) (limited to 'src/ui/menu.cc') diff --git a/src/ui/menu.cc b/src/ui/menu.cc index c20457d..9485e82 100644 --- a/src/ui/menu.cc +++ b/src/ui/menu.cc @@ -4,6 +4,7 @@ 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" @@ -15,39 +16,118 @@ const float element_width = 256.0f; const float element_height = 64.0f; const float element_margin = 24.0f; -Menu::Menu(Window *parent, const char *label) : Window(parent), menu_container(this) +Menu::Menu(Window *parent, const char *label) : Window(parent) { set_label(label); set_border(false); - menu_container.set_label("container"); + menu_background = new Bitmap(this); + menu_container = new Window(this); + + menu_container->set_label("container"); } Menu::~Menu() { } -void Menu::add_label(char const * text) +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 { + 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 { + ini.unkown_key(); + } + } else if (ini.in_section("label")) { + if (ini.got_key_string("text", strval)) { + label->set_text(strval); + } 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) { - new Label(&menu_container, text); + return new Label(menu_container, text); } -void Menu::add_button(char const *text, char const *command) +Button *Menu::add_button(char const *text, char const *command) { - new Button(&menu_container, text, command); + return new Button(menu_container, text, command); } void Menu::resize() { - set_size(parent()->size().x, parent()->size().y); + size().assign(parent()->size()); + menu_background->size().assign(size()); - float n = (float) menu_container.children().size(); - menu_container.set_size(2.0f * element_width, n * (element_height + element_margin) + element_height); - menu_container.set_location(element_margin, (height() - menu_container.height()) / 2.0f); + float n = (float) menu_container->children().size(); + menu_container->set_size(2.0f * element_width, n * (element_height + element_margin) + element_height); + menu_container->set_location(element_margin, (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++) { + for (Children::iterator it = menu_container->children().begin(); it != menu_container->children().end(); it++) { Widget *w = (*it); w->set_size(element_width, element_height); w->set_location(element_width * 0.5f, element_height * 0.5f + i * (element_height + element_margin)); -- cgit v1.2.3