/* 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 "core/application.h" #include "filesystem/filesystem.h" #include "render/gl.h" #include "sys/sys.h" #include "ui/button.h" #include "ui/label.h" #include "ui/paint.h" #include "ui/ui.h" #include "ui/widget.h" namespace ui { /* -- static functions --------------------------------------------- */ bool UI::ui_debug = false; math::Vector2f UI::elementsize(256, 32); float UI::padding = 24.0f; float UI::margin = 16.0f; float UI::pointer_size = 48.0f; UI *global_ui = 0; 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; } } void shutdown() { con_print << "^BShutting down user interface..." << std::endl; 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); // intialize console ui_console = new Console(this); ui_console->hide(); // default palette ui_palette = new Palette(); set_palette(ui_palette); // default fonts ui_font_tiny = new Font("gui", 10, 18); 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"); mouse_buttonleft_pressed = false; } UI::~UI() { delete ui_palette; delete ui_font_small; delete ui_font_large; } void UI::load_settings() { ui_mouse_focus = this; // open ui.ini std::string filename("ini/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; float w = elementsize.width(); float h = elementsize.height(); float m = margin; float s = padding; float p = pointer_size; while (ini.getline()) { if (ini.got_section()) { if (ini.got_section("ui")) { continue; // section default colors } else if (ini.got_section("colors")) { continue; // section hud configuration } else if (ini.got_section("hud")) { continue; // section text colors } else if (ini.got_section("text")) { continue; } else { ini.unknown_section(); continue; } } else if (ini.got_key()) { if (ini.in_section("ui")) { if (ini.got_key_float("elementwidth", w)) { elementsize.assign(w, h); continue; } else if (ini.got_key_float("elementheight", h)) { elementsize.assign(w, h); continue; } else if (ini.got_key_float("margin", m)) { margin = m; continue; } else if (ini.got_key_float("pointer", p)) { pointer_size = p; continue; } else if (ini.got_key_float("padding", s)) { padding = s; continue; } else { ini.unknown_key(); continue; } } 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("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("text", color)) { ui_palette->set_text(color); continue; } else if (ini.got_key_color("highlight", color)) { ui_palette->set_highlight(color); continue; } else if (ini.got_key_color("disabled", color)) { ui_palette->set_disabled(color); continue; } else if (ini.got_key_color("pointer", color)) { ui_palette->set_pointer(color); continue; } else if (ini.got_key_color("active", color)) { ui_palette->set_active(color); continue; } else if (ini.got_key_color("debug", color)) { ui_palette->set_debug(color); continue; } else { ini.unknown_key(); continue; } } else if (ini.in_section("hud")) { if (ini.got_key_color("mission", color)) { ui_palette->set_mission(color); continue; } else { ini.unknown_key(); continue; } } else if (ini.in_section("text")) { if (ini.got_key_color("bold", color)) { ui_palette->set_bold(color); } else if (ini.got_key_color("fancy", color)) { ui_palette->set_fancy(color); } else if (ini.got_key_color("warning", color)) { ui_palette->set_warning(color); } else if (ini.got_key_color("error", color)) { ui_palette->set_error(color); } } } } // apply palette colors to the render subsystem Paint::assign_system_color('N', palette()->text()); Paint::assign_system_color('D', palette()->debug()); Paint::assign_system_color('B', palette()->bold()); Paint::assign_system_color('F', palette()->fancy()); Paint::assign_system_color('W', palette()->warning()); Paint::assign_system_color('E', palette()->error()); ini.close(); } void UI::list() const { size_t n = Widget::list(0); con_print << n << " user interface widgets" << std::endl; } void UI::list_visible() const { size_t n = Widget::list(0, true); con_print << n << " visible user interface widgets" << std::endl; } void UI::frame() { ui_input_focus = find_input_focus(); Widget *f = 0; if (!mouse_buttonleft_pressed) { f = find_mouse_focus(mouse_cursor); } else { f = find_visible_child(ui_mouse_focus); } if (f) { f->event_mouse(mouse_cursor); } ui_mouse_focus = f; // reset mouse pointer ui::root()->set_pointer("pointer"); // draw the widget stack event_draw(); // draw the mouse pointer 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); } bool UI::input_mouse_button(const bool pressed, unsigned int button) { bool handled = false; if (button == SDL_BUTTON_LEFT) { mouse_buttonleft_pressed = pressed; } // set mouse focus Widget *f = find_mouse_focus(mouse_cursor); if (f) { f->event_mouse(mouse_cursor); } ui_mouse_focus = f; // send mouse button events if (ui_mouse_focus) { handled = ui_mouse_focus->event_mouse_button(pressed, button); } return handled; } bool UI::input_mouse_wheel(const math::Vector2f & direction) { bool handled = false; // set mouse focus Widget *f = find_mouse_focus(mouse_cursor); if (f) { f->event_mouse(mouse_cursor); } ui_mouse_focus = f; // send mouse wheel events if (ui_mouse_focus) { handled = ui_mouse_focus->event_mouse_wheel(direction); } return handled; } bool UI::input_key(const bool pressed, const int key, const unsigned int modifier) { bool handled = false; // send keyboard events Widget *f = find_input_focus(); if (f) { handled = f->event_key(pressed, key, modifier); } ui_input_focus = f; return handled; } bool UI::on_keypress(const int key, const unsigned int modifier) { 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; } 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("bitmaps/pointers/"); texture.append(mouse_pointer_bitmap); if (mouse_pointer_animated) { gl::push(); 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; gl::rotate(angle, math::Vector3f(0, 0, 1.0f)); gl::translate(-mouse_cursor.x(), -mouse_cursor.y(), 0); } Paint::draw_bitmap(pos, s, c, texture); if (mouse_pointer_animated) { gl::pop(); } } }