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