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/uniform.h>
5 #include <msp/input/keys.h>
6 #include <msp/time/units.h>
7 #include <msp/time/utils.h>
15 Root::Root(const Resources &r, Graphics::Window &window):
17 keyboard(new Input::Keyboard(window)),
18 mouse(new Input::Mouse(window)),
25 Root::Root(const 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 if(GL::ARB_shader_objects)
48 shprog = new GL::Program;
49 GL::ProgramBuilder::StandardFeatures features;
50 features.material = true;
51 features.texture = true;
52 GL::ProgramBuilder(features).add_shaders(*shprog);
62 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
63 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
64 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
69 keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
70 keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
71 keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
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));
94 if(tooltip_timeout && Time::now()>tooltip_timeout)
97 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
99 tip = wdg->get_tooltip();
100 tooltip_target = wdg;
104 tip = signal_tooltip.emit(pointer_x, pointer_y);
105 tooltip_target = this;
112 lbl_tooltip = new Label;
114 lbl_tooltip->set_style("tooltip");
117 lbl_tooltip->set_text(tip);
118 lbl_tooltip->autosize();
119 const Geometry &tip_geom = lbl_tooltip->get_geometry();
120 unsigned x = pointer_x+10;
121 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
122 if(x+tip_geom.w>geom.w)
124 if(pointer_x>static_cast<int>(tip_geom.w+2))
125 x = pointer_x-2-tip_geom.w;
127 x = geom.w-tip_geom.w;
129 lbl_tooltip->set_position(x, y);
131 lbl_tooltip->set_visible(true);
134 tooltip_timeout = Time::TimeStamp();
138 void Root::render() const
140 GL::Bind bind_blend(GL::Blend::alpha());
142 GL::Renderer renderer(&camera);
143 renderer.set_shader_program(shprog);
144 Widget::render(renderer);
147 bool Root::button_press_event(unsigned btn)
151 Widget *old_focus = pointer_focus;
155 button_press(x, y, btn);
157 if(pointer_focus || old_focus)
164 bool Root::button_release_event(unsigned btn)
168 Widget *old_focus = pointer_focus;
172 button_release(x, y, btn);
174 if(pointer_focus || old_focus)
181 bool Root::axis_motion_event(unsigned, float, float)
187 pointer_motion(x, y);
193 tooltip_timeout = Time::now()+700*Time::msec;
195 else if(get_descendant_at(x, y)!=tooltip_target)
198 lbl_tooltip->set_visible(false);
209 bool Root::key_press_event(unsigned key)
214 Widget *old_focus = input_focus;
218 if(input_focus || old_focus)
225 bool Root::key_release_event(unsigned key)
229 Widget *old_focus = input_focus;
233 if(input_focus || old_focus)
240 bool Root::character_event(StringCodec::unichar ch)
244 Widget *old_focus = input_focus;
248 if(input_focus || old_focus)
255 bool Root::touch_press_event(unsigned finger)
259 Widget *old_focus = touch_focus;
262 get_touch(finger, x, y);
263 touch_press(x, y, finger);
265 if(touch_focus || old_focus)
272 bool Root::touch_release_event(unsigned finger)
276 Widget *old_focus = touch_focus;
279 get_touch(finger, x, y);
280 touch_release(x, y, finger);
282 if(touch_focus || old_focus)
289 bool Root::touch_motion_event(unsigned axis, float, float)
293 unsigned finger = axis/2;
295 get_touch(finger, x, y);
296 touch_motion(x, y, finger);
305 void Root::get_pointer(int &x, int &y)
307 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
308 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
311 void Root::get_touch(unsigned finger, int &x, int &y)
313 x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
314 y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
317 void Root::update_camera()
319 camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
320 camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
321 camera.set_orthographic(geom.w, geom.h);
324 void Root::on_geometry_change()
326 Panel::on_geometry_change();
330 void Root::on_child_added(Widget &wdg)
332 if(&wdg!=lbl_tooltip)
333 Panel::on_child_added(wdg);