From 69eed715f80c24c2435c82bb9fa1954697bf3af0 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 4 Sep 2011 17:54:51 +0000 Subject: Moved main menu infrastructure into client namespace, removed ui::Container and ui::Menu classes. --- src/client/mainmenu.cc | 314 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 308 insertions(+), 6 deletions(-) (limited to 'src/client/mainmenu.cc') diff --git a/src/client/mainmenu.cc b/src/client/mainmenu.cc index 6a4f97a..cfebade 100644 --- a/src/client/mainmenu.cc +++ b/src/client/mainmenu.cc @@ -4,15 +4,27 @@ the terms and conditions of the GNU General Public License version 2 */ +#include "client/buttonmenu.h" #include "client/mainmenu.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::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; @@ -23,31 +35,273 @@ MainMenu::MainMenu(ui::Widget *parent) : ui::Window(parent) 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(); + } + + // menu label + } else if (ini.got_section("label")) { + + if (!menu) { + ini.unknown_error("label section without menu defintion"); + } else { + label = menu->add_label(); + } + + } else { + ini.unknown_section(); + continue; + } + + } else if (ini.got_key()) { + + if (ini.in_section("menu")) { + + if (ini.got_key_string("label", strval)) { + aux::to_label(strval); + + for (ui::Widget::Children::iterator child = children().begin(); child != children().end(); ++child) { + if ((*child)->label().compare(strval) == 0) { + menu = static_cast((*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(mainmenu_mainmenu)->set_compact(); + + // main menu when connected + mainmenu_gamemenu = (ui::Widget *) new ButtonMenu(this, "game"); + static_cast(mainmenu_gamemenu)->set_compact(); + + // menu to join a game + mainmenu_joinmenu = (ui::Widget *) new ButtonMenu(this, "join"); + static_cast(mainmenu_joinmenu)->set_compact(); + + // 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 = 0; + + // save game menu + mainmenu_savemenu = 0; + + 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() { - ui::Widget::show(); + if (!mainmenu_activemenu) + show_menu(); + ui::Window::show(); } void MainMenu::hide() { - ui::Widget::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_size(width() - smallmargin * 2, height() - smallmargin * 4); (*child)->set_location(smallmargin, smallmargin * 2); } } @@ -61,4 +315,52 @@ void MainMenu::draw_background() } } +void MainMenu::draw() +{ + if (mainmenu_activemenu == mainmenu_mainmenu) { + if (core::game()->interactive()) { + hide(); + return; + } + + } else if (mainmenu_activemenu == mainmenu_joinmenu) { + 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); + } + } else { + show_menu(); + } + } + + return true; +} + } // namespace client -- cgit v1.2.3