/* 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 "audio/audio.h" #include "auxiliary/functions.h" #include "core/core.h" #include "filesystem/filesystem.h" #include "render/gl.h" #include "sys/sys.h" #include "ui/button.h" #include "ui/label.h" #include "ui/menu.h" #include "ui/paint.h" #include "ui/ui.h" #include "ui/widget.h" namespace ui { /* -- static functions --------------------------------------------- */ 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 [name] show a menu" << std::endl; con_print << " menu back return to the previous menu" << std::endl; con_print << " menu previous return to the previous menu" << std::endl; con_print << " menu close close the current menu" << std::endl; con_print << " menu hide hide the current 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_menu(); } else if (command.compare("close") == 0) { root()->hide_menu(); } else if (command.compare("back") == 0) { root()->previous_menu(); } else if (command.compare("previous") == 0) { root()->previous_menu(); } else if (command.compare("list") == 0) { root()->list_menus(); } else { root()->show_menu(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; } } /* -- class UI ----------------------------------------------------- */ UI::UI() : Window(0) { set_label("root"); set_size(1024, 768); set_border(false); set_background(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_mouse_focus = this; ui_input_focus = this; set_focus(); mouse_pointer_color = Palette::Pointer; mouse_pointer_bitmap.assign("pointer"); } UI::~UI() { delete ui_palette; delete ui_font_small; delete ui_font_large; } /* remove all existing child windows and load ini/ui.ini */ void UI::load() { Menus::iterator it; for (it = ui_menus.begin(); it != ui_menus.end(); it++) { Window *menu = (*it); remove_child(menu); } ui_menus.clear(); ui_mouse_focus = this; ui_active_menu = 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()); add_menu(menu); 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 if (ini.got_key_color("pointer", color)) { ui_palette->set_pointer(color); } else if (ini.got_key_color("active", color)) { ui_palette->set_active(color); } 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"); } } void UI::list() const { size_t n = Widget::list(0); con_print << n << " user interface widgets" << std::endl; } UI::Menus::iterator UI::find_menu(Window *menu) { Menus::iterator it; for (it = ui_menus.begin(); it != ui_menus.end(); it++) { if ((*it) == menu) return it; } return it; } Window *UI::find_menu(const char *label) { for (Menus::const_iterator it = ui_menus.begin(); it != ui_menus.end(); it++) { if ((*it)->label().compare(label) == 0) { return (*it); } } return 0; } void UI::list_menus() const { for (Menus::const_iterator it = ui_menus.begin(); it != ui_menus.end(); it++) { const Window *menu = (*it); con_print << " " << menu->label() << std::endl; } con_print << ui_menus.size() << " menus" << std::endl; } void UI::add_menu(Window *menu) { Menus::iterator it = find_menu(menu); if (it == ui_menus.end()) { ui_menus.push_back(menu); } } void UI::show_menu(const char *label) { Window *menu = find_menu(label); if (menu) { if (ui_active_menu) { ui_active_menu->hide(); menu->set_previous(ui_active_menu); } else { menu->clear_previous(); } ui_mouse_focus = this; ui_input_focus = this; ui_active_menu = menu; ui_active_menu->event_resize(); ui_active_menu->show(); set_pointer("pointer"); } else { con_warn << "Unknown window '" << label << "'" << std::endl; } } void UI::hide_menu() { if (ui_active_menu) { ui_active_menu->hide(); ui_active_menu = 0; } } void UI::previous_menu() { if (ui_active_menu) { if (ui_active_menu->previous().size()) { show_menu(ui_active_menu->previous().c_str()); } else { hide_menu(); } } } void UI::frame() { if (ui_active_menu && !ui_active_menu->visible()) { ui_active_menu = 0; } event_draw(); if (visible()) draw_pointer(); } /* -- global event handlers ---------------------------------------- */ /* These functions receive input events from the client input subsystem and distributes them to the widget hierarchy */ void UI::input_mouse(const float x, const float y) { mouse_cursor.assign(x, y); Widget *f = find_mouse_focus(mouse_cursor); if (f) { f->event_mouse(mouse_cursor); } ui_mouse_focus = f; } bool UI::input_key(const bool pressed, const int key, const unsigned int modifier) { bool handled = false; if (key < 512) { // keyboard keys Widget *f = find_input_focus(); if (f) { handled = f->event_key(pressed, key, modifier); } ui_input_focus = f; } else { // mosue buttons if (ui_mouse_focus) handled = ui_mouse_focus->event_key(pressed, key, modifier); } return handled; } bool UI::on_keypress(const int key, const unsigned int modifier) { switch( key ) { case SDLK_ESCAPE: if (active()) { hide_menu(); audio::play("ui/menu"); } else { if (core::application()->connected()) { show_menu("game"); audio::play("ui/menu"); } } return true; break; default: break; } return false; } bool UI::on_keyrelease(const int key, const unsigned int modifier) { return false; } void UI::set_pointer(const char *pointerbitmap, const Palette::Color color, const bool animated) { if (!pointerbitmap) { mouse_pointer_bitmap.clear(); } else { mouse_pointer_bitmap.assign(pointerbitmap); } mouse_pointer_animated = animated; mouse_pointer_color = color; } void UI::draw_pointer() { if (!mouse_pointer_bitmap.size()) return; math::Color c(palette()->color(mouse_pointer_color)); if (mouse_pointer_animated) { c.a = 1; } else { c.a = 0.5f; } paint::color(c); math::Vector2f pos(mouse_cursor.x - pointer_size * 0.5f, mouse_cursor.y - pointer_size * 0.5f); math::Vector2f s(pointer_size, pointer_size); std::string texture("pointers/"); texture.append(mouse_pointer_bitmap); if (mouse_pointer_animated) { render::gl::push(); render::gl::translate(mouse_cursor.x, mouse_cursor.y, 0); float angle = core::application()->time()* 0.75f - floorf(core::application()->time() * 0.75f); angle *= 360.0f; render::gl::rotate(angle, math::Vector3f(0, 0, 1.0f)); render::gl::translate(-mouse_cursor.x, -mouse_cursor.y, 0); } paint::bitmap(pos, s, texture); if (mouse_pointer_animated) { render::gl::pop(); } } }