]> git.tdb.fi Git - libs/gltk.git/blob - source/root.cpp
ad1ca5b7936cbef133056a0aa7296d7474055b6f
[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         update_style();
62
63         if(mouse)
64         {
65                 mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
66                 mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
67                 mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
68         }
69
70         if(keyboard && !input_method)
71                 input_method = new SystemKeyboardInput(*this, *keyboard);
72
73         if(touchscreen)
74         {
75                 touchscreen->signal_button_press.connect(sigc::mem_fun(this, &Root::touch_press_event));
76                 touchscreen->signal_button_release.connect(sigc::mem_fun(this, &Root::touch_release_event));
77                 touchscreen->signal_axis_motion.connect(sigc::mem_fun(this, &Root::touch_motion_event));
78         }
79 }
80
81 Root::~Root()
82 {
83         delete shprog;
84         delete input_method;
85         if(own_input)
86         {
87                 delete keyboard;
88                 delete mouse;
89         }
90 }
91
92 void Root::tick()
93 {
94         Time::TimeStamp t = Time::now();
95         Time::TimeDelta dt = (last_tick ? t-last_tick : Time::zero);
96         last_tick = t;
97
98         animate(dt);
99
100         if(tooltip_timeout && Time::now()>tooltip_timeout)
101         {
102                 std::string tip;
103                 if(Widget *wdg = get_descendant_at(pointer_x, pointer_y))
104                 {
105                         tip = wdg->get_tooltip();
106                         tooltip_target = wdg;
107                 }
108                 else
109                 {
110                         tip = signal_tooltip.emit(pointer_x, pointer_y);
111                         tooltip_target = this;
112                 }
113
114                 if(!tip.empty())
115                 {
116                         if(!lbl_tooltip)
117                         {
118                                 lbl_tooltip = new Label;
119                                 add(*lbl_tooltip);
120                                 lbl_tooltip->set_style("tooltip");
121                         }
122
123                         lbl_tooltip->set_text(tip);
124                         lbl_tooltip->autosize();
125                         const Geometry &tip_geom = lbl_tooltip->get_geometry();
126                         unsigned x = pointer_x+10;
127                         unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
128                         if(x+tip_geom.w>geom.w)
129                         {
130                                 if(pointer_x>static_cast<int>(tip_geom.w+2))
131                                         x = pointer_x-2-tip_geom.w;
132                                 else
133                                         x = geom.w-tip_geom.w;
134                         }
135                         lbl_tooltip->set_position(x, y);
136                         raise(*lbl_tooltip);
137                         lbl_tooltip->set_visible(true);
138                 }
139
140                 tooltip_timeout = Time::TimeStamp();
141         }
142 }
143
144 void Root::render() const
145 {
146         GL::Bind bind_blend(GL::Blend::alpha());
147
148         GL::Renderer renderer(&camera);
149         renderer.set_shader_program(shprog);
150         Widget::render(renderer);
151 }
152
153 void Root::render(GL::Renderer &renderer, const GL::Tag &tag) const
154 {
155         if(tag.id)
156                 return;
157
158         GL::Renderer::Push push(renderer);
159         renderer.set_camera(camera);
160         renderer.set_shader_program(shprog);
161         GL::BindRestore bind_blend(GL::Blend::alpha());
162         GL::BindRestore unbind_dtest(static_cast<GL::DepthTest *>(0));
163         Widget::render(renderer);
164 }
165
166 bool Root::button_press_event(unsigned btn)
167 {
168         if(visible)
169         {
170                 Widget *old_focus = pointer_focus;
171
172                 int x, y;
173                 get_pointer(x, y);
174                 button_press(x, y, btn);
175
176                 if(pointer_focus || old_focus)
177                         return true;
178         }
179
180         return false;
181 }
182
183 bool Root::button_release_event(unsigned btn)
184 {
185         if(visible)
186         {
187                 Widget *old_focus = pointer_focus;
188
189                 int x, y;
190                 get_pointer(x, y);
191                 button_release(x, y, btn);
192
193                 if(pointer_focus || old_focus)
194                         return true;
195         }
196
197         return false;
198 }
199
200 bool Root::axis_motion_event(unsigned, float, float)
201 {
202         if(visible)
203         {
204                 int x, y;
205                 get_pointer(x, y);
206                 pointer_motion(x, y);
207
208                 if(!tooltip_target)
209                 {
210                         pointer_x = x;
211                         pointer_y = y;
212                         tooltip_timeout = Time::now()+700*Time::msec;
213                 }
214                 else if(get_descendant_at(x, y)!=tooltip_target)
215                 {
216                         if(lbl_tooltip)
217                                 lbl_tooltip->set_visible(false);
218                         tooltip_target = 0;
219                 }
220
221                 if(pointer_focus)
222                         return true;
223         }
224
225         return false;
226 }
227
228 bool Root::touch_press_event(unsigned finger)
229 {
230         if(visible)
231         {
232                 Widget *old_focus = touch_focus;
233
234                 int x, y;
235                 get_touch(finger, x, y);
236                 touch_press(x, y, finger);
237
238                 if(touch_focus || old_focus)
239                         return true;
240         }
241
242         return false;
243 }
244
245 bool Root::touch_release_event(unsigned finger)
246 {
247         if(visible)
248         {
249                 Widget *old_focus = touch_focus;
250
251                 int x, y;
252                 get_touch(finger, x, y);
253                 touch_release(x, y, finger);
254
255                 if(touch_focus || old_focus)
256                         return true;
257         }
258
259         return false;
260 }
261
262 bool Root::touch_motion_event(unsigned axis, float, float)
263 {
264         if(visible)
265         {
266                 unsigned finger = axis/2;
267                 int x, y;
268                 get_touch(finger, x, y);
269                 touch_motion(x, y, finger);
270
271                 if(touch_focus)
272                         return true;
273         }
274
275         return false;
276 }
277
278 void Root::get_pointer(int &x, int &y)
279 {
280         x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
281         y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
282 }
283
284 void Root::get_touch(unsigned finger, int &x, int &y)
285 {
286         x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
287         y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
288 }
289
290 void Root::update_camera()
291 {
292         camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
293         camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
294         camera.set_orthographic(geom.w, geom.h);
295 }
296
297 void Root::on_geometry_change()
298 {
299         Panel::on_geometry_change();
300         update_camera();
301 }
302
303 void Root::on_child_added(Widget &wdg)
304 {
305         if(&wdg!=lbl_tooltip)
306                 Panel::on_child_added(wdg);
307 }
308
309 } // namespace GLtk
310 } // namespace Msp