imtk

view src/slider.c @ 22:17f5ff624da3

minor fixes in the layout system
author John Tsiombikas <nuclear@siggraph.org>
date Sat, 30 Apr 2011 06:12:51 +0300
parents c7a7ddbe7714
children
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "imtk.h"
5 #include "state.h"
6 #include "draw.h"
8 #define SLIDER_SIZE 100
9 #define THUMB_WIDTH 10
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)
15 {
16 int mousex, mousey, thumb_x, thumb_y, txsz, over = 0;
17 float range = max - min;
19 assert(id >= 0);
21 if(x == IMTK_AUTO || y == IMTK_AUTO) {
22 imtk_layout_get_pos(&x, &y);
23 }
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)) {
36 imtk_set_hot(id);
37 over = 1;
38 }
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 }
53 if(imtk_is_active(id)) {
54 int prevx;
55 float dx;
57 imtk_get_prev_mouse(&prevx, 0);
59 dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
60 pos += dx;
61 imtk_set_prev_mouse(mousex, mousey);
63 if(pos < 0.0) pos = 0.0;
64 if(pos > 1.0) pos = 1.0;
65 }
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 }
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;
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);
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);
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);
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);
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 }