diff options
author | Stijn Buys <ingar@osirion.org> | 2008-10-07 20:35:52 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-10-07 20:35:52 +0000 |
commit | bc50666e86d5739ccde633eb630cc75b3e0fcb71 (patch) | |
tree | afad7eb043d6773a7b2a426f4d7b1f600301b5cd | |
parent | f54bd48a884e4e3c95818f042a4b2418a6e070a4 (diff) |
libui updates
-rw-r--r-- | src/client/input.cc | 2 | ||||
-rw-r--r-- | src/ui/button.cc | 18 | ||||
-rw-r--r-- | src/ui/button.h | 11 | ||||
-rw-r--r-- | src/ui/ui.cc | 64 | ||||
-rw-r--r-- | src/ui/ui.h | 12 | ||||
-rw-r--r-- | src/ui/widget.cc | 50 | ||||
-rw-r--r-- | src/ui/widget.h | 24 |
7 files changed, 129 insertions, 52 deletions
diff --git a/src/client/input.cc b/src/client/input.cc index 41f79fe..0020a1f 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -714,7 +714,7 @@ void frame(float seconds) mouse_x = event.motion.x; mouse_y = event.motion.y; mouse_lastmoved = client()->time(); - ui::root()->event_mousecursor((float) mouse_x, (float) mouse_y); + ui::root()->event_mousemove((float) mouse_x, (float) mouse_y); break; case SDL_MOUSEBUTTONDOWN: diff --git a/src/ui/button.cc b/src/ui/button.cc index 0910995..e06e883 100644 --- a/src/ui/button.cc +++ b/src/ui/button.cc @@ -41,9 +41,18 @@ void Button::set_command(std::string const &command) button_command.assign(command); } -void Button::draw() +void Button::draw_border() { - Label::draw(); + if (!border()) + return; + + if (palette()) { + if (has_focus()) + render::gl::color(palette()->foreground()); + else + render::gl::color(palette()->border()); + } + render::primitives::border(global_location(), size()); } void Button::draw_text() @@ -61,12 +70,12 @@ void Button::draw_text() render::primitives::text_centered(global_location(), size(), text()); } -void Button::event_keypress(unsigned int key, unsigned int modifier) +void Button::keypress(unsigned int key, unsigned int modifier) { } -void Button::event_keyrelease(unsigned int key, unsigned int modifier) +void Button::keyrelease(unsigned int key, unsigned int modifier) { if (key == 512 + SDL_BUTTON_LEFT) { core::cmd() << button_command << std::endl; @@ -75,4 +84,3 @@ void Button::event_keyrelease(unsigned int key, unsigned int modifier) } - diff --git a/src/ui/button.h b/src/ui/button.h index bdf310b..34c3ba4 100644 --- a/src/ui/button.h +++ b/src/ui/button.h @@ -25,14 +25,15 @@ public: /// print button description virtual void print(size_t indent); - /// handle keyboard input - virtual void event_keypress(unsigned int key, unsigned int modifier); - virtual void event_keyrelease(unsigned int key, unsigned int modifier); + /// handle keyboard events + virtual void keypress(unsigned int key, unsigned int modifier); + virtual void keyrelease(unsigned int key, unsigned int modifier); protected: - virtual void draw(); + /// draw the button border + virtual void draw_border(); - /// draw the button + /// draw the button text virtual void draw_text(); private: diff --git a/src/ui/ui.cc b/src/ui/ui.cc index 0700c37..d6c53f0 100644 --- a/src/ui/ui.cc +++ b/src/ui/ui.cc @@ -168,8 +168,8 @@ void shutdown() UI::UI() : Window(0) { - set_palette(new Palette()); - set_label("user interface"); + set_palette(&ui_palette); + set_label("root"); set_size(1024, 768); set_border(false); @@ -237,16 +237,16 @@ void UI::load() } else if (ini.in_section("colors")) { if (ini.got_key_color("foreground", color)) { - widget_palette->set_foreground(color); + ui_palette.set_foreground(color); continue; } else if (ini.got_key_color("highlight", color)) { - widget_palette->set_highlight(color); + ui_palette.set_highlight(color); continue; } else if (ini.got_key_color("background", color)) { - widget_palette->set_background(color); + ui_palette.set_background(color); continue; } else if (ini.got_key_color("border", color)) { - widget_palette->set_border(color); + ui_palette.set_border(color); continue; } else { ini.unkown_key(); @@ -257,6 +257,24 @@ 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; + 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"); + } } UI::~UI() @@ -297,22 +315,30 @@ void UI::remove_window(Window *window) Window::remove_window(window); } -void UI::show_window(char const *label) +Window *UI::find_window(const char *label) { - Windows::iterator it; - for (it = window_children.begin(); it != window_children.end(); it++) { + 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 = (*it); + ui_active_window = window; ui_active_window->event_resize(); ui_active_window->show(); - ui_focus = this; - return; - } + ui_focus = window; + } else { + con_warn << "Unknown window '" << label << "'" << std::endl; } - - con_warn << "Unknown window '" << label << "'" << std::endl; } void UI::hide_window() @@ -329,21 +355,19 @@ void UI::frame() event_draw(); } -void UI::event_mousecursor(float x, float y) +void UI::event_mousemove(float x, float y) { mouse_cursor.assign(x, y); } void UI::event_keypress(unsigned int key, unsigned int modifier) { - if (ui_focus != this) - ui_focus->event_keypress(key, modifier); + ui_focus->keypress(key, modifier); } void UI::event_keyrelease(unsigned int key, unsigned int modifier) { - if (ui_focus != this) - ui_focus->event_keyrelease(key, modifier); + ui_focus->keyrelease(key, modifier); } } diff --git a/src/ui/ui.h b/src/ui/ui.h index 28b6997..5195cbd 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -31,7 +31,7 @@ public: void load(); /// make a window the active window - void show_window(char const *label); + void show_window(const char *label); /// hide the active window void hide_window(); @@ -40,11 +40,11 @@ public: Window *active() { return ui_active_window; } /// handle mouse cursor - virtual void event_mousecursor(float x, float y); + void event_mousemove(float x, float y); /// handle keyboard input - virtual void event_keypress(unsigned int key, unsigned int modifier); - virtual void event_keyrelease(unsigned int key, unsigned int modifier); + void event_keypress(unsigned int key, unsigned int modifier); + void event_keyrelease(unsigned int key, unsigned int modifier); /// run a user interface frame void frame(); @@ -53,10 +53,14 @@ public: inline Widget *focus() { return ui_focus; } protected: + Window *find_window(const char *label); + virtual void add_window(Window *window); virtual void remove_window(Window *window); private: + Palette ui_palette; + Window *ui_active_window; Widget *ui_focus; math::Vector2f mouse_cursor; diff --git a/src/ui/widget.cc b/src/ui/widget.cc index 506d1e7..11d7427 100644 --- a/src/ui/widget.cc +++ b/src/ui/widget.cc @@ -13,18 +13,20 @@ namespace ui { Widget::Widget(Widget *parent) { - widget_parent = parent; widget_visible = true; widget_border = true; widget_background = false; + widget_palette = 0; widget_label.assign("widget"); - if (widget_parent) { - parent->add_child(this); - widget_palette = parent->widget_palette; + if (!parent) { + widget_parent = root(); } else { - widget_palette = 0; + widget_parent = parent; } + + if (widget_parent) + widget_parent->add_child(this); } Widget::~Widget() @@ -52,6 +54,14 @@ void Widget::print(size_t indent) con_print << aux::pad_left(marker, indent*2) << label() << std::endl; } +Palette const *Widget::palette() const { + if (widget_palette) { + return widget_palette; + } else { + return parent()->palette(); + } +} + void Widget::show() { widget_visible = true; @@ -135,6 +145,30 @@ void Widget::remove_child(Widget *child) } } +void Widget::raise() +{ + if (!parent()) + return; + + Children::iterator it = find_child(this); + if (it != parent()->children().end()) { + parent()->children().erase(it); + parent()->children().push_front(this); + } +} + +void Widget::lower() +{ + if (!parent()) + return; + + Children::iterator it = find_child(this); + if (it != parent()->children().end()) { + parent()->children().erase(it); + parent()->children().push_back(this); + } +} + void Widget::event_resize() { resize(); @@ -215,13 +249,13 @@ bool Widget::has_focus() const return (root()->focus() == this); } -void Widget::event_mousecursor(float x, float y) +void Widget::mousemove(float x, float y) {} -void Widget::event_keypress(unsigned int key, unsigned int modifier) +void Widget::keypress(unsigned int key, unsigned int modifier) {} -void Widget::event_keyrelease(unsigned int key, unsigned int modifier) +void Widget::keyrelease(unsigned int key, unsigned int modifier) {} } diff --git a/src/ui/widget.h b/src/ui/widget.h index 13f392e..26832c7 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h @@ -45,7 +45,6 @@ public: inline float const height() const { return widget_size.y; } - inline Palette const *palette() const { return widget_palette; } inline std::string const &label() const { return widget_label; } @@ -53,10 +52,12 @@ public: inline bool background() const { return widget_background; } - inline Widget *parent() { return widget_parent; } + inline Widget *parent() const { return widget_parent; } inline bool visible() const { return widget_visible; } + Palette const *palette() const; + bool has_focus() const; @@ -99,17 +100,23 @@ public: /// draw event virtual void event_draw(); - /// handle mouse cursor - virtual void event_mousecursor(float x, float y); + /// handle mouse movement + virtual void mousemove(float x, float y); - /// handle keyboard input - virtual void event_keypress(unsigned int key, unsigned int modifier); + /// handle keyboard events + virtual void keypress(unsigned int key, unsigned int modifier); - virtual void event_keyrelease(unsigned int key, unsigned int modifier); + virtual void keyrelease(unsigned int key, unsigned int modifier); /// child widgets inline Children &children() { return widget_children; } + /// raise the widget to the top of the widget stack + void raise(); + + /// lower the widget to the bottom of the widget stack + void lower(); + protected: /// draw the widget virtual void draw(); @@ -171,8 +178,6 @@ protected: */ Widget *find_focus(math::Vector2f const & pos); - Palette *widget_palette; - private: bool widget_visible; bool widget_background; @@ -183,6 +188,7 @@ private: Children widget_children; + Palette *widget_palette; Widget *widget_parent; Children::iterator find_child(Widget *child); |