diff options
| author | navewindre <boneyaard@gmail.com> | 2025-09-03 20:10:09 +0200 |
|---|---|---|
| committer | navewindre <boneyaard@gmail.com> | 2025-09-03 20:10:09 +0200 |
| commit | f8b92ce3aa08b1445c9f956d8166830946562d12 (patch) | |
| tree | 94e63a5aec9f8f52b577f56799e0c9201fd976a5 /src/gui/button.cpp | |
a
Diffstat (limited to 'src/gui/button.cpp')
| -rw-r--r-- | src/gui/button.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/gui/button.cpp b/src/gui/button.cpp new file mode 100644 index 0000000..2b57772 --- /dev/null +++ b/src/gui/button.cpp @@ -0,0 +1,66 @@ +#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 ); + + CLR col = gui_is_fg_window( btn )? ui_clr.border : ui_clr.border_inactive; + gui_draw_frect( x, y, btn->w, btn->h, col ); + gui_draw_frect( x+1, y+1, btn->w-2, btn->h-2, ui_clr.bg_sec ); + + I32 middle = x + btn->w/2; + I32 middle_y = y + btn->h/2 - 7; + + gui_draw_str( middle, middle_y, ALIGN_C, FNT_JPN12, ui_clr.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; +} |
