Padding and margin for containers

This commit is contained in:
2025-06-14 10:45:32 +02:00
parent e2606ecedf
commit d8aeca2d42
4 changed files with 34 additions and 47 deletions
+1 -3
View File
@@ -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);
}
+8 -6
View File
@@ -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
+21 -35
View File
@@ -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;
}
}
+4 -3
View File
@@ -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);