1 #include <msp/gl/blend.h>
2 #include <msp/gl/extensions/arb_shader_objects.h>
3 #include <msp/gl/programbuilder.h>
4 #include <msp/gl/tests.h>
5 #include <msp/gl/uniform.h>
6 #include <msp/input/keys.h>
7 #include <msp/time/units.h>
8 #include <msp/time/utils.h>
12 #include "systemkeyboardinput.h"
17 Root::Root(Resources &r, Graphics::Window &window):
19 keyboard(new Input::Keyboard(window)),
21 mouse(new Input::Mouse(window)),
28 Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
39 void Root::init(Graphics::Window *window)
42 set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
47 camera.set_orthographic(geom.w, geom.h);
50 if(GL::ARB_shader_objects)
52 shprog = new GL::Program;
53 GL::ProgramBuilder::StandardFeatures features;
54 features.material = true;
55 features.texture = true;
56 GL::ProgramBuilder(features).add_shaders(*shprog);
66 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
67 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
68 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
71 if(keyboard && !input_method)
72 input_method = new SystemKeyboardInput(*this, *keyboard);
76 touchscreen->signal_button_press.connect(sigc::mem_fun(this, &Root::touch_press_event));
77 touchscreen->signal_button_release.connect(sigc::mem_fun(this, &Root::touch_release_event));
78 touchscreen->signal_axis_motion.connect(sigc::mem_fun(this, &Root::touch_motion_event));
95 if(tooltip_timeout && Time::now()>tooltip_timeout)
98 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
100 tip = wdg->get_tooltip();
101 tooltip_target = wdg;
105 tip = signal_tooltip.emit(pointer_x, pointer_y);
106 tooltip_target = this;
113 lbl_tooltip = new Label;
115 lbl_tooltip->set_style("tooltip");
118 lbl_tooltip->set_text(tip);
119 lbl_tooltip->autosize();
120 const Geometry &tip_geom = lbl_tooltip->get_geometry();
121 unsigned x = pointer_x+10;
122 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
123 if(x+tip_geom.w>geom.w)
125 if(pointer_x>static_cast<int>(tip_geom.w+2))
126 x = pointer_x-2-tip_geom.w;
128 x = geom.w-tip_geom.w;
130 lbl_tooltip->set_position(x, y);
132 lbl_tooltip->set_visible(true);
135 tooltip_timeout = Time::TimeStamp();
139 void Root::render() const
141 GL::Bind bind_blend(GL::Blend::alpha());
143 GL::Renderer renderer(&camera);
144 renderer.set_shader_program(shprog);
145 Widget::render(renderer);
148 void Root::render(GL::Renderer &renderer, const GL::Tag &tag) const
153 GL::Renderer::Push push(renderer);
154 renderer.set_camera(camera);
155 renderer.set_shader_program(shprog);
156 GL::BindRestore bind_blend(GL::Blend::alpha());
157 GL::BindRestore unbind_dtest(static_cast<GL::DepthTest *>(0));
158 Widget::render(renderer);
161 bool Root::button_press_event(unsigned btn)
165 Widget *old_focus = pointer_focus;
169 button_press(x, y, btn);
171 if(pointer_focus || old_focus)
178 bool Root::button_release_event(unsigned btn)
182 Widget *old_focus = pointer_focus;
186 button_release(x, y, btn);
188 if(pointer_focus || old_focus)
195 bool Root::axis_motion_event(unsigned, float, float)
201 pointer_motion(x, y);
207 tooltip_timeout = Time::now()+700*Time::msec;
209 else if(get_descendant_at(x, y)!=tooltip_target)
212 lbl_tooltip->set_visible(false);
223 bool Root::touch_press_event(unsigned finger)
227 Widget *old_focus = touch_focus;
230 get_touch(finger, x, y);
231 touch_press(x, y, finger);
233 if(touch_focus || old_focus)
240 bool Root::touch_release_event(unsigned finger)
244 Widget *old_focus = touch_focus;
247 get_touch(finger, x, y);
248 touch_release(x, y, finger);
250 if(touch_focus || old_focus)
257 bool Root::touch_motion_event(unsigned axis, float, float)
261 unsigned finger = axis/2;
263 get_touch(finger, x, y);
264 touch_motion(x, y, finger);
273 void Root::get_pointer(int &x, int &y)
275 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
276 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
279 void Root::get_touch(unsigned finger, int &x, int &y)
281 x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
282 y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
285 void Root::update_camera()
287 camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
288 camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
289 camera.set_orthographic(geom.w, geom.h);
292 void Root::on_geometry_change()
294 Panel::on_geometry_change();
298 void Root::on_child_added(Widget &wdg)
300 if(&wdg!=lbl_tooltip)
301 Panel::on_child_added(wdg);