diff options
author | Stijn Buys <ingar@osirion.org> | 2009-01-26 22:12:04 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2009-01-26 22:12:04 +0000 |
commit | 76a49efdf62a53a54e2deeb559422f11c1e955dd (patch) | |
tree | 883d335f03dcd24002b749456d05db1602b09fd5 /src | |
parent | 8ee6f0c6effeffd6fa3322149435c9a5aba7120f (diff) |
read main menus from menu.ini
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/menu.cc | 92 | ||||
-rw-r--r-- | src/ui/menu.h | 8 | ||||
-rw-r--r-- | src/ui/ui.cc | 159 | ||||
-rw-r--r-- | src/ui/ui.h | 7 |
4 files changed, 146 insertions, 120 deletions
diff --git a/src/ui/menu.cc b/src/ui/menu.cc index 36599be..a7e2d68 100644 --- a/src/ui/menu.cc +++ b/src/ui/menu.cc @@ -30,98 +30,12 @@ Menu::~Menu() // menu_container and menu_background are deleted by Widget::~Widget() } -void Menu::load() +void Menu::set_background_texture(const char *texture) { - 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()) { - if (ini.got_section("menu")) { - continue; - - } 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()) { - if (ini.in_section("menu")) { - if (ini.got_key_string("background", strval)) { - menu_background->set_texture(strval); - } else { - ini.unkown_key(); - } - } else 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(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(); + menu_background->set_texture(texture); } -void Menu::set_background_texture(const char *texture) +void Menu::set_background_texture(const std::string &texture) { menu_background->set_texture(texture); } diff --git a/src/ui/menu.h b/src/ui/menu.h index e3a853a..bbcea5a 100644 --- a/src/ui/menu.h +++ b/src/ui/menu.h @@ -22,14 +22,14 @@ class Menu : public Window public: /// create a new menu - Menu(Window *parent, const char * label); + Menu(Window *parent, const char * label = 0); ~Menu(); - /// load a menu from ini/menus/label.ini - void load(); - /// set the background bitmap void set_background_texture(const char *texture); + + /// set the background bitmap + void set_background_texture(const std::string &texture); /// add a label Label *add_label(char const * text=0); diff --git a/src/ui/ui.cc b/src/ui/ui.cc index e9036c5..ff6810f 100644 --- a/src/ui/ui.cc +++ b/src/ui/ui.cc @@ -44,7 +44,8 @@ void func_list_ui(std::string const &args) void func_ui_restart(std::string const &args) { if (global_ui) { - global_ui->load(); + global_ui->load_menus(); + global_ui->load_settings(); global_ui->apply_render_options(); } } @@ -94,7 +95,8 @@ void func_ui(std::string const &args) } else if (command.compare("list") == 0) { global_ui->list(); } else if (command.compare("restart") == 0) { - global_ui->load(); + global_ui->load_menus(); + global_ui->load_settings(); global_ui->apply_render_options(); } else { help(); @@ -170,8 +172,9 @@ void init() con_warn << "User interface already initialized!" << std::endl; return; } - - global_ui->load(); + + global_ui->load_menus(); + global_ui->load_settings(); core::Func *func = core::Func::add("list_ui", func_list_ui); func->set_info("list user interface widgets"); @@ -245,10 +248,7 @@ UI::~UI() delete ui_font_large; } -/* - remove all existing child windows and load ini/ui.ini -*/ -void UI::load() +void UI::load_menus() { Menus::iterator it; for (it = ui_menus.begin(); it != ui_menus.end(); it++) { @@ -257,15 +257,138 @@ void UI::load() } ui_menus.clear(); + // add specia view menu add_menu(new MenuView(this, "view")); + std::string filename("menu"); + filesystem::IniFile ini; + ini.open(filename); + if (!ini.is_open()) { + con_error << "Could not open " << ini.name() << std::endl; + } else { + std::string strval; + Button *button = 0; + Label *label = 0; + Menu *menu = 0; + + while (ini.getline()) { + if (ini.got_section()) { + if (ini.got_section("menu")) { + menu = new Menu(this); + add_menu(menu); + continue; + } else if (menu) { + if (ini.got_section("button")) { + button = menu->add_button(); + + } else if (ini.got_section("label")) { + label = menu->add_label(); + + } else { + ini.unknown_section(); + } + } else { + ini.unknown_section(); + } + } else if (menu && ini.got_key()) { + if (ini.in_section("menu")) { + if (ini.got_key_string("label", strval)) { + aux::to_label(strval); + menu->set_label(strval); + } else if (ini.got_key_string("background", strval)) { + menu->set_background_texture(strval); + } else { + ini.unkown_key(); + } + } else 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(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() << " " << ui_menus.size() << " menus" << std::endl; + ini.close(); + } + + // fallback main menu + if (!find_menu("main")) { + con_warn << "menu 'main' not found, using default" << std::endl; + Menu *menu = new Menu(this, "main"); + menu->add_label("Main Menu"); + menu->add_button("Connect", "connect"); + menu->add_button("Quit", "quit"); + } + + // fallback game menu + if (!find_menu("game")) { + con_warn << "menu 'game' not found, using default" << std::endl; + Menu *menu = new Menu(this, "game"); + menu->add_label("Game Menu"); + menu->add_button("Disconnect", "disconnect"); + menu->add_button("Quit", "quit"); + } + + // fallback join menu + if (!find_menu("join")) { + con_warn << "menu 'join' not found, using default" << std::endl; + Menu *menu = new Menu(this, "join"); + menu->add_label("Join Menu"); + menu->add_button("Join", "join; menu hide"); + menu->add_button("Game menu", "menu game"); + } +} + + +void UI::load_settings() +{ + ui_mouse_focus = this; ui_active_menu = 0; std::string filename("ui"); - filesystem::IniFile ini; - ini.open(filename); if (!ini.is_open()) { @@ -275,7 +398,6 @@ void UI::load() std::string strval; math::Color color; - Menu *menu = 0; float w = elementsize.width(); float h = elementsize.height(); @@ -285,9 +407,6 @@ void UI::load() while (ini.getline()) { if (ini.got_section()) { - - //con_debug << " " << ini.name() << " [" << ini.section() << "]" << std::endl; - if (ini.got_section("ui")) { continue; @@ -307,17 +426,8 @@ void UI::load() } else if (ini.got_key()) { - //con_debug << " " << ini.name() << " " << ini.key() << "=" << ini.value() << std::endl; - if (ini.in_section("ui")) { - - if (ini.got_key_string("menu", strval)) { - aux::to_label(strval); - menu = new Menu(this, strval.c_str()); - add_menu(menu); - menu->load(); - continue; - } else if (ini.got_key_float("elementwidth", w)) { + if (ini.got_key_float("elementwidth", w)) { elementsize.assign(w, h); continue; } else if (ini.got_key_float("elementheight", h)) { @@ -389,7 +499,6 @@ void UI::load() } } - con_debug << " " << ini.name() << " " << ui_menus.size() << " menus" << std::endl; ini.close(); // fallback main menu diff --git a/src/ui/ui.h b/src/ui/ui.h index 95577cf..fae827e 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -31,8 +31,11 @@ public: /// list meus void list_menus() const; - /// reload menu files - void load(); + /// load menus from menu.ini + void load_menus(); + + /// load settings from ui.ini + void load_settings(); /// apply UI options to the render engine void apply_render_options(); |