imtk

view src/slider.c @ 12:17c9525b2e35

merged -lGLU fix with listbox patch
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 17 Apr 2011 18:20:23 +0300
parents 6d35e6c7b2ca
children df2bc9406561
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include "imtk.h"
4 #include "state.h"
5 #include "draw.h"
7 #define SLIDER_SIZE 100
8 #define THUMB_WIDTH 10
9 #define THUMB_HEIGHT 20
11 static void draw_slider(int id, float pos, float min, float max, int x, int y);
13 float imtk_slider(int id, float pos, float min, float max, int x, int y)
14 {
15 int mousex, mousey, thumb_x, thumb_y, over = 0;
16 float range = max - min;
18 assert(id >= 0);
20 imtk_get_mouse(&mousex, &mousey);
22 pos = (pos - min) / range;
23 if(pos < 0.0) pos = 0.0;
24 if(pos > 1.0) pos = 1.0;
26 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
27 thumb_y = y - THUMB_HEIGHT / 2;
29 if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
30 imtk_set_hot(id);
31 over = 1;
32 }
34 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
35 if(over && imtk_is_hot(id)) {
36 if(!imtk_is_active(id)) {
37 imtk_set_prev_mouse(mousex, mousey);
38 }
39 imtk_set_active(id);
40 }
41 } else {
42 if(imtk_is_active(id)) {
43 imtk_set_active(-1);
44 }
45 }
47 if(imtk_is_active(id)) {
48 int prevx;
49 float dx;
51 imtk_get_prev_mouse(&prevx, 0);
53 dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
54 pos += dx;
55 imtk_set_prev_mouse(mousex, mousey);
57 if(pos < 0.0) pos = 0.0;
58 if(pos > 1.0) pos = 1.0;
59 }
61 draw_slider(id, pos, min, max, x, y);
62 return pos * range + min;
63 }
66 static void draw_slider(int id, float pos, float min, float max, int x, int y)
67 {
68 float range = max - min;
69 int thumb_x, thumb_y;
70 char buf[32];
72 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
73 thumb_y = y - THUMB_HEIGHT / 2;
75 /* draw trough */
76 imtk_draw_rect(x, y - 2, SLIDER_SIZE, 4, imtk_get_color(IMTK_BASE_COLOR));
77 imtk_draw_frame(x, y - 2, SLIDER_SIZE, 4, FRAME_INSET);
79 if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
80 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
81 } else {
82 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
83 }
85 /* draw handle */
86 imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, 0);
87 imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET);
89 /* draw display */
90 sprintf(buf, "%.3f", pos * range + min);
91 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
92 imtk_draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf);
93 }