Skip to content

Commit 5ffde26

Browse files
committed
Improve layout engine (a few bugs still to go)
1 parent c9e7e1f commit 5ffde26

2 files changed

Lines changed: 132 additions & 148 deletions

File tree

src/engine.zig

Lines changed: 22 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,14 @@ pub fn Display(comptime T: type) type {
386386
},
387387
.texture = null,
388388
.pad = .{ .left = 0, .right = 0, .top = 0, .bottom = 0 },
389-
.maximum = .{ .width = 100, .height = 100 },
389+
.minimum = .{
390+
.width = @as(f32, @floatFromInt(window_width)) * density,
391+
.height = @as(f32, @floatFromInt(window_height)) * density,
392+
},
393+
.maximum = .{
394+
.width = @as(f32, @floatFromInt(window_width)) * density,
395+
.height = @as(f32, @floatFromInt(window_height)) * density,
396+
},
390397
.layout = .{ .x = .grows, .y = .grows },
391398
.child_align = .{ .x = .centre, .y = .start },
392399
.colour = .{},
@@ -664,83 +671,15 @@ pub fn Display(comptime T: type) type {
664671
/// Apply the relayout algorithm to the currently visible root
665672
/// panels/scenes, then descend to relayout each of the child panels.
666673
pub fn relayout(display: *Self) void {
667-
display.need_relayout = false;
668-
669-
for (display.root.type.panel.children.items) |*scene| {
670-
if (scene.*.visible != .visible) continue;
671-
var updated = false;
672-
673-
// Remember the actual pad for this parent scene
674-
const user_pad: Clip = scene.*.pad;
675-
676-
// Children of the root panel are "scenes" or "screens". They
677-
// all inherit the device safe are except the background.
678-
if (!std.mem.eql(u8, "background", scene.*.name)) {
679-
scene.*.pad.top += display.safe_area.top;
680-
scene.*.pad.bottom += display.safe_area.bottom;
681-
scene.*.pad.left += display.safe_area.left;
682-
scene.*.pad.right += display.safe_area.right;
683-
}
684-
685-
// Root scene/panels get placement before relayout begins.
686-
const new_width = directional_clamp(
687-
scene.*.layout.x,
688-
scene.*.minimum.width,
689-
display.root.rect.width,
690-
scene.*.maximum.width,
691-
);
692-
if (new_width != scene.*.rect.width) {
693-
scene.*.rect.width = new_width;
694-
updated = true;
695-
}
674+
if (display.need_relayout == false) return;
696675

697-
const new_height = directional_clamp(
698-
scene.*.layout.y,
699-
scene.*.minimum.height,
700-
display.root.rect.height,
701-
scene.*.maximum.height,
702-
);
703-
if (new_height != scene.*.rect.height) {
704-
scene.*.rect.height = new_height;
705-
updated = true;
706-
}
707-
708-
// Place panel at start, centre or end.
709-
const x = switch (scene.*.child_align.x) {
710-
.start => 0,
711-
.end => display.root.rect.width - scene.*.rect.width,
712-
.centre => @round(display.root.rect.width / 2 - scene.*.rect.width / 2),
713-
};
714-
if (x != scene.*.rect.x) {
715-
scene.*.rect.x = x;
716-
updated = true;
717-
}
718-
719-
const y = switch (scene.*.child_align.y) {
720-
.start => 0,
721-
.end => display.root.rect.height - scene.*.rect.height,
722-
.centre => @round(display.root.rect.height / 2 - scene.*.rect.height / 2),
723-
};
724-
if (y != scene.*.rect.y) {
725-
scene.*.rect.y = y;
726-
updated = true;
727-
}
676+
display.need_relayout = false;
728677

729-
// After root panel sizes are established, the child entities
730-
// are layed out inside the panel.
731-
scene.*.type.panel.relayout(display, scene.*);
732-
if (updated) {
733-
if (display.propagate_resize_event(scene.*))
734-
scene.*.type.panel.relayout(display, scene.*);
678+
const resized = display.root.type.panel.layout(display, &display.root);
679+
if (resized) {
680+
if (display.on_resized.call(display, &display.root)) {
681+
_ = display.root.type.panel.layout(display, &display.root);
735682
}
736-
737-
// Children are given opportunity to resize themselves
738-
// in response to the resize event.
739-
const did_resize = display.on_resized.call(display, scene.*);
740-
if (did_resize)
741-
scene.*.type.panel.relayout(display, scene.*);
742-
743-
scene.*.pad = user_pad;
744683
}
745684
}
746685

@@ -1567,6 +1506,10 @@ pub fn Display(comptime T: type) type {
15671506
updated = true;
15681507
if (display.root.rect.height != @as(f32, @floatFromInt(rheight)))
15691508
updated = true;
1509+
if (display.root.minimum.width != @as(f32, @floatFromInt(rwidth)))
1510+
updated = true;
1511+
if (display.root.minimum.height != @as(f32, @floatFromInt(rheight)))
1512+
updated = true;
15701513

15711514
if (!updated) return;
15721515

@@ -1580,10 +1523,10 @@ pub fn Display(comptime T: type) type {
15801523
}
15811524
display.root.rect.width = @as(f32, @floatFromInt(rwidth));
15821525
display.root.rect.height = @as(f32, @floatFromInt(rheight));
1583-
display.root.minimum.width = display.root.rect.x;
1584-
display.root.maximum.width = display.root.rect.x;
1585-
display.root.minimum.height = display.root.rect.y;
1586-
display.root.maximum.height = display.root.rect.y;
1526+
display.root.minimum.width = display.root.rect.width;
1527+
display.root.maximum.width = display.root.rect.width;
1528+
display.root.minimum.height = display.root.rect.height;
1529+
display.root.maximum.height = display.root.rect.height;
15871530

15881531
if (display.recalculate_safe_area()) {
15891532
updated = true;

0 commit comments

Comments
 (0)