From b417df720584c101f3799874a0c836a543a8d0a8 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 12 Oct 2008 14:55:10 +0000 Subject: user interface updates, work-in-progress --- src/ui/ui.cc | 191 +++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 120 insertions(+), 71 deletions(-) (limited to 'src/ui/ui.cc') diff --git a/src/ui/ui.cc b/src/ui/ui.cc index 8242381..8d60543 100644 --- a/src/ui/ui.cc +++ b/src/ui/ui.cc @@ -18,7 +18,10 @@ #include "ui/widget.h" #include "ui/window.h" -namespace ui { +namespace ui +{ + +/* -- static functions --------------------------------------------- */ UI *global_ui = 0; @@ -59,7 +62,7 @@ void func_ui(std::string const &args) con_warn << "User Interface not available!" << std::endl; return; } - + if (!args.size()) { help(); return; @@ -68,7 +71,7 @@ void func_ui(std::string const &args) std::string command; argstr >> command; aux::to_label(command); - + if (command.compare("help") == 0) { help(); } else if (command.compare("list") == 0) { @@ -103,28 +106,28 @@ void func_menu(std::string const &args) 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("back") == 0) { root()->previous_window(); - + } else if (command.compare("previous") == 0) { root()->previous_window(); - + } else if (command.compare("list") == 0) { root()->list_menus(); } else { @@ -132,6 +135,8 @@ void func_menu(std::string const &args) } } +/* -- class UI ----------------------------------------------------- */ + UI *root() { return global_ui; @@ -140,28 +145,28 @@ UI *root() 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"); } @@ -169,42 +174,51 @@ void init() 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); - + // 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(); } 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() { Windows::iterator it; @@ -213,47 +227,47 @@ void UI::load() remove_child(window); } window_children.clear(); - ui_focus = this; + ui_mouse_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 { + + } 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()); @@ -262,9 +276,9 @@ void UI::load() } else { ini.unkown_key(); } - + } else if (ini.in_section("colors")) { - + if (ini.got_key_color("foreground", color)) { ui_palette->set_foreground(color); continue; @@ -277,6 +291,10 @@ void UI::load() } 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(); } @@ -286,7 +304,7 @@ void UI::load() 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; @@ -295,7 +313,7 @@ void UI::load() 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; @@ -306,17 +324,16 @@ void UI::load() } } -void UI::list() +void UI::list() const { size_t n = Widget::list(0); con_print << n << " user interface widgets" << std::endl; } -void UI::list_menus() +void UI::list_menus() const { - Windows::iterator it; - for (it = window_children.begin(); it != window_children.end(); it++) { - Window *window = (*it); + for (Windows::const_iterator it = window_children.begin(); it != window_children.end(); it++) { + const Window *window = (*it); con_print << " " << window->label() << std::endl; } con_print << window_children.size() << " menus" << std::endl; @@ -332,15 +349,16 @@ void UI::remove_window(Window *window) { if (ui_active_window == window) { ui_active_window = 0; - ui_focus = this; + ui_mouse_focus = this; + ui_input_focus = this; } - + Window::remove_window(window); } -Window *UI::find_window(const char *label) +Window *UI::find_window(const char *label) const { - for (Windows::iterator it = window_children.begin(); it != window_children.end(); it++) { + for (Windows::const_iterator it = window_children.begin(); it != window_children.end(); it++) { if ((*it)->label().compare(label) == 0) { return (*it); } @@ -351,19 +369,21 @@ Window *UI::find_window(const char *label) void UI::show_window(const char *label) { Window *window = find_window(label); - + if (window) { - if (ui_active_window) { - ui_active_window->hide(); - window->set_previous(ui_active_window); - } else { - window->clear_previous(); - } - ui_active_window = window; - ui_active_window->event_resize(); - ui_active_window->raise(); - ui_active_window->show(); - ui_focus = window; + if (ui_active_window) { + ui_active_window->hide(); + window->set_previous(ui_active_window); + } else { + window->clear_previous(); + } + ui_active_window = window; + ui_active_window->event_resize(); + ui_active_window->raise(); + ui_active_window->show(); + ui_active_window->set_focus(); + ui_mouse_focus = this; + ui_input_focus = this; } else { con_warn << "Unknown window '" << label << "'" << std::endl; } @@ -390,28 +410,57 @@ void UI::previous_window() void UI::frame() { - ui_focus = event_focus(mouse_cursor); + if (ui_active_window && !ui_active_window->visible()) { + ui_active_window = 0; + } + event_draw(); } -void UI::input_mouse(float x, float y) +/* -- 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; } -void UI::input_key(bool pressed, unsigned int key, unsigned int modifier) +void UI::input_key(const bool pressed, const int key, const unsigned int modifier) { - ui_focus->event_key(pressed, key, modifier); + if (key < 512) { + // keyboard keys + Widget *f = find_input_focus(); + if (f) { + f->event_key(pressed, key, modifier); + } + ui_input_focus = f; + } else { + // mosue buttons + if (ui_mouse_focus) + ui_mouse_focus->event_key(pressed, key, modifier); + } } -bool UI::keypress(unsigned int key, unsigned int modifier) +/* -- event handlers ----------------------------------------------- */ +/* + These functions handle events for this widget only. They are + the fallback input handlers +*/ +bool UI::on_keypress(const int key, const unsigned int modifier) { - return true; + return true; } -bool UI::keyrelease(unsigned int key, unsigned int modifier) +bool UI::on_keyrelease(const int key, const unsigned int modifier) { - return true; + return true; } } -- cgit v1.2.3