/* 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 "client/entitymenu.h" #include "client/client.h" #include "ui/ui.h" #include "ui/paint.h" namespace client { EntityMenu::EntityMenu(ui::Widget *parent, const char * label) : ButtonMenu(parent, label) { set_border(false); set_background(false); menu_generated_entity = 0; hide(); } EntityMenu::~EntityMenu() { } void EntityMenu::generate(core::Entity *entity, const char *menulabel) { 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(); 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) { event_resize(); return; } if (menudescr->text().size()) { std:: string title (entity->name()); aux::to_uppercase(title); ui::Label *label; label = add_label(title.c_str()); label->set_alignment(ui::AlignCenter); label->set_font(ui::root()->font_large()); label->set_border(false); label->set_border(false); label = add_label(menudescr->text().c_str()); label->set_alignment(ui::AlignCenter); label->set_font(ui::root()->font_small()); label->set_border(false); label->set_border(false); } 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); } } ui::Button *button = add_button(buttondescr->text().c_str(), result.c_str()); switch (buttondescr->alignment()) { case core::ButtonDescription::Center: button->set_alignment(ui::AlignCenter); break; case core::ButtonDescription::Left: button->set_alignment(ui::AlignLeft | ui::AlignVCenter); break; case core::ButtonDescription::Right: button->set_alignment(ui::AlignRight | ui::AlignVCenter); break; } } event_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(); Client::show_menu("game"); } return true; } break; default: break; } return Widget::on_keypress(key, modifier); } }