[ui layout] implement ui box min size, + small fixes in layout code of ui sample

This commit is contained in:
Martin Fouilleul 2023-09-15 12:41:23 +02:00
parent ae392a1fc2
commit 6a50a6bbdc
6 changed files with 1366 additions and 992 deletions

View File

@ -120,7 +120,7 @@ void column_begin(const char* header, f32 widthFraction)
}
oc_ui_style_next(&(oc_ui_style){ .size.width = { OC_UI_SIZE_PARENT, 1 },
.size.height = { OC_UI_SIZE_PARENT, 0.8 },
.size.height = { OC_UI_SIZE_PARENT, 1, 1 },
.layout.align.x = OC_UI_ALIGN_START,
.layout.margin.x = 16,
.layout.spacing = 24 },
@ -158,8 +158,16 @@ void labeled_slider(const char* label, f32* value)
}
}
ORCA_EXPORT void oc_on_resize(u32 width, u32 height)
{
frameSize.x = width;
frameSize.y = height;
}
ORCA_EXPORT void oc_on_frame_refresh(void)
{
oc_arena_scope scratch = oc_scratch_begin();
switch(command)
{
case CMD_SET_DARK_THEME:
@ -173,10 +181,15 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
}
command = CMD_NONE;
oc_arena_scope scratch = oc_scratch_begin();
oc_ui_style defaultStyle = { .font = fontRegular };
oc_ui_style_mask defaultMask = OC_UI_STYLE_FONT;
oc_ui_frame(frameSize, &defaultStyle, defaultMask)
{
oc_ui_style_next(&(oc_ui_style){ .size.width = { OC_UI_SIZE_PARENT, 1, .minSize = 1200 },
.size.height = { OC_UI_SIZE_PARENT, 1, .minSize = 838 } },
OC_UI_STYLE_SIZE);
oc_ui_container("top level", OC_UI_FLAG_NONE)
{
//--------------------------------------------------------------------------------------------
// Menu bar
@ -205,7 +218,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
}
oc_ui_style_next(&(oc_ui_style){ .size.width = { OC_UI_SIZE_PARENT, 1 },
.size.height = { OC_UI_SIZE_PARENT_MINUS_PIXELS, 28 },
.size.height = { OC_UI_SIZE_PARENT, 1, 1 },
.layout.axis = OC_UI_AXIS_X,
.layout.margin.x = 16,
.layout.margin.y = 16,
@ -214,6 +227,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
| OC_UI_STYLE_LAYOUT_AXIS
| OC_UI_STYLE_LAYOUT_MARGINS
| OC_UI_STYLE_LAYOUT_SPACING);
oc_ui_container("background", OC_UI_FLAG_DRAW_BACKGROUND)
{
column("Widgets", 1.0 / 3)
@ -379,7 +393,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
// Scrollable panel
//-------------------------------------------------------------------------------------
oc_ui_style_next(&(oc_ui_style){ .size.width = { OC_UI_SIZE_PARENT, 1 },
.size.height = { OC_UI_SIZE_PIXELS, 430 },
.size.height = { OC_UI_SIZE_PARENT, 1, 1 },
.layout.margin.x = 16,
.layout.margin.y = 16,
.layout.spacing = 8,
@ -396,7 +410,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
| OC_UI_STYLE_ROUNDNESS);
oc_ui_panel("log", OC_UI_FLAG_DRAW_BACKGROUND | OC_UI_FLAG_DRAW_BORDER)
{
if(oc_list_empty(&logLines.list))
if(oc_list_empty(logLines.list))
{
oc_ui_style_next(&(oc_ui_style){ .color = ui.theme->text2 },
OC_UI_STYLE_COLOR);
@ -404,7 +418,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
}
i32 i = 0;
oc_list_for(&logLines.list, logLine, oc_str8_elt, listElt)
oc_list_for(logLines.list, logLine, oc_str8_elt, listElt)
{
char id[15];
snprintf(id, sizeof(id), "%d", i);
@ -787,7 +801,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
}
}
}
}
oc_canvas_select(canvas);
oc_surface_select(surface);
oc_ui_draw();

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -1083,11 +1083,11 @@ void oc_ui_layout_upward_dependent_size(oc_ui_context* ui, oc_ui_box* box, int a
oc_ui_size* size = &child->style.size.c[axis];
if(size->kind == OC_UI_SIZE_PARENT)
{
child->rect.c[2 + axis] = availableSize * size->value;
child->rect.c[2 + axis] = oc_max(size->minSize, availableSize * size->value);
}
else if(size->kind == OC_UI_SIZE_PARENT_MINUS_PIXELS)
{
child->rect.c[2 + axis] = oc_max(0, availableSize - size->value);
child->rect.c[2 + axis] = oc_max(size->minSize, oc_max(0, availableSize - size->value));
}
}
@ -1122,7 +1122,11 @@ void oc_ui_layout_upward_dependent_size(oc_ui_context* ui, oc_ui_box* box, int a
oc_list_for(box->children, child, oc_ui_box, listElt)
{
f32 relax = child->style.size.c[axis].relax;
child->rect.c[2 + axis] -= alpha * child->rect.c[2 + axis] * relax;
f32 minSize = child->style.size.c[axis].minSize;
f32 remove = alpha * child->rect.c[2 + axis] * relax;
child->rect.c[2 + axis] = oc_max(minSize, child->rect.c[2 + axis] - remove);
sum += child->rect.c[2 + axis];
}
}

View File

@ -82,6 +82,7 @@ typedef struct oc_ui_size
oc_ui_size_kind kind;
f32 value;
f32 relax;
f32 minSize;
} oc_ui_size;
typedef union oc_ui_box_size