]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/root.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / root.cpp
index a90ea5a23d9eec4a3b3bacc34b94fb1a912b8b4c..16ed5bd58c1d9f94a22236b8900c1db8a1be0831 100644 (file)
@@ -1,59 +1,48 @@
 #include <msp/gl/blend.h>
-#include <msp/gl/extensions/arb_shader_objects.h>
-#include <msp/gl/programbuilder.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 "resources.h"
 #include "root.h"
+#include "systemkeyboardinput.h"
+
+using namespace std;
 
 namespace Msp {
 namespace GLtk {
 
-Root::Root(const Resources &r, Graphics::Window &window):
-       resources(r),
-       keyboard(new Input::Keyboard(window)),
-       mouse(new Input::Mouse(window)),
-       touchscreen(0),
-       own_input(true)
+Root::Root(Resources &r, Graphics::Window &window, unique_ptr<Input::Keyboard> k, unique_ptr<Input::Mouse> m):
+       Root(r, &window, k.get(), m.get(), nullptr)
 {
-       init(&window);
+       own_input[0] = move(k);
+       own_input[1] = move(m);
 }
 
-Root::Root(const Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
+Root::Root(Resources &r, Graphics::Window &window):
+       Root(r, window, make_unique<Input::Keyboard>(window), make_unique<Input::Mouse>(window))
+{ }
+
+Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
        resources(r),
        keyboard(k),
        mouse(m),
-       touchscreen(t),
-       own_input(false)
-{
-       init(window);
-}
-
-void Root::init(Graphics::Window *window)
+       touchscreen(t)
 {
        if(window)
                set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
 
-       lbl_tooltip = 0;
-       tooltip_target = 0;
-
        camera.set_orthographic(geom.w, geom.h);
        update_camera();
 
-       if(GL::ARB_shader_objects)
-       {
-               shprog = new GL::Program;
-               GL::ProgramBuilder::StandardFeatures features;
-               features.material = true;
-               features.texture = true;
-               GL::ProgramBuilder(features).add_shaders(*shprog);
-               shprog->link();
-       }
-       else
-               shprog = 0;
+       shprog = &resources.get<GL::Program>("ui.shader");
+
+       blend.enabled = true;
+       blend.src_factor = GL::SRC_ALPHA;
+       blend.dst_factor = GL::ONE_MINUS_SRC_ALPHA;
+
+       if(keyboard)
+               set_state(FOCUS);
 
        update_style();
 
@@ -64,12 +53,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 = make_unique<SystemKeyboardInput>(*this, *keyboard);
 
        if(touchscreen)
        {
@@ -79,22 +64,18 @@ void Root::init(Graphics::Window *window)
        }
 }
 
-Root::~Root()
-{
-       delete shprog;
-       if(own_input)
-       {
-               delete keyboard;
-               delete mouse;
-       }
-}
-
 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;
-               if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
+               if(Widget *wdg = find_descendant_at(pointer_x, pointer_y))
                {
                        tip = wdg->get_tooltip();
                        tooltip_target = wdg;
@@ -109,8 +90,7 @@ void Root::tick()
                {
                        if(!lbl_tooltip)
                        {
-                               lbl_tooltip = new Label;
-                               add(*lbl_tooltip);
+                               lbl_tooltip = &add_new<Label>();
                                lbl_tooltip->set_style("tooltip");
                        }
 
@@ -135,12 +115,21 @@ void Root::tick()
        }
 }
 
-void Root::render() const
+void Root::setup_frame(GL::Renderer &)
 {
-       GL::Bind bind_blend(GL::Blend::alpha());
+       rebuild_hierarchy();
+}
+
+void Root::render(GL::Renderer &renderer, GL::Tag tag) const
+{
+       if(tag.id)
+               return;
 
-       GL::Renderer renderer(&camera);
+       GL::Renderer::Push push(renderer);
+       renderer.set_camera(camera);
        renderer.set_shader_program(shprog);
+       renderer.set_blend(&blend);
+       renderer.set_depth_test(nullptr);
        Widget::render(renderer);
 }
 
@@ -192,11 +181,11 @@ bool Root::axis_motion_event(unsigned, float, float)
                        pointer_y = y;
                        tooltip_timeout = Time::now()+700*Time::msec;
                }
-               else if(get_descendant_at(x, y)!=tooltip_target)
+               else if(find_descendant_at(x, y)!=tooltip_target)
                {
                        if(lbl_tooltip)
                                lbl_tooltip->set_visible(false);
-                       tooltip_target = 0;
+                       tooltip_target = nullptr;
                }
 
                if(pointer_focus)
@@ -206,52 +195,6 @@ bool Root::axis_motion_event(unsigned, float, float)
        return false;
 }
 
-bool Root::key_press_event(unsigned key)
-{
-       // XXX Modifiers
-       if(visible)
-       {
-               Widget *old_focus = input_focus;
-
-               key_press(key, 0);
-
-               if(input_focus || old_focus)
-                       return true;
-       }
-
-       return false;
-}
-
-bool Root::key_release_event(unsigned key)
-{
-       if(visible)
-       {
-               Widget *old_focus = input_focus;
-
-               key_release(key, 0);
-
-               if(input_focus || old_focus)
-                       return true;
-       }
-
-       return false;
-}
-
-bool Root::character_event(StringCodec::unichar ch)
-{
-       if(visible)
-       {
-               Widget *old_focus = input_focus;
-
-               character(ch);
-
-               if(input_focus || old_focus)
-                       return true;
-       }
-
-       return false;
-}
-
 bool Root::touch_press_event(unsigned finger)
 {
        if(visible)
@@ -321,9 +264,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();
 }