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/widget.cc | 253 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 168 insertions(+), 85 deletions(-) (limited to 'src/ui/widget.cc') diff --git a/src/ui/widget.cc b/src/ui/widget.cc index b2c436d..e17e5ab 100644 --- a/src/ui/widget.cc +++ b/src/ui/widget.cc @@ -10,22 +10,25 @@ #include "ui/ui.h" #include "ui/widget.h" -namespace ui { +namespace ui +{ -Widget::Widget(Widget *parent) { +Widget::Widget(Widget *parent) +{ + widget_focus = false; widget_visible = true; widget_border = true; widget_background = false; widget_palette = 0; widget_font = 0; widget_label.assign("widget"); - + if (!parent) { widget_parent = root(); } else { widget_parent = parent; } - + if (widget_parent) widget_parent->add_child(this); } @@ -33,39 +36,47 @@ Widget::Widget(Widget *parent) { Widget::~Widget() { for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { - delete (*it); + delete(*it); (*it) = 0; } widget_children.clear(); } -size_t Widget::list(size_t indent) +size_t Widget::list(const size_t indent) const { print(indent); size_t n = 1; - for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { + for (Children::const_iterator it = widget_children.begin(); it != widget_children.end(); it++) { n += (*it)->list(indent+1); } return n; } -void Widget::print(size_t indent) +void Widget::print(const size_t indent) const { - std::string marker(""); - con_print << aux::pad_left(marker, indent*2) << label() << std::endl; + if (indent) { + std::string marker; + if (widget_focus) + marker.assign("^B* ^N"); + else + marker.assign(" "); + con_print << aux::pad_left(marker, indent*2) << label() << std::endl; + } } -Palette const *Widget::palette() const { +const Palette *Widget::palette() const +{ if (widget_palette) { - return widget_palette; + return widget_palette; } else { return parent()->palette(); } } -Font const *Widget::font() const { +const Font *Widget::font() const +{ if (widget_font) { - return widget_font; + return widget_font; } else { return parent()->font(); } @@ -75,7 +86,7 @@ void Widget::lower() { if (!parent()) return; - + Children::iterator it = parent()->find_child(this); if (it != parent()->children().end()) { parent()->children().erase(it); @@ -87,7 +98,7 @@ void Widget::raise() { if (!parent()) return; - + Children::iterator it = parent()->find_child(this); if (it != parent()->children().end()) { parent()->children().erase(it); @@ -95,6 +106,20 @@ void Widget::raise() } } +void Widget::set_focus() +{ + if (!parent()) { + widget_focus = true; + return; + } + + for (Children::iterator it = parent()->children().begin(); it != parent()->children().end(); it++) { + (*it)->widget_focus = false; + } + + widget_focus = true; +} + void Widget::show() { widget_visible = true; @@ -108,7 +133,10 @@ void Widget::hide() void Widget::set_visible(bool visible) { - widget_visible = visible; + if (visible) + show(); + else + hide(); } void Widget::set_border(bool border) @@ -133,20 +161,26 @@ void Widget::set_label(char const *label) aux::to_label(widget_label); } -void Widget::set_palette(Palette *palette) +void Widget::set_palette(const Palette *palette) { widget_palette = palette; } -void Widget::set_font(Font *font) +void Widget::set_font(const Font *font) { widget_font = font; } -void Widget::set_location(float const x, float const y) { +void Widget::set_location(float const x, float const y) +{ widget_location.assign(x, y); } +void Widget::set_location(const math::Vector2f &location) +{ + widget_location.assign(location); +} + void Widget::set_size(float const w, float const h) { widget_size.assign(w, h); @@ -173,7 +207,7 @@ Widget::Children::iterator Widget::find_child(Widget *child) if ((*it) == child) return it; } - + return it; } @@ -184,118 +218,167 @@ void Widget::add_child(Widget *child) widget_children.push_back(child); } } - + void Widget::remove_child(Widget *child) { Children::iterator it = find_child(child); if (it != widget_children.end()) { - delete (*it); + delete(*it); widget_children.erase(it); } } -void Widget::event_resize() +Widget *Widget::find_input_focus() { - resize(); - for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { - (*it)->event_resize(); + if (!visible() || !widget_focus) + return 0; + + for (Children::const_reverse_iterator rit = widget_children.rbegin(); rit != widget_children.rend(); ++rit) { + Widget *w = (*rit); + if (w->visible() && w->widget_focus) { + Widget *f = w->find_input_focus(); + if (f) + return f; + } } + + // no child with input focus + return this; } -void Widget::resize() +Widget *Widget::find_mouse_focus(const math::Vector2f & pos) { + // this widget is not visible + if (!visible() || !size().contains(pos)) + return 0; + + // reverse-iterate children + for (Children::const_reverse_iterator rit = widget_children.rbegin(); rit != widget_children.rend(); ++rit) { + Widget *w = (*rit); + if (w->visible()) { + Widget *f = w->find_mouse_focus(pos - w->location()); + if (f) + return f; + } + + } + + // no child with mouse focus + return this; } -void Widget::event_draw() +bool Widget::has_mouse_focus() const { - if (!widget_visible) - return; - - draw(); - for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { - if ((*it)->visible()) - (*it)->event_draw(); - } + return (root()->mouse_focus() == this); } -void Widget::draw() +bool Widget::has_input_focus() const { - draw_background(); - draw_border(); + return (root()->input_focus() == this); } -void Widget::draw_background() +/* -- event distributors ------------------------------------------- */ + +bool Widget::event_key(const bool pressed, const int key, const unsigned int modifier) { - if (!widget_background) - return; + bool handled = false; + + if (pressed) { + handled = on_keypress(key, modifier); + } else { + handled = on_keyrelease(key, modifier); + } + + if (!handled && parent()) + handled = parent()->event_key(pressed, key, modifier); + + return handled; +} - paint::color(palette()->background()); - paint::rectangle(global_location(), size()); +bool Widget::event_mouse(const math::Vector2f &cursor) +{ + math::Vector2f local_cursor = to_local_coords(cursor); + bool handled = false; + + if (root()->mouse_focus() != this) { + on_mouseover(local_cursor); + } + + on_mousemove(local_cursor); + return handled; } -void Widget::draw_border() +void Widget::event_draw() { - if (!widget_border) + if (!visible()) return; - - paint::color(palette()->border()); - paint::border(global_location(), size()); + + draw(); + + for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { + if ((*it)->visible()) + (*it)->event_draw(); + } } -Widget *Widget::event_focus(math::Vector2f const & pos) +void Widget::event_resize() { - // this widget is not visible - if (!visible()) - return 0; + resize(); + for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { + (*it)->event_resize(); + } +} - // pos is outside this - if ((pos.x < 0) || (pos.y < 0) || (pos.x > size().x) || (pos.y > size().y)) - return 0; +/* -- event handlers ----------------------------------------------- */ - // reverse-iterate children - for (Children::reverse_iterator rit = widget_children.rbegin(); rit != widget_children.rend(); ++rit) { - Widget *w = (*rit); - if (w->visible()) { - Widget *f = w->event_focus(pos - w->location()); - if (f) - return f; - } +void Widget::on_mouseover(const math::Vector2f &cursor) +{ + return; +} - } - - // no child has focus - return this; +void Widget::on_mousemove(const math::Vector2f &cursor) +{ + return; } -bool Widget::has_focus() const +bool Widget::on_keypress(const int key, const unsigned int modifier) { - return (root()->focus() == this); + return false; } -bool Widget::event_key(bool pressed, unsigned int key, unsigned int modifier) +bool Widget::on_keyrelease(const int key, const unsigned int modifier) { - bool handled; + return false; +} - if (pressed) { - handled = keypress(key, modifier); - } else { - handled = keyrelease(key, modifier); - } +/* -- draw functions ----------------------------------------------- */ - if (!handled && parent()) - handled = parent()->event_key(pressed, key, modifier); +void Widget::resize() +{ +} - return handled; +void Widget::draw() +{ + draw_background(); + draw_border(); } -bool Widget::keypress(unsigned int key, unsigned int modifier) +void Widget::draw_background() { - return false; + if (!widget_background) + return; + + paint::color(palette()->background()); + paint::rectangle(global_location(), size()); } -bool Widget::keyrelease(unsigned int key, unsigned int modifier) +void Widget::draw_border() { - return false; + if (!widget_border) + return; + + paint::color(palette()->border()); + paint::border(global_location(), size()); } } -- cgit v1.2.3