1 #include <msp/gl/blend.h>
2 #include <msp/input/keys.h>
3 #include <msp/time/utils.h>
8 #include "systemkeyboardinput.h"
13 Root::Root(Resources &r, Graphics::Window &window):
15 keyboard(new Input::Keyboard(window)),
17 mouse(new Input::Mouse(window)),
24 Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
35 void Root::init(Graphics::Window *window)
38 set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
43 camera.set_orthographic(geom.w, geom.h);
46 shprog = &resources.get<GL::Program>("ui.shader");
49 blend.src_factor = GL::SRC_ALPHA;
50 blend.dst_factor = GL::ONE_MINUS_SRC_ALPHA;
59 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
60 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
61 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
64 if(keyboard && !input_method)
65 input_method = new SystemKeyboardInput(*this, *keyboard);
69 touchscreen->signal_button_press.connect(sigc::mem_fun(this, &Root::touch_press_event));
70 touchscreen->signal_button_release.connect(sigc::mem_fun(this, &Root::touch_release_event));
71 touchscreen->signal_axis_motion.connect(sigc::mem_fun(this, &Root::touch_motion_event));
87 Time::TimeStamp t = Time::now();
88 Time::TimeDelta dt = (last_tick ? t-last_tick : Time::zero);
93 if(tooltip_timeout && Time::now()>tooltip_timeout)
96 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
98 tip = wdg->get_tooltip();
103 tip = signal_tooltip.emit(pointer_x, pointer_y);
104 tooltip_target = this;
111 lbl_tooltip = new Label;
113 lbl_tooltip->set_style("tooltip");
116 lbl_tooltip->set_text(tip);
117 lbl_tooltip->autosize();
118 const Geometry &tip_geom = lbl_tooltip->get_geometry();
119 unsigned x = pointer_x+10;
120 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
121 if(x+tip_geom.w>geom.w)
123 if(pointer_x>static_cast<int>(tip_geom.w+2))
124 x = pointer_x-2-tip_geom.w;
126 x = geom.w-tip_geom.w;
128 lbl_tooltip->set_position(x, y);
130 lbl_tooltip->set_visible(true);
133 tooltip_timeout = Time::TimeStamp();
137 void Root::setup_frame(GL::Renderer &)
142 void Root::render(GL::Renderer &renderer, GL::Tag tag) const
147 GL::Renderer::Push push(renderer);
148 renderer.set_camera(camera);
149 renderer.set_shader_program(shprog);
150 renderer.set_blend(&blend);
151 renderer.set_depth_test(0);
152 Widget::render(renderer);
155 bool Root::button_press_event(unsigned btn)
159 Widget *old_focus = pointer_focus;
163 button_press(x, y, btn);
165 if(pointer_focus || old_focus)
172 bool Root::button_release_event(unsigned btn)
176 Widget *old_focus = pointer_focus;
180 button_release(x, y, btn);
182 if(pointer_focus || old_focus)
189 bool Root::axis_motion_event(unsigned, float, float)
195 pointer_motion(x, y);
201 tooltip_timeout = Time::now()+700*Time::msec;
203 else if(get_descendant_at(x, y)!=tooltip_target)
206 lbl_tooltip->set_visible(false);
217 bool Root::touch_press_event(unsigned finger)
221 Widget *old_focus = touch_focus;
224 get_touch(finger, x, y);
225 touch_press(x, y, finger);
227 if(touch_focus || old_focus)
234 bool Root::touch_release_event(unsigned finger)
238 Widget *old_focus = touch_focus;
241 get_touch(finger, x, y);
242 touch_release(x, y, finger);
244 if(touch_focus || old_focus)
251 bool Root::touch_motion_event(unsigned axis, float, float)
255 unsigned finger = axis/2;
257 get_touch(finger, x, y);
258 touch_motion(x, y, finger);
267 void Root::get_pointer(int &x, int &y)
269 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
270 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
273 void Root::get_touch(unsigned finger, int &x, int &y)
275 x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
276 y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
279 void Root::update_camera()
281 camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
282 camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
283 camera.set_orthographic(geom.w, geom.h);
286 void Root::on_size_change()
288 Panel::on_size_change();
292 void Root::on_child_added(Widget &wdg)
294 if(&wdg!=lbl_tooltip)
295 Panel::on_child_added(wdg);