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