imtk

view 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 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 prevx = mousex;
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 glBegin(GL_QUADS);
77 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
78 glVertex2f(x, y - 2);
79 glVertex2f(x + SLIDER_SIZE, y - 2);
80 glVertex2f(x + SLIDER_SIZE, y + 2);
81 glVertex2f(x, y + 2);
82 glEnd();
83 imtk_draw_frame(x, y - 2, SLIDER_SIZE, 4, FRAME_INSET);
85 if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
86 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
87 } else {
88 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
89 }
91 /* draw handle */
92 glBegin(GL_QUADS);
93 glVertex2f(thumb_x, thumb_y);
94 glVertex2f(thumb_x + THUMB_WIDTH, thumb_y);
95 glVertex2f(thumb_x + THUMB_WIDTH, thumb_y + THUMB_HEIGHT);
96 glVertex2f(thumb_x, thumb_y + THUMB_HEIGHT);
97 glEnd();
98 imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET);
100 /* draw display */
101 sprintf(buf, "%.3f", pos * range + min);
102 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
103 imtk_draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf);
104 }