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)),
24 Root::Root(const Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m):
33 void Root::init(Graphics::Window *window)
36 set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
41 camera.set_orthographic(geom.w, geom.h);
44 if(GL::ARB_shader_objects)
46 shprog = new GL::Program;
47 GL::ProgramBuilder::StandardFeatures features;
48 features.material = true;
49 features.texture = true;
50 GL::ProgramBuilder(features).add_shaders(*shprog);
60 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
61 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
62 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
67 keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
68 keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
69 keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
85 if(tooltip_timeout && Time::now()>tooltip_timeout)
88 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
90 tip = wdg->get_tooltip();
95 tip = signal_tooltip.emit(pointer_x, pointer_y);
96 tooltip_target = this;
103 lbl_tooltip = new Label;
105 lbl_tooltip->set_style("tooltip");
108 lbl_tooltip->set_text(tip);
109 lbl_tooltip->autosize();
110 const Geometry &tip_geom = lbl_tooltip->get_geometry();
111 unsigned x = pointer_x+10;
112 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
113 if(x+tip_geom.w>geom.w)
115 if(pointer_x>static_cast<int>(tip_geom.w+2))
116 x = pointer_x-2-tip_geom.w;
118 x = geom.w-tip_geom.w;
120 lbl_tooltip->set_position(x, y);
122 lbl_tooltip->set_visible(true);
125 tooltip_timeout = Time::TimeStamp();
129 void Root::render() const
131 GL::Bind bind_blend(GL::Blend::alpha());
133 GL::Renderer renderer(&camera);
134 renderer.set_shader_program(shprog);
135 Widget::render(renderer);
138 bool Root::button_press_event(unsigned btn)
142 Widget *old_focus = pointer_focus;
146 button_press(x, y, btn);
148 if(pointer_focus || old_focus)
155 bool Root::button_release_event(unsigned btn)
159 Widget *old_focus = pointer_focus;
163 button_release(x, y, btn);
165 if(pointer_focus || old_focus)
172 bool Root::axis_motion_event(unsigned, float, float)
178 pointer_motion(x, y);
184 tooltip_timeout = Time::now()+700*Time::msec;
186 else if(get_descendant_at(x, y)!=tooltip_target)
189 lbl_tooltip->set_visible(false);
200 bool Root::key_press_event(unsigned key)
205 Widget *old_focus = input_focus;
209 if(input_focus || old_focus)
216 bool Root::key_release_event(unsigned key)
220 Widget *old_focus = input_focus;
224 if(input_focus || old_focus)
231 bool Root::character_event(StringCodec::unichar ch)
235 Widget *old_focus = input_focus;
239 if(input_focus || old_focus)
246 void Root::get_pointer(int &x, int &y)
248 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
249 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
252 void Root::update_camera()
254 camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
255 camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
256 camera.set_orthographic(geom.w, geom.h);
259 void Root::on_geometry_change()
261 Panel::on_geometry_change();
265 void Root::on_child_added(Widget &wdg)
267 if(&wdg!=lbl_tooltip)
268 Panel::on_child_added(wdg);