- added libdrawtext
[demo_prior] / src / imtk / slider.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "imtk.h"
5 #include "state.h"
6 #include "draw.h"
7
8 #define SLIDER_SIZE             100
9 #define THUMB_WIDTH             10
10 #define THUMB_HEIGHT    20
11
12 static int draw_slider(int id, float pos, float min, float max, int x, int y, int over);
13
14 float imtk_slider(int id, float pos, float min, float max, int x, int y)
15 {
16         int mousex, mousey, thumb_x, thumb_y, txsz, over = 0;
17         float range = max - min;
18
19         assert(id >= 0);
20
21         if(x == IMTK_AUTO || y == IMTK_AUTO) {
22                 imtk_layout_get_pos(&x, &y);
23         }
24         y += THUMB_HEIGHT / 2;
25
26         imtk_get_mouse(&mousex, &mousey);
27
28         pos = (pos - min) / range;
29         if(pos < 0.0) pos = 0.0;
30         if(pos > 1.0) pos = 1.0;
31
32         thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
33         thumb_y = y - THUMB_HEIGHT / 2;
34
35         if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
36                 imtk_set_hot(id);
37                 over = 1;
38         }
39
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);
44                         }
45                         imtk_set_active(id);
46                 }
47         } else {
48                 if(imtk_is_active(id)) {
49                         imtk_set_active(-1);
50                 }
51         }
52
53         if(imtk_is_active(id)) {
54                 int prevx;
55                 float dx;
56
57                 imtk_get_prev_mouse(&prevx, 0);
58
59                 dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
60                 pos += dx;
61                 imtk_set_prev_mouse(mousex, mousey);
62
63                 if(pos < 0.0) pos = 0.0;
64                 if(pos > 1.0) pos = 1.0;
65         }
66
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;
70 }
71
72
73 static int draw_slider(int id, float pos, float min, float max, int x, int y, int over)
74 {
75         float range = max - min;
76         int thumb_x, thumb_y;
77         char buf[32];
78         float tcol[4], bcol[4];
79         unsigned int attr = 0;
80
81         thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
82         thumb_y = y - THUMB_HEIGHT / 2;
83
84         memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof tcol);
85         memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR), sizeof bcol);
86
87         /* draw trough */
88         imtk_draw_rect(x, y - 2, SLIDER_SIZE, 5, tcol, bcol);
89         imtk_draw_frame(x, y - 2, SLIDER_SIZE, 5, FRAME_INSET);
90
91         if(over) {
92                 attr |= IMTK_FOCUS_BIT;
93         }
94         if(imtk_is_active(id)) {
95                 attr |= IMTK_PRESS_BIT;
96         }
97         memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol);
98         memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol);
99
100         /* draw handle */
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);
103
104         /* draw display */
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);
109 }
110