Skip to content

Commit 2aef617

Browse files
author
Jay
committed
Support swapping child elements.
Add element set_visibility to catch when redraw is needed. Allow 'fit' sprite to also use 'child_align' to position image inside the element rect.
1 parent 106d5e2 commit 2aef617

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/engine.zig

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,12 @@ pub const Element = struct {
736736
}
737737
}
738738

739+
pub inline fn set_visibility(self: *Element, display: *Display, visible: Visibility) void {
740+
if (self.visible == visible) return;
741+
self.visible = visible;
742+
display.need_relayout = true;
743+
}
744+
739745
/// set_texture replaces the current texture of an element with a new
740746
/// texture found in the resource bucket.
741747
pub inline fn set_texture(
@@ -1016,12 +1022,26 @@ pub const Element = struct {
10161022
pub inline fn insert_element(
10171023
self: *Element,
10181024
allocator: Allocator,
1025+
display: *Display,
1026+
conf: Element,
10191027
location: usize,
1020-
child: *Element,
10211028
) error{OutOfMemory}!void {
10221029
std.debug.assert(self.type == .panel);
10231030
std.debug.assert(location <= self.type.panel.children.items.len);
1031+
const child = try allocator.create(Element);
1032+
child.* = conf;
1033+
try display.setup_element(allocator, child);
10241034
try self.type.panel.children.insert(allocator, location, child);
1035+
return child;
1036+
}
1037+
1038+
pub inline fn swap(self: *Element, from: usize, to: usize) void {
1039+
std.debug.assert(self.type == .panel);
1040+
std.debug.assert(from < self.type.panel.children.items.len);
1041+
std.debug.assert(to < self.type.panel.children.items.len);
1042+
const s = self.type.panel.children.items[from];
1043+
self.type.panel.children.items[from] = self.type.panel.children.items[to];
1044+
self.type.panel.children.items[to] = s;
10251045
}
10261046

10271047
/// Use `remove_element_at` to attach a child element in a specific location
@@ -1602,9 +1622,23 @@ pub const Element = struct {
16021622
if (src_scale >= dst_scale) {
16031623
// image too wide, hight will have blank space
16041624
dest.height = dest.width * src_scale;
1625+
// sprite is drawn at top of its rect, unless a
1626+
// different child alignment is chosen.
1627+
switch (element.child_align.y) {
1628+
.start => {}, // already at top
1629+
.centre => dest.y += (element.rect.height - dest.height) / 2,
1630+
.end => dest.y += (element.rect.height - dest.height),
1631+
}
16051632
} else {
16061633
// image too tall/high, width will have blank space
16071634
dest.width = dest.height * src_scale;
1635+
// sprite is drawn at start/left of its rect, unless
1636+
// a different child alignment is chosen.
1637+
switch (element.child_align.x) {
1638+
.start => {}, // already at top
1639+
.centre => dest.x += (element.rect.width - dest.width) / 2,
1640+
.end => dest.x += (element.rect.width - dest.width),
1641+
}
16081642
}
16091643
},
16101644
.fill => {

0 commit comments

Comments
 (0)