rev |
line source |
nuclear@14
|
1 #include <string.h>
|
nuclear@6
|
2 #include <assert.h>
|
nuclear@6
|
3 #include "imtk.h"
|
nuclear@6
|
4 #include "state.h"
|
nuclear@6
|
5 #include "draw.h"
|
nuclear@6
|
6
|
nuclear@6
|
7 static void calc_button_size(const char *label, int *wret, int *hret);
|
nuclear@14
|
8 static void draw_button(int id, const char *label, int x, int y, int over);
|
nuclear@6
|
9
|
nuclear@6
|
10 int imtk_button(int id, const char *label, int x, int y)
|
nuclear@6
|
11 {
|
nuclear@6
|
12 int w, h, res = 0;
|
nuclear@6
|
13 int over = 0;
|
nuclear@6
|
14
|
nuclear@6
|
15 assert(id >= 0);
|
nuclear@6
|
16
|
nuclear@20
|
17 if(x == IMTK_AUTO || y == IMTK_AUTO) {
|
nuclear@20
|
18 imtk_layout_get_pos(&x, &y);
|
nuclear@20
|
19 }
|
nuclear@20
|
20
|
nuclear@6
|
21 calc_button_size(label, &w, &h);
|
nuclear@6
|
22
|
nuclear@6
|
23 if(imtk_hit_test(x, y, w, h)) {
|
nuclear@6
|
24 imtk_set_hot(id);
|
nuclear@6
|
25 over = 1;
|
nuclear@6
|
26 }
|
nuclear@6
|
27
|
nuclear@6
|
28 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
|
nuclear@6
|
29 if(over) {
|
nuclear@6
|
30 imtk_set_active(id);
|
nuclear@6
|
31 }
|
nuclear@6
|
32 } else { /* mouse button up */
|
nuclear@6
|
33 if(imtk_is_active(id)) {
|
nuclear@6
|
34 imtk_set_active(-1);
|
nuclear@6
|
35 if(imtk_is_hot(id) && over) {
|
nuclear@6
|
36 res = 1;
|
nuclear@6
|
37 }
|
nuclear@6
|
38 }
|
nuclear@6
|
39 }
|
nuclear@6
|
40
|
nuclear@14
|
41 draw_button(id, label, x, y, over);
|
nuclear@20
|
42 imtk_layout_advance(w, h);
|
nuclear@6
|
43 return res;
|
nuclear@6
|
44 }
|
nuclear@6
|
45
|
nuclear@14
|
46 static void draw_button(int id, const char *label, int x, int y, int over)
|
nuclear@6
|
47 {
|
nuclear@14
|
48 float tcol[4], bcol[4];
|
nuclear@14
|
49 int width, height, active = imtk_is_active(id);
|
nuclear@14
|
50 unsigned int attr = 0;
|
nuclear@14
|
51
|
nuclear@14
|
52 if(over) attr |= IMTK_FOCUS_BIT;
|
nuclear@14
|
53 if(active) attr |= IMTK_PRESS_BIT;
|
nuclear@6
|
54
|
nuclear@6
|
55 calc_button_size(label, &width, &height);
|
nuclear@6
|
56
|
nuclear@14
|
57 memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol);
|
nuclear@14
|
58 memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol);
|
nuclear@6
|
59
|
nuclear@14
|
60 imtk_draw_rect(x, y, width, height, tcol, bcol);
|
nuclear@14
|
61 imtk_draw_frame(x, y, width, height, active ? FRAME_INSET : FRAME_OUTSET);
|
nuclear@6
|
62
|
nuclear@6
|
63 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
|
nuclear@6
|
64 imtk_draw_string(x + 20, y + 15, label);
|
nuclear@6
|
65 }
|
nuclear@6
|
66
|
nuclear@6
|
67 static void calc_button_size(const char *label, int *wret, int *hret)
|
nuclear@6
|
68 {
|
nuclear@6
|
69 int strsz = imtk_string_size(label);
|
nuclear@6
|
70 if(wret) *wret = strsz + 40;
|
nuclear@6
|
71 if(hret) *hret = 20;
|
nuclear@6
|
72 }
|