8 #define SLIDER_SIZE 100
10 #define THUMB_HEIGHT 20
12 static int draw_slider(int id, float pos, float min, float max, int x, int y, int over);
14 float imtk_slider(int id, float pos, float min, float max, int x, int y)
16 int mousex, mousey, thumb_x, thumb_y, txsz, over = 0;
17 float range = max - min;
21 if(x == IMTK_AUTO || y == IMTK_AUTO) {
22 imtk_layout_get_pos(&x, &y);
24 y += THUMB_HEIGHT / 2;
26 imtk_get_mouse(&mousex, &mousey);
28 pos = (pos - min) / range;
29 if(pos < 0.0) pos = 0.0;
30 if(pos > 1.0) pos = 1.0;
32 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
33 thumb_y = y - THUMB_HEIGHT / 2;
35 if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
40 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
41 if(over && imtk_is_hot(id)) {
42 if(!imtk_is_active(id)) {
43 imtk_set_prev_mouse(mousex, mousey);
48 if(imtk_is_active(id)) {
53 if(imtk_is_active(id)) {
57 imtk_get_prev_mouse(&prevx, 0);
59 dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
61 imtk_set_prev_mouse(mousex, mousey);
63 if(pos < 0.0) pos = 0.0;
64 if(pos > 1.0) pos = 1.0;
67 txsz = draw_slider(id, pos, min, max, x, y, over);
68 imtk_layout_advance(SLIDER_SIZE + THUMB_WIDTH + txsz, THUMB_HEIGHT);
69 return pos * range + min;
73 static int draw_slider(int id, float pos, float min, float max, int x, int y, int over)
75 float range = max - min;
78 float tcol[4], bcol[4];
79 unsigned int attr = 0;
81 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
82 thumb_y = y - THUMB_HEIGHT / 2;
84 memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof tcol);
85 memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR), sizeof bcol);
88 imtk_draw_rect(x, y - 2, SLIDER_SIZE, 5, tcol, bcol);
89 imtk_draw_frame(x, y - 2, SLIDER_SIZE, 5, FRAME_INSET);
92 attr |= IMTK_FOCUS_BIT;
94 if(imtk_is_active(id)) {
95 attr |= IMTK_PRESS_BIT;
97 memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol);
98 memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol);
101 imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, tcol, bcol);
102 imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET);
105 sprintf(buf, "%.3f", pos * range + min);
106 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
107 imtk_draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf);
108 return imtk_string_size(buf);