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):
14 Root(r, &window, new Input::Keyboard(window), new Input::Mouse(window), 0)
19 Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
26 set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
28 camera.set_orthographic(geom.w, geom.h);
31 shprog = &resources.get<GL::Program>("ui.shader");
34 blend.src_factor = GL::SRC_ALPHA;
35 blend.dst_factor = GL::ONE_MINUS_SRC_ALPHA;
44 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
45 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
46 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
49 if(keyboard && !input_method)
50 input_method = new SystemKeyboardInput(*this, *keyboard);
54 touchscreen->signal_button_press.connect(sigc::mem_fun(this, &Root::touch_press_event));
55 touchscreen->signal_button_release.connect(sigc::mem_fun(this, &Root::touch_release_event));
56 touchscreen->signal_axis_motion.connect(sigc::mem_fun(this, &Root::touch_motion_event));
72 Time::TimeStamp t = Time::now();
73 Time::TimeDelta dt = (last_tick ? t-last_tick : Time::zero);
78 if(tooltip_timeout && Time::now()>tooltip_timeout)
81 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
83 tip = wdg->get_tooltip();
88 tip = signal_tooltip.emit(pointer_x, pointer_y);
89 tooltip_target = this;
96 lbl_tooltip = new Label;
98 lbl_tooltip->set_style("tooltip");
101 lbl_tooltip->set_text(tip);
102 lbl_tooltip->autosize();
103 const Geometry &tip_geom = lbl_tooltip->get_geometry();
104 unsigned x = pointer_x+10;
105 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
106 if(x+tip_geom.w>geom.w)
108 if(pointer_x>static_cast<int>(tip_geom.w+2))
109 x = pointer_x-2-tip_geom.w;
111 x = geom.w-tip_geom.w;
113 lbl_tooltip->set_position(x, y);
115 lbl_tooltip->set_visible(true);
118 tooltip_timeout = Time::TimeStamp();
122 void Root::setup_frame(GL::Renderer &)
127 void Root::render(GL::Renderer &renderer, GL::Tag tag) const
132 GL::Renderer::Push push(renderer);
133 renderer.set_camera(camera);
134 renderer.set_shader_program(shprog);
135 renderer.set_blend(&blend);
136 renderer.set_depth_test(0);
137 Widget::render(renderer);
140 bool Root::button_press_event(unsigned btn)
144 Widget *old_focus = pointer_focus;
148 button_press(x, y, btn);
150 if(pointer_focus || old_focus)
157 bool Root::button_release_event(unsigned btn)
161 Widget *old_focus = pointer_focus;
165 button_release(x, y, btn);
167 if(pointer_focus || old_focus)
174 bool Root::axis_motion_event(unsigned, float, float)
180 pointer_motion(x, y);
186 tooltip_timeout = Time::now()+700*Time::msec;
188 else if(get_descendant_at(x, y)!=tooltip_target)
191 lbl_tooltip->set_visible(false);
202 bool Root::touch_press_event(unsigned finger)
206 Widget *old_focus = touch_focus;
209 get_touch(finger, x, y);
210 touch_press(x, y, finger);
212 if(touch_focus || old_focus)
219 bool Root::touch_release_event(unsigned finger)
223 Widget *old_focus = touch_focus;
226 get_touch(finger, x, y);
227 touch_release(x, y, finger);
229 if(touch_focus || old_focus)
236 bool Root::touch_motion_event(unsigned axis, float, float)
240 unsigned finger = axis/2;
242 get_touch(finger, x, y);
243 touch_motion(x, y, finger);
252 void Root::get_pointer(int &x, int &y)
254 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
255 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
258 void Root::get_touch(unsigned finger, int &x, int &y)
260 x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
261 y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
264 void Root::update_camera()
266 camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
267 camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
268 camera.set_orthographic(geom.w, geom.h);
271 void Root::on_size_change()
273 Panel::on_size_change();
277 void Root::on_child_added(Widget &wdg)
279 if(&wdg!=lbl_tooltip)
280 Panel::on_child_added(wdg);