]> 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 1c030e82c8c38f103fa5234bde231163fceb3706..16ed5bd58c1d9f94a22236b8900c1db8a1be0831 100644 (file)
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <msp/gl/blend.h>
 #include <msp/input/keys.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(Resources &r, Graphics::Window &w):
-       Panel(r),
-       window(w)
+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)
+{
+       own_input[0] = move(k);
+       own_input[1] = move(m);
+}
+
+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)
 {
-       set_geometry(Geometry(0, 0, window.get_width(), window.get_height()));
+       if(window)
+               set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
+
+       camera.set_orthographic(geom.w, geom.h);
+       update_camera();
+
+       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();
 
-       window.signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
-       window.signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
-       window.signal_pointer_motion.connect(sigc::mem_fun(this, &Root::pointer_motion_event));
-       window.signal_key_press.connect(sigc::mem_fun(this, &Root::key_press_event));
-       window.signal_key_release.connect(sigc::mem_fun(this, &Root::key_release_event));
+       if(mouse)
+       {
+               mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
+               mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
+               mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
+       }
+
+       if(keyboard && !input_method)
+               input_method = make_unique<SystemKeyboardInput>(*this, *keyboard);
+
+       if(touchscreen)
+       {
+               touchscreen->signal_button_press.connect(sigc::mem_fun(this, &Root::touch_press_event));
+               touchscreen->signal_button_release.connect(sigc::mem_fun(this, &Root::touch_release_event));
+               touchscreen->signal_axis_motion.connect(sigc::mem_fun(this, &Root::touch_motion_event));
+       }
 }
 
-void Root::button_press_event(int x, int y, unsigned btn, unsigned)
+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 = find_descendant_at(pointer_x, pointer_y))
+               {
+                       tip = wdg->get_tooltip();
+                       tooltip_target = wdg;
+               }
+               else
+               {
+                       tip = signal_tooltip.emit(pointer_x, pointer_y);
+                       tooltip_target = this;
+               }
+
+               if(!tip.empty())
+               {
+                       if(!lbl_tooltip)
+                       {
+                               lbl_tooltip = &add_new<Label>();
+                               lbl_tooltip->set_style("tooltip");
+                       }
+
+                       lbl_tooltip->set_text(tip);
+                       lbl_tooltip->autosize();
+                       const Geometry &tip_geom = lbl_tooltip->get_geometry();
+                       unsigned x = pointer_x+10;
+                       unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
+                       if(x+tip_geom.w>geom.w)
+                       {
+                               if(pointer_x>static_cast<int>(tip_geom.w+2))
+                                       x = pointer_x-2-tip_geom.w;
+                               else
+                                       x = geom.w-tip_geom.w;
+                       }
+                       lbl_tooltip->set_position(x, y);
+                       raise(*lbl_tooltip);
+                       lbl_tooltip->set_visible(true);
+               }
+
+               tooltip_timeout = Time::TimeStamp();
+       }
+}
+
+void Root::setup_frame(GL::Renderer &)
+{
+       rebuild_hierarchy();
+}
+
+void Root::render(GL::Renderer &renderer, GL::Tag tag) const
+{
+       if(tag.id)
+               return;
+
+       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);
+}
+
+bool Root::button_press_event(unsigned btn)
 {
        if(visible)
        {
-               translate_coords(x, y);
+               Widget *old_focus = pointer_focus;
+
+               int x, y;
+               get_pointer(x, y);
                button_press(x, y, btn);
+
+               if(pointer_focus || old_focus)
+                       return true;
        }
+
+       return false;
 }
 
-void Root::button_release_event(int x, int y, unsigned btn, unsigned)
+bool Root::button_release_event(unsigned btn)
 {
        if(visible)
        {
-               translate_coords(x, y);
+               Widget *old_focus = pointer_focus;
+
+               int x, y;
+               get_pointer(x, y);
                button_release(x, y, btn);
+
+               if(pointer_focus || old_focus)
+                       return true;
        }
+
+       return false;
 }
 
-void Root::pointer_motion_event(int x, int y)
+bool Root::axis_motion_event(unsigned, float, float)
 {
        if(visible)
        {
-               translate_coords(x, y);
+               int x, y;
+               get_pointer(x, y);
                pointer_motion(x, y);
+
+               if(!tooltip_target)
+               {
+                       pointer_x = x;
+                       pointer_y = y;
+                       tooltip_timeout = Time::now()+700*Time::msec;
+               }
+               else if(find_descendant_at(x, y)!=tooltip_target)
+               {
+                       if(lbl_tooltip)
+                               lbl_tooltip->set_visible(false);
+                       tooltip_target = nullptr;
+               }
+
+               if(pointer_focus)
+                       return true;
+       }
+
+       return false;
+}
+
+bool Root::touch_press_event(unsigned finger)
+{
+       if(visible)
+       {
+               Widget *old_focus = touch_focus;
+
+               int x, y;
+               get_touch(finger, x, y);
+               touch_press(x, y, finger);
+
+               if(touch_focus || old_focus)
+                       return true;
        }
+
+       return false;
 }
 
-void Root::key_press_event(unsigned key, unsigned mod, wchar_t ch)
+bool Root::touch_release_event(unsigned finger)
 {
        if(visible)
-               key_press(Input::key_from_sys(key), mod, ch);
+       {
+               Widget *old_focus = touch_focus;
+
+               int x, y;
+               get_touch(finger, x, y);
+               touch_release(x, y, finger);
+
+               if(touch_focus || old_focus)
+                       return true;
+       }
+
+       return false;
 }
 
-void Root::key_release_event(unsigned key, unsigned mod)
+bool Root::touch_motion_event(unsigned axis, float, float)
 {
        if(visible)
-               key_release(Input::key_from_sys(key), mod);
+       {
+               unsigned finger = axis/2;
+               int x, y;
+               get_touch(finger, x, y);
+               touch_motion(x, y, finger);
+
+               if(touch_focus)
+                       return true;
+       }
+
+       return false;
+}
+
+void Root::get_pointer(int &x, int &y)
+{
+       x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
+       y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
+}
+
+void Root::get_touch(unsigned finger, int &x, int &y)
+{
+       x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
+       y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
+}
+
+void Root::update_camera()
+{
+       camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
+       camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
+       camera.set_orthographic(geom.w, geom.h);
+}
+
+void Root::on_size_change()
+{
+       Panel::on_size_change();
+       update_camera();
 }
 
-void Root::translate_coords(int &x, int &y)
+void Root::on_child_added(Widget &wdg)
 {
-       x=x*geom.w/window.get_width();
-       y=geom.h-1-y*geom.h/window.get_height();
+       if(&wdg!=lbl_tooltip)
+               Panel::on_child_added(wdg);
 }
 
 } // namespace GLtk