imtk

changeset 3:038e5577d527

added slider and progress bar widgets
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 31 Dec 2010 18:10:13 +0200
parents 3d661dd17af3
children 00a4ea4ee6dc
files src/imtk.c src/imtk.h test.c
diffstat 3 files changed, 143 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/src/imtk.c	Fri Dec 31 01:54:53 2010 +0200
     1.2 +++ b/src/imtk.c	Fri Dec 31 18:10:13 2010 +0200
     1.3 @@ -1,3 +1,4 @@
     1.4 +#include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <string.h>
     1.7  #include <ctype.h>
     1.8 @@ -11,6 +12,9 @@
     1.9  
    1.10  #define CHECKBOX_SIZE	14
    1.11  #define TEXTBOX_SIZE	100
    1.12 +#define SLIDER_SIZE		100
    1.13 +#define THUMB_WIDTH		10
    1.14 +#define THUMB_HEIGHT	20
    1.15  
    1.16  enum {
    1.17  	FRAME_OUTSET,
    1.18 @@ -30,6 +34,8 @@
    1.19  static void calc_button_size(const char *label, int *wret, int *hret);
    1.20  static void draw_checkbox(int id, const char *label, int x, int y, int state);
    1.21  static void draw_textbox(int id, const char *text, int x, int y);
    1.22 +static void draw_slider(int id, float pos, float min, float max, int x, int y);
    1.23 +static void draw_progress(int id, float pos, int x, int y);
    1.24  static void draw_string(int x, int y, const char *str);
    1.25  static int string_size(const char *str);
    1.26  static void draw_frame(int x, int y, int w, int h, int style);
    1.27 @@ -45,7 +51,7 @@
    1.28  };
    1.29  
    1.30  static int scr_width = 1, scr_height = 1;
    1.31 -static int mousex, mousey, mouse_bnmask;
    1.32 +static int mousex, mousey, prevx, mouse_bnmask;
    1.33  static int active = -1, hot = -1, input = -1;
    1.34  
    1.35  static struct key_node *key_list, *key_tail;
    1.36 @@ -249,9 +255,61 @@
    1.37  	draw_textbox(id, textbuf, x, y);
    1.38  }
    1.39  
    1.40 +float imtk_slider(int id, float pos, float min, float max, int x, int y)
    1.41 +{
    1.42 +	int thumb_x, thumb_y, over = 0;
    1.43 +	float range = max - min;
    1.44 +
    1.45 +	assert(id >= 0);
    1.46 +
    1.47 +	pos = (pos - min) / range;
    1.48 +	if(pos < 0.0) pos = 0.0;
    1.49 +	if(pos > 1.0) pos = 1.0;
    1.50 +
    1.51 +	thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
    1.52 +	thumb_y = y - THUMB_HEIGHT / 2;
    1.53 +
    1.54 +	if(hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
    1.55 +		set_hot(id);
    1.56 +		over = 1;
    1.57 +	}
    1.58 +
    1.59 +	if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
    1.60 +		if(over && hot == id) {
    1.61 +			if(active != id) {
    1.62 +				prevx = mousex;
    1.63 +			}
    1.64 +			set_active(id);
    1.65 +		}
    1.66 +	} else {
    1.67 +		if(active == id) {
    1.68 +			set_active(-1);
    1.69 +		}
    1.70 +	}
    1.71 +
    1.72 +	if(active == id) {
    1.73 +		float dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
    1.74 +		pos += dx;
    1.75 +		prevx = mousex;
    1.76 +
    1.77 +		if(pos < 0.0) pos = 0.0;
    1.78 +		if(pos > 1.0) pos = 1.0;
    1.79 +	}
    1.80 +
    1.81 +	draw_slider(id, pos, min, max, x, y);
    1.82 +	return pos * range + min;
    1.83 +}
    1.84 +
    1.85 +void imtk_progress(int id, float pos, int x, int y)
    1.86 +{
    1.87 +	draw_progress(id, pos, x, y);
    1.88 +}
    1.89 +
    1.90  static void set_active(int id)
    1.91  {
    1.92 -	active = id;
    1.93 +	if(id == -1 || hot == id) {
    1.94 +		active = id;
    1.95 +	}
    1.96  }
    1.97  
    1.98  static int set_hot(int id)
    1.99 @@ -368,6 +426,76 @@
   1.100  	draw_frame(x, y, TEXTBOX_SIZE, 20, FRAME_INSET);
   1.101  }
   1.102  
   1.103 +static void draw_slider(int id, float pos, float min, float max, int x, int y)
   1.104 +{
   1.105 +	float range = max - min;
   1.106 +	int thumb_x, thumb_y;
   1.107 +	char buf[32];
   1.108 +
   1.109 +	thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
   1.110 +	thumb_y = y - THUMB_HEIGHT / 2;
   1.111 +
   1.112 +	/* draw trough */
   1.113 +	glBegin(GL_QUADS);
   1.114 +	glColor3fv(colors[IMTK_BASE_COLOR]);
   1.115 +	glVertex2f(x, y - 2);
   1.116 +	glVertex2f(x + SLIDER_SIZE, y - 2);
   1.117 +	glVertex2f(x + SLIDER_SIZE, y + 2);
   1.118 +	glVertex2f(x, y + 2);
   1.119 +	glEnd();
   1.120 +	draw_frame(x, y - 2, SLIDER_SIZE, 4, FRAME_INSET);
   1.121 +
   1.122 +	if(hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
   1.123 +		glColor3fv(colors[IMTK_FOCUS_COLOR]);
   1.124 +	} else {
   1.125 +		glColor3fv(colors[IMTK_BASE_COLOR]);
   1.126 +	}
   1.127 +
   1.128 +	/* draw handle */
   1.129 +	glBegin(GL_QUADS);
   1.130 +	glVertex2f(thumb_x, thumb_y);
   1.131 +	glVertex2f(thumb_x + THUMB_WIDTH, thumb_y);
   1.132 +	glVertex2f(thumb_x + THUMB_WIDTH, thumb_y + THUMB_HEIGHT);
   1.133 +	glVertex2f(thumb_x, thumb_y + THUMB_HEIGHT);
   1.134 +	glEnd();
   1.135 +	draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET);
   1.136 +
   1.137 +	/* draw display */
   1.138 +	sprintf(buf, "%.3f", pos * range + min);
   1.139 +	glColor3fv(colors[IMTK_TEXT_COLOR]);
   1.140 +	draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf);
   1.141 +}
   1.142 +
   1.143 +static void draw_progress(int id, float pos, int x, int y)
   1.144 +{
   1.145 +	int bar_size = SLIDER_SIZE * pos;
   1.146 +
   1.147 +	if(pos < 0.0) pos = 0.0;
   1.148 +	if(pos > 1.0) pos = 1.0;
   1.149 +
   1.150 +	/* through */
   1.151 +	glBegin(GL_QUADS);
   1.152 +	glColor3fv(colors[IMTK_BASE_COLOR]);
   1.153 +	glVertex2f(x - 1, y - 1);
   1.154 +	glVertex2f(x + SLIDER_SIZE + 1, y - 1);
   1.155 +	glVertex2f(x + SLIDER_SIZE + 1, y + 17);
   1.156 +	glVertex2f(x - 1, y + 17);
   1.157 +	glEnd();
   1.158 +	draw_frame(x - 1, y - 1, SLIDER_SIZE + 2, 17, FRAME_INSET);
   1.159 +
   1.160 +	if(pos > 0.0) {
   1.161 +		/* bar */
   1.162 +		glBegin(GL_QUADS);
   1.163 +		glColor3fv(colors[IMTK_BASE_COLOR]);
   1.164 +		glVertex2f(x, y);
   1.165 +		glVertex2f(x + bar_size, y);
   1.166 +		glVertex2f(x + bar_size, y + 15);
   1.167 +		glVertex2f(x, y + 15);
   1.168 +		glEnd();
   1.169 +		draw_frame(x, y, bar_size, 15, FRAME_OUTSET);
   1.170 +	}
   1.171 +}
   1.172 +
   1.173  static void draw_string(int x, int y, const char *str)
   1.174  {
   1.175  	glRasterPos2i(x, y);
     2.1 --- a/src/imtk.h	Fri Dec 31 01:54:53 2010 +0200
     2.2 +++ b/src/imtk.h	Fri Dec 31 18:10:13 2010 +0200
     2.3 @@ -33,9 +33,10 @@
     2.4  void imtk_begin(void);
     2.5  void imtk_end(void);
     2.6  
     2.7 -void imtk_window(int id, const char *title, int x, int y, int width, int height);
     2.8  int imtk_button(int id, const char *label, int x, int y);
     2.9  int imtk_checkbox(int id, const char *label, int x, int y, int state);
    2.10  void imtk_textbox(int id, char *textbuf, size_t buf_sz, int x, int y);
    2.11 +float imtk_slider(int id, float pos, float min, float max, int x, int y);
    2.12 +void imtk_progress(int id, float pos, int x, int y);
    2.13  
    2.14  #endif	/* IMTK_H_ */
     3.1 --- a/test.c	Fri Dec 31 01:54:53 2010 +0200
     3.2 +++ b/test.c	Fri Dec 31 18:10:13 2010 +0200
     3.3 @@ -19,6 +19,7 @@
     3.4  void motion(int x, int y);
     3.5  
     3.6  int xsz, ysz;
     3.7 +float angle;
     3.8  
     3.9  int main(int argc, char **argv)
    3.10  {
    3.11 @@ -69,6 +70,7 @@
    3.12  	glLoadIdentity();
    3.13  	glTranslatef(0, 0, -8);
    3.14  	glRotatef(25, 1, 0, 0);
    3.15 +	glRotatef(angle, 0, 1, 0);
    3.16  
    3.17  	glFrontFace(GL_CW);
    3.18  	glutSolidTeapot(1.0);
    3.19 @@ -89,6 +91,7 @@
    3.20  	static int bnshow;
    3.21  	static char textbuf[256];
    3.22  	static char textbuf2[256];
    3.23 +	static float val;
    3.24  
    3.25  	imtk_begin();
    3.26  
    3.27 @@ -122,6 +125,14 @@
    3.28  		}
    3.29  	}
    3.30  
    3.31 +	val = imtk_slider(IMUID, angle, 0.0, 360.0, 30, 390);
    3.32 +	if(val != angle) {
    3.33 +		angle = val;
    3.34 +		glutPostRedisplay();
    3.35 +	}
    3.36 +
    3.37 +	imtk_progress(IMUID, val / 360.0, 30, 420);
    3.38 +
    3.39  	if(imtk_button(IMUID, "Quit", 30, 500)) {
    3.40  		exit(0);
    3.41  	}