imtk

changeset 18:737e9047d9c9

added radio group
author John Tsiombikas <nuclear@siggraph.org>
date Mon, 25 Apr 2011 08:54:05 +0300
parents ac2a8d8fca9a
children 11da537aeba6
files Makefile src/draw.c src/draw.h src/imtk.h src/listbox.c test.c
diffstat 6 files changed, 91 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Tue Apr 19 08:36:23 2011 +0300
     1.2 +++ b/Makefile	Mon Apr 25 08:54:05 2011 +0300
     1.3 @@ -5,7 +5,7 @@
     1.4  
     1.5  CC = gcc
     1.6  CFLAGS = -pedantic -Wall -g -Isrc
     1.7 -LDFLAGS = $(libgl)
     1.8 +LDFLAGS = $(libgl) -lm
     1.9  
    1.10  ifeq ($(shell uname -s), Darwin)
    1.11  	libgl = -framework OpenGL -framework GLUT
     2.1 --- a/src/draw.c	Tue Apr 19 08:36:23 2011 +0300
     2.2 +++ b/src/draw.c	Mon Apr 25 08:54:05 2011 +0300
     2.3 @@ -1,4 +1,5 @@
     2.4  #include <string.h>
     2.5 +#include <math.h>
     2.6  #include <assert.h>
     2.7  #include "draw.h"
     2.8  #include "imtk.h"
     2.9 @@ -115,6 +116,45 @@
    2.10  	glEnd();
    2.11  }
    2.12  
    2.13 +void imtk_draw_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot)
    2.14 +{
    2.15 +	int i;
    2.16 +	float t, dtheta, theta = 0.0;
    2.17 +	float color[4];
    2.18 +	float cx = (float)x;
    2.19 +	float cy = (float)y;
    2.20 +
    2.21 +	subdiv += 3;
    2.22 +	dtheta = 2.0 * M_PI / subdiv;
    2.23 +
    2.24 +	color[0] = (ctop[0] + cbot[0]) * 0.5;
    2.25 +	color[1] = (ctop[1] + cbot[1]) * 0.5;
    2.26 +	color[2] = (ctop[2] + cbot[2]) * 0.5;
    2.27 +	color[3] = (ctop[3] + cbot[3]) * 0.5;
    2.28 +
    2.29 +	glBegin(GL_TRIANGLE_FAN);
    2.30 +	glColor4fv(color);
    2.31 +	glVertex2f(cx, cy);
    2.32 +
    2.33 +	for(i=0; i<=subdiv; i++) {
    2.34 +		float vx, vy;
    2.35 +
    2.36 +		vx = cx + cos(theta) * rad;
    2.37 +		vy = cy + sin(theta) * rad;
    2.38 +		theta += dtheta;
    2.39 +
    2.40 +		t = (vy - (cy - rad)) / (2.0 * rad);
    2.41 +		color[0] = ctop[0] + (cbot[0] - ctop[0]) * t;
    2.42 +		color[1] = ctop[1] + (cbot[1] - ctop[1]) * t;
    2.43 +		color[2] = ctop[2] + (cbot[2] - ctop[2]) * t;
    2.44 +		color[3] = ctop[3] + (cbot[3] - ctop[3]) * t;
    2.45 +
    2.46 +		glColor4fv(color);
    2.47 +		glVertex2f(vx, vy);
    2.48 +	}
    2.49 +	glEnd();
    2.50 +}
    2.51 +
    2.52  void imtk_draw_frame(int x, int y, int w, int h, int style)
    2.53  {
    2.54  	float tcol[4], bcol[4];
     3.1 --- a/src/draw.h	Tue Apr 19 08:36:23 2011 +0300
     3.2 +++ b/src/draw.h	Mon Apr 25 08:54:05 2011 +0300
     3.3 @@ -14,6 +14,8 @@
     3.4  };
     3.5  
     3.6  void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot);
     3.7 +void imtk_draw_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot);
     3.8 +
     3.9  void imtk_draw_frame(int x, int y, int w, int h, int style);
    3.10  void imtk_draw_string(int x, int y, const char *str);
    3.11  int imtk_string_size(const char *str);
     4.1 --- a/src/imtk.h	Tue Apr 19 08:36:23 2011 +0300
     4.2 +++ b/src/imtk.h	Mon Apr 25 08:54:05 2011 +0300
     4.3 @@ -60,9 +60,7 @@
     4.4  float imtk_slider(int id, float pos, float min, float max, int x, int y);
     4.5  void imtk_progress(int id, float pos, int x, int y);
     4.6  int imtk_listbox(int id, const char *list, int sel, int x, int y);
     4.7 -/*
     4.8 -int imtk_combobox(int id, char *textbuf, size_t buf_sz, const char *list, int sel, int x, int y);
     4.9 -*/
    4.10 +int imtk_radiogroup(int id, const char *list, int sel, int x, int y);
    4.11  
    4.12  /* helper functions to create and destroy item lists for listboxes and comboboxes */
    4.13  char *imtk_create_list(const char *first, ...);
     5.1 --- a/src/listbox.c	Tue Apr 19 08:36:23 2011 +0300
     5.2 +++ b/src/listbox.c	Mon Apr 25 08:54:05 2011 +0300
     5.3 @@ -8,10 +8,22 @@
     5.4  #define ITEM_HEIGHT		18
     5.5  #define PAD				3
     5.6  
     5.7 +static int list_radio(int id, const char *list, int sel, int x, int y, void (*draw)());
     5.8  static void draw_listbox(int id, const char *list, int sel, int x, int y, int width, int nitems, int over);
     5.9 +static void draw_radio(int id, const char *list, int sel, int x, int y, int width, int nitems, int over);
    5.10  
    5.11  int imtk_listbox(int id, const char *list, int sel, int x, int y)
    5.12  {
    5.13 +	return list_radio(id, list, sel, x, y, draw_listbox);
    5.14 +}
    5.15 +
    5.16 +int imtk_radiogroup(int id, const char *list, int sel, int x, int y)
    5.17 +{
    5.18 +	return list_radio(id, list, sel, x, y, draw_radio);
    5.19 +}
    5.20 +
    5.21 +static int list_radio(int id, const char *list, int sel, int x, int y, void (*draw)())
    5.22 +{
    5.23  	int i, max_width, nitems, over;
    5.24  	const char *ptr;
    5.25  
    5.26 @@ -48,7 +60,7 @@
    5.27  		}
    5.28  	}
    5.29  
    5.30 -	draw_listbox(id, list, sel, x, y, max_width, nitems, over);
    5.31 +	draw(id, list, sel, x, y, max_width, nitems, over);
    5.32  	return sel;
    5.33  }
    5.34  
    5.35 @@ -127,3 +139,36 @@
    5.36  
    5.37  	imtk_draw_frame(x, y, width, ITEM_HEIGHT * nitems, FRAME_INSET);
    5.38  }
    5.39 +
    5.40 +static void draw_radio(int id, const char *list, int sel, int x, int y, int width, int nitems, int over)
    5.41 +{
    5.42 +	int i;
    5.43 +	const char *item = list;
    5.44 +	float rad = ITEM_HEIGHT * 0.5;
    5.45 +
    5.46 +	for(i=0; i<nitems; i++) {
    5.47 +		int item_y = i * ITEM_HEIGHT + y;
    5.48 +		unsigned int attr = 0;
    5.49 +		float tcol[4], bcol[4];
    5.50 +
    5.51 +		if(over - 1 == i) {
    5.52 +			attr |= IMTK_FOCUS_BIT;
    5.53 +		}
    5.54 +
    5.55 +		memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol);
    5.56 +		memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol);
    5.57 +
    5.58 +		imtk_draw_disc(x + rad, item_y + rad, rad * 0.8, 5, tcol, bcol);
    5.59 +
    5.60 +		if(i == sel) {
    5.61 +			attr |= IMTK_SEL_BIT;
    5.62 +			memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol);
    5.63 +			memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol);
    5.64 +			imtk_draw_disc(x + rad, item_y + ITEM_HEIGHT / 2, rad * 0.6, 5, tcol, bcol);
    5.65 +		}
    5.66 +
    5.67 +		glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
    5.68 +		imtk_draw_string(x + rad * 2.0 + 3, item_y + ITEM_HEIGHT - 5, item);
    5.69 +		item += strlen(item) + 1;
    5.70 +	}
    5.71 +}
     6.1 --- a/test.c	Tue Apr 19 08:36:23 2011 +0300
     6.2 +++ b/test.c	Mon Apr 25 08:54:05 2011 +0300
     6.3 @@ -146,7 +146,7 @@
     6.4  	}
     6.5  
     6.6  	itemlist = imtk_create_list("teapot", "torus", "sphere", NULL);
     6.7 -	if((objsel = imtk_listbox(IMUID, itemlist, prev_sel, 30, 120)) != prev_sel) {
     6.8 +	if((objsel = imtk_radiogroup(IMUID, itemlist, prev_sel, 30, 120)) != prev_sel) {
     6.9  		prev_sel = objsel;
    6.10  		glutPostRedisplay();
    6.11  	}