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 = new Label;
95 lbl_tooltip->set_style("tooltip");
98 lbl_tooltip->set_text(tip);
99 lbl_tooltip->autosize();
100 const Geometry &tip_geom = lbl_tooltip->get_geometry();
101 unsigned x = pointer_x+10;
102 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
103 if(x+tip_geom.w>geom.w)
105 if(pointer_x>static_cast<int>(tip_geom.w+2))
106 x = pointer_x-2-tip_geom.w;
108 x = geom.w-tip_geom.w;
110 lbl_tooltip->set_position(x, y);
112 lbl_tooltip->set_visible(true);
115 tooltip_timeout = Time::TimeStamp();
119 void Root::setup_frame(GL::Renderer &)
124 void Root::render(GL::Renderer &renderer, GL::Tag tag) const
129 GL::Renderer::Push push(renderer);
130 renderer.set_camera(camera);
131 renderer.set_shader_program(shprog);
132 renderer.set_blend(&blend);
133 renderer.set_depth_test(nullptr);
134 Widget::render(renderer);
137 bool Root::button_press_event(unsigned btn)
141 Widget *old_focus = pointer_focus;
145 button_press(x, y, btn);
147 if(pointer_focus || old_focus)
154 bool Root::button_release_event(unsigned btn)
158 Widget *old_focus = pointer_focus;
162 button_release(x, y, btn);
164 if(pointer_focus || old_focus)
171 bool Root::axis_motion_event(unsigned, float, float)
177 pointer_motion(x, y);
183 tooltip_timeout = Time::now()+700*Time::msec;
185 else if(find_descendant_at(x, y)!=tooltip_target)
188 lbl_tooltip->set_visible(false);
189 tooltip_target = nullptr;
199 bool Root::touch_press_event(unsigned finger)
203 Widget *old_focus = touch_focus;
206 get_touch(finger, x, y);
207 touch_press(x, y, finger);
209 if(touch_focus || old_focus)
216 bool Root::touch_release_event(unsigned finger)
220 Widget *old_focus = touch_focus;
223 get_touch(finger, x, y);
224 touch_release(x, y, finger);
226 if(touch_focus || old_focus)
233 bool Root::touch_motion_event(unsigned axis, float, float)
237 unsigned finger = axis/2;
239 get_touch(finger, x, y);
240 touch_motion(x, y, finger);
249 void Root::get_pointer(int &x, int &y)
251 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
252 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
255 void Root::get_touch(unsigned finger, int &x, int &y)
257 x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
258 y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
261 void Root::update_camera()
263 camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
264 camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
265 camera.set_orthographic(geom.w, geom.h);
268 void Root::on_size_change()
270 Panel::on_size_change();
274 void Root::on_child_added(Widget &wdg)
276 if(&wdg!=lbl_tooltip)
277 Panel::on_child_added(wdg);