X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fpanel.cpp;h=dc2808ed4bf5a237d97aad9cd1ec75e068ac7580;hb=10c448468c4e225fab701e69bdc296422bb3f509;hp=930a68f980f96a065552f11942c1b14f519ac2db;hpb=72db2a8d41cc0eb8404572d1a720d59fab0551cd;p=libs%2Fgltk.git diff --git a/source/panel.cpp b/source/panel.cpp index 930a68f..dc2808e 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -3,16 +3,19 @@ #include #include "button.h" #include "column.h" +#include "draghandle.h" #include "dropdown.h" #include "entry.h" #include "grid.h" #include "hslider.h" +#include "image.h" #include "indicator.h" #include "label.h" #include "list.h" #include "panel.h" #include "part.h" #include "row.h" +#include "stack.h" #include "toggle.h" #include "vslider.h" @@ -23,7 +26,9 @@ namespace GLtk { Panel::Panel(): layout(0) -{ } +{ + input_type = INPUT_NAVIGATION; +} Panel::~Panel() { @@ -38,10 +43,10 @@ void Panel::set_layout(Layout *l) layout = l; } -void Panel::autosize() +void Panel::autosize_special(const Part &part, Geometry &ageom) const { - if(layout) - layout->autosize(); + if(part.get_name()=="children" && layout) + layout->autosize(ageom); } void Panel::render_special(const Part &part, GL::Renderer &renderer) const @@ -54,6 +59,95 @@ 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 nav_x = (nav==NAV_RIGHT ? 1 : nav==NAV_LEFT ? -1 : 0); + int nav_y = (nav==NAV_UP ? 1 : nav==NAV_DOWN ? -1 : 0); + + int origin_x, origin_y, origin_dim; + if(input_focus) + { + const Geometry &fgeom = input_focus->get_geometry(); + origin_x = fgeom.x+(nav_x*0.5+0.5)*fgeom.w; + origin_y = fgeom.y+(nav_y*0.5+0.5)*fgeom.h; + origin_dim = abs(nav_x)*fgeom.h+abs(nav_y)*fgeom.w; + } + else + { + origin_x = geom.w*(0.5-nav_x*0.5); + origin_y = geom.h*(0.5-nav_y*0.5); + origin_dim = abs(nav_x)*geom.h+abs(nav_y)*geom.w; + } + + Widget *sibling = find_next_child(origin_x, origin_y, origin_dim, nav_x, nav_y); + if(!sibling && input_focus) + { + const Geometry &fgeom = input_focus->get_geometry(); + origin_x -= fgeom.w*(nav_x*0.5); + origin_y -= fgeom.h*(nav_y*0.5); + sibling = find_next_child(origin_x, origin_y, origin_dim, nav_x, nav_y); + } + + if(sibling) + { + set_input_focus(sibling); + if(Panel *panel = dynamic_cast(sibling)) + panel->navigate(nav); + return true; + } + } + + return false; +} + +Widget *Panel::find_next_child(int origin_x, int origin_y, int origin_dim, int nav_x, int nav_y) const +{ + 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 = compute_delta(cgeom.x, cgeom.w, origin_x, origin_dim, nav_x); + int dy = compute_delta(cgeom.y, cgeom.h, origin_y, origin_dim, nav_y); + + int score = -1; + if(nav_y && nav_y*dy>=0) + score = nav_y*dy+abs(dx)*4; + else if(nav_x && nav_x*dx>=0) + score = nav_x*dx+abs(dy)*4; + + if(score>=0 && (!sibling || scorewidget; + best_score = score; + } + } + + return sibling; +} + +int Panel::compute_delta(int pos, int dim, int origin_pos, int origin_dim, int nav) +{ + if(nav<0) + return pos+dim-origin_pos; + else if(nav>0) + return pos-origin_pos; + else if(pos+dimorigin_pos+origin_dim/2) + return pos-origin_pos-origin_dim/2; + else + return 0; +} + void Panel::on_geometry_change() { if(layout) @@ -87,6 +181,7 @@ Panel::Loader::Loader(Panel &p, map &m): add("button", &Loader::child