imtk

view src/checkbox.c @ 14:df2bc9406561

added gradients
author John Tsiombikas <nuclear@siggraph.org>
date Tue, 19 Apr 2011 03:01:46 +0300
parents 9c7987064bb0
children c7a7ddbe7714
line source
1 #include <string.h>
2 #include <assert.h>
3 #include "imtk.h"
4 #include "state.h"
5 #include "draw.h"
8 #define CHECKBOX_SIZE 14
11 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over);
13 int imtk_checkbox(int id, const char *label, int x, int y, int state)
14 {
15 int sz = CHECKBOX_SIZE;
16 int over = 0;
18 assert(id >= 0);
20 if(imtk_hit_test(x, y, sz + imtk_string_size(label) + 5, sz)) {
21 imtk_set_hot(id);
22 over = 1;
23 }
25 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
26 if(over) {
27 imtk_set_active(id);
28 }
29 } else { /* mouse button up */
30 if(imtk_is_active(id)) {
31 imtk_set_active(-1);
32 if(imtk_is_hot(id) && over) {
33 state = !state;
34 }
35 }
36 }
38 draw_checkbox(id, label, x, y, state, over);
39 return state;
40 }
42 static float v[][2] = {
43 {-0.2, 0.63}, /* 0 */
44 {0.121, 0.325}, /* 1 */
45 {0.15, 0.5}, /* 2 */
46 {0.28, 0.125}, /* 3 */
47 {0.38, 0.33}, /* 4 */
48 {0.42, -0.122}, /* 5 */
49 {0.58, 0.25}, /* 6 */
50 {0.72, 0.52}, /* 7 */
51 {0.625, 0.65}, /* 8 */
52 {0.89, 0.78}, /* 9 */
53 {0.875, 0.92}, /* 10 */
54 {1.13, 1.145} /* 11 */
55 };
56 #define TRI(a, b, c) (glVertex2fv(v[a]), glVertex2fv(v[b]), glVertex2fv(v[c]))
58 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over)
59 {
60 static const int sz = CHECKBOX_SIZE;
61 unsigned int attr = 0;
62 float tcol[4], bcol[4];
64 if(over) {
65 attr |= IMTK_FOCUS_BIT;
66 }
67 if(imtk_is_active(id)) {
68 attr |= IMTK_PRESS_BIT;
69 }
70 memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol);
71 memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol);
73 imtk_draw_rect(x, y, sz, sz, tcol, bcol);
74 imtk_draw_frame(x, y, sz, sz, FRAME_INSET);
76 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
77 if(state) {
78 glMatrixMode(GL_MODELVIEW);
79 glPushMatrix();
80 glTranslatef(x, y + sz, 0);
81 glScalef(sz * 1.2, -sz * 1.3, 1);
83 glBegin(GL_TRIANGLES);
84 glColor4fv(imtk_get_color(IMTK_CHECK_COLOR));
85 TRI(0, 1, 2);
86 TRI(1, 3, 2);
87 TRI(3, 4, 2);
88 TRI(3, 5, 4);
89 TRI(4, 5, 6);
90 TRI(4, 6, 7);
91 TRI(4, 7, 8);
92 TRI(8, 7, 9);
93 TRI(8, 9, 10);
94 TRI(10, 9, 11);
95 glEnd();
97 glPopMatrix();
98 }
100 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
101 imtk_draw_string(x + sz + 5, y + sz - 2, label);
102 }