From d72fb81ebf602b216d271a66d8e9c5931de5b75d Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Sun, 15 Jun 2025 14:29:22 +0200 Subject: [PATCH] Some stuff, I dunno --- game/res/shaders/uiwidget.frag | 10 +-- game/src/ui/uiman.c | 22 ++---- game/src/ui/widget.c | 138 +++++++++++---------------------- game/src/ui/widget.h | 22 ++++-- 4 files changed, 70 insertions(+), 122 deletions(-) diff --git a/game/res/shaders/uiwidget.frag b/game/res/shaders/uiwidget.frag index a4f1fbd..c1276a8 100644 --- a/game/res/shaders/uiwidget.frag +++ b/game/res/shaders/uiwidget.frag @@ -3,15 +3,16 @@ precision mediump float; uniform bool has_background_image; -uniform vec4 background_color; +uniform bool has_icon; +uniform bool has_scrollbar; + +uniform vec2 widget_size; uniform float border_width; uniform vec4 border_color; -uniform vec2 widget_size; -uniform bool has_icon; +uniform vec4 background_color; -uniform bool has_scrollbar; uniform vec2 scrollbar_position; uniform vec2 scrollbar_size; @@ -54,7 +55,6 @@ void main () if (has_icon) { vec4 fg = texture2D (icons, f_icon_uv); - gl_FragColor = mix (base_color, fg, fg.a); base_color = mix (base_color, fg, fg.a); } diff --git a/game/src/ui/uiman.c b/game/src/ui/uiman.c index cd088b3..1c22a89 100644 --- a/game/src/ui/uiman.c +++ b/game/src/ui/uiman.c @@ -48,18 +48,6 @@ uiman_init () Widget_add_child_weighed (chkboxes, Widget_composite_checkbox_make ("Wireframe", "wireframe_enabled"), 1); Widget_add_child_weighed (chkboxes, Widget_composite_checkbox_make ("Flight", "flight_enabled"), 1); Widget_add_child_weighed (debug_menu, chkboxes, 1); - - Widget *content = Widget_plain_new (); - content->background_image = RESTEX ("terrain1"); - VEC2_SETI (content->size, 400, 600); - - Widget *scrollable_window = Widget_composite_scrollable_make (content); - scrollable_window->background_image = RESTEX ("terrain4"); - VEC2_SETI (scrollable_window->position, 390, 32); - VEC2_SETI (scrollable_window->size, 350, 350); - scrollable_window->border_width = 4; - VEC4_SETI (scrollable_window->border_color, 0, 0, 1, 1); - Widget_add_child (ui_root, scrollable_window); } void @@ -111,21 +99,21 @@ uiman_cb_mouse_motion (SDL_Event *event, void *data) { UNUSED (event, data); - Widget *widget = Widget_find_xy (ui_root, (vec2) { event->motion.x, event->motion.y }); + Widget *new_hover = Widget_find_xy (ui_root, (vec2) { event->motion.x, event->motion.y }); - if (widget != hovered_widget) + if (new_hover != hovered_widget) { if (hovered_widget && hovered_widget->on_mouse_leave) { hovered_widget->on_mouse_leave (hovered_widget, hovered_widget->mouse_leave_data); } - if (widget && widget->on_mouse_enter) + if (new_hover && new_hover->on_mouse_enter) { - widget->on_mouse_enter (widget, widget->mouse_enter_data); + new_hover->on_mouse_enter (new_hover, new_hover->mouse_enter_data); } - hovered_widget = widget; + hovered_widget = new_hover; } if (hovered_widget && hovered_widget->on_mouse_move) diff --git a/game/src/ui/widget.c b/game/src/ui/widget.c index 5cdf04c..6210a89 100644 --- a/game/src/ui/widget.c +++ b/game/src/ui/widget.c @@ -51,14 +51,23 @@ Widget_scrollable_new () } Widget * -Widget_scrollbar_new (Widget *scrollable, gboolean horizontal) +Widget_scrollbar_new (Widget *target, gboolean horizontal) { Widget *e = ui_el_new_internal (WIDGET_SCROLLBAR); - e->scrollbar.scrollable = scrollable; + e->scrollbar.target = target; e->scrollbar.horizontal = horizontal; return e; } +Widget * +Widget_multiline_new (const char *text, TextAlignment alignment) +{ + Widget *e = ui_el_new_internal (WIDGET_MULTILINE); + e->multiline.text = g_string_new (text); + e->multiline.alignment = alignment; + return e; +} + void Widget_add_child (Widget *parent, Widget *child) { @@ -423,7 +432,7 @@ Widget_render (Widget *widget, mat4 projection, int window_height) /* Calculate the scrollbar size. */ unsigned int idx = widget->scrollbar.horizontal ? 0 : 1; - Widget *scr = widget->scrollbar.scrollable; + Widget *scr = widget->scrollbar.target; float content_size = scr->scrollable.content_size[idx]; float scroll_pos = scr->scrollable.scroll_position[idx]; @@ -561,11 +570,11 @@ uicb_decrement_int (Widget *self, void *userdata) } void -uicb_scrollable_scroll_down (Widget *self, void *userdata) +uicb_set_background (Widget *self, void *userdata) { UNUSED (self); - Widget *scrollable = userdata; - Widget_scrollable_scroll (scrollable, (vec2) { 0.0, 1.0 }); + vec4 *color = userdata; + glm_vec4_copy (*color, self->background_color); } void @@ -577,19 +586,11 @@ uicb_scrollable_scroll_up (Widget *self, void *userdata) } void -uicb_scrollable_scroll_left (Widget *self, void *userdata) +uicb_scrollable_scroll_down (Widget *self, void *userdata) { UNUSED (self); Widget *scrollable = userdata; - Widget_scrollable_scroll (scrollable, (vec2) { -1.0, 0.0 }); -} - -void -uicb_scrollable_scroll_right (Widget *self, void *userdata) -{ - UNUSED (self); - Widget *scrollable = userdata; - Widget_scrollable_scroll (scrollable, (vec2) { 1.0, 0.0 }); + Widget_scrollable_scroll (scrollable, (vec2) { 0.0, 1.0 }); } void @@ -597,9 +598,9 @@ uicb_scrollbar_start_scroll (Widget *self, void *userdata) { UNUSED (userdata); - Widget *scr = self->scrollbar.scrollable; + Widget *scr = self->scrollbar.target; vec2 mouse_pos; - SDL_GetMouseState ((float*)&mouse_pos[0], (float*)&mouse_pos[1]); + SDL_GetMouseState ((float*) &mouse_pos[0], (float*) &mouse_pos[1]); unsigned int idx = self->scrollbar.horizontal ? 0 : 1; @@ -636,7 +637,7 @@ uicb_scrollbar_scroll (Widget *self, void *userdata) return; } - Widget *scr = self->scrollbar.scrollable; + Widget *scr = self->scrollbar.target; vec2 mouse_pos; SDL_GetMouseState((float*) &mouse_pos[0], (float*) &mouse_pos[1]); @@ -695,6 +696,21 @@ Widget_composite_spinner_make_int (const char *fmt, int *ptr) right->icon = ICON_SPINNER_RIGHT; right->aspect_ratio = 1.0; + vec4 *enter_color = g_new (vec4, 1); + glm_vec4_copy ((vec4) { 1, 1, 1, 0.3 }, *enter_color); + vec4 *leave_color = g_new (vec4, 1); + glm_vec4_copy ((vec4) { 1, 1, 1, 0.0 }, *leave_color); + + left->on_mouse_enter = uicb_set_background; + left->on_mouse_leave = uicb_set_background; + right->on_mouse_enter = uicb_set_background; + right->on_mouse_leave = uicb_set_background; + + left->mouse_enter_data = enter_color; + left->mouse_leave_data = leave_color; + right->mouse_enter_data = enter_color; + right->mouse_leave_data = leave_color; + Widget_add_child_weighed (cont, left, 1); Widget_add_child_weighed (cont, label, 1); Widget_add_child_weighed (cont, right, 1); @@ -714,82 +730,20 @@ Widget_composite_checkbox_make (const char *label, char *flag) box->aspect_ratio = 1.0; Widget_set_on_tick (box, uicb_checkbox_show_flag, flag); Widget_set_on_left_up (box, uicb_switch_flag, flag); + + box->on_mouse_enter = uicb_set_background; + box->on_mouse_leave = uicb_set_background; + vec4 *enter_color = g_new (vec4, 1); + glm_vec4_copy ((vec4) { 1, 1, 1, 0.3 }, *enter_color); + vec4 *leave_color = g_new (vec4, 1); + glm_vec4_copy ((vec4) { 1, 1, 1, 0.0 }, *leave_color); + + box->mouse_enter_data = enter_color; + box->mouse_leave_data = leave_color; + Widget_add_child_weighed (cont, box, 1); Widget_add_child_weighed (cont, Widget_label_new (label, TEXT_ALIGN_LEFT), 1); return cont; } - -Widget * -Widget_composite_scrollable_make (Widget *content) -{ - Widget *cont_main = Widget_container_new (FALSE, 8, 4); - Widget *scrollable = Widget_scrollable_new (); - Widget_add_child (scrollable, content); - - scrollable->on_scroll_down = uicb_scrollable_scroll_down; - scrollable->scroll_down_data = scrollable; - scrollable->on_scroll_up = uicb_scrollable_scroll_up; - scrollable->scroll_up_data = scrollable; - - Widget *scroll_up = Widget_plain_new (); - Widget *scroll_down = Widget_plain_new (); - - scroll_up->icon = ICON_ARROW_UP; - Widget_set_on_left_up (scroll_up, uicb_scrollable_scroll_up, scrollable); - scroll_up->aspect_ratio = 1.0; - - scroll_down->icon = ICON_ARROW_DOWN; - Widget_set_on_left_up (scroll_down, uicb_scrollable_scroll_down, scrollable); - scroll_down->aspect_ratio = 1.0; - - Widget *cont_horiz = Widget_container_new (TRUE, 0, 4); - Widget *scrollbar_vertical = Widget_container_new (FALSE, 0, 0); - Widget *actual_scrollbar_vertical = Widget_scrollbar_new (scrollable, FALSE); - - actual_scrollbar_vertical->on_left_down = uicb_scrollbar_start_scroll; - actual_scrollbar_vertical->on_left_up = uicb_scrollbar_stop_scroll; - actual_scrollbar_vertical->on_mouse_leave = uicb_scrollbar_stop_scroll; - actual_scrollbar_vertical->on_mouse_move = uicb_scrollbar_scroll; - - Widget_add_child_weighed (scrollbar_vertical, scroll_up, 1); - Widget_add_child_weighed (scrollbar_vertical, actual_scrollbar_vertical, 1); - Widget_add_child_weighed (scrollbar_vertical, scroll_down, 1); - - Widget_add_child_weighed (cont_horiz, scrollable, 0.93); - Widget_add_child_weighed (cont_horiz, scrollbar_vertical, 0.07); - - Widget_add_child_weighed (cont_main, cont_horiz, 0.93); - - Widget *scroll_left = Widget_plain_new (); - Widget *scroll_right = Widget_plain_new (); - - scroll_left->icon = ICON_ARROW_LEFT; - Widget_set_on_left_up (scroll_left, uicb_scrollable_scroll_left, scrollable); - scroll_left->aspect_ratio = 1.0; - - scroll_right->icon = ICON_ARROW_RIGHT; - Widget_set_on_left_up (scroll_right, uicb_scrollable_scroll_right, scrollable); - scroll_right->aspect_ratio = 1.0; - - Widget *scrollbar_horizontal = Widget_container_new (TRUE, 0, 0); - Widget *actual_scrollbar_horizontal = Widget_scrollbar_new (scrollable, TRUE); - - actual_scrollbar_horizontal->on_left_down = uicb_scrollbar_start_scroll; - actual_scrollbar_horizontal->on_left_up = uicb_scrollbar_stop_scroll; - actual_scrollbar_horizontal->on_mouse_leave = uicb_scrollbar_stop_scroll; - actual_scrollbar_horizontal->on_mouse_move = uicb_scrollbar_scroll; - - Widget *padding = Widget_plain_new (); - padding->aspect_ratio = 1.0; - - Widget_add_child_weighed (scrollbar_horizontal, scroll_left, 1); - Widget_add_child_weighed (scrollbar_horizontal, actual_scrollbar_horizontal, 1); - Widget_add_child_weighed (scrollbar_horizontal, scroll_right, 1); - Widget_add_child_weighed (scrollbar_horizontal, padding, 1); - - Widget_add_child_weighed (cont_main, scrollbar_horizontal, 0.07); - - return cont_main; -} diff --git a/game/src/ui/widget.h b/game/src/ui/widget.h index cf35930..34b713f 100644 --- a/game/src/ui/widget.h +++ b/game/src/ui/widget.h @@ -19,7 +19,8 @@ typedef enum WidgetKind WIDGET_CONTAINER, WIDGET_LABEL, WIDGET_SCROLLABLE, - WIDGET_SCROLLBAR + WIDGET_SCROLLBAR, + WIDGET_MULTILINE } WidgetKind; typedef enum WidgetIcon @@ -65,13 +66,19 @@ typedef struct ScrollableWidget typedef struct ScrollbarWidget { - struct Widget *scrollable; /* Rename to target. */ + struct Widget *target; gboolean horizontal; /* Used for input. */ gboolean dragging; float drag_offset; } ScrollbarWidget; +typedef struct MultilineWidget +{ + GString *text; + TextAlignment alignment; +} MultilineWidget; + typedef struct Widget { struct Widget *parent; @@ -83,6 +90,7 @@ typedef struct Widget LabelWidget label; ScrollableWidget scrollable; ScrollbarWidget scrollbar; + MultilineWidget multiline; }; gboolean visible; @@ -128,7 +136,8 @@ Widget *Widget_plain_new (); Widget *Widget_container_new (gboolean horizontal, float padding, float child_spacing); Widget *Widget_label_new (const char *text, TextAlignment alignment); Widget *Widget_scrollable_new (); -Widget *Widget_scrollbar_new (Widget *scrollable, gboolean horizontal); +Widget *Widget_scrollbar_new (Widget *target, gboolean horizontal); +Widget *Widget_multiline_new (const char *text, TextAlignment alignment); void Widget_add_child (Widget *parent, Widget *child); void Widget_add_child_weighed (Widget *parent, Widget *child, @@ -168,12 +177,10 @@ void uicb_checkbox_show_flag (Widget *self, void *userdata); void uicb_switch_flag (Widget *self, void *userdata); void uicb_increment_int (Widget *self, void *userdata); void uicb_decrement_int (Widget *self, void *userdata); +void uicb_set_background (Widget *self, void *userdata); -void uicb_scrollable_scroll_down (Widget *self, void *userdata); void uicb_scrollable_scroll_up (Widget *self, void *userdata); -void uicb_scrollable_scroll_left (Widget *self, void *userdata); -void uicb_scrollable_scroll_right (Widget *self, void *userdata); - +void uicb_scrollable_scroll_down (Widget *self, void *userdata); void uicb_scrollbar_start_scroll (Widget *self, void *userdata); void uicb_scrollbar_scroll (Widget *self, void *userdata); void uicb_scrollbar_stop_scroll (Widget *self, void *userdata); @@ -183,4 +190,3 @@ Widget *Widget_label_make_float (const char *fmt, float *ptr, TextAlignment alig Widget *Widget_composite_spinner_make_int (const char *format, int *ptr); Widget *Widget_composite_checkbox_make (const char *label, char *flag); -Widget *Widget_composite_scrollable_make (Widget *content);