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)),
18 set_geometry(Geometry(0, 0, window.get_width(), window.get_height()));
23 Root::Root(const Resources &r, Input::Keyboard *k, Input::Mouse *m):
41 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
42 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
43 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
48 keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
49 keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
50 keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
65 if(tooltip_timeout && Time::now()>tooltip_timeout)
68 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
70 tip = wdg->get_tooltip();
75 tip = signal_tooltip.emit(pointer_x, pointer_y);
76 tooltip_target = this;
83 lbl_tooltip = new Label;
85 lbl_tooltip->set_style("tooltip");
88 lbl_tooltip->set_text(tip);
89 lbl_tooltip->autosize();
90 const Geometry &tip_geom = lbl_tooltip->get_geometry();
91 unsigned x = pointer_x+10;
92 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
93 if(x+tip_geom.w>geom.w)
95 if(pointer_x>static_cast<int>(tip_geom.w+2))
96 x = pointer_x-2-tip_geom.w;
98 x = geom.w-tip_geom.w;
100 lbl_tooltip->set_position(x, y);
102 lbl_tooltip->set_visible(true);
105 tooltip_timeout = Time::TimeStamp();
109 void Root::render() const
111 GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(geom.w, geom.h);
112 GL::MatrixStack::modelview() = GL::Matrix();
113 GL::Bind bind_blend(GL::Blend::alpha());
115 GL::Renderer renderer(0);
116 Widget::render(renderer);
119 bool Root::button_press_event(unsigned btn)
123 Widget *old_focus = pointer_focus;
127 button_press(x, y, btn);
129 if(pointer_focus || old_focus)
136 bool Root::button_release_event(unsigned btn)
140 Widget *old_focus = pointer_focus;
144 button_release(x, y, btn);
146 if(pointer_focus || old_focus)
153 bool Root::axis_motion_event(unsigned, float, float)
159 pointer_motion(x, y);
165 tooltip_timeout = Time::now()+700*Time::msec;
167 else if(get_descendant_at(x, y)!=tooltip_target)
170 lbl_tooltip->set_visible(false);
181 bool Root::key_press_event(unsigned key)
186 Widget *old_focus = input_focus;
190 if(input_focus || old_focus)
197 bool Root::key_release_event(unsigned key)
201 Widget *old_focus = input_focus;
205 if(input_focus || old_focus)
212 bool Root::character_event(StringCodec::unichar ch)
216 Widget *old_focus = input_focus;
220 if(input_focus || old_focus)
227 void Root::get_pointer(int &x, int &y)
229 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
230 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;