]> git.tdb.fi Git - libs/gltk.git/blob - source/root.cpp
Rudimentary touchscreen support
[libs/gltk.git] / source / root.cpp
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>
8 #include "label.h"
9 #include "style.h"
10 #include "root.h"
11
12 namespace Msp {
13 namespace GLtk {
14
15 Root::Root(const Resources &r, Graphics::Window &window):
16         resources(r),
17         keyboard(new Input::Keyboard(window)),
18         mouse(new Input::Mouse(window)),
19         touchscreen(0),
20         own_input(true)
21 {
22         init(&window);
23 }
24
25 Root::Root(const Resources &r, Graphics::Window *window, Input::Keyboard *k, Input::Mouse *m, Input::Touchscreen *t):
26         resources(r),
27         keyboard(k),
28         mouse(m),
29         touchscreen(t),
30         own_input(false)
31 {
32         init(window);
33 }
34
35 void Root::init(Graphics::Window *window)
36 {
37         if(window)
38                 set_geometry(Geometry(0, 0, window->get_width(), window->get_height()));
39
40         lbl_tooltip = 0;
41         tooltip_target = 0;
42
43         camera.set_orthographic(geom.w, geom.h);
44         update_camera();
45
46         if(GL::ARB_shader_objects)
47         {
48                 shprog = new GL::Program;
49                 GL::ProgramBuilder::StandardFeatures features;
50                 features.material = true;
51                 features.texture = true;
52                 GL::ProgramBuilder(features).add_shaders(*shprog);
53                 shprog->link();
54         }
55         else
56                 shprog = 0;
57
58         update_style();
59
60         if(mouse)
61         {
62                 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
63                 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
64                 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
65         }
66
67         if(keyboard)
68         {
69                 keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
70                 keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
71                 keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
72         }
73
74         if(touchscreen)
75         {
76                 touchscreen->signal_button_press.connect(sigc::mem_fun(this, &Root::touch_press_event));
77                 touchscreen->signal_button_release.connect(sigc::mem_fun(this, &Root::touch_release_event));
78                 touchscreen->signal_axis_motion.connect(sigc::mem_fun(this, &Root::touch_motion_event));
79         }
80 }
81
82 Root::~Root()
83 {
84         delete shprog;
85         if(own_input)
86         {
87                 delete keyboard;
88                 delete mouse;
89         }
90 }
91
92 void Root::tick()
93 {
94         if(tooltip_timeout && Time::now()>tooltip_timeout)
95         {
96                 std::string tip;
97                 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
98                 {
99                         tip = wdg->get_tooltip();
100                         tooltip_target = wdg;
101                 }
102                 else
103                 {
104                         tip = signal_tooltip.emit(pointer_x, pointer_y);
105                         tooltip_target = this;
106                 }
107
108                 if(!tip.empty())
109                 {
110                         if(!lbl_tooltip)
111                         {
112                                 lbl_tooltip = new Label;
113                                 add(*lbl_tooltip);
114                                 lbl_tooltip->set_style("tooltip");
115                         }
116
117                         lbl_tooltip->set_text(tip);
118                         lbl_tooltip->autosize();
119                         const Geometry &tip_geom = lbl_tooltip->get_geometry();
120                         unsigned x = pointer_x+10;
121                         unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
122                         if(x+tip_geom.w>geom.w)
123                         {
124                                 if(pointer_x>static_cast<int>(tip_geom.w+2))
125                                         x = pointer_x-2-tip_geom.w;
126                                 else
127                                         x = geom.w-tip_geom.w;
128                         }
129                         lbl_tooltip->set_position(x, y);
130                         raise(*lbl_tooltip);
131                         lbl_tooltip->set_visible(true);
132                 }
133
134                 tooltip_timeout = Time::TimeStamp();
135         }
136 }
137
138 void Root::render() const
139 {
140         GL::Bind bind_blend(GL::Blend::alpha());
141
142         GL::Renderer renderer(&camera);
143         renderer.set_shader_program(shprog);
144         Widget::render(renderer);
145 }
146
147 bool Root::button_press_event(unsigned btn)
148 {
149         if(visible)
150         {
151                 Widget *old_focus = pointer_focus;
152
153                 int x, y;
154                 get_pointer(x, y);
155                 button_press(x, y, btn);
156
157                 if(pointer_focus || old_focus)
158                         return true;
159         }
160
161         return false;
162 }
163
164 bool Root::button_release_event(unsigned btn)
165 {
166         if(visible)
167         {
168                 Widget *old_focus = pointer_focus;
169
170                 int x, y;
171                 get_pointer(x, y);
172                 button_release(x, y, btn);
173
174                 if(pointer_focus || old_focus)
175                         return true;
176         }
177
178         return false;
179 }
180
181 bool Root::axis_motion_event(unsigned, float, float)
182 {
183         if(visible)
184         {
185                 int x, y;
186                 get_pointer(x, y);
187                 pointer_motion(x, y);
188
189                 if(!tooltip_target)
190                 {
191                         pointer_x = x;
192                         pointer_y = y;
193                         tooltip_timeout = Time::now()+700*Time::msec;
194                 }
195                 else if(get_descendant_at(x, y)!=tooltip_target)
196                 {
197                         if(lbl_tooltip)
198                                 lbl_tooltip->set_visible(false);
199                         tooltip_target = 0;
200                 }
201
202                 if(pointer_focus)
203                         return true;
204         }
205
206         return false;
207 }
208
209 bool Root::key_press_event(unsigned key)
210 {
211         // XXX Modifiers
212         if(visible)
213         {
214                 Widget *old_focus = input_focus;
215
216                 key_press(key, 0);
217
218                 if(input_focus || old_focus)
219                         return true;
220         }
221
222         return false;
223 }
224
225 bool Root::key_release_event(unsigned key)
226 {
227         if(visible)
228         {
229                 Widget *old_focus = input_focus;
230
231                 key_release(key, 0);
232
233                 if(input_focus || old_focus)
234                         return true;
235         }
236
237         return false;
238 }
239
240 bool Root::character_event(StringCodec::unichar ch)
241 {
242         if(visible)
243         {
244                 Widget *old_focus = input_focus;
245
246                 character(ch);
247
248                 if(input_focus || old_focus)
249                         return true;
250         }
251
252         return false;
253 }
254
255 bool Root::touch_press_event(unsigned finger)
256 {
257         if(visible)
258         {
259                 Widget *old_focus = touch_focus;
260
261                 int x, y;
262                 get_touch(finger, x, y);
263                 touch_press(x, y, finger);
264
265                 if(touch_focus || old_focus)
266                         return true;
267         }
268
269         return false;
270 }
271
272 bool Root::touch_release_event(unsigned finger)
273 {
274         if(visible)
275         {
276                 Widget *old_focus = touch_focus;
277
278                 int x, y;
279                 get_touch(finger, x, y);
280                 touch_release(x, y, finger);
281
282                 if(touch_focus || old_focus)
283                         return true;
284         }
285
286         return false;
287 }
288
289 bool Root::touch_motion_event(unsigned axis, float, float)
290 {
291         if(visible)
292         {
293                 unsigned finger = axis/2;
294                 int x, y;
295                 get_touch(finger, x, y);
296                 touch_motion(x, y, finger);
297
298                 if(touch_focus)
299                         return true;
300         }
301
302         return false;
303 }
304
305 void Root::get_pointer(int &x, int &y)
306 {
307         x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
308         y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
309 }
310
311 void Root::get_touch(unsigned finger, int &x, int &y)
312 {
313         x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
314         y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
315 }
316
317 void Root::update_camera()
318 {
319         camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
320         camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
321         camera.set_orthographic(geom.w, geom.h);
322 }
323
324 void Root::on_geometry_change()
325 {
326         Panel::on_geometry_change();
327         update_camera();
328 }
329
330 void Root::on_child_added(Widget &wdg)
331 {
332         if(&wdg!=lbl_tooltip)
333                 Panel::on_child_added(wdg);
334 }
335
336 } // namespace GLtk
337 } // namespace Msp