]> git.tdb.fi Git - libs/gltk.git/blob - source/root.cpp
8d32c467b26eb5924e1825d6d51ff37d01de65a7
[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 = new Label;
94                                 add(*lbl_tooltip);
95                                 lbl_tooltip->set_style("tooltip");
96                         }
97
98                         lbl_tooltip->set_text(tip);
99                         lbl_tooltip->autosize();
100                         const Geometry &tip_geom = lbl_tooltip->get_geometry();
101                         unsigned x = pointer_x+10;
102                         unsigned y = pointer_y-10-lbl_tooltip->get_geometry().h;
103                         if(x+tip_geom.w>geom.w)
104                         {
105                                 if(pointer_x>static_cast<int>(tip_geom.w+2))
106                                         x = pointer_x-2-tip_geom.w;
107                                 else
108                                         x = geom.w-tip_geom.w;
109                         }
110                         lbl_tooltip->set_position(x, y);
111                         raise(*lbl_tooltip);
112                         lbl_tooltip->set_visible(true);
113                 }
114
115                 tooltip_timeout = Time::TimeStamp();
116         }
117 }
118
119 void Root::setup_frame(GL::Renderer &)
120 {
121         rebuild_hierarchy();
122 }
123
124 void Root::render(GL::Renderer &renderer, GL::Tag tag) const
125 {
126         if(tag.id)
127                 return;
128
129         GL::Renderer::Push push(renderer);
130         renderer.set_camera(camera);
131         renderer.set_shader_program(shprog);
132         renderer.set_blend(&blend);
133         renderer.set_depth_test(nullptr);
134         Widget::render(renderer);
135 }
136
137 bool Root::button_press_event(unsigned btn)
138 {
139         if(visible)
140         {
141                 Widget *old_focus = pointer_focus;
142
143                 int x, y;
144                 get_pointer(x, y);
145                 button_press(x, y, btn);
146
147                 if(pointer_focus || old_focus)
148                         return true;
149         }
150
151         return false;
152 }
153
154 bool Root::button_release_event(unsigned btn)
155 {
156         if(visible)
157         {
158                 Widget *old_focus = pointer_focus;
159
160                 int x, y;
161                 get_pointer(x, y);
162                 button_release(x, y, btn);
163
164                 if(pointer_focus || old_focus)
165                         return true;
166         }
167
168         return false;
169 }
170
171 bool Root::axis_motion_event(unsigned, float, float)
172 {
173         if(visible)
174         {
175                 int x, y;
176                 get_pointer(x, y);
177                 pointer_motion(x, y);
178
179                 if(!tooltip_target)
180                 {
181                         pointer_x = x;
182                         pointer_y = y;
183                         tooltip_timeout = Time::now()+700*Time::msec;
184                 }
185                 else if(find_descendant_at(x, y)!=tooltip_target)
186                 {
187                         if(lbl_tooltip)
188                                 lbl_tooltip->set_visible(false);
189                         tooltip_target = nullptr;
190                 }
191
192                 if(pointer_focus)
193                         return true;
194         }
195
196         return false;
197 }
198
199 bool Root::touch_press_event(unsigned finger)
200 {
201         if(visible)
202         {
203                 Widget *old_focus = touch_focus;
204
205                 int x, y;
206                 get_touch(finger, x, y);
207                 touch_press(x, y, finger);
208
209                 if(touch_focus || old_focus)
210                         return true;
211         }
212
213         return false;
214 }
215
216 bool Root::touch_release_event(unsigned finger)
217 {
218         if(visible)
219         {
220                 Widget *old_focus = touch_focus;
221
222                 int x, y;
223                 get_touch(finger, x, y);
224                 touch_release(x, y, finger);
225
226                 if(touch_focus || old_focus)
227                         return true;
228         }
229
230         return false;
231 }
232
233 bool Root::touch_motion_event(unsigned axis, float, float)
234 {
235         if(visible)
236         {
237                 unsigned finger = axis/2;
238                 int x, y;
239                 get_touch(finger, x, y);
240                 touch_motion(x, y, finger);
241
242                 if(touch_focus)
243                         return true;
244         }
245
246         return false;
247 }
248
249 void Root::get_pointer(int &x, int &y)
250 {
251         x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
252         y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
253 }
254
255 void Root::get_touch(unsigned finger, int &x, int &y)
256 {
257         x = (touchscreen->get_axis_value(finger*2)*0.5+0.5)*geom.w;
258         y = (touchscreen->get_axis_value(finger*2+1)*0.5+0.5)*geom.h;
259 }
260
261 void Root::update_camera()
262 {
263         camera.set_position(GL::Vector3(geom.w/2.0f, geom.h/2.0f, geom.h/2.0f));
264         camera.set_depth_clip(geom.h*0.1f, geom.h*0.9f);
265         camera.set_orthographic(geom.w, geom.h);
266 }
267
268 void Root::on_size_change()
269 {
270         Panel::on_size_change();
271         update_camera();
272 }
273
274 void Root::on_child_added(Widget &wdg)
275 {
276         if(&wdg!=lbl_tooltip)
277                 Panel::on_child_added(wdg);
278 }
279
280 } // namespace GLtk
281 } // namespace Msp