imtk

diff src/imtk.c @ 20:c7a7ddbe7714

half-arsed automatic layout
author John Tsiombikas <nuclear@siggraph.org>
date Sat, 30 Apr 2011 05:23:59 +0300
parents 9c7987064bb0
children 3138c38433e2
line diff
     1.1 --- a/src/imtk.c	Tue Apr 26 22:53:21 2011 +0300
     1.2 +++ b/src/imtk.c	Sat Apr 30 05:23:59 2011 +0300
     1.3 @@ -22,6 +22,20 @@
     1.4  {
     1.5  	int width, height;
     1.6  
     1.7 +	glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
     1.8 +
     1.9 +	glDisable(GL_DEPTH_TEST);
    1.10 +	glDisable(GL_STENCIL_TEST);
    1.11 +	glDisable(GL_ALPHA_TEST);
    1.12 +	glDisable(GL_TEXTURE_1D);
    1.13 +	glDisable(GL_TEXTURE_2D);
    1.14 +	glDisable(GL_CULL_FACE);
    1.15 +	glDisable(GL_SCISSOR_TEST);
    1.16 +	glDisable(GL_LIGHTING);
    1.17 +	glEnable(GL_BLEND);
    1.18 +	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    1.19 +
    1.20 +
    1.21  	imtk_get_viewport(&width, &height);
    1.22  
    1.23  	glMatrixMode(GL_PROJECTION);
    1.24 @@ -33,42 +47,88 @@
    1.25  	glMatrixMode(GL_MODELVIEW);
    1.26  	glPushMatrix();
    1.27  	glLoadIdentity();
    1.28 -
    1.29 -	glPushAttrib(GL_ENABLE_BIT);
    1.30 -	glDisable(GL_DEPTH_TEST);
    1.31 -	glDisable(GL_CULL_FACE);
    1.32 -	glDisable(GL_LIGHTING);
    1.33 -	glEnable(GL_BLEND);
    1.34 -
    1.35 -	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    1.36  }
    1.37  
    1.38  void imtk_end(void)
    1.39  {
    1.40 -	glPopAttrib();
    1.41 -
    1.42  	glMatrixMode(GL_PROJECTION);
    1.43  	glPopMatrix();
    1.44  	glMatrixMode(GL_MODELVIEW);
    1.45  	glPopMatrix();
    1.46 +
    1.47 +	glPopAttrib();
    1.48  }
    1.49  
    1.50  void imtk_label(const char *str, int x, int y)
    1.51  {
    1.52 +	if(x == IMTK_AUTO || y == IMTK_AUTO) {
    1.53 +		imtk_layout_get_pos(&x, &y);
    1.54 +	}
    1.55 +
    1.56  	glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
    1.57 -	imtk_draw_string(x, y, str);
    1.58 +	imtk_draw_string(x, y + 15, str);
    1.59 +	imtk_layout_advance(imtk_string_size(str), 12);
    1.60  }
    1.61  
    1.62  
    1.63 -/*
    1.64 -int imtk_combobox(int id, char *textbuf, size_t buf_sz, const char *list, int sel, int x, int y)
    1.65 +static int box[4], span[4];
    1.66 +static int spacing;
    1.67 +static int layout;
    1.68 +
    1.69 +void imtk_layout_start(int x, int y, int sp, int dir)
    1.70  {
    1.71 -	imtk_textbox(id + 1, textbuf, buf_sz, x, y);
    1.72 -	imtk_button(id + 3, "V", x + TEXTBOX_SIZE, y);
    1.73 +	box[0] = span[0] = x;
    1.74 +	box[1] = span[1] = y;
    1.75 +	box[2] = box[3] = span[2] = span[3] = 0;
    1.76 +	spacing = sp;
    1.77 +	layout = dir;
    1.78 +}
    1.79  
    1.80 -	if(prev_active == id + 3) {
    1.81 -		sel = imtk_listbox(id + 5, list, sel, x, y + 20);
    1.82 +void imtk_layout_dir(int dir)
    1.83 +{
    1.84 +	layout = dir;
    1.85 +	if(dir == IMTK_VERTICAL) {
    1.86 +		imtk_layout_newline();
    1.87  	}
    1.88 -	return sel;
    1.89  }
    1.90 -*/
    1.91 +
    1.92 +void imtk_layout_advance(int width, int height)
    1.93 +{
    1.94 +	int max_span_y, max_box_y;
    1.95 +
    1.96 +	box[2] += width + spacing;
    1.97 +	span[2] += width + spacing;
    1.98 +
    1.99 +	if(height > span[3]) {
   1.100 +		span[3] = height;
   1.101 +	}
   1.102 +
   1.103 +	max_span_y = span[1] + span[3];
   1.104 +	max_box_y = box[1] + box[3];
   1.105 +
   1.106 +	if(max_span_y > max_box_y) {
   1.107 +		box[3] = max_span_y - box[1];
   1.108 +	}
   1.109 +
   1.110 +	if(layout == IMTK_VERTICAL) {
   1.111 +		imtk_layout_newline();
   1.112 +	}
   1.113 +}
   1.114 +
   1.115 +void imtk_layout_newline(void)
   1.116 +{
   1.117 +	span[0] = box[0];
   1.118 +	span[1] = box[1] + box[3] + spacing;
   1.119 +	span[2] = span[3] = 0;
   1.120 +}
   1.121 +
   1.122 +void imtk_layout_get_pos(int *x, int *y)
   1.123 +{
   1.124 +	*x = span[0] + span[2];
   1.125 +	*y = span[1];
   1.126 +}
   1.127 +
   1.128 +void imtk_layout_get_bounds(int *bbox)
   1.129 +{
   1.130 +	memcpy(bbox, box, sizeof box);
   1.131 +}