From 094ac735e16ad94c12bc1d866253f1c1d84a9af6 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 17 Jul 2020 23:08:03 +0200 Subject: Added Widget::tooltip() support. Minor cleanups. --- src/ui/ui.cc | 10 +++++----- src/ui/widget.cc | 33 +++++++++++++++++++++++++-------- src/ui/widget.h | 42 +++++++++++++++++++++++++++++------------- 3 files changed, 59 insertions(+), 26 deletions(-) (limited to 'src/ui') diff --git a/src/ui/ui.cc b/src/ui/ui.cc index 0877519..700099e 100644 --- a/src/ui/ui.cc +++ b/src/ui/ui.cc @@ -266,7 +266,7 @@ void UI::frame() Widget *f = 0; if (!mouse_buttonleft_pressed) { - f = find_mouse_focus(mouse_cursor); + f = find_widget_in_location(mouse_cursor); } else { f = find_visible_child(ui_mouse_focus); } @@ -306,7 +306,7 @@ bool UI::input_mouse_button(const bool pressed, unsigned int button) } // set mouse focus - Widget *f = find_mouse_focus(mouse_cursor); + Widget *f = find_widget_in_location(mouse_cursor); if (f) { f->event_mouse(mouse_cursor); @@ -326,7 +326,7 @@ bool UI::input_mouse_wheel(const math::Vector2f & direction) bool handled = false; // set mouse focus - Widget *f = find_mouse_focus(mouse_cursor); + Widget *f = find_widget_in_location(mouse_cursor); if (f) { f->event_mouse(mouse_cursor); @@ -399,8 +399,8 @@ void UI::draw_pointer() 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; + const float t = core::application()->time() * 0.75f; + const float angle = (t - floorf(t)) * 360.0f; gl::rotate(angle, math::Vector3f(0, 0, 1.0f)); gl::translate(-mouse_cursor.x(), -mouse_cursor.y(), 0); } diff --git a/src/ui/widget.cc b/src/ui/widget.cc index 982b792..8c40c04 100644 --- a/src/ui/widget.cc +++ b/src/ui/widget.cc @@ -228,14 +228,31 @@ void Widget::set_label(const std::string & label) widget_label.assign(label); aux::to_label(widget_label); } - + void Widget::set_label(const char *label) { - if (label) { + if (label == nullptr) + { + widget_label.clear(); + } else { widget_label.assign(label); aux::to_label(widget_label); + } +} + + +void Widget::set_tooltip(const std::string &tooltip) +{ + widget_tooltip.assign(tooltip); +} + +void Widget::set_tooltip(const char *tooltip) +{ + if (tooltip == nullptr) + { + widget_tooltip.clear(); } else { - widget_label.clear(); + widget_label.assign(tooltip); } } @@ -300,7 +317,7 @@ Widget *Widget::next_sibling() // find this widget in the parent's children Children::iterator it = parent()->children().begin(); while (it != parent()->children().end() && ((*it) != this)) { - it++; + it++; } // assert this widget is a child of its parent @@ -384,17 +401,17 @@ Widget *Widget::find_visible_child(const Widget *widget) return 0; } -Widget *Widget::find_mouse_focus(const math::Vector2f & pos) +Widget *Widget::find_widget_in_location(const math::Vector2f & location) { // this widget is not visible - if (!visible() || !size().contains(pos)) - return 0; + if (!visible() || !size().contains(location)) + return nullptr; // reverse-iterate children for (Children::reverse_iterator rit = widget_children.rbegin(); rit != widget_children.rend(); ++rit) { Widget *w = (*rit); if (w->visible() && w->enabled()) { - Widget *f = w->find_mouse_focus(pos - w->location()); + Widget *f = w->find_widget_in_location(location - w->location()); if (f) return f; } diff --git a/src/ui/widget.h b/src/ui/widget.h index 14bea7b..243fdda 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h @@ -129,6 +129,11 @@ public: inline const std::string &label() const { return widget_label; } + + /// widget tooltip + inline const std::string &tooltip() const { + return widget_tooltip; + } /// true if this widget will draw a background inline const bool background() const { @@ -220,29 +225,35 @@ public: /// set location of the top-left corner, relative to the parent void set_location(const math::Vector2f &location); - /// set the widgets width and height + /// set the widget's width and height void set_size(const float w, const float h); - /// set the widgets width and height + /// set the widget's width and height void set_size(const math::Vector2f &size); - /// set the widgets width + /// set the widget's width void set_width(const float w); - /// set the widgets height + /// set the widget's height void set_height(const float h); - /// set the widgets palette + /// set the widget's palette void set_palette(const Palette *palette); - /// set the widgets font + /// set the widget's font void set_font(const Font *font); - /// set the widgets label + /// set the widget's label void set_label(const std::string &label); - /// set the widgets label - void set_label(const char *label); + /// set the widget's label + void set_label(const char *label = nullptr); + + /// set the wdiget's widget_tooltip + void set_tooltip(const std::string &tooltip); + + /// set the wdiget's widget_tooltip + void set_tooltip(const char *tooltip = nullptr); /// enable or disable widget border void set_border(const bool border = true); @@ -309,10 +320,14 @@ protected: /// find the widget that has input focus virtual Widget *find_input_focus(); - /// find widget that has mouse focus - /** @param cursor mouse cursor position relative to this widget's location - */ - Widget *find_mouse_focus(const math::Vector2f & cursor); + /** + * @brief find the widget in a given location + * Searches the widget's children tree for the leaf widget visible in a given location. + * If no child contains the location, the widget itself is tested. Returns a nullptr ig + * the location is not within the widget's boundries. + * @param location search position, relative to this widget's location + **/ + Widget *find_widget_in_location(const math::Vector2f &location); /// find a visible widget Widget *find_visible_child(const Widget *widget); @@ -431,6 +446,7 @@ private: math::Vector2f widget_location; math::Vector2f widget_size; std::string widget_label; + std::string widget_tooltip; Children widget_children; -- cgit v1.2.3