]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/root.cpp
Defer widget rebuild to just before they are rendered
[libs/gltk.git] / source / root.cpp
index c54ab7b00682b78bd39041eb09565bc46cd3254d..64c7dd5548fa2af424df645ac414860221dfc482 100644 (file)
@@ -1,20 +1,22 @@
 #include <msp/gl/blend.h>
 #include <msp/gl/extensions/arb_shader_objects.h>
 #include <msp/gl/programbuilder.h>
+#include <msp/gl/tests.h>
 #include <msp/gl/uniform.h>
 #include <msp/input/keys.h>
-#include <msp/time/units.h>
 #include <msp/time/utils.h>
 #include "label.h"
 #include "style.h"
 #include "root.h"
+#include "systemkeyboardinput.h"
 
 namespace Msp {
 namespace GLtk {
 
-Root::Root(const Resources &r, Graphics::Window &window):
+Root::Root(Resources &r, Graphics::Window &window):
        resources(r),
        keyboard(new Input::Keyboard(window)),
+       input_method(0),
        mouse(new Input::Mouse(window)),
        touchscreen(0),
        own_input(true)
@@ -22,9 +24,10 @@ Root::Root(const Resources &r, Graphics::Window &window):
        init(&window);
 }
 
-Root::Root(const Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
+Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
        resources(r),
        keyboard(k),
+       input_method(0),
        mouse(m),
        touchscreen(t),
        own_input(false)
@@ -55,6 +58,9 @@ void Root::init(Graphics::Window *window)
        else
                shprog = 0;
 
+       if(keyboard)
+               set_state(FOCUS);
+
        update_style();
 
        if(mouse)
@@ -64,12 +70,8 @@ void Root::init(Graphics::Window *window)
                mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
        }
 
-       if(keyboard)
-       {
-               keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
-               keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
-               keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
-       }
+       if(keyboard && !input_method)
+               input_method = new SystemKeyboardInput(*this, *keyboard);
 
        if(touchscreen)
        {
@@ -82,6 +84,7 @@ void Root::init(Graphics::Window *window)
 Root::~Root()
 {
        delete shprog;
+       delete input_method;
        if(own_input)
        {
                delete keyboard;
@@ -91,6 +94,12 @@ Root::~Root()
 
 void Root::tick()
 {
+       Time::TimeStamp t = Time::now();
+       Time::TimeDelta dt = (last_tick ? t-last_tick : Time::zero);
+       last_tick = t;
+
+       animate(dt);
+
        if(tooltip_timeout && Time::now()>tooltip_timeout)
        {
                std::string tip;
@@ -135,8 +144,10 @@ void Root::tick()
        }
 }
 
-void Root::render() const
+void Root::render()
 {
+       rebuild_hierarchy();
+
        GL::Bind bind_blend(GL::Blend::alpha());
 
        GL::Renderer renderer(&camera);
@@ -144,6 +155,24 @@ void Root::render() const
        Widget::render(renderer);
 }
 
+void Root::setup_frame(GL::Renderer &)
+{
+       rebuild_hierarchy();
+}
+
+void Root::render(GL::Renderer &renderer, const GL::Tag &tag) const
+{
+       if(tag.id)
+               return;
+
+       GL::Renderer::Push push(renderer);
+       renderer.set_camera(camera);
+       renderer.set_shader_program(shprog);
+       GL::BindRestore bind_blend(GL::Blend::alpha());
+       GL::BindRestore unbind_dtest(static_cast<GL::DepthTest *>(0));
+       Widget::render(renderer);
+}
+
 bool Root::button_press_event(unsigned btn)
 {
        if(visible)
@@ -206,31 +235,6 @@ bool Root::axis_motion_event(unsigned, float, float)
        return false;
 }
 
-bool Root::key_press_event(unsigned key)
-{
-       // XXX Modifiers
-       if(visible)
-               return key_press(key, 0);
-
-       return false;
-}
-
-bool Root::key_release_event(unsigned key)
-{
-       if(visible)
-               return key_release(key, 0);
-
-       return false;
-}
-
-bool Root::character_event(StringCodec::unichar ch)
-{
-       if(visible)
-               return character(ch);
-
-       return false;
-}
-
 bool Root::touch_press_event(unsigned finger)
 {
        if(visible)
@@ -300,9 +304,9 @@ void Root::update_camera()
        camera.set_orthographic(geom.w, geom.h);
 }
 
-void Root::on_geometry_change()
+void Root::on_size_change()
 {
-       Panel::on_geometry_change();
+       Panel::on_size_change();
        update_camera();
 }