1 #include <msp/gl/blend.h>
2 #include <msp/input/keys.h>
3 #include <msp/time/utils.h>
8 #include "systemkeyboardinput.h"
15 Root::Root(Resources &r, Graphics::Window &window, unique_ptr<Input::Keyboard> k, unique_ptr<Input::Mouse> m):
16 Root(r, &window, k.get(), m.get(), nullptr)
18 own_input[0] = move(k);
19 own_input[1] = move(m);
22 Root::Root(Resources &r, Graphics::Window &window):
23 Root(r, window, make_unique<Input::Keyboard>(window), make_unique<Input::Mouse>(window))
26 Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
33 set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
35 camera.set_orthographic(geom.w, geom.h);
38 shprog = &resources.get<GL::Program>("ui.shader");
41 blend.src_factor = GL::SRC_ALPHA;
42 blend.dst_factor = GL::ONE_MINUS_SRC_ALPHA;
51 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
52 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
53 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
56 if(keyboard && !input_method)
57 input_method = make_unique<SystemKeyboardInput>(*this, *keyboard);
61 touchscreen->signal_button_press.connect(sigc::mem_fun(this, &Root::touch_press_event));
62 touchscreen->signal_button_release.connect(sigc::mem_fun(this, &Root::touch_release_event));
63 touchscreen->signal_axis_motion.connect(sigc::mem_fun(this, &Root::touch_motion_event));
69 Time::TimeStamp t = Time::now();
70 Time::TimeDelta dt = (last_tick ? t-last_tick : Time::zero);
75 if(tooltip_timeout && Time::now()>tooltip_timeout)
78 if(Widget *wdg = find_descendant_at(pointer_x, pointer_y))
80 tip = wdg->get_tooltip();
85 tip = signal_tooltip.emit(pointer_x, pointer_y);
86 tooltip_target = this;
93 lbl_tooltip = &add_new<Label>();
94 lbl_tooltip->set_style("tooltip");
97 lbl_tooltip->set_text(tip);
98 lbl_tooltip->autosize();
99 const Geometry &tip_geom = lbl_tooltip->get_geometry();
100 unsigned x = pointer_x+10;
101 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
102 if(x+tip_geom.w>geom.w)
104 if(pointer_x>static_cast<int>(tip_geom.w+2))
105 x = pointer_x-2-tip_geom.w;
107 x = geom.w-tip_geom.w;
109 lbl_tooltip->set_position(x, y);
111 lbl_tooltip->set_visible(true);
114 tooltip_timeout = Time::TimeStamp();
118 void Root::setup_frame(GL::Renderer &)
123 void Root::render(GL::Renderer &renderer, GL::Tag tag) const
128 GL::Renderer::Push push(renderer);
129 renderer.set_camera(camera);
130 renderer.set_shader_program(shprog);
131 renderer.set_blend(&blend);
132 renderer.set_depth_test(nullptr);
133 Widget::render(renderer);
136 bool Root::button_press_event(unsigned btn)
140 Widget *old_focus = pointer_focus;
144 button_press(x, y, btn);
146 if(pointer_focus || old_focus)
153 bool Root::button_release_event(unsigned btn)
157 Widget *old_focus = pointer_focus;
161 button_release(x, y, btn);
163 if(pointer_focus || old_focus)
170 bool Root::axis_motion_event(unsigned, float, float)
176 pointer_motion(x, y);
182 tooltip_timeout = Time::now()+700*Time::msec;
184 else if(find_descendant_at(x, y)!=tooltip_target)
187 lbl_tooltip->set_visible(false);
188 tooltip_target = nullptr;
198 bool Root::touch_press_event(unsigned finger)
202 Widget *old_focus = touch_focus;
205 get_touch(finger, x, y);
206 touch_press(x, y, finger);
208 if(touch_focus || old_focus)
215 bool Root::touch_release_event(unsigned finger)
219 Widget *old_focus = touch_focus;
222 get_touch(finger, x, y);
223 touch_release(x, y, finger);
225 if(touch_focus || old_focus)
232 bool Root::touch_motion_event(unsigned axis, float, float)
236 unsigned finger = axis/2;
238 get_touch(finger, x, y);
239 touch_motion(x, y, finger);
248 void Root::get_pointer(int &x, int &y)
250 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
251 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
254 void Root::get_touch(unsigned finger, int &x, int &y)
256 x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
257 y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
260 void Root::update_camera()
262 camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
263 camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
264 camera.set_orthographic(geom.w, geom.h);
267 void Root::on_size_change()
269 Panel::on_size_change();
273 void Root::on_child_added(Widget &wdg)
275 if(&wdg!=lbl_tooltip)
276 Panel::on_child_added(wdg);