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/utils.h>
11 #include "systemkeyboardinput.h"
16 Root::Root(Resources &r, Graphics::Window &window):
18 keyboard(new Input::Keyboard(window)),
20 mouse(new Input::Mouse(window)),
27 Root::Root(Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
38 void Root::init(Graphics::Window *window)
41 set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
46 camera.set_orthographic(geom.w, geom.h);
49 if(GL::ARB_shader_objects)
51 shprog = new GL::Program;
52 GL::ProgramBuilder::StandardFeatures features;
53 features.material = true;
54 features.texture = true;
55 GL::ProgramBuilder(features).add_shaders(*shprog);
68 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
69 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
70 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
73 if(keyboard && !input_method)
74 input_method = new SystemKeyboardInput(*this, *keyboard);
78 touchscreen->signal_button_press.connect(sigc::mem_fun(this, &Root::touch_press_event));
79 touchscreen->signal_button_release.connect(sigc::mem_fun(this, &Root::touch_release_event));
80 touchscreen->signal_axis_motion.connect(sigc::mem_fun(this, &Root::touch_motion_event));
97 Time::TimeStamp t = Time::now();
98 Time::TimeDelta dt = (last_tick ? t-last_tick : Time::zero);
103 if(tooltip_timeout && Time::now()>tooltip_timeout)
106 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
108 tip = wdg->get_tooltip();
109 tooltip_target = wdg;
113 tip = signal_tooltip.emit(pointer_x, pointer_y);
114 tooltip_target = this;
121 lbl_tooltip = new Label;
123 lbl_tooltip->set_style("tooltip");
126 lbl_tooltip->set_text(tip);
127 lbl_tooltip->autosize();
128 const Geometry &tip_geom = lbl_tooltip->get_geometry();
129 unsigned x = pointer_x+10;
130 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
131 if(x+tip_geom.w>geom.w)
133 if(pointer_x>static_cast<int>(tip_geom.w+2))
134 x = pointer_x-2-tip_geom.w;
136 x = geom.w-tip_geom.w;
138 lbl_tooltip->set_position(x, y);
140 lbl_tooltip->set_visible(true);
143 tooltip_timeout = Time::TimeStamp();
147 void Root::render() const
149 GL::Bind bind_blend(GL::Blend::alpha());
151 GL::Renderer renderer(&camera);
152 renderer.set_shader_program(shprog);
153 Widget::render(renderer);
156 void Root::render(GL::Renderer &renderer, const GL::Tag &tag) const
161 GL::Renderer::Push push(renderer);
162 renderer.set_camera(camera);
163 renderer.set_shader_program(shprog);
164 GL::BindRestore bind_blend(GL::Blend::alpha());
165 GL::BindRestore unbind_dtest(static_cast<GL::DepthTest *>(0));
166 Widget::render(renderer);
169 bool Root::button_press_event(unsigned btn)
173 Widget *old_focus = pointer_focus;
177 button_press(x, y, btn);
179 if(pointer_focus || old_focus)
186 bool Root::button_release_event(unsigned btn)
190 Widget *old_focus = pointer_focus;
194 button_release(x, y, btn);
196 if(pointer_focus || old_focus)
203 bool Root::axis_motion_event(unsigned, float, float)
209 pointer_motion(x, y);
215 tooltip_timeout = Time::now()+700*Time::msec;
217 else if(get_descendant_at(x, y)!=tooltip_target)
220 lbl_tooltip->set_visible(false);
231 bool Root::touch_press_event(unsigned finger)
235 Widget *old_focus = touch_focus;
238 get_touch(finger, x, y);
239 touch_press(x, y, finger);
241 if(touch_focus || old_focus)
248 bool Root::touch_release_event(unsigned finger)
252 Widget *old_focus = touch_focus;
255 get_touch(finger, x, y);
256 touch_release(x, y, finger);
258 if(touch_focus || old_focus)
265 bool Root::touch_motion_event(unsigned axis, float, float)
269 unsigned finger = axis/2;
271 get_touch(finger, x, y);
272 touch_motion(x, y, finger);
281 void Root::get_pointer(int &x, int &y)
283 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
284 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
287 void Root::get_touch(unsigned finger, int &x, int &y)
289 x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
290 y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
293 void Root::update_camera()
295 camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
296 camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
297 camera.set_orthographic(geom.w, geom.h);
300 void Root::on_geometry_change()
302 Panel::on_geometry_change();
306 void Root::on_child_added(Widget &wdg)
308 if(&wdg!=lbl_tooltip)
309 Panel::on_child_added(wdg);