gameui

annotate src/theme.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
children e0916bb20b7f
rev   line source
nuclear@3 1 #include <stdio.h>
nuclear@3 2 #include "theme.h"
nuclear@3 3 #include "widget.h"
nuclear@3 4
nuclear@3 5 #ifdef WIN32
nuclear@3 6 #include <windows.h>
nuclear@3 7 #endif
nuclear@3 8 #ifdef __APPLE__
nuclear@3 9 #include <OpenGL/gl.h>
nuclear@3 10 #else
nuclear@3 11 #include <GL/gl.h>
nuclear@3 12 #endif
nuclear@3 13
nuclear@3 14
nuclear@3 15 namespace gameui {
nuclear@3 16
nuclear@3 17 Theme *theme;
nuclear@3 18
nuclear@3 19 Theme::Theme()
nuclear@3 20 {
nuclear@3 21 so = 0;
nuclear@3 22 }
nuclear@3 23
nuclear@3 24 bool Theme::load(const char *name)
nuclear@3 25 {
nuclear@3 26 fprintf(stderr, "theme loading not implemented yet!\n");
nuclear@3 27 return false;
nuclear@3 28 }
nuclear@3 29
nuclear@3 30 widget_draw_func Theme::get_draw_func(const char *type) const
nuclear@3 31 {
nuclear@3 32 std::map<std::string, widget_draw_func>::const_iterator it = draw_func.find(type);
nuclear@3 33 if(it == draw_func.end()) {
nuclear@3 34 return default_draw_func;
nuclear@3 35 }
nuclear@3 36 return it->second;
nuclear@3 37 }
nuclear@3 38
nuclear@3 39 #define LERP(a, b, t) ((a) + ((b) - (a)) * t)
nuclear@3 40 #define DEF_TEX_SZ 32
nuclear@3 41 void default_draw_func(const Widget *w)
nuclear@3 42 {
nuclear@3 43 static unsigned int tex;
nuclear@3 44
nuclear@3 45 if(!tex) {
nuclear@3 46 unsigned char *pixels = new unsigned char[DEF_TEX_SZ * DEF_TEX_SZ * 3];
nuclear@3 47 unsigned char *ptr = pixels;
nuclear@3 48 for(int i=0; i<DEF_TEX_SZ; i++) {
nuclear@3 49 for(int j=0; j<DEF_TEX_SZ; j++) {
nuclear@3 50 bool stripe = (((j + i) / 8) & 1) == 1;
nuclear@3 51 ptr[0] = ptr[1] = ptr[2] = stripe ? 255 : 0;
nuclear@3 52 ptr += 3;
nuclear@3 53 }
nuclear@3 54 }
nuclear@3 55
nuclear@3 56 glGenTextures(1, &tex);
nuclear@3 57 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@3 58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@3 59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@3 60 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, DEF_TEX_SZ, DEF_TEX_SZ, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
nuclear@3 61 delete [] pixels;
nuclear@3 62 }
nuclear@3 63
nuclear@3 64 Vec2 pos = w->get_position();
nuclear@3 65 Vec2 sz = w->get_size();
nuclear@3 66 float aspect = sz.x / sz.y;
nuclear@3 67
nuclear@3 68 glPushAttrib(GL_ENABLE_BIT);
nuclear@3 69 glEnable(GL_TEXTURE_2D);
nuclear@3 70 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@3 71
nuclear@3 72 float offs = w->get_pressed() * 0.1 * sz.y;
nuclear@3 73 glMatrixMode(GL_MODELVIEW);
nuclear@3 74 glPushMatrix();
nuclear@3 75 glTranslatef(offs, -offs, 0);
nuclear@3 76
nuclear@3 77 float active = w->get_active();
nuclear@3 78 float hover = w->get_under_mouse();
nuclear@3 79
nuclear@3 80 float rg = LERP(0.4, 1.0, hover);
nuclear@3 81 float b = LERP(rg, 0, active);
nuclear@3 82 glColor3f(rg, rg, b);
nuclear@3 83
nuclear@3 84 glBegin(GL_QUADS);
nuclear@3 85 glTexCoord2f(0, 1);
nuclear@3 86 glVertex2f(pos.x, pos.y);
nuclear@3 87 glTexCoord2f(aspect, 1);
nuclear@3 88 glVertex2f(pos.x + sz.x, pos.y);
nuclear@3 89 glTexCoord2f(aspect, 0);
nuclear@3 90 glVertex2f(pos.x + sz.x, pos.y + sz.y);
nuclear@3 91 glTexCoord2f(0, 0);
nuclear@3 92 glVertex2f(pos.x, pos.y + sz.y);
nuclear@3 93 glEnd();
nuclear@3 94
nuclear@3 95 glPopMatrix();
nuclear@3 96
nuclear@3 97 glPopAttrib();
nuclear@3 98 }
nuclear@3 99
nuclear@3 100 } // namespace gameui