imtk

annotate 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
rev   line source
nuclear@6 1 #include <assert.h>
nuclear@6 2 #include "imtk.h"
nuclear@6 3 #include "state.h"
nuclear@6 4 #include "draw.h"
nuclear@6 5
nuclear@6 6
nuclear@6 7 #define CHECKBOX_SIZE 14
nuclear@6 8
nuclear@6 9
nuclear@11 10 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over);
nuclear@6 11
nuclear@6 12 int imtk_checkbox(int id, const char *label, int x, int y, int state)
nuclear@6 13 {
nuclear@6 14 int sz = CHECKBOX_SIZE;
nuclear@6 15 int over = 0;
nuclear@6 16
nuclear@6 17 assert(id >= 0);
nuclear@6 18
nuclear@11 19 if(imtk_hit_test(x, y, sz + imtk_string_size(label) + 5, sz)) {
nuclear@6 20 imtk_set_hot(id);
nuclear@6 21 over = 1;
nuclear@6 22 }
nuclear@6 23
nuclear@6 24 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
nuclear@6 25 if(over) {
nuclear@6 26 imtk_set_active(id);
nuclear@6 27 }
nuclear@6 28 } else { /* mouse button up */
nuclear@6 29 if(imtk_is_active(id)) {
nuclear@6 30 imtk_set_active(-1);
nuclear@6 31 if(imtk_is_hot(id) && over) {
nuclear@6 32 state = !state;
nuclear@6 33 }
nuclear@6 34 }
nuclear@6 35 }
nuclear@6 36
nuclear@11 37 draw_checkbox(id, label, x, y, state, over);
nuclear@6 38 return state;
nuclear@6 39 }
nuclear@6 40
nuclear@11 41 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over)
nuclear@6 42 {
nuclear@6 43 static const int sz = CHECKBOX_SIZE;
nuclear@13 44 static float v[][2] = {
nuclear@13 45 {-0.2, 0.63}, /* 0 */
nuclear@13 46 {0.121, 0.325}, /* 1 */
nuclear@13 47 {0.15, 0.5}, /* 2 */
nuclear@13 48 {0.28, 0.125}, /* 3 */
nuclear@13 49 {0.38, 0.33}, /* 4 */
nuclear@13 50 {0.42, -0.122}, /* 5 */
nuclear@13 51 {0.58, 0.25}, /* 6 */
nuclear@13 52 {0.72, 0.52}, /* 7 */
nuclear@13 53 {0.625, 0.65}, /* 8 */
nuclear@13 54 {0.89, 0.78}, /* 9 */
nuclear@13 55 {0.875, 0.92}, /* 10 */
nuclear@13 56 {1.13, 1.145} /* 11 */
nuclear@13 57 };
nuclear@13 58 #define TRI(a, b, c) (glVertex2fv(v[a]), glVertex2fv(v[b]), glVertex2fv(v[c]))
nuclear@6 59
nuclear@11 60 if(over) {
nuclear@6 61 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
nuclear@6 62 } else {
nuclear@6 63 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
nuclear@6 64 }
nuclear@6 65
nuclear@8 66 imtk_draw_rect(x, y, sz, sz, 0);
nuclear@6 67 imtk_draw_frame(x, y, sz, sz, FRAME_INSET);
nuclear@6 68
nuclear@6 69 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
nuclear@6 70 if(state) {
nuclear@13 71 glMatrixMode(GL_MODELVIEW);
nuclear@13 72 glPushMatrix();
nuclear@13 73 glTranslatef(x, y + sz, 0);
nuclear@13 74 glScalef(sz * 1.2, -sz * 1.3, 1);
nuclear@6 75
nuclear@13 76 glBegin(GL_TRIANGLES);
nuclear@13 77 glColor4fv(imtk_get_color(IMTK_CHECK_COLOR));
nuclear@13 78 TRI(0, 1, 2);
nuclear@13 79 TRI(1, 3, 2);
nuclear@13 80 TRI(3, 4, 2);
nuclear@13 81 TRI(3, 5, 4);
nuclear@13 82 TRI(4, 5, 6);
nuclear@13 83 TRI(4, 6, 7);
nuclear@13 84 TRI(4, 7, 8);
nuclear@13 85 TRI(8, 7, 9);
nuclear@13 86 TRI(8, 9, 10);
nuclear@13 87 TRI(10, 9, 11);
nuclear@6 88 glEnd();
nuclear@6 89
nuclear@13 90 glPopMatrix();
nuclear@6 91 }
nuclear@6 92
nuclear@13 93 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
nuclear@6 94 imtk_draw_string(x + sz + 5, y + sz - 2, label);
nuclear@6 95 }
nuclear@6 96