summaryrefslogtreecommitdiff
path: root/src/gui/list.cpp
diff options
context:
space:
mode:
authornavewindre <boneyaard@gmail.com>2025-09-03 20:10:09 +0200
committernavewindre <boneyaard@gmail.com>2025-09-03 20:10:09 +0200
commitf8b92ce3aa08b1445c9f956d8166830946562d12 (patch)
tree94e63a5aec9f8f52b577f56799e0c9201fd976a5 /src/gui/list.cpp
a
Diffstat (limited to 'src/gui/list.cpp')
-rw-r--r--src/gui/list.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/gui/list.cpp b/src/gui/list.cpp
new file mode 100644
index 0000000..90f2f49
--- /dev/null
+++ b/src/gui/list.cpp
@@ -0,0 +1,98 @@
+#include "base.h"
+
+const I32 LIST_TITLE_OFFSET = 15;
+const I32 LIST_ITEM_HEIGHT = 18;
+
+void gui_list_draw_fn( void* ptr ) {
+ GUI_LIST* list = (GUI_LIST*)ptr;
+
+ I32 x = gui_relx( list );
+ I32 y = gui_rely( list );
+
+ gui_draw_str( x, y, ALIGN_L, FNT_JPN12, ui_clr.txt, list->name );
+ y += LIST_TITLE_OFFSET;
+
+ CLR col = gui_is_fg_window( list )? ui_clr.border : ui_clr.border_inactive;
+ gui_draw_frect( x, y, list->w, list->h, col );
+ gui_draw_frect( x+1, y+1, list->w-2, list->h-2, ui_clr.bg_sec );
+
+ I32 yoff = 0;
+ list->plist->each( fn( GUI_LIST_ENTRY* e ) {
+ U8 selected = e->val == *list->pval;
+ CLR col = selected? ui_clr.txt : ui_clr.txt_inactive;
+
+ gui_draw_str( x + 4, y + yoff + 6, ALIGN_L, FNT_JPN12, col, e->title );
+ yoff += LIST_ITEM_HEIGHT;
+ } );
+}
+
+void gui_list_input_fn( void* ptr ) {
+ GUI_LIST* list = (GUI_LIST*)ptr;
+
+ I32 x = gui_relx( list );
+ I32 y = gui_rely( list );
+ I32 w = list->w;
+ I32 h = list->h;
+
+ U8 m1 = gui_mbutton_down( 0 );
+ I32 mx, my;
+ gui_cursor_pos( &mx, &my );
+
+ U8 inbounds = mx >= x && mx <= x + w && my >= y && my <= y + h;
+ if( !inbounds )
+ return;
+
+ I32 diff = my - y;
+ I32 idx = diff / LIST_ITEM_HEIGHT - 1;
+ if( idx >= list->plist->size ) idx = list->plist->size - 1;
+ if( idx < 0 ) idx = 0;
+
+ if( m1 ) {
+ if( !list->held ) {
+ GUI_LIST_ENTRY* e = &list->plist->data[idx];
+ I32 prevval = *list->pval;
+ *list->pval = e->val;
+
+ if( list->cb && prevval != *list->pval )
+ list->cb( list );
+ }
+ list->held = 1;
+ } else {
+ list->held = 0;
+ }
+}
+
+GUI_LIST* gui_list( I32 x, I32 y, I32 w, I32 h, const char* title, LIST<GUI_LIST_ENTRY>* plist, I32* pval ) {
+ if( !gui_check_target() ) return 0;
+
+ GUI_LIST* list = new GUI_LIST;
+ list->x = x;
+ list->y = y;
+ list->w = w;
+ list->h = h;
+
+ list->xbound = w;
+ list->ybound = h + LIST_TITLE_OFFSET;
+
+ list->cb = 0;
+ list->pval = pval;
+ list->plist = plist;
+ list->draw_fn = gui_list_draw_fn;
+ list->input_fn = gui_list_input_fn;
+ strcpy( list->name, title );
+
+ list->parent = gui_get_view();
+
+ gui_get_view()->children.push( list );
+ return list;
+}
+
+GUI_LIST_ENTRY* gui_list_get_selected( GUI_LIST* list ) {
+ for( U32 i = 0; i < list->plist->size; ++i ) {
+ GUI_LIST_ENTRY* e = &list->plist->data[i];
+ if( e->val == *list->pval )
+ return e;
+ }
+
+ return 0;
+}