From f8b92ce3aa08b1445c9f956d8166830946562d12 Mon Sep 17 00:00:00 2001 From: navewindre Date: Wed, 3 Sep 2025 20:10:09 +0200 Subject: a --- src/gui/base.h | 314 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 314 insertions(+) create mode 100644 src/gui/base.h (limited to 'src/gui/base.h') diff --git a/src/gui/base.h b/src/gui/base.h new file mode 100644 index 0000000..96e0fa5 --- /dev/null +++ b/src/gui/base.h @@ -0,0 +1,314 @@ +#pragma once +#include "../util/color.h" +#include "../util/allocator.h" + +// ======================================= [ colorscheme ] ======================================== + +static const struct { + CLR txt = CLR::WHITE(); + CLR txt_inactive = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.3f ); + CLR border = CLR::WHITE(); + CLR border_inactive = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.55f ); + CLR bg = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.92f ); + CLR bg_sec = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.97f ); + CLR bg_alt = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.985f ); +} ui_clr; + +// ========================================== [ base ] ============================================ + +const U32 GUI_STR_BUF_MAX = 16384; +const U32 GUI_TEXTBOX_MAX = 1024; +const U32 GUI_NAME_LEN = 512; + +/* impl */ +extern void gui_init( struct GAME_DATA* game ); +extern void gui_draw( struct GAME_DATA* game ); +extern void gui_input( struct GAME_DATA* game ); +extern void gui_onframe( struct GAME_DATA* game ); +extern void gui_end(); +/* */ + +extern I32 gui_relx( struct GUI_BASE* node ); //relative x pos for node +extern I32 gui_rely( struct GUI_BASE* node ); //relative y pos for node + +extern struct GUI_BASE* gui_find_node( struct GUI_BASE* root, const char* name ); +extern struct GUI_WINDOW* gui_get_parent_wnd( struct GUI_BASE* node ); +extern void gui_empty_children( struct GUI_BASE* node ); + +extern void gui_free( struct GUI_BASE* node ); + +extern struct GUI_VIEW* gui_get_view(); // gets currently edited view +extern void gui_set_view( struct GUI_VIEW* view ); // sets currently edited view +extern struct GUI_WINDOW* gui_get_window(); // gets currently edited window +extern void gui_set_window( struct GUI_WINDOW* wnd ); // sets currently edited window +extern void gui_bring_to_top( struct GUI_WINDOW* wnd ); // pushes window to be rendered last +extern U8 gui_is_fg_window( struct GUI_BASE* node ); // 1 if parent window (or window itself) is rendered last + +// ======================================= [ components ] ========================================= + +struct GUI_LIST_ENTRY { + I32 val; + char title[256]; +}; + +typedef void( *GUI_CALLBACK )( void* ptr ); + +extern struct GUI_VIEW* gui_view( I32 x, I32 y, I32 w, I32 h ); // sets current view +extern struct GUI_WINDOW* gui_window( I32 w, I32 h ); // sets current view and window +extern struct GUI_WINDOW* gui_window( I32 x, I32 y, I32 w, I32 h ); // sets current view and window +extern struct GUI_TITLE* gui_title( const char* title ); + +extern struct GUI_LABEL* gui_label( I32 x, I32 y, const char* title, ... ); +extern struct GUI_BUTTON* gui_button( I32 x, I32 y, I32 w, I32 h, const char* title, GUI_CALLBACK cb ); +extern struct GUI_TEXTBOX* gui_textbox( I32 x, I32 y, I32 w, I32 h, const char* title, U32 maxlen, U8 allow_newl = 0 ); +extern struct GUI_CHECKBOX* gui_checkbox( I32 x, I32 y, const char* title, U8* pval ); + +extern struct GUI_LIST* gui_list( I32 x, I32 y, I32 w, I32 h, const char* title, LIST* list, I32* pval ); +extern struct GUI_LIST_ENTRY* gui_list_get_selected( struct GUI_LIST* list ); + +extern struct GUI_FLOATINPUT* gui_floatinput( + I32 x, I32 y, I32 w, + const char* title, + F32* pval, + F32 min, + F32 max, + F32 step = 1.f, + const char* printfmt = "%.2f" +); + +extern struct GUI_VECTORINPUT* gui_vectorinput( + I32 x, I32 y, I32 w, + const char* title, + F32* pval, + U32 valc, + F32 min, + F32 max, + F32 step = 1.f, + const char* letters = "xyzw", + const char* printfmt = "%.2f" +); + +extern struct GUI_COLORINPUT* gui_colorinput( + I32 x, + I32 y, + I32 w, + const char* title, + CLR* pval, + U8 showalpha = 1 +); + +// ======================================= [ definitions ] ======================================== + +enum GuiTxtAlign_t { + ALIGN_L, + ALIGN_R, + ALIGN_C +}; + +enum GuiFont_t { + FNT_JPN12, + FNT_JPN16, + FNT_LAST +}; + +typedef void( *GUI_DRAW_FN )( void* el ); +typedef void( *GUI_INPUT_FN )( void* el ); + +extern void gui_base_input_fn( void* ); + +struct GUI_BASE { + I32 x{}, y{}; + I32 w{}, h{}; + U8 enabled{1}; + U8 canvas{}; + + char name[GUI_NAME_LEN]; + + I32 xbound{}, ybound{}; + I32 xoff{}, yoff{}; + + GUI_BASE* parent{}; + LIST children{}; + + GUI_DRAW_FN draw_fn{}; + GUI_INPUT_FN input_fn = gui_base_input_fn; +}; + +struct GUI_VIEW : GUI_BASE { + U8 initheld; +}; +struct GUI_WINDOW : GUI_VIEW { + U8 locked{}; + U8 ontop{}; +}; + +struct GUI_LABEL : GUI_BASE {}; + +struct GUI_TITLE : GUI_BASE { + I32 xoff, yoff; + U8 held{}; +}; + +struct GUI_LIST : GUI_BASE { + LIST* plist; + I32* pval; + U8 held; + + GUI_CALLBACK cb; +}; + +struct GUI_BUTTON : GUI_BASE { + GUI_CALLBACK cb; + U8 held; + void* extra; +}; + +struct GUI_CHECKBOX : GUI_BASE { + U8 held; + + U8* pval; + GUI_CALLBACK cb; +}; + +struct GUI_TEXTBOX : GUI_BASE { + char value[GUI_TEXTBOX_MAX]; + + U32 len; + U32 maxlen; + + U8 keys[0xff]; + U8 prevkeys[0xff]; + + U8 heldkey; + F32 heldtime; + F32 repeattime; + + F32 blinktime; + U8 blink; + + U8 allow_newl; + U8 active; + + U8 m1held; + + GUI_CALLBACK cb; +}; + +struct GUI_FLOATINPUT : GUI_BASE { + const char* valfmt; + F32* pval; + + F32 min; + F32 max; + F32 step; + + I32 lastmx; + U8 heldoutbounds; + U8 held; + + // for color sliders + CLR bgcol; + U8 customclr; + + U8 wraparound; + GUI_CALLBACK cb; +}; + +struct GUI_VECTORINPUT : GUI_VIEW { + const char* valfmt; + F32* pval; + U32 valc; + + const char* letters; + F32 min; + F32 max; + F32 step; + + GUI_VIEW* inputview; + LIST inputs; + + GUI_CALLBACK cb; +}; + +struct GUI_COLORINPUT : GUI_VECTORINPUT {}; + +// ======================================== [ internal ] ========================================== + +extern void gui_push_callback( GUI_CALLBACK cb ); +extern void gui_push_callback( void* data, GUI_CALLBACK cb ); +extern void gui_run_callbacks(); + +extern U8 gui_check_target(); + +extern void gui_draw_line( I32 x0, I32 y0, I32 x1, I32 y1, CLR col ); +extern void gui_draw_rect( I32 x, I32 y, I32 w, I32 h, CLR col ); +extern void gui_draw_frect( I32 x, I32 y, I32 w, I32 h, CLR col ); +extern void gui_draw_circle( I32 x, I32 y, I32 r, CLR col ); + +extern void gui_draw_str( I32 x, I32 y, U8 align, U32 fnt, CLR col, const char* fmt, ... ); +extern void gui_draw_get_str_bounds( I32* w, I32* h, U32 fnt, const char* fmt, ... ); + +extern void gui_draw_get_clip( I32* x, I32* y, I32* w, I32* h ); +extern void gui_draw_set_clip( I32 x, I32 y, I32 w, I32 h ); +extern void gui_draw_reset_clip(); +extern void gui_draw_push_clip( I32 x, I32 y, I32 w, I32 h ); +extern void gui_draw_pop_clip(); + +extern F32 gui_frametime(); +extern F32 gui_time(); +extern void gui_cursor_pos( I32* x, I32* y ); +extern U8 gui_key_down( U8 key ); +extern U8 gui_mbutton_down( U8 button ); +extern void gui_capture_scroll(); + +enum __gui_internal_mouse_button { + GUI_MBTNLEFT, + GUI_MBTNRIGHT, + GUI_MBTNMIDDLE, + GUI_MBTNSCROLL +}; + +struct __gui_internal { + GUI_VIEW* cur_view; + GUI_WINDOW* cur_window; + LIST windows; + struct clip_rect { + I32 x,y,w,h; + }; + + LIST clip_rects; + struct callback_entry { + GUI_CALLBACK callback; + void* data; + }; + + LIST callbacks; + + struct __font { + struct GL_FONT* glfnt; + }; + + struct { + __font jpn12; + __font jpn16; + } fonts; + + // draw stuff below + struct GL_SHADER_PROGRAM* gl2d; + struct GL_SHADER_PROGRAM* gl2d_font; +}; + +void __gui_internal_vectorinput_init( + GUI_VECTORINPUT* input, + I32 x, I32 y, I32 w, + const char* title, + F32* pval, + U32 valc, + F32 min, + F32 max, + F32 step, + const char* letters, + const char* printfmt +); + +extern __gui_internal _gui; -- cgit v1.2.3