Fix UI size calculation

This commit is contained in:
2023-12-22 15:13:23 +01:00
parent da1eeefe70
commit 331f40f4cb
2 changed files with 41 additions and 32 deletions

View File

@@ -185,52 +185,57 @@ static void calculateAxisSizePreorder(const BzUI *ui, const BzUIAxis axis, BzUIN
break;
case BZ_UI_SIZE_NULL:
default:
break;
return;
}
if (node->computedSize[axis] != compSize) {
node->canInteract = true;
node->canInteract = false;
}
node->computedSize[axis] = compSize;
}
static void calculateAxisSizePostorder(const BzUI *ui, const BzUIAxis axis, const BzUINode *node) {
static void calculateAxisSizePostorder(const BzUI *ui, const BzUIAxis axis, BzUINode *node) {
f32 compSize = 0;
switch (node->semanticSize[axis].kind) {
case BZ_UI_SIZE_CHILD_SUM:
BZ_ASSERT(node->first);
for (BzUINode *child = node->first; child; child = child->next) {
compSize += child->computedSize[axis];
compSize += child->computedSize[axis] + child->margin[axis] + child->margin[axis * 2];
}
break;
case BZ_UI_SIZE_CHILD_MAX:
BZ_ASSERT(node->first);
for (BzUINode *child = node->first; child; child = child->next) {
compSize = BZ_MAX(compSize, child->computedSize[axis]);
f32 childSize = child->computedSize[axis] + child->margin[axis] + child->margin[axis * 2];
compSize = BZ_MAX(compSize, childSize);
}
break;
default:
break;
return;
}
if (node->computedSize[axis] != compSize) {
node->canInteract = false;
node->computedSize[axis] = compSize;
}
}
static void calculateSizes(const BzUI *ui, BzUINode *node) {
BzUINode *child = node->first;
calculateAxisSizePreorder(ui, BZ_UI_AXIS_X, node);
calculateAxisSizePreorder(ui, BZ_UI_AXIS_Y, node);
while (child != NULL) {
calculateSizes(ui, child);
child = child->next;
}
calculateAxisSizePostorder(ui, BZ_UI_AXIS_X, node);
calculateAxisSizePostorder(ui, BZ_UI_AXIS_Y, node);
node->computedSize[BZ_UI_AXIS_X] += node->padding[BZ_UI_AXIS_X] + node->padding[BZ_UI_AXIS_X + 2];
node->computedSize[BZ_UI_AXIS_Y] += node->padding[BZ_UI_AXIS_Y] + node->padding[BZ_UI_AXIS_Y + 2];
// Border thickness counts as margin
if (node->flags & BZ_UI_DRAW_BORDER) {
BzUIBorderStyle style = bzUIGetBorderStyle(ui, node);
node->margin[BZ_UI_AXIS_X] += style.thickness * 2;
node->margin[BZ_UI_AXIS_Y] += style.thickness * 2;
}
calculateAxisSizePreorder(ui, BZ_UI_AXIS_X, node);
calculateAxisSizePreorder(ui, BZ_UI_AXIS_Y, node);
node->computedSize[BZ_UI_AXIS_X] += node->padding[BZ_UI_AXIS_X] + node->padding[BZ_UI_AXIS_X + 2];
node->computedSize[BZ_UI_AXIS_Y] += node->padding[BZ_UI_AXIS_Y] + node->padding[BZ_UI_AXIS_Y + 2];
for (BzUINode *child = node->first; child; child = child->next) {
calculateSizes(ui, child);
}
calculateAxisSizePostorder(ui, BZ_UI_AXIS_X, node);
calculateAxisSizePostorder(ui, BZ_UI_AXIS_Y, node);
}
static void calculatePositions(BzUI *ui, BzUINode *node, f32 x, f32 y);
@@ -428,8 +433,11 @@ static void renderNode(BzUI *ui, BzUINode *node) {
drawRect.x, drawRect.y
}, style.fontSize, style.fontSpacing, color);
}
if (node->flags & BZ_UI_DRAW_DEBUG) {
DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, RED);
}
node->canInteract = false;
node->canInteract = true;
BzUINode *child = node->first;
while (child != NULL) {

View File

@@ -87,18 +87,19 @@ typedef struct BzUIStyle {
enum {
BZ_UI_NONE = 0,
BZ_UI_CLICKABLE = (1 << 0),
BZ_UI_DRAW_BACKGROUND = (1 << 1),
BZ_UI_DRAW_BOX_SHADOW = (1 << 2),
BZ_UI_DRAW_BORDER = (1 << 3),
BZ_UI_DRAW_TEXT = (1 << 4),
BZ_UI_DRAW_TEXT_SHADOW = (1 << 5),
BZ_UI_DRAW_SPRITE = (1 << 6),
BZ_UI_ALIGN_HORIZ_START = (1 << 7),
BZ_UI_ALIGN_HORIZ_CENTER = (1 << 8),
BZ_UI_ALIGN_HORIZ_END = (1 << 9),
BZ_UI_ALIGN_VERT_START = (1 << 10),
BZ_UI_ALIGN_VERT_CENTER = (1 << 11),
BZ_UI_ALIGN_VERT_END = (1 << 12),
BZ_UI_DRAW_DEBUG = (1 << 1),
BZ_UI_DRAW_BACKGROUND = (1 << 2),
BZ_UI_DRAW_BOX_SHADOW = (1 << 3),
BZ_UI_DRAW_BORDER = (1 << 4),
BZ_UI_DRAW_TEXT = (1 << 5),
BZ_UI_DRAW_TEXT_SHADOW = (1 << 6),
BZ_UI_DRAW_SPRITE = (1 << 7),
BZ_UI_ALIGN_HORIZ_START = (1 << 8),
BZ_UI_ALIGN_HORIZ_CENTER = (1 << 9),
BZ_UI_ALIGN_HORIZ_END = (1 << 10),
BZ_UI_ALIGN_VERT_START = (1 << 11),
BZ_UI_ALIGN_VERT_CENTER = (1 << 12),
BZ_UI_ALIGN_VERT_END = (1 << 13),
BZ_UI_ALIGN_CENTER = BZ_UI_ALIGN_HORIZ_CENTER | BZ_UI_ALIGN_VERT_CENTER,
};