#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; CLR fill = ui_clr.bg_sec; CLR txt = ui_clr.txt; if( active ) { fill = CLR::blend( ui_clr.bg_sec, ui_clr.border, 0.22f ); } 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; }