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