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()));
42 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
43 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
44 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
49 keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
50 keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
51 keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
66 if(tooltip_timeout && Time::now()>tooltip_timeout)
69 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
71 tip = wdg->get_tooltip();
76 tip = signal_tooltip.emit(pointer_x, pointer_y);
77 tooltip_target = this;
84 lbl_tooltip = new Label;
86 lbl_tooltip->set_style("tooltip");
89 lbl_tooltip->set_text(tip);
90 lbl_tooltip->autosize();
91 const Geometry &tip_geom = lbl_tooltip->get_geometry();
92 unsigned x = pointer_x+10;
93 unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
94 if(x+tip_geom.w>geom.w)
96 if(pointer_x>static_cast<int>(tip_geom.w+2))
97 x = pointer_x-2-tip_geom.w;
99 x = geom.w-tip_geom.w;
101 lbl_tooltip->set_position(x, y);
103 lbl_tooltip->set_visible(true);
106 tooltip_timeout = Time::TimeStamp();
110 void Root::render() const
112 GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(geom.w, geom.h);
113 GL::MatrixStack::modelview() = GL::Matrix();
114 GL::Bind bind_blend(GL::Blend::alpha());
116 GL::Renderer renderer(0);
117 Widget::render(renderer);
120 bool Root::button_press_event(unsigned btn)
124 Widget *old_focus = pointer_focus;
128 button_press(x, y, btn);
130 if(pointer_focus || old_focus)
137 bool Root::button_release_event(unsigned btn)
141 Widget *old_focus = pointer_focus;
145 button_release(x, y, btn);
147 if(pointer_focus || old_focus)
154 bool Root::axis_motion_event(unsigned, float, float)
160 pointer_motion(x, y);
166 tooltip_timeout = Time::now()+700*Time::msec;
168 else if(get_descendant_at(x, y)!=tooltip_target)
171 lbl_tooltip->set_visible(false);
182 bool Root::key_press_event(unsigned key)
187 Widget *old_focus = input_focus;
191 if(input_focus || old_focus)
198 bool Root::key_release_event(unsigned key)
202 Widget *old_focus = input_focus;
206 if(input_focus || old_focus)
213 bool Root::character_event(StringCodec::unichar ch)
217 Widget *old_focus = input_focus;
221 if(input_focus || old_focus)
228 void Root::get_pointer(int &x, int &y)
230 x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
231 y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;