imtk

diff src/slider.c @ 6:38609a9f7586

reorganizing ...
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Apr 2011 14:22:42 +0300
parents
children 6d35e6c7b2ca
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/slider.c	Thu Apr 14 14:22:42 2011 +0300
     1.3 @@ -0,0 +1,105 @@
     1.4 +#include <stdio.h>
     1.5 +#include <assert.h>
     1.6 +#include "imtk.h"
     1.7 +#include "state.h"
     1.8 +#include "draw.h"
     1.9 +
    1.10 +#define SLIDER_SIZE		100
    1.11 +#define THUMB_WIDTH		10
    1.12 +#define THUMB_HEIGHT	20
    1.13 +
    1.14 +static void draw_slider(int id, float pos, float min, float max, int x, int y);
    1.15 +
    1.16 +float imtk_slider(int id, float pos, float min, float max, int x, int y)
    1.17 +{
    1.18 +	int mousex, mousey, thumb_x, thumb_y, over = 0;
    1.19 +	float range = max - min;
    1.20 +
    1.21 +	assert(id >= 0);
    1.22 +
    1.23 +	imtk_get_mouse(&mousex, &mousey);
    1.24 +
    1.25 +	pos = (pos - min) / range;
    1.26 +	if(pos < 0.0) pos = 0.0;
    1.27 +	if(pos > 1.0) pos = 1.0;
    1.28 +
    1.29 +	thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
    1.30 +	thumb_y = y - THUMB_HEIGHT / 2;
    1.31 +
    1.32 +	if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
    1.33 +		imtk_set_hot(id);
    1.34 +		over = 1;
    1.35 +	}
    1.36 +
    1.37 +	if(imtk_button_state(IMTK_LEFT_BUTTON)) {
    1.38 +		if(over && imtk_is_hot(id)) {
    1.39 +			if(!imtk_is_active(id)) {
    1.40 +				imtk_set_prev_mouse(mousex, mousey);
    1.41 +			}
    1.42 +			imtk_set_active(id);
    1.43 +		}
    1.44 +	} else {
    1.45 +		if(imtk_is_active(id)) {
    1.46 +			imtk_set_active(-1);
    1.47 +		}
    1.48 +	}
    1.49 +
    1.50 +	if(imtk_is_active(id)) {
    1.51 +		int prevx;
    1.52 +		float dx;
    1.53 +
    1.54 +		imtk_get_prev_mouse(&prevx, 0);
    1.55 +
    1.56 +		dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
    1.57 +		pos += dx;
    1.58 +		prevx = mousex;
    1.59 +
    1.60 +		if(pos < 0.0) pos = 0.0;
    1.61 +		if(pos > 1.0) pos = 1.0;
    1.62 +	}
    1.63 +
    1.64 +	draw_slider(id, pos, min, max, x, y);
    1.65 +	return pos * range + min;
    1.66 +}
    1.67 +
    1.68 +
    1.69 +static void draw_slider(int id, float pos, float min, float max, int x, int y)
    1.70 +{
    1.71 +	float range = max - min;
    1.72 +	int thumb_x, thumb_y;
    1.73 +	char buf[32];
    1.74 +
    1.75 +	thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
    1.76 +	thumb_y = y - THUMB_HEIGHT / 2;
    1.77 +
    1.78 +	/* draw trough */
    1.79 +	glBegin(GL_QUADS);
    1.80 +	glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
    1.81 +	glVertex2f(x, y - 2);
    1.82 +	glVertex2f(x + SLIDER_SIZE, y - 2);
    1.83 +	glVertex2f(x + SLIDER_SIZE, y + 2);
    1.84 +	glVertex2f(x, y + 2);
    1.85 +	glEnd();
    1.86 +	imtk_draw_frame(x, y - 2, SLIDER_SIZE, 4, FRAME_INSET);
    1.87 +
    1.88 +	if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
    1.89 +		glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
    1.90 +	} else {
    1.91 +		glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
    1.92 +	}
    1.93 +
    1.94 +	/* draw handle */
    1.95 +	glBegin(GL_QUADS);
    1.96 +	glVertex2f(thumb_x, thumb_y);
    1.97 +	glVertex2f(thumb_x + THUMB_WIDTH, thumb_y);
    1.98 +	glVertex2f(thumb_x + THUMB_WIDTH, thumb_y + THUMB_HEIGHT);
    1.99 +	glVertex2f(thumb_x, thumb_y + THUMB_HEIGHT);
   1.100 +	glEnd();
   1.101 +	imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET);
   1.102 +
   1.103 +	/* draw display */
   1.104 +	sprintf(buf, "%.3f", pos * range + min);
   1.105 +	glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
   1.106 +	imtk_draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf);
   1.107 +}
   1.108 +