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