1 #include <msp/gl/blend.h>
2 #include <msp/input/keys.h>
3 #include <msp/time/units.h>
4 #include <msp/time/utils.h>
12 Root::Root(const Resources &r, Graphics::Window &window):
14 keyboard(new Input::Keyboard(window)),
15 mouse(new Input::Mouse(window)),
21 Root::Root(const Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m):
30 void Root::init(Graphics::Window *window)
33 set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
38 camera.set_orthographic(geom.w, geom.h);
45 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
46 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
47 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
52 keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
53 keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
54 keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
69 if(tooltip_timeout && Time::now()>tooltip_timeout)
72 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
74 tip = wdg->get_tooltip();
79 tip = signal_tooltip.emit(pointer_x, pointer_y);
80 tooltip_target = this;
87 lbl_tooltip = new Label;
89 lbl_tooltip->set_style("tooltip");
92 lbl_tooltip->set_text(tip);
93 lbl_tooltip->autosize();
94 const Geometry &tip_geom = lbl_tooltip->get_geometry();
95 unsigned x = pointer_x+10;
96 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
97 if(x+tip_geom.w>geom.w)
99 if(pointer_x>static_cast<int>(tip_geom.w+2))
100 x = pointer_x-2-tip_geom.w;
102 x = geom.w-tip_geom.w;
104 lbl_tooltip->set_position(x, y);
106 lbl_tooltip->set_visible(true);
109 tooltip_timeout = Time::TimeStamp();
113 void Root::render() const
115 GL::Bind bind_blend(GL::Blend::alpha());
117 GL::Renderer renderer(&camera);
118 Widget::render(renderer);
121 bool Root::button_press_event(unsigned btn)
125 Widget *old_focus = pointer_focus;
129 button_press(x, y, btn);
131 if(pointer_focus || old_focus)
138 bool Root::button_release_event(unsigned btn)
142 Widget *old_focus = pointer_focus;
146 button_release(x, y, btn);
148 if(pointer_focus || old_focus)
155 bool Root::axis_motion_event(unsigned, float, float)
161 pointer_motion(x, y);
167 tooltip_timeout = Time::now()+700*Time::msec;
169 else if(get_descendant_at(x, y)!=tooltip_target)
172 lbl_tooltip->set_visible(false);
183 bool Root::key_press_event(unsigned key)
188 Widget *old_focus = input_focus;
192 if(input_focus || old_focus)
199 bool Root::key_release_event(unsigned key)
203 Widget *old_focus = input_focus;
207 if(input_focus || old_focus)
214 bool Root::character_event(StringCodec::unichar ch)
218 Widget *old_focus = input_focus;
222 if(input_focus || old_focus)
229 void Root::get_pointer(int &x, int &y)
231 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
232 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
235 void Root::update_camera()
237 camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
238 camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
239 camera.set_orthographic(geom.w, geom.h);
242 void Root::on_geometry_change()
244 Panel::on_geometry_change();
248 void Root::on_child_added(Widget &wdg)
250 if(&wdg!=lbl_tooltip)
251 Panel::on_child_added(wdg);