imtk

view src/checkbox.c @ 13:9c7987064bb0

- fixed the frame drawing a bit - added global alpha value and various drawing parameters - backported the checkbox check mark from glamtk - fixed progress bar drawing so that the bevels of the trough and the bar won't overlap
author John Tsiombikas <nuclear@siggraph.org>
date Mon, 18 Apr 2011 06:15:46 +0300
parents eae09a1dca1d
children df2bc9406561
line source
1 #include <assert.h>
2 #include "imtk.h"
3 #include "state.h"
4 #include "draw.h"
7 #define CHECKBOX_SIZE 14
10 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over);
12 int imtk_checkbox(int id, const char *label, int x, int y, int state)
13 {
14 int sz = CHECKBOX_SIZE;
15 int over = 0;
17 assert(id >= 0);
19 if(imtk_hit_test(x, y, sz + imtk_string_size(label) + 5, sz)) {
20 imtk_set_hot(id);
21 over = 1;
22 }
24 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
25 if(over) {
26 imtk_set_active(id);
27 }
28 } else { /* mouse button up */
29 if(imtk_is_active(id)) {
30 imtk_set_active(-1);
31 if(imtk_is_hot(id) && over) {
32 state = !state;
33 }
34 }
35 }
37 draw_checkbox(id, label, x, y, state, over);
38 return state;
39 }
41 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over)
42 {
43 static const int sz = CHECKBOX_SIZE;
44 static float v[][2] = {
45 {-0.2, 0.63}, /* 0 */
46 {0.121, 0.325}, /* 1 */
47 {0.15, 0.5}, /* 2 */
48 {0.28, 0.125}, /* 3 */
49 {0.38, 0.33}, /* 4 */
50 {0.42, -0.122}, /* 5 */
51 {0.58, 0.25}, /* 6 */
52 {0.72, 0.52}, /* 7 */
53 {0.625, 0.65}, /* 8 */
54 {0.89, 0.78}, /* 9 */
55 {0.875, 0.92}, /* 10 */
56 {1.13, 1.145} /* 11 */
57 };
58 #define TRI(a, b, c) (glVertex2fv(v[a]), glVertex2fv(v[b]), glVertex2fv(v[c]))
60 if(over) {
61 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
62 } else {
63 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
64 }
66 imtk_draw_rect(x, y, sz, sz, 0);
67 imtk_draw_frame(x, y, sz, sz, FRAME_INSET);
69 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
70 if(state) {
71 glMatrixMode(GL_MODELVIEW);
72 glPushMatrix();
73 glTranslatef(x, y + sz, 0);
74 glScalef(sz * 1.2, -sz * 1.3, 1);
76 glBegin(GL_TRIANGLES);
77 glColor4fv(imtk_get_color(IMTK_CHECK_COLOR));
78 TRI(0, 1, 2);
79 TRI(1, 3, 2);
80 TRI(3, 4, 2);
81 TRI(3, 5, 4);
82 TRI(4, 5, 6);
83 TRI(4, 6, 7);
84 TRI(4, 7, 8);
85 TRI(8, 7, 9);
86 TRI(8, 9, 10);
87 TRI(10, 9, 11);
88 glEnd();
90 glPopMatrix();
91 }
93 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
94 imtk_draw_string(x + sz + 5, y + sz - 2, label);
95 }