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