From ae62c05fe97d341c4f219656cdce7aadf321991b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 1 Sep 2016 10:46:57 +0300 Subject: [PATCH] Move navigation logic from Container to Panel A number of widgets inherit from Container because they have internal sub-widgets, but have their own navigation logic. Panel is the user- visible generic container. --- source/container.cpp | 64 +++----------------------------------------- source/panel.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++ source/panel.h | 4 +++ 3 files changed, 72 insertions(+), 60 deletions(-) diff --git a/source/container.cpp b/source/container.cpp index b50bc29..c38509f 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -304,66 +304,10 @@ void Container::focus_out() bool Container::navigate(Navigation nav) { - if(input_focus && input_focus->navigate(nav)) - return true; - - if(nav==NAV_UP || nav==NAV_DOWN || nav==NAV_LEFT || nav==NAV_RIGHT) - { - int x = geom.w/2; - int y = geom.h/2; - if(input_focus) - { - const Geometry &fgeom = input_focus->get_geometry(); - x = fgeom.x+fgeom.w/2; - y = fgeom.y+fgeom.h/2; - } - else if(nav==NAV_UP) - y = 0; - else if(nav==NAV_DOWN) - y = geom.h; - else if(nav==NAV_RIGHT) - x = 0; - else if(nav==NAV_LEFT) - x = geom.w; - - Widget *sibling = 0; - int best_score = 0; - for(list::const_iterator i=children.begin(); i!=children.end(); ++i) - { - if((*i)->widget==input_focus || !(*i)->widget->is_focusable()) - continue; - - const Geometry &cgeom = (*i)->widget->get_geometry(); - int dx = cgeom.x+cgeom.w/2-x; - int dy = cgeom.y+cgeom.h/2-y; - - int score = -1; - if(nav==NAV_UP && dy>0) - score = dy+abs(dx)*4; - else if(nav==NAV_DOWN && dy<0) - score = -dy+abs(dx)*4; - else if(nav==NAV_RIGHT && dx>0) - score = dx+abs(dy)*4; - else if(nav==NAV_LEFT && dx<0) - score = -dx+abs(dy)*4; - - if(score>0 && (!sibling || scorewidget; - best_score = score; - } - } - - if(sibling) - { - set_input_focus(sibling); - if(Container *container = dynamic_cast(sibling)) - container->navigate(nav); - return true; - } - } - - return false; + if(input_focus) + return input_focus->navigate(nav); + else + return false; } void Container::on_reparent() diff --git a/source/panel.cpp b/source/panel.cpp index c17fe4b..7e34c59 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -59,6 +59,70 @@ void Panel::render_special(const Part &part, GL::Renderer &renderer) const } } +bool Panel::navigate(Navigation nav) +{ + if(Container::navigate(nav)) + return true; + + if(nav==NAV_UP || nav==NAV_DOWN || nav==NAV_LEFT || nav==NAV_RIGHT) + { + int x = geom.w/2; + int y = geom.h/2; + if(input_focus) + { + const Geometry &fgeom = input_focus->get_geometry(); + x = fgeom.x+fgeom.w/2; + y = fgeom.y+fgeom.h/2; + } + else if(nav==NAV_UP) + y = 0; + else if(nav==NAV_DOWN) + y = geom.h; + else if(nav==NAV_RIGHT) + x = 0; + else if(nav==NAV_LEFT) + x = geom.w; + + Widget *sibling = 0; + int best_score = 0; + for(list::const_iterator i=children.begin(); i!=children.end(); ++i) + { + if((*i)->widget==input_focus || !(*i)->widget->is_focusable()) + continue; + + const Geometry &cgeom = (*i)->widget->get_geometry(); + int dx = cgeom.x+cgeom.w/2-x; + int dy = cgeom.y+cgeom.h/2-y; + + int score = -1; + if(nav==NAV_UP && dy>0) + score = dy+abs(dx)*4; + else if(nav==NAV_DOWN && dy<0) + score = -dy+abs(dx)*4; + else if(nav==NAV_RIGHT && dx>0) + score = dx+abs(dy)*4; + else if(nav==NAV_LEFT && dx<0) + score = -dx+abs(dy)*4; + + if(score>0 && (!sibling || scorewidget; + best_score = score; + } + } + + if(sibling) + { + set_input_focus(sibling); + if(Panel *panel = dynamic_cast(sibling)) + panel->navigate(nav); + return true; + } + } + + return false; +} + void Panel::on_geometry_change() { if(layout) diff --git a/source/panel.h b/source/panel.h index e0eed01..ebe765d 100644 --- a/source/panel.h +++ b/source/panel.h @@ -71,6 +71,10 @@ protected: virtual void autosize_special(const Part &, Geometry &) const; virtual void render_special(const Part &, GL::Renderer &) const; +public: + virtual bool navigate(Navigation); + +protected: virtual void on_geometry_change(); virtual void on_child_added(Widget &); virtual void on_child_removed(Widget &); -- 2.43.0