istereo2

annotate libs/goatkit/screen.cc @ 35:643f4ab609a4

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