istereo2
view libs/goatkit/screen.cc @ 20:2b85d05df3f2
animation controls for easier screenshot grabbing
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 02 Oct 2015 04:54:55 +0300 |
parents | 3bccfc7d10fe |
children |
line source
1 /*
2 GoatKit - a themable/animated widget toolkit for games
3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <vector>
22 #include "screen.h"
23 #include "widget.h"
24 #include "boolanm.h"
26 #define MAX_BUTTONS 16
28 namespace goatkit {
30 struct ScreenImpl {
31 BoolAnim visible;
32 std::vector<Widget*> widgets;
33 BBox box;
35 Widget *inp_focused, *over, *pressed[MAX_BUTTONS];
36 Widget *mgrab;
37 };
39 static Vec2 world_to_scr(const ScreenImpl *scr, const Vec2 &v);
40 //static Vec2 scr_to_world(const ScreenImpl *scr, const Vec2 &v)
42 Screen::Screen()
43 {
44 scr = new ScreenImpl;
46 scr->box.bmin = Vec2(0, 0);
47 scr->box.bmax = Vec2(1, 1);
49 scr->inp_focused = scr->over = 0;
50 for(int i=0; i<MAX_BUTTONS; i++) {
51 scr->pressed[i] = 0;
52 }
54 scr->visible = true;
55 scr->mgrab = 0;
56 }
58 Screen::~Screen()
59 {
60 delete scr;
61 }
63 void Screen::set_position(float x, float y)
64 {
65 set_position(Vec2(x, y));
66 }
68 void Screen::set_position(const Vec2 &pos)
69 {
70 Vec2 sz = get_size();
72 scr->box.bmin = pos;
73 scr->box.bmax.x = pos.x + sz.x;
74 scr->box.bmax.y = pos.y + sz.y;
75 }
77 const Vec2 &Screen::get_position() const
78 {
79 return scr->box.bmin;
80 }
82 void Screen::set_size(float x, float y)
83 {
84 set_size(Vec2(x, y));
85 }
87 void Screen::set_size(const Vec2 &sz)
88 {
89 scr->box.bmax.x = scr->box.bmin.x + sz.x;
90 scr->box.bmax.y = scr->box.bmin.y + sz.y;
91 }
93 const Vec2 Screen::get_size() const
94 {
95 return Vec2(scr->box.bmax.x - scr->box.bmin.x,
96 scr->box.bmax.y - scr->box.bmin.y);
97 }
99 const BBox &Screen::get_box() const
100 {
101 return scr->box;
102 }
104 void Screen::add_widget(Widget *w)
105 {
106 scr->widgets.push_back(w);
107 if(scr->visible) {
108 w->show();
109 } else {
110 w->hide();
111 }
113 w->set_screen(this);
114 }
116 int Screen::get_widget_count() const
117 {
118 return (int)scr->widgets.size();
119 }
121 Widget *Screen::get_widget(int idx) const
122 {
123 if(idx < 0 || idx >= (int)scr->widgets.size()) {
124 return 0;
125 }
126 return scr->widgets[idx];
127 }
129 Widget *Screen::get_widget(const char *name) const
130 {
131 for(size_t i=0; i<scr->widgets.size(); i++) {
132 if(strcmp(scr->widgets[i]->get_name(), name) == 0) {
133 return scr->widgets[i];
134 }
135 }
136 return 0;
137 }
139 void Screen::show()
140 {
141 scr->visible.change(true);
143 for(size_t i=0; i<scr->widgets.size(); i++) {
144 scr->widgets[i]->show();
145 }
146 }
148 void Screen::hide()
149 {
150 scr->visible.change(false);
152 for(size_t i=0; i<scr->widgets.size(); i++) {
153 scr->widgets[i]->hide();
154 }
155 }
157 bool Screen::is_visible() const
158 {
159 return scr->visible;
160 }
162 float Screen::get_visibility() const
163 {
164 return scr->visible.get_value();
165 }
167 void Screen::set_visibility_transition(long msec)
168 {
169 scr->visible.set_transition_duration(msec);
170 for(size_t i=0; i<scr->widgets.size(); i++) {
171 scr->widgets[i]->set_visibility_transition(msec);
172 }
173 }
175 long Screen::get_visibility_transition() const
176 {
177 return scr->visible.get_transition_duration();
178 }
180 bool Screen::grab_mouse(Widget *w)
181 {
182 if(!scr->mgrab || !w) {
183 scr->mgrab = w;
184 return true;
185 }
186 return false;
187 }
189 void Screen::draw() const
190 {
191 for(size_t i=0; i<scr->widgets.size(); i++) {
192 scr->widgets[i]->draw();
193 }
194 }
196 static Widget *find_widget_at(const ScreenImpl *scr, const Vec2 &pt)
197 {
198 for(size_t i=0; i<scr->widgets.size(); i++) {
199 Widget *w = scr->widgets[i];
201 if(w->hit_test(pt)) {
202 return w;
203 }
204 }
205 return 0;
206 }
208 void Screen::sysev_keyboard(int key, bool press)
209 {
210 Event ev;
212 if(scr->inp_focused) {
213 ev.type = EV_KEY;
214 ev.key.key = key;
215 ev.key.press = press;
216 scr->inp_focused->handle_event(ev);
217 }
218 }
220 void Screen::sysev_mouse_button(int bn, bool press, float x, float y)
221 {
222 Event ev;
223 Vec2 pt = world_to_scr(scr, Vec2(x, y));
224 Widget *new_over = scr->mgrab ? scr->mgrab : find_widget_at(scr, pt);
226 ev.type = EV_MOUSE_BUTTON;
227 ev.button.button = bn;
228 ev.button.pos = pt;
229 ev.button.press = press;
231 if(press) {
232 if(bn == 0) {
233 // left click gives input focus
234 // TODO: add input focus event in widget
235 if(new_over && new_over != scr->inp_focused && new_over->can_focus()) {
236 printf("input focus %p -> %p\n", (void*)scr->inp_focused, (void*)new_over);
237 new_over->focusin();
239 if(scr->inp_focused) {
240 scr->inp_focused->focusout();
241 }
242 scr->inp_focused = new_over;
243 }
244 }
246 scr->pressed[bn] = new_over;
247 scr->over = new_over;
249 if(new_over) {
250 new_over->handle_event(ev);
251 }
253 } else {
254 // send the mouse release event to the widget that got the matching press
255 if(scr->pressed[bn]) {
256 scr->pressed[bn]->handle_event(ev);
257 scr->pressed[bn] = 0;
258 }
259 }
261 // if we're not over the same widget any more send the leave/enter events
262 // TODO also add drag/drop events
263 if(scr->over != new_over) {
264 ev.type = EV_MOUSE_FOCUS;
265 if(scr->over) {
266 ev.focus.enter = false;
267 scr->over->handle_event(ev);
268 }
269 if(new_over) {
270 ev.focus.enter = true;
271 new_over->handle_event(ev);
272 }
273 scr->over = new_over;
274 }
275 }
277 void Screen::sysev_mouse_motion(float x, float y)
278 {
279 Event ev;
280 Vec2 pt = world_to_scr(scr, Vec2(x, y));
281 Widget *new_over = scr->mgrab ? scr->mgrab : find_widget_at(scr, pt);
283 // if we're not over the same widget any more send the leave/enter events
284 if(scr->over != new_over) {
285 ev.type = EV_MOUSE_FOCUS;
286 if(scr->over) {
287 ev.focus.enter = false;
288 scr->over->handle_event(ev);
289 }
290 if(new_over) {
291 ev.focus.enter = true;
292 new_over->handle_event(ev);
293 }
294 scr->over = new_over;
295 }
297 if(new_over) {
298 // send motion event
299 ev.type = EV_MOUSE_MOTION;
300 ev.motion.pos = pt;
301 new_over->handle_event(ev);
302 }
303 }
305 static Vec2 world_to_scr(const ScreenImpl *scr, const Vec2 &v)
306 {
307 return Vec2(v.x - scr->box.bmin.x, v.y - scr->box.bmin.y);
308 }
310 /*
311 static Vec2 scr_to_world(const ScreenImpl *scr, const Vec2 &v)
312 {
313 return Vec2(v.x + scr->box.bmin.x, v.y + scr->box.bmin.y);
314 }
315 */
318 } // namespace goatkit