gameui
diff test.cc @ 3:f1014234dece
transitions in gui elements are awesome :)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 21 Mar 2014 03:37:16 +0200 |
parents | 3aa12cdb9925 |
children | e0916bb20b7f |
line diff
1.1 --- a/test.cc Thu Mar 20 07:03:58 2014 +0200 1.2 +++ b/test.cc Fri Mar 21 03:37:16 2014 +0200 1.3 @@ -1,11 +1,14 @@ 1.4 #include <stdio.h> 1.5 #include <stdlib.h> 1.6 #include <assert.h> 1.7 +#include <vector> 1.8 #include <GL/glut.h> 1.9 +#include "gameui.h" 1.10 1.11 static bool init(); 1.12 static void cleanup(); 1.13 static void disp(); 1.14 +static void idle(); 1.15 static void reshape(int x, int y); 1.16 static void keypress(unsigned char key, int x, int y); 1.17 static void keyrelease(unsigned char key, int x, int y); 1.18 @@ -14,6 +17,8 @@ 1.19 static void mouse(int bn, int st, int x, int y); 1.20 static void motion(int x, int y); 1.21 1.22 +static std::vector<gameui::Widget*> widgets; 1.23 + 1.24 int main(int argc, char **argv) 1.25 { 1.26 glutInitWindowSize(800, 600); 1.27 @@ -22,6 +27,7 @@ 1.28 glutCreateWindow("gameui test"); 1.29 1.30 glutDisplayFunc(disp); 1.31 + glutIdleFunc(idle); 1.32 glutReshapeFunc(reshape); 1.33 glutKeyboardFunc(keypress); 1.34 glutKeyboardUpFunc(keyrelease); 1.35 @@ -43,27 +49,45 @@ 1.36 1.37 static bool init() 1.38 { 1.39 + gameui::Button *button = new gameui::Button; 1.40 + button->set_position(350, 280); 1.41 + button->set_size(100, 40); 1.42 + widgets.push_back(button); 1.43 + 1.44 return true; 1.45 } 1.46 1.47 static void cleanup() 1.48 { 1.49 + for(size_t i=0; i<widgets.size(); i++) { 1.50 + delete widgets[i]; 1.51 + } 1.52 } 1.53 1.54 static void disp() 1.55 { 1.56 + glClearColor(0.2, 0.2, 0.2, 1.0); 1.57 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.58 1.59 + for(size_t i=0; i<widgets.size(); i++) { 1.60 + widgets[i]->draw(); 1.61 + } 1.62 + 1.63 glutSwapBuffers(); 1.64 assert(glGetError() == GL_NO_ERROR); 1.65 } 1.66 1.67 +static void idle() 1.68 +{ 1.69 + glutPostRedisplay(); 1.70 +} 1.71 + 1.72 static void reshape(int x, int y) 1.73 { 1.74 glViewport(0, 0, x, y); 1.75 glMatrixMode(GL_PROJECTION); 1.76 glLoadIdentity(); 1.77 - glOrtho(0, x, y, 0, -1, 1); 1.78 + glOrtho(0, x, 0, y, -1, 1); 1.79 } 1.80 1.81 static void keypress(unsigned char key, int x, int y) 1.82 @@ -87,8 +111,46 @@ 1.83 1.84 static void mouse(int bn, int st, int x, int y) 1.85 { 1.86 + int bidx = bn - GLUT_LEFT_BUTTON; 1.87 + bool down = st == GLUT_DOWN; 1.88 + 1.89 + for(size_t i=0; i<widgets.size(); i++) { 1.90 + gameui::Widget *w = widgets[i]; 1.91 + 1.92 + if(w->hit_test(gameui::Vec2(x, y))) { 1.93 + gameui::Event ev; 1.94 + ev.type = gameui::EV_MOUSE_BUTTON; 1.95 + ev.button.button = bidx; 1.96 + ev.button.press = down; 1.97 + ev.button.pos = gameui::Vec2(x, y); 1.98 + w->handle_event(ev); 1.99 + } 1.100 + } 1.101 } 1.102 1.103 static void motion(int x, int y) 1.104 { 1.105 + static gameui::Widget *active; 1.106 + 1.107 + if(active && !active->hit_test(gameui::Vec2(x, y))) { 1.108 + gameui::Event ev; 1.109 + ev.type = gameui::EV_MOUSE_FOCUS; 1.110 + ev.focus.enter = false; 1.111 + active->handle_event(ev); 1.112 + active = 0; 1.113 + } 1.114 + 1.115 + for(size_t i=0; i<widgets.size(); i++) { 1.116 + gameui::Widget *w = widgets[i]; 1.117 + 1.118 + if(w->hit_test(gameui::Vec2(x, y))) { 1.119 + if(active != w) { 1.120 + gameui::Event ev; 1.121 + ev.type = gameui::EV_MOUSE_FOCUS; 1.122 + ev.focus.enter = true; 1.123 + w->handle_event(ev); 1.124 + active = w; 1.125 + } 1.126 + } 1.127 + } 1.128 }