From d8aeca2d4262412bbf1d7ef1415874e4502f190f Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Sat, 14 Jun 2025 10:45:32 +0200 Subject: [PATCH] Padding and margin for containers --- game/res/shaders/uiwidget.frag | 4 +-- game/src/ui/uiman.c | 14 +++++---- game/src/ui/widget.c | 56 +++++++++++++--------------------- game/src/ui/widget.h | 7 +++-- 4 files changed, 34 insertions(+), 47 deletions(-) diff --git a/game/res/shaders/uiwidget.frag b/game/res/shaders/uiwidget.frag index d480d44..cc6f365 100644 --- a/game/res/shaders/uiwidget.frag +++ b/game/res/shaders/uiwidget.frag @@ -20,9 +20,7 @@ void main () float y_border = step (f_uv.y, border_uv.y) + step (1.0 - border_uv.y, f_uv.y); float is_border = clamp (x_border + y_border, 0.0, 1.0); - vec4 bgc = has_background_image - ? mix (texture2D (background_image, f_uv), background_color, 0.5) - : background_color; + vec4 bgc = has_background_image ? texture2D (background_image, f_uv) : background_color; gl_FragColor = mix (bgc, border_color, is_border); } diff --git a/game/src/ui/uiman.c b/game/src/ui/uiman.c index 9f38db4..4ddc7f1 100644 --- a/game/src/ui/uiman.c +++ b/game/src/ui/uiman.c @@ -23,9 +23,8 @@ uiman_init () ui_widget_mesh_init (); glm_ortho (0.0, (float) sdl_window_w, (float) sdl_window_h, 0.0, -1.0, 1.0, projection); - Widget *debug_menu = Widget_container_new (FALSE); - Widget_set_background_image (debug_menu, RESTEX ("terrain1")); - VEC4_SETI (debug_menu->background_color, 0, 0, 0, 0.7); + Widget *debug_menu = Widget_container_new (FALSE, 8, 4); + Widget_set_background_image (debug_menu, RESTEX ("terrain4")); VEC2_SETI (debug_menu->position, 32, 32); VEC2_SETI (debug_menu->size, 350, 150); debug_menu->border_width = 2; @@ -37,14 +36,17 @@ uiman_init () Widget_add_child_weighed (debug_menu, Widget_label_make_int ("Chunks rendered: %d", &n_frame_chunks, TEXT_ALIGN_LEFT), 1); Widget_add_child_weighed (debug_menu, Widget_label_make_int ("Entities rendered: %d", &n_frame_ents, TEXT_ALIGN_LEFT), 1); - Widget *poscont = Widget_container_new (TRUE); + Widget *poscont = Widget_container_new (TRUE, 0, 0); Widget_add_child_weighed (poscont, Widget_label_make_float ("X: %.1f", &camera_position[0], TEXT_ALIGN_LEFT), 1); Widget_add_child_weighed (poscont, Widget_label_make_float ("Y: %.1f", &camera_position[1], TEXT_ALIGN_CENTER), 1); Widget_add_child_weighed (poscont, Widget_label_make_float ("Z: %.1f", &camera_position[2], TEXT_ALIGN_RIGHT), 1); Widget_add_child_weighed (debug_menu, poscont, 1); - Widget_add_child_weighed (debug_menu, Widget_label_make_flag_switch_button ("Toggle wireframe", "wireframe_enabled"), 1); - Widget_add_child_weighed (debug_menu, Widget_label_make_flag_switch_button ("Toggle flying", "flight_enabled"), 1); + Widget *buttons = Widget_container_new (TRUE, 0, 4); + + Widget_add_child_weighed (buttons, Widget_label_make_flag_switch_button ("Toggle wireframe", "wireframe_enabled"), 1); + Widget_add_child_weighed (buttons, Widget_label_make_flag_switch_button ("Toggle flying", "flight_enabled"), 1); + Widget_add_child_weighed (debug_menu, buttons, 1); } void diff --git a/game/src/ui/widget.c b/game/src/ui/widget.c index d171676..7cf36ec 100644 --- a/game/src/ui/widget.c +++ b/game/src/ui/widget.c @@ -24,10 +24,12 @@ Widget_plain_new () } Widget * -Widget_container_new (gboolean horizontal) +Widget_container_new (gboolean horizontal, float padding, float child_spacing) { Widget *e = ui_el_new_internal (WIDGET_CONTAINER); e->container.horizontal = horizontal; + e->container.padding = padding; + e->container.child_spacing = child_spacing; return e; } @@ -214,7 +216,6 @@ Widget_container_layout (Widget *parent, gboolean horizontal) float total_fixed = 0.0; Widget *child; - WIDGET_FOREACH_CHILD (parent, i, child) { if (child->layout_weight == 0.0) { @@ -226,46 +227,31 @@ Widget_container_layout (Widget *parent, gboolean horizontal) } } - float weights_size = ((horizontal ? parent->size[0] : parent->size[1]) - total_fixed) / total_weights; - - if (weights_size < 0) - { - g_warning ("Fixed size children exceed the size of the parent container."); - } - - float offset = 0.0; + float spacing_total = glm_max (0, parent->children->len - 1) * parent->container.child_spacing; + float total_size = (horizontal ? parent->size[0] : parent->size[1]) + - 2.0 * parent->container.padding - spacing_total; + float weights_size = total_weights > 0.0 ? (total_size - total_fixed) / total_weights : 0; + float offset = parent->container.padding; WIDGET_FOREACH_CHILD (parent, i, child) { - if (child->layout_weight == 0.0) + float size = (child->layout_weight == 0.0) + ? (horizontal ? child->size[0] : child->size[1]) + : child->layout_weight * weights_size; + + if (horizontal) { - if (horizontal) - { - VEC2_SETI (child->position, parent->position[0] + offset, parent->position[1]); - child->size[1] = parent->size[1]; - offset += child->size[0]; - } - else - { - VEC2_SETI (child->position, parent->position[0], parent->position[1] + offset); - child->size[0] = parent->size[0]; - offset += child->size[1]; - } + VEC2_SETI (child->position, parent->position[0] + offset, + parent->position[1] + parent->container.padding); + VEC2_SETI (child->size, size, parent->size[1] - 2 * parent->container.padding); } else { - if (horizontal) - { - VEC2_SETI (child->position, parent->position[0] + offset, parent->position[1]); - VEC2_SETI (child->size, child->layout_weight * weights_size, parent->size[1]); - offset += child->size[0]; - } - else - { - VEC2_SETI (child->position, parent->position[0], parent->position[1] + offset); - VEC2_SETI (child->size, parent->size[0], child->layout_weight * weights_size); - offset += child->size[1]; - } + VEC2_SETI (child->position, parent->position[0] + parent->container.padding, + parent->position[1] + offset); + VEC2_SETI (child->size, parent->size[0] - 2 * parent->container.padding, size); } + + offset += size + parent->container.child_spacing; } } diff --git a/game/src/ui/widget.h b/game/src/ui/widget.h index 9095320..e975d53 100644 --- a/game/src/ui/widget.h +++ b/game/src/ui/widget.h @@ -23,6 +23,8 @@ typedef enum WidgetKind typedef struct ContainerWidget { gboolean horizontal; + float padding; + float child_spacing; } ContainerWidget; typedef enum TextAlignment @@ -39,7 +41,6 @@ typedef struct LabelWidget TextAlignment text_alignment; } LabelWidget; - typedef struct Widget { struct Widget *parent; @@ -65,7 +66,7 @@ typedef struct Widget gboolean has_background_image; unsigned int background_image; - unsigned int border_width; + float border_width; vec4 border_color; void (*on_left_click) (struct Widget *, void *); @@ -77,7 +78,7 @@ typedef struct Widget } Widget; Widget *Widget_plain_new (); -Widget *Widget_container_new (gboolean horizontal); +Widget *Widget_container_new (gboolean horizontal, float padding, float child_spacing); Widget *Widget_label_new (const char *text, TextAlignment alignment); void Widget_add_child (Widget *parent, Widget *child);