istereo2

view libs/goatkit/screen.cc @ 9:64e15874f3bd

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 26 Sep 2015 02:56:07 +0300
parents
children 7bd4264bf74a
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"
25 #define MAX_BUTTONS 16
27 namespace goatkit {
29 struct ScreenImpl {
30 bool visible;
31 std::vector<Widget*> widgets;
32 BBox box;
34 Widget *inp_focused, *over, *pressed[MAX_BUTTONS];
35 Widget *mgrab;
36 };
38 static Vec2 world_to_scr(const ScreenImpl *scr, const Vec2 &v);
39 //static Vec2 scr_to_world(const ScreenImpl *scr, const Vec2 &v)
41 Screen::Screen()
42 {
43 scr = new ScreenImpl;
45 scr->box.bmin = Vec2(0, 0);
46 scr->box.bmax = Vec2(1, 1);
48 scr->inp_focused = scr->over = 0;
49 for(int i=0; i<MAX_BUTTONS; i++) {
50 scr->pressed[i] = 0;
51 }
53 scr->visible = true;
54 scr->mgrab = 0;
55 }
57 Screen::~Screen()
58 {
59 delete scr;
60 }
62 void Screen::set_position(float x, float y)
63 {
64 set_position(Vec2(x, y));
65 }
67 void Screen::set_position(const Vec2 &pos)
68 {
69 Vec2 sz = get_size();
71 scr->box.bmin = pos;
72 scr->box.bmax.x = pos.x + sz.x;
73 scr->box.bmax.y = pos.y + sz.y;
74 }
76 const Vec2 &Screen::get_position() const
77 {
78 return scr->box.bmin;
79 }
81 void Screen::set_size(float x, float y)
82 {
83 set_size(Vec2(x, y));
84 }
86 void Screen::set_size(const Vec2 &sz)
87 {
88 scr->box.bmax.x = scr->box.bmin.x + sz.x;
89 scr->box.bmax.y = scr->box.bmin.y + sz.y;
90 }
92 const Vec2 Screen::get_size() const
93 {
94 return Vec2(scr->box.bmax.x - scr->box.bmin.x,
95 scr->box.bmax.y - scr->box.bmin.y);
96 }
98 const BBox &Screen::get_box() const
99 {
100 return scr->box;
101 }
103 void Screen::add_widget(Widget *w)
104 {
105 scr->widgets.push_back(w);
106 if(scr->visible) {
107 w->show();
108 } else {
109 w->hide();
110 }
112 w->set_screen(this);
113 }
115 int Screen::get_widget_count() const
116 {
117 return (int)scr->widgets.size();
118 }
120 Widget *Screen::get_widget(int idx) const
121 {
122 if(idx < 0 || idx >= (int)scr->widgets.size()) {
123 return 0;
124 }
125 return scr->widgets[idx];
126 }
128 Widget *Screen::get_widget(const char *name) const
129 {
130 for(size_t i=0; i<scr->widgets.size(); i++) {
131 if(strcmp(scr->widgets[i]->get_name(), name) == 0) {
132 return scr->widgets[i];
133 }
134 }
135 return 0;
136 }
138 void Screen::show()
139 {
140 scr->visible = true;
142 for(size_t i=0; i<scr->widgets.size(); i++) {
143 scr->widgets[i]->show();
144 }
145 }
147 void Screen::hide()
148 {
149 scr->visible = false;
151 for(size_t i=0; i<scr->widgets.size(); i++) {
152 scr->widgets[i]->hide();
153 }
154 }
156 bool Screen::is_visible() const
157 {
158 return scr->visible;
159 }
161 bool Screen::grab_mouse(Widget *w)
162 {
163 if(!scr->mgrab || !w) {
164 scr->mgrab = w;
165 return true;
166 }
167 return false;
168 }
170 void Screen::draw() const
171 {
172 for(size_t i=0; i<scr->widgets.size(); i++) {
173 scr->widgets[i]->draw();
174 }
175 }
177 static Widget *find_widget_at(const ScreenImpl *scr, const Vec2 &pt)
178 {
179 for(size_t i=0; i<scr->widgets.size(); i++) {
180 Widget *w = scr->widgets[i];
182 if(w->hit_test(pt)) {
183 return w;
184 }
185 }
186 return 0;
187 }
189 void Screen::sysev_keyboard(int key, bool press)
190 {
191 Event ev;
193 if(scr->inp_focused) {
194 ev.type = EV_KEY;
195 ev.key.key = key;
196 ev.key.press = press;
197 scr->inp_focused->handle_event(ev);
198 }
199 }
201 void Screen::sysev_mouse_button(int bn, bool press, float x, float y)
202 {
203 Event ev;
204 Vec2 pt = world_to_scr(scr, Vec2(x, y));
205 Widget *new_over = scr->mgrab ? scr->mgrab : find_widget_at(scr, pt);
207 ev.type = EV_MOUSE_BUTTON;
208 ev.button.button = bn;
209 ev.button.pos = pt;
210 ev.button.press = press;
212 if(press) {
213 if(bn == 0) {
214 // left click gives input focus
215 // TODO: add input focus event in widget
216 if(new_over && new_over != scr->inp_focused && new_over->can_focus()) {
217 printf("input focus %p -> %p\n", (void*)scr->inp_focused, (void*)new_over);
218 new_over->focusin();
220 if(scr->inp_focused) {
221 scr->inp_focused->focusout();
222 }
223 scr->inp_focused = new_over;
224 }
225 }
227 scr->pressed[bn] = new_over;
228 scr->over = new_over;
230 if(new_over) {
231 new_over->handle_event(ev);
232 }
234 } else {
235 // send the mouse release event to the widget that got the matching press
236 if(scr->pressed[bn]) {
237 scr->pressed[bn]->handle_event(ev);
238 scr->pressed[bn] = 0;
239 }
240 }
242 // if we're not over the same widget any more send the leave/enter events
243 // TODO also add drag/drop events
244 if(scr->over != new_over) {
245 ev.type = EV_MOUSE_FOCUS;
246 if(scr->over) {
247 ev.focus.enter = false;
248 scr->over->handle_event(ev);
249 }
250 if(new_over) {
251 ev.focus.enter = true;
252 new_over->handle_event(ev);
253 }
254 scr->over = new_over;
255 }
256 }
258 void Screen::sysev_mouse_motion(float x, float y)
259 {
260 Event ev;
261 Vec2 pt = world_to_scr(scr, Vec2(x, y));
262 Widget *new_over = scr->mgrab ? scr->mgrab : find_widget_at(scr, pt);
264 // if we're not over the same widget any more send the leave/enter events
265 if(scr->over != new_over) {
266 ev.type = EV_MOUSE_FOCUS;
267 if(scr->over) {
268 ev.focus.enter = false;
269 scr->over->handle_event(ev);
270 }
271 if(new_over) {
272 ev.focus.enter = true;
273 new_over->handle_event(ev);
274 }
275 scr->over = new_over;
276 }
278 if(new_over) {
279 // send motion event
280 ev.type = EV_MOUSE_MOTION;
281 ev.motion.pos = pt;
282 new_over->handle_event(ev);
283 }
284 }
286 static Vec2 world_to_scr(const ScreenImpl *scr, const Vec2 &v)
287 {
288 return Vec2(v.x - scr->box.bmin.x, v.y - scr->box.bmin.y);
289 }
291 /*
292 static Vec2 scr_to_world(const ScreenImpl *scr, const Vec2 &v)
293 {
294 return Vec2(v.x + scr->box.bmin.x, v.y + scr->box.bmin.y);
295 }
296 */
299 } // namespace goatkit