1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
#include "base.h"
void gui_button_draw_fn( void* ptr ) {
GUI_BUTTON* btn = (GUI_BUTTON*)ptr;
I32 x = gui_relx( btn );
I32 y = gui_rely( btn );
I32 mx, my;
gui_cursor_pos( &mx, &my );
U8 inbounds = mx >= x && mx <= x + btn->w && my >= y && my <= y + btn->h;
U8 active = btn->held && inbounds && gui_mbutton_down( GUI_MBTNLEFT );
U8 hover = inbounds && !active;
CLR border = gui_is_fg_window( btn )? ui_clr.border : ui_clr.border_inactive;
if( active )
border = { 0.f, 1.f, 0.f, 1.f };
CLR fill = ui_clr.bg_sec;
CLR txt = ui_clr.txt;
if( hover ) {
fill = ui_clr.border;
txt = CLR::BLACK();
}
gui_draw_frect( x, y, btn->w, btn->h, border );
gui_draw_frect( x+1, y+1, btn->w-2, btn->h-2, fill );
I32 middle = x + btn->w/2;
I32 middle_y = y + btn->h/2 - 7;
gui_draw_str( middle, middle_y, ALIGN_C, FNT_JPN12, txt, btn->name );
}
void gui_button_input_fn( void* ptr ) {
GUI_BUTTON* btn = (GUI_BUTTON*)ptr;
I32 x = gui_relx( btn );
I32 y = gui_rely( btn );
U8 m1 = gui_mbutton_down( 0 );
I32 mx, my;
gui_cursor_pos( &mx, &my );
U8 inbounds = mx >= x && mx <= x + btn->w && my >= y && my <= y + btn->h;
if( !m1 ) {
// button could be destroyed by callback
U8 was_held = btn->held;
btn->held = 0;
if( inbounds && was_held )
btn->cb( btn );
return;
}
if( inbounds ) {
btn->held = 1;
}
}
GUI_BUTTON* gui_button( I32 x, I32 y, I32 w, I32 h, const char* title, GUI_CALLBACK cb ) {
if( !gui_check_target() ) return 0;
GUI_BUTTON* btn = new GUI_BUTTON;
btn->x = x;
btn->y = y;
btn->xbound = btn->w = w;
btn->ybound = btn->h = h;
btn->cb = cb;
btn->draw_fn = gui_button_draw_fn;
btn->input_fn = gui_button_input_fn;
btn->parent = _gui.cur_view;
strcpy( btn->name, title );
btn->extra = 0;
btn->held = 0;
gui_get_view()->children.push( btn );
return btn;
}
|