imtk

changeset 5:09b6e8a5dc14

reorganizing the code
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Apr 2011 14:22:20 +0300
parents 00a4ea4ee6dc
children 38609a9f7586
files src/imtk.c src/imtk.h
diffstat 2 files changed, 9 insertions(+), 467 deletions(-) [+]
line diff
     1.1 --- a/src/imtk.c	Sat Mar 05 09:25:27 2011 +0200
     1.2 +++ b/src/imtk.c	Thu Apr 14 14:22:20 2011 +0300
     1.3 @@ -11,107 +11,9 @@
     1.4  #endif
     1.5  #include "imtk.h"
     1.6  
     1.7 -#define CHECKBOX_SIZE	14
     1.8 -#define TEXTBOX_SIZE	100
     1.9 -#define SLIDER_SIZE		100
    1.10 -#define THUMB_WIDTH		10
    1.11 -#define THUMB_HEIGHT	20
    1.12  
    1.13 -enum {
    1.14 -	FRAME_OUTSET,
    1.15 -	FRAME_INSET
    1.16 -};
    1.17 +static void draw_progress(int id, float pos, int x, int y);
    1.18  
    1.19 -struct key_node {
    1.20 -	int key;
    1.21 -	struct key_node *next;
    1.22 -};
    1.23 -
    1.24 -static void set_active(int id);
    1.25 -static int set_hot(int id);
    1.26 -static int hit_test(int x, int y, int w, int h);
    1.27 -
    1.28 -static void draw_button(int id, const char *label, int x, int y);
    1.29 -static void calc_button_size(const char *label, int *wret, int *hret);
    1.30 -static void draw_checkbox(int id, const char *label, int x, int y, int state);
    1.31 -static void draw_textbox(int id, const char *text, int x, int y);
    1.32 -static void draw_slider(int id, float pos, float min, float max, int x, int y);
    1.33 -static void draw_progress(int id, float pos, int x, int y);
    1.34 -static void draw_string(int x, int y, const char *str);
    1.35 -static int string_size(const char *str);
    1.36 -static void draw_frame(int x, int y, int w, int h, int style);
    1.37 -
    1.38 -
    1.39 -/* default colors, can be changed with imtk_set_color */
    1.40 -static float colors[][4] = {
    1.41 -	{0.0, 0.0, 0.0},	/* text color */
    1.42 -	{0.7, 0.7, 0.7},	/* base color */
    1.43 -	{0.85, 0.85, 0.85},	/* focus color */
    1.44 -	{1.0, 1.0, 1.0},	/* lit bevel */
    1.45 -	{0.3, 0.3, 0.3}		/* shadowed bevel */
    1.46 -};
    1.47 -
    1.48 -static int scr_width = 1, scr_height = 1;
    1.49 -static int mousex, mousey, prevx, mouse_bnmask;
    1.50 -static int active = -1, hot = -1, input = -1, prev_active = -1;
    1.51 -
    1.52 -static struct key_node *key_list, *key_tail;
    1.53 -
    1.54 -
    1.55 -void imtk_set_color(int col, float r, float g, float b)
    1.56 -{
    1.57 -	assert(col >= 0 && col < sizeof colors / sizeof *colors);
    1.58 -
    1.59 -	colors[col][0] = r;
    1.60 -	colors[col][1] = g;
    1.61 -	colors[col][2] = b;
    1.62 -}
    1.63 -
    1.64 -void imtk_inp_key(int key, int state)
    1.65 -{
    1.66 -	if(state == IMTK_DOWN) {
    1.67 -		struct key_node *node;
    1.68 -
    1.69 -		if(!(node = malloc(sizeof *node))) {
    1.70 -			return;
    1.71 -		}
    1.72 -		node->key = key;
    1.73 -		node->next = 0;
    1.74 -
    1.75 -		if(key_list) {
    1.76 -			key_tail->next = node;
    1.77 -			key_tail = node;
    1.78 -		} else {
    1.79 -			key_list = key_tail = node;
    1.80 -		}
    1.81 -	}
    1.82 -
    1.83 -	glutPostRedisplay();
    1.84 -}
    1.85 -
    1.86 -void imtk_inp_mouse(int bn, int state)
    1.87 -{
    1.88 -	if(state == IMTK_DOWN) {
    1.89 -		mouse_bnmask |= 1 << bn;
    1.90 -	} else {
    1.91 -		mouse_bnmask &= ~(1 << bn);
    1.92 -	}
    1.93 -	glutPostRedisplay();
    1.94 -}
    1.95 -
    1.96 -void imtk_inp_motion(int x, int y)
    1.97 -{
    1.98 -	mousex = x;
    1.99 -	mousey = y;
   1.100 -
   1.101 -	glutPostRedisplay();
   1.102 -}
   1.103 -
   1.104 -void imtk_inp_reshape(int x, int y)
   1.105 -{
   1.106 -	scr_width = x;
   1.107 -	scr_height = y;
   1.108 -}
   1.109  
   1.110  void imtk_begin(void)
   1.111  {
   1.112 @@ -141,165 +43,13 @@
   1.113  	glPopMatrix();
   1.114  }
   1.115  
   1.116 -int imtk_button(int id, const char *label, int x, int y)
   1.117 +
   1.118 +void imtk_post_redisplay(void)
   1.119  {
   1.120 -	int w, h, res = 0;
   1.121 -	int over = 0;
   1.122 -
   1.123 -	assert(id >= 0);
   1.124 -
   1.125 -	calc_button_size(label, &w, &h);
   1.126 -
   1.127 -	if(hit_test(x, y, w, h)) {
   1.128 -		set_hot(id);
   1.129 -		over = 1;
   1.130 -	}
   1.131 -
   1.132 -	if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
   1.133 -		if(over) {
   1.134 -			set_active(id);
   1.135 -		}
   1.136 -	} else { /* mouse button up */
   1.137 -		if(active == id) {
   1.138 -			set_active(-1);
   1.139 -			if(hot == id && over) {
   1.140 -				res = 1;
   1.141 -			}
   1.142 -		}
   1.143 -	}
   1.144 -
   1.145 -	draw_button(id, label, x, y);
   1.146 -	return res;
   1.147 +	glutPostRedisplay();
   1.148  }
   1.149  
   1.150 -int imtk_checkbox(int id, const char *label, int x, int y, int state)
   1.151 -{
   1.152 -	int sz = CHECKBOX_SIZE;
   1.153 -	int over = 0;
   1.154  
   1.155 -	assert(id >= 0);
   1.156 -
   1.157 -	if(hit_test(x, y, sz, sz)) {
   1.158 -		set_hot(id);
   1.159 -		over = 1;
   1.160 -	}
   1.161 -
   1.162 -	if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
   1.163 -		if(over) {
   1.164 -			set_active(id);
   1.165 -		}
   1.166 -	} else { /* mouse button up */
   1.167 -		if(active == id) {
   1.168 -			set_active(-1);
   1.169 -			if(hot == id && over) {
   1.170 -				state = !state;
   1.171 -			}
   1.172 -		}
   1.173 -	}
   1.174 -
   1.175 -	draw_checkbox(id, label, x, y, state);
   1.176 -	return state;
   1.177 -}
   1.178 -
   1.179 -void imtk_textbox(int id, char *textbuf, size_t buf_sz, int x, int y)
   1.180 -{
   1.181 -	int len, over = 0;
   1.182 -
   1.183 -	assert(id >= 0);
   1.184 -
   1.185 -	if(hit_test(x, y, TEXTBOX_SIZE, 20)) {
   1.186 -		set_hot(id);
   1.187 -		over = 1;
   1.188 -	}
   1.189 -
   1.190 -	if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
   1.191 -		if(over) {
   1.192 -			set_active(id);
   1.193 -		}
   1.194 -	} else {
   1.195 -		if(active == id) {
   1.196 -			set_active(-1);
   1.197 -			if(hot == id && over) {
   1.198 -				input = id;
   1.199 -			}
   1.200 -		}
   1.201 -	}
   1.202 -
   1.203 -	if(input == id) {
   1.204 -		len = strlen(textbuf);
   1.205 -		while(key_list) {
   1.206 -			struct key_node *node = key_list;
   1.207 -			key_list = key_list->next;
   1.208 -
   1.209 -			if(isprint(node->key)) {
   1.210 -				if(len < buf_sz) {
   1.211 -					textbuf[len++] = (char)node->key;
   1.212 -				}
   1.213 -			} else {
   1.214 -				switch(node->key) {
   1.215 -				case '\b':
   1.216 -					if(len > 0) {
   1.217 -						textbuf[--len] = 0;
   1.218 -					}
   1.219 -					break;
   1.220 -
   1.221 -				default:
   1.222 -					break;
   1.223 -				}
   1.224 -			}
   1.225 -
   1.226 -			free(node);
   1.227 -		}
   1.228 -		key_list = key_tail = 0;
   1.229 -	}
   1.230 -
   1.231 -	draw_textbox(id, textbuf, x, y);
   1.232 -}
   1.233 -
   1.234 -float imtk_slider(int id, float pos, float min, float max, int x, int y)
   1.235 -{
   1.236 -	int thumb_x, thumb_y, over = 0;
   1.237 -	float range = max - min;
   1.238 -
   1.239 -	assert(id >= 0);
   1.240 -
   1.241 -	pos = (pos - min) / range;
   1.242 -	if(pos < 0.0) pos = 0.0;
   1.243 -	if(pos > 1.0) pos = 1.0;
   1.244 -
   1.245 -	thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
   1.246 -	thumb_y = y - THUMB_HEIGHT / 2;
   1.247 -
   1.248 -	if(hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
   1.249 -		set_hot(id);
   1.250 -		over = 1;
   1.251 -	}
   1.252 -
   1.253 -	if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
   1.254 -		if(over && hot == id) {
   1.255 -			if(active != id) {
   1.256 -				prevx = mousex;
   1.257 -			}
   1.258 -			set_active(id);
   1.259 -		}
   1.260 -	} else {
   1.261 -		if(active == id) {
   1.262 -			set_active(-1);
   1.263 -		}
   1.264 -	}
   1.265 -
   1.266 -	if(active == id) {
   1.267 -		float dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
   1.268 -		pos += dx;
   1.269 -		prevx = mousex;
   1.270 -
   1.271 -		if(pos < 0.0) pos = 0.0;
   1.272 -		if(pos > 1.0) pos = 1.0;
   1.273 -	}
   1.274 -
   1.275 -	draw_slider(id, pos, min, max, x, y);
   1.276 -	return pos * range + min;
   1.277 -}
   1.278  
   1.279  void imtk_progress(int id, float pos, int x, int y)
   1.280  {
   1.281 @@ -380,167 +130,6 @@
   1.282  	free(list);
   1.283  }
   1.284  
   1.285 -static void set_active(int id)
   1.286 -{
   1.287 -	if(id == -1 || hot == id) {
   1.288 -		prev_active = active;
   1.289 -		active = id;
   1.290 -	}
   1.291 -}
   1.292 -
   1.293 -static int set_hot(int id)
   1.294 -{
   1.295 -	if(active == -1) {
   1.296 -		hot = id;
   1.297 -		return 1;
   1.298 -	}
   1.299 -	return 0;
   1.300 -}
   1.301 -
   1.302 -static int hit_test(int x, int y, int w, int h)
   1.303 -{
   1.304 -	return mousex >= x && mousex < (x + w) &&
   1.305 -		mousey >= y && mousey < (y + h);
   1.306 -}
   1.307 -
   1.308 -static void draw_button(int id, const char *label, int x, int y)
   1.309 -{
   1.310 -	int width, height;
   1.311 -
   1.312 -	calc_button_size(label, &width, &height);
   1.313 -
   1.314 -	glBegin(GL_QUADS);
   1.315 -	if(hit_test(x, y, width, height)) {
   1.316 -		glColor3fv(colors[IMTK_FOCUS_COLOR]);
   1.317 -	} else {
   1.318 -		glColor3fv(colors[IMTK_BASE_COLOR]);
   1.319 -	}
   1.320 -	glVertex2f(x, y);
   1.321 -	glVertex2f(x + width, y);
   1.322 -	glVertex2f(x + width, y + height);
   1.323 -	glVertex2f(x, y + height);
   1.324 -	glEnd();
   1.325 -
   1.326 -	draw_frame(x, y, width, height, active == id ? FRAME_INSET : FRAME_OUTSET);
   1.327 -
   1.328 -	glColor3fv(colors[IMTK_TEXT_COLOR]);
   1.329 -	draw_string(x + 20, y + 15, label);
   1.330 -}
   1.331 -
   1.332 -static void calc_button_size(const char *label, int *wret, int *hret)
   1.333 -{
   1.334 -	int strsz = string_size(label);
   1.335 -	if(wret) *wret = strsz + 40;
   1.336 -	if(hret) *hret = 20;
   1.337 -}
   1.338 -
   1.339 -static void draw_checkbox(int id, const char *label, int x, int y, int state)
   1.340 -{
   1.341 -	static const int sz = CHECKBOX_SIZE;
   1.342 -
   1.343 -	if(hit_test(x, y, sz, sz)) {
   1.344 -		glColor3fv(colors[IMTK_FOCUS_COLOR]);
   1.345 -	} else {
   1.346 -		glColor3fv(colors[IMTK_BASE_COLOR]);
   1.347 -	}
   1.348 -
   1.349 -	glBegin(GL_QUADS);
   1.350 -	glVertex2f(x, y);
   1.351 -	glVertex2f(x + sz, y);
   1.352 -	glVertex2f(x + sz, y + sz);
   1.353 -	glVertex2f(x, y + sz);
   1.354 -	glEnd();
   1.355 -
   1.356 -	draw_frame(x, y, sz, sz, FRAME_INSET);
   1.357 -
   1.358 -	glColor3fv(colors[IMTK_TEXT_COLOR]);
   1.359 -	if(state) {
   1.360 -		glPushAttrib(GL_LINE_BIT);
   1.361 -		glLineWidth(2);
   1.362 -
   1.363 -		glBegin(GL_LINES);
   1.364 -		glVertex2f(x + 2, y + 2);
   1.365 -		glVertex2f(x + sz - 2, y + sz - 2);
   1.366 -		glVertex2f(x + sz - 2, y + 2);
   1.367 -		glVertex2f(x + 2, y + sz - 2);
   1.368 -		glEnd();
   1.369 -
   1.370 -		glPopAttrib();
   1.371 -	}
   1.372 -
   1.373 -	draw_string(x + sz + 5, y + sz - 2, label);
   1.374 -}
   1.375 -
   1.376 -static void draw_textbox(int id, const char *text, int x, int y)
   1.377 -{
   1.378 -	int strsz = string_size(text);
   1.379 -
   1.380 -	if(hit_test(x, y, TEXTBOX_SIZE, 20)) {
   1.381 -		glColor3fv(colors[IMTK_FOCUS_COLOR]);
   1.382 -	} else {
   1.383 -		glColor3fv(colors[IMTK_BASE_COLOR]);
   1.384 -	}
   1.385 -
   1.386 -	glBegin(GL_QUADS);
   1.387 -	glVertex2f(x, y);
   1.388 -	glVertex2f(x + TEXTBOX_SIZE, y);
   1.389 -	glVertex2f(x + TEXTBOX_SIZE, y + 20);
   1.390 -	glVertex2f(x, y + 20);
   1.391 -	glEnd();
   1.392 -
   1.393 -	glColor3fv(colors[IMTK_TEXT_COLOR]);
   1.394 -
   1.395 -	if(input == id) {
   1.396 -		glBegin(GL_LINES);
   1.397 -		glVertex2f(x + strsz + 2, y + 2);
   1.398 -		glVertex2f(x + strsz + 2, y + 18);
   1.399 -		glEnd();
   1.400 -	}
   1.401 -
   1.402 -	draw_string(x + 2, y + 15, text);
   1.403 -
   1.404 -	draw_frame(x, y, TEXTBOX_SIZE, 20, FRAME_INSET);
   1.405 -}
   1.406 -
   1.407 -static void draw_slider(int id, float pos, float min, float max, int x, int y)
   1.408 -{
   1.409 -	float range = max - min;
   1.410 -	int thumb_x, thumb_y;
   1.411 -	char buf[32];
   1.412 -
   1.413 -	thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
   1.414 -	thumb_y = y - THUMB_HEIGHT / 2;
   1.415 -
   1.416 -	/* draw trough */
   1.417 -	glBegin(GL_QUADS);
   1.418 -	glColor3fv(colors[IMTK_BASE_COLOR]);
   1.419 -	glVertex2f(x, y - 2);
   1.420 -	glVertex2f(x + SLIDER_SIZE, y - 2);
   1.421 -	glVertex2f(x + SLIDER_SIZE, y + 2);
   1.422 -	glVertex2f(x, y + 2);
   1.423 -	glEnd();
   1.424 -	draw_frame(x, y - 2, SLIDER_SIZE, 4, FRAME_INSET);
   1.425 -
   1.426 -	if(hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
   1.427 -		glColor3fv(colors[IMTK_FOCUS_COLOR]);
   1.428 -	} else {
   1.429 -		glColor3fv(colors[IMTK_BASE_COLOR]);
   1.430 -	}
   1.431 -
   1.432 -	/* draw handle */
   1.433 -	glBegin(GL_QUADS);
   1.434 -	glVertex2f(thumb_x, thumb_y);
   1.435 -	glVertex2f(thumb_x + THUMB_WIDTH, thumb_y);
   1.436 -	glVertex2f(thumb_x + THUMB_WIDTH, thumb_y + THUMB_HEIGHT);
   1.437 -	glVertex2f(thumb_x, thumb_y + THUMB_HEIGHT);
   1.438 -	glEnd();
   1.439 -	draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET);
   1.440 -
   1.441 -	/* draw display */
   1.442 -	sprintf(buf, "%.3f", pos * range + min);
   1.443 -	glColor3fv(colors[IMTK_TEXT_COLOR]);
   1.444 -	draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf);
   1.445 -}
   1.446  
   1.447  static void draw_progress(int id, float pos, int x, int y)
   1.448  {
   1.449 @@ -571,46 +160,3 @@
   1.450  		draw_frame(x, y, bar_size, 15, FRAME_OUTSET);
   1.451  	}
   1.452  }
   1.453 -
   1.454 -static void draw_string(int x, int y, const char *str)
   1.455 -{
   1.456 -	glRasterPos2i(x, y);
   1.457 -	while(*str) {
   1.458 -		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
   1.459 -	}
   1.460 -}
   1.461 -
   1.462 -static int string_size(const char *str)
   1.463 -{
   1.464 -	return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
   1.465 -}
   1.466 -
   1.467 -static void draw_frame(int x, int y, int w, int h, int style)
   1.468 -{
   1.469 -	float tcol[3], bcol[3];
   1.470 -
   1.471 -	switch(style) {
   1.472 -	case FRAME_INSET:
   1.473 -		memcpy(tcol, colors[IMTK_BEVEL_SHAD_COLOR], sizeof tcol);
   1.474 -		memcpy(bcol, colors[IMTK_BEVEL_LIT_COLOR], sizeof bcol);
   1.475 -		break;
   1.476 -
   1.477 -	case FRAME_OUTSET:
   1.478 -	default:
   1.479 -		memcpy(tcol, colors[IMTK_BEVEL_LIT_COLOR], sizeof tcol);
   1.480 -		memcpy(bcol, colors[IMTK_BEVEL_SHAD_COLOR], sizeof bcol);
   1.481 -	}
   1.482 -
   1.483 -	glBegin(GL_LINES);
   1.484 -	glColor3fv(tcol);
   1.485 -	glVertex2f(x, y + h);
   1.486 -	glVertex2f(x, y);
   1.487 -	glVertex2f(x, y);
   1.488 -	glVertex2f(x + w, y);
   1.489 -	glColor3fv(bcol);
   1.490 -	glVertex2f(x + w, y);
   1.491 -	glVertex2f(x + w, y + h);
   1.492 -	glVertex2f(x + w, y + h);
   1.493 -	glVertex2f(x, y + h);
   1.494 -	glEnd();
   1.495 -}
     2.1 --- a/src/imtk.h	Sat Mar 05 09:25:27 2011 +0200
     2.2 +++ b/src/imtk.h	Thu Apr 14 14:22:20 2011 +0300
     2.3 @@ -1,16 +1,12 @@
     2.4  #ifndef IMTK_H_
     2.5  #define IMTK_H_
     2.6  
     2.7 +#include <stdlib.h>
     2.8 +
     2.9 +
    2.10  #define IMUID			(__LINE__ << 10)
    2.11  #define IMUID_IDX(i)	((__LINE__ << 10) + ((i) << 1))
    2.12  
    2.13 -enum {
    2.14 -	IMTK_TEXT_COLOR,
    2.15 -	IMTK_BASE_COLOR,
    2.16 -	IMTK_FOCUS_COLOR,
    2.17 -	IMTK_BEVEL_LIT_COLOR,
    2.18 -	IMTK_BEVEL_SHAD_COLOR
    2.19 -};
    2.20  
    2.21  /* key/button state enum */
    2.22  enum {
    2.23 @@ -24,13 +20,13 @@
    2.24  	IMTK_RIGHT_BUTTON
    2.25  };
    2.26  
    2.27 -void imtk_set_color(int col, float r, float g, float b);
    2.28 -
    2.29  void imtk_inp_key(int key, int state);
    2.30  void imtk_inp_mouse(int bn, int state);
    2.31  void imtk_inp_motion(int x, int y);
    2.32  void imtk_inp_reshape(int x, int y);
    2.33  
    2.34 +void imtk_post_redisplay(void);
    2.35 +
    2.36  void imtk_begin(void);
    2.37  void imtk_end(void);
    2.38