/* ui/ui.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include #include #include "auxiliary/functions.h" #include "core/core.h" #include "filesystem/filesystem.h" #include "sys/sys.h" #include "ui/button.h" #include "ui/label.h" #include "ui/menu.h" #include "ui/ui.h" #include "ui/widget.h" #include "ui/window.h" namespace ui { UI *global_ui = 0; void func_list_ui(std::string const &args) { if (global_ui) { global_ui->list(); } } void func_ui_restart(std::string const &args) { if (global_ui) { global_ui->load(); } } void func_list_menu(std::string const &args) { if (global_ui) { global_ui->list_menus(); } } void help() { con_print << "^BUser interface functions" << std::endl; con_print << " ui help show this help" << std::endl; con_print << " ui list list widgets" << std::endl; con_print << " ui show show user interface" << std::endl; con_print << " ui hide hide user interface" << std::endl; con_print << " ui restart reload user interface files" << std::endl; } void func_ui(std::string const &args) { if (!global_ui) { con_warn << "User Interface not available!" << std::endl; return; } if (!args.size()) { help(); return; } std::stringstream argstr(args); std::string command; argstr >> command; aux::to_label(command); if (command.compare("help") == 0) { help(); } else if (command.compare("list") == 0) { global_ui->list(); } else if (command.compare("show") == 0) { global_ui->show(); } else if (command.compare("hide") == 0) { global_ui->hide(); } else if (command.compare("restart") == 0) { global_ui->load(); } else { help(); } } void help_menu() { con_print << "^Bmenu functions" << std::endl; con_print << " menu help show this help" << std::endl; con_print << " menu list list available menus" << std::endl; con_print << " menu hide hide the current menu" << std::endl; con_print << " menu close close the current menu" << std::endl; con_print << " menu [name] show a menu" << std::endl; root()->list_menus(); } void func_menu(std::string const &args) { if (!global_ui) { con_warn << "User Interface not available!" << std::endl; return; } if (!args.size()) { return; } std::stringstream argstr(args); std::string command; argstr >> command; aux::to_label(command); if (command.compare("hide") == 0) { root()->hide_window(); } else if (command.compare("close") == 0) { root()->hide_window(); } else if (command.compare("list") == 0) { root()->list_menus(); } else { root()->show_window(command.c_str()); } } UI *root() { return global_ui; } void init() { con_print << "^BInitializing user interface..." << std::endl; if (!global_ui) { global_ui = new UI(); } else { con_warn << "User interface already initialized!" << std::endl; return; } global_ui->load(); core::Func *func = core::Func::add("list_ui", func_list_ui); func->set_info("list user interface widgets"); func = core::Func::add("list_menu", func_list_menu); func->set_info("list available menus"); func = core::Func::add("ui", func_ui); func->set_info("[command] user interface functions"); func = core::Func::add("ui_restart", func_ui_restart); func->set_info("[command] [options] reload user interface files"); func = core::Func::add("menu", func_menu); func->set_info("[command] menu functions"); } void shutdown() { con_print << "^BShutting down user interface..." << std::endl; core::Func::remove("list_ui"); core::Func::remove("list_menu"); core::Func::remove("menu"); core::Func::remove("ui"); if (global_ui) { delete global_ui; global_ui = 0; } } UI::UI() : Window(0) { set_label("root"); set_size(1024, 768); set_border(false); // default palette ui_palette = new Palette(); set_palette(ui_palette); // default fonts ui_font_small = new Font("gui", 12, 18); ui_font_large = new Font("gui", 14, 24); set_font(ui_font_small); } UI::~UI() { delete ui_palette; delete ui_font_small; delete ui_font_large; } void UI::load() { Windows::iterator it; for (it = window_children.begin(); it != window_children.end(); it++) { Window *window = (*it); remove_child(window); } window_children.clear(); ui_focus = this; ui_active_window = 0; std::string filename("ui"); filesystem::IniFile ini; ini.open(filename); if (!ini.is_open()) { con_error << "Could not open " << ini.name() << std::endl; return; } std::string strval; math::Color color; Menu *menu = 0; while (ini.getline()) { if (ini.got_section()) { //con_debug << " " << ini.name() << " [" << ini.section() << "]" << std::endl; if (ini.got_section("ui")) { continue; } else if (ini.got_section("colors")) { continue; } else { ini.unknown_section(); continue; } } 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()); menu->load(); continue; } else { ini.unkown_key(); } } else if (ini.in_section("colors")) { if (ini.got_key_color("foreground", color)) { ui_palette->set_foreground(color); continue; } else if (ini.got_key_color("highlight", color)) { ui_palette->set_highlight(color); continue; } else if (ini.got_key_color("background", color)) { ui_palette->set_background(color); continue; } else if (ini.got_key_color("border", color)) { ui_palette->set_border(color); continue; } else { ini.unkown_key(); } } } } con_debug << " " << ini.name() << " " << window_children.size() << " menus" << std::endl; ini.close(); // fallback main menu if (!find_window("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_window("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"); } } void UI::list() { size_t n = Widget::list(0); con_print << n << " user interface widgets" << std::endl; } void UI::list_menus() { Windows::iterator it; for (it = window_children.begin(); it != window_children.end(); it++) { Window *window = (*it); con_print << " " << window->label() << std::endl; } con_print << window_children.size() << " menus" << std::endl; } void UI::add_window(Window *window) { Window::add_window(window); window->hide(); } void UI::remove_window(Window *window) { if (ui_active_window == window) { ui_active_window = 0; ui_focus = this; } Window::remove_window(window); } Window *UI::find_window(const char *label) { for (Windows::iterator it = window_children.begin(); it != window_children.end(); it++) { if ((*it)->label().compare(label) == 0) { return (*it); } } return 0; } void UI::show_window(const char *label) { Window *window = find_window(label); if (window) { if (ui_active_window) ui_active_window->hide(); ui_active_window = window; ui_active_window->event_resize(); ui_active_window->raise(); ui_active_window->show(); ui_focus = window; } else { con_warn << "Unknown window '" << label << "'" << std::endl; } } void UI::hide_window() { if (ui_active_window) { ui_active_window->hide(); ui_active_window = 0; } } void UI::frame() { ui_focus = event_focus(mouse_cursor); event_draw(); } void UI::input_mouse(float x, float y) { mouse_cursor.assign(x, y); } void UI::input_key(bool pressed, unsigned int key, unsigned int modifier) { ui_focus->event_key(pressed, key, modifier); } bool UI::keypress(unsigned int key, unsigned int modifier) { return true; } bool UI::keyrelease(unsigned int key, unsigned int modifier) { return true; } }