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