imtk
view src/button.c @ 14:df2bc9406561
added gradients
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Tue, 19 Apr 2011 03:01:46 +0300 |
parents | 10604ff95527 |
children | c7a7ddbe7714 |
line source
1 #include <string.h>
2 #include <assert.h>
3 #include "imtk.h"
4 #include "state.h"
5 #include "draw.h"
7 static void calc_button_size(const char *label, int *wret, int *hret);
8 static void draw_button(int id, const char *label, int x, int y, int over);
10 int imtk_button(int id, const char *label, int x, int y)
11 {
12 int w, h, res = 0;
13 int over = 0;
15 assert(id >= 0);
17 calc_button_size(label, &w, &h);
19 if(imtk_hit_test(x, y, w, h)) {
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 res = 1;
33 }
34 }
35 }
37 draw_button(id, label, x, y, over);
38 return res;
39 }
41 static void draw_button(int id, const char *label, int x, int y, int over)
42 {
43 float tcol[4], bcol[4];
44 int width, height, active = imtk_is_active(id);
45 unsigned int attr = 0;
47 if(over) attr |= IMTK_FOCUS_BIT;
48 if(active) attr |= IMTK_PRESS_BIT;
50 calc_button_size(label, &width, &height);
52 memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol);
53 memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol);
55 imtk_draw_rect(x, y, width, height, tcol, bcol);
56 imtk_draw_frame(x, y, width, height, active ? FRAME_INSET : FRAME_OUTSET);
58 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
59 imtk_draw_string(x + 20, y + 15, label);
60 }
62 static void calc_button_size(const char *label, int *wret, int *hret)
63 {
64 int strsz = imtk_string_size(label);
65 if(wret) *wret = strsz + 40;
66 if(hret) *hret = 20;
67 }