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