glamtk

changeset 7:a115dff39a54

rounded crappy buttons
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 11 Mar 2011 02:25:49 +0200
parents 9b623dc0f296
children cd00a5775373
files src/draw.c src/draw.h src/imtk.c
diffstat 3 files changed, 108 insertions(+), 19 deletions(-) [+]
line diff
     1.1 --- a/src/draw.c	Wed Mar 09 10:12:31 2011 +0200
     1.2 +++ b/src/draw.c	Fri Mar 11 02:25:49 2011 +0200
     1.3 @@ -1,3 +1,5 @@
     1.4 +#include <math.h>
     1.5 +
     1.6  #ifndef __APPLE__
     1.7  #include <GL/gl.h>
     1.8  #else
     1.9 @@ -10,8 +12,15 @@
    1.10  	float r, g, b, a;
    1.11  };
    1.12  
    1.13 -static struct color fgcolor = {0, 0, 0, 1};
    1.14 -static struct color bgcolor = {0.4, 0.4, 0.4, 1};
    1.15 +
    1.16 +static void linestart(int x, int y);
    1.17 +static void lineto(int x, int y);
    1.18 +static void arcto(int x, int y, int cx, int cy, int rad);
    1.19 +static void arc(float x0, float y0, float x1, float y1, float cx, float cy, float rad, int subdiv);
    1.20 +
    1.21 +
    1.22 +static struct color fgcolor = {0, 0, 0, 0.5};
    1.23 +static struct color bgcolor = {0.4, 0.4, 0.4, 0.5};
    1.24  
    1.25  void imtk_draw_color(float r, float g, float b, float a)
    1.26  {
    1.27 @@ -21,6 +30,14 @@
    1.28  	fgcolor.a = a;
    1.29  }
    1.30  
    1.31 +void imtk_draw_colorv(float *v)
    1.32 +{
    1.33 +	fgcolor.r = v[0];
    1.34 +	fgcolor.g = v[1];
    1.35 +	fgcolor.b = v[2];
    1.36 +	fgcolor.a = v[3];
    1.37 +}
    1.38 +
    1.39  void imtk_draw_background(float r, float g, float b, float a)
    1.40  {
    1.41  	bgcolor.r = r;
    1.42 @@ -29,21 +46,49 @@
    1.43  	bgcolor.a = a;
    1.44  }
    1.45  
    1.46 +void imtk_draw_backgroundv(float *v)
    1.47 +{
    1.48 +	bgcolor.r = v[0];
    1.49 +	bgcolor.g = v[1];
    1.50 +	bgcolor.b = v[2];
    1.51 +	bgcolor.a = v[3];
    1.52 +}
    1.53 +
    1.54  void imtk_draw_rect(int x, int y, int w, int h, int rad)
    1.55  {
    1.56 -	glBegin(GL_LINE_LOOP);
    1.57 -	linestart(x + rad, y);
    1.58 -	lineto(x + w - rad, y);
    1.59 -	arcto(x + w, y + rad);
    1.60 -	lineto(x + w, y + h - rad);
    1.61 -	arcto(x + w - rad, y + h);
    1.62 -	lineto(x + rad, y + h);
    1.63 -	arcto(x, y + h - rad);
    1.64 -	lineto(x, y + rad);
    1.65 -	arcto(x + rad, y);
    1.66 -	glEnd();
    1.67 +	int i;
    1.68 +
    1.69 +	glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
    1.70 +	glEnable(GL_LINE_SMOOTH);
    1.71 +	glEnable(GL_POLYGON_SMOOTH);
    1.72 +	glEnable(GL_BLEND);
    1.73 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    1.74 +	glLineWidth(1.5);
    1.75 +
    1.76 +	for(i=0; i<2; i++) {
    1.77 +		if(i == 0) {
    1.78 +			glBegin(GL_POLYGON);
    1.79 +			glColor4f(bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a);
    1.80 +		} else {
    1.81 +			glBegin(GL_LINE_STRIP);
    1.82 +			glColor4f(fgcolor.r, fgcolor.g, fgcolor.b, fgcolor.a);
    1.83 +		}
    1.84 +		linestart(x + rad, y);
    1.85 +		lineto(x + w - rad, y);
    1.86 +		arcto(x + w, y + rad, x + w - rad, y + rad, rad);
    1.87 +		lineto(x + w, y + h - rad);
    1.88 +		arcto(x + w - rad, y + h, x + w - rad, y + h - rad, rad);
    1.89 +		lineto(x + rad, y + h);
    1.90 +		arcto(x, y + h - rad, x + rad, y + h - rad, rad);
    1.91 +		lineto(x, y + rad);
    1.92 +		arcto(x + rad, y, x + rad, y + rad, rad);
    1.93 +		glEnd();
    1.94 +	}
    1.95 +
    1.96 +	glPopAttrib();
    1.97  }
    1.98  
    1.99 +
   1.100  static int px, py;
   1.101  
   1.102  static void linestart(int x, int y)
   1.103 @@ -60,6 +105,29 @@
   1.104  	glVertex2i(x, y);
   1.105  }
   1.106  
   1.107 -static void arcto(int x, int y, int rad)
   1.108 +static void arcto(int x, int y, int cx, int cy, int rad)
   1.109  {
   1.110 +	arc(px, py, x, y, cx, cy, rad, 1);
   1.111  }
   1.112 +
   1.113 +static void arc(float x0, float y0, float x1, float y1, float cx, float cy, float rad, int subdiv)
   1.114 +{
   1.115 +	float x, y, vx, vy, len;
   1.116 +
   1.117 +	x = x0 + (x1 - x0) * 0.5;
   1.118 +	y = y0 + (y1 - y0) * 0.5;
   1.119 +	vx = x - cx;
   1.120 +	vy = y - cy;
   1.121 +	len = sqrt(vx * vx + vy * vy);
   1.122 +	x = cx + vx * rad / len;
   1.123 +	y = cy + vy * rad / len;
   1.124 +
   1.125 +	if(!subdiv) {
   1.126 +		glVertex2f(x, y);
   1.127 +		glVertex2f(x1, y1);
   1.128 +		return;
   1.129 +	}
   1.130 +
   1.131 +	arc(x0, y0, x, y, cx, cy, rad, subdiv - 1);
   1.132 +	arc(x, y, x1, y1, cx, cy, rad, subdiv - 1);
   1.133 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/draw.h	Fri Mar 11 02:25:49 2011 +0200
     2.3 @@ -0,0 +1,11 @@
     2.4 +#ifndef DRAW_H_
     2.5 +#define DRAW_H_
     2.6 +
     2.7 +void imtk_draw_color(float r, float g, float b, float a);
     2.8 +void imtk_draw_colorv(float *v);
     2.9 +void imtk_draw_background(float r, float g, float b, float a);
    2.10 +void imtk_draw_backgroundv(float *v);
    2.11 +
    2.12 +void imtk_draw_rect(int x, int y, int w, int h, int rad);
    2.13 +
    2.14 +#endif	/* DRAW_H_ */
     3.1 --- a/src/imtk.c	Wed Mar 09 10:12:31 2011 +0200
     3.2 +++ b/src/imtk.c	Fri Mar 11 02:25:49 2011 +0200
     3.3 @@ -10,6 +10,7 @@
     3.4  #include <GLUT/glut.h>
     3.5  #endif
     3.6  #include "imtk.h"
     3.7 +#include "draw.h"
     3.8  
     3.9  #define CHECKBOX_SIZE	14
    3.10  #define TEXTBOX_SIZE	100
    3.11 @@ -44,11 +45,11 @@
    3.12  
    3.13  /* default colors, can be changed with imtk_set_color */
    3.14  static float colors[][4] = {
    3.15 -	{0.0, 0.0, 0.0},	/* text color */
    3.16 -	{0.7, 0.7, 0.7},	/* base color */
    3.17 -	{0.85, 0.85, 0.85},	/* focus color */
    3.18 -	{1.0, 1.0, 1.0},	/* lit bevel */
    3.19 -	{0.3, 0.3, 0.3}		/* shadowed bevel */
    3.20 +	{0.0, 0.0, 0.0, 0.7},		/* text color */
    3.21 +	{0.6, 0.6, 0.6, 0.7},		/* base color */
    3.22 +	{0.7, 0.7, 0.7, 0.7},	/* focus color */
    3.23 +	{1.0, 1.0, 1.0, 1.0},		/* lit bevel */
    3.24 +	{0.3, 0.3, 0.3, 1.0}		/* shadowed bevel */
    3.25  };
    3.26  
    3.27  static int scr_width = 1, scr_height = 1;
    3.28 @@ -409,6 +410,7 @@
    3.29  
    3.30  	calc_button_size(label, &width, &height);
    3.31  
    3.32 +	/*
    3.33  	glBegin(GL_QUADS);
    3.34  	if(hit_test(x, y, width, height)) {
    3.35  		glColor3fv(colors[IMTK_FOCUS_COLOR]);
    3.36 @@ -422,6 +424,14 @@
    3.37  	glEnd();
    3.38  
    3.39  	draw_frame(x, y, width, height, active == id ? FRAME_INSET : FRAME_OUTSET);
    3.40 +	*/
    3.41 +
    3.42 +	if(hit_test(x, y, width, height)) {
    3.43 +		imtk_draw_backgroundv(colors[IMTK_FOCUS_COLOR]);
    3.44 +	} else {
    3.45 +		imtk_draw_backgroundv(colors[IMTK_BASE_COLOR]);
    3.46 +	}
    3.47 +	imtk_draw_rect(x, y, width, height, 8);
    3.48  
    3.49  	glColor3fv(colors[IMTK_TEXT_COLOR]);
    3.50  	draw_string(x + 20, y + 15, label);