Skip to content

Commit 5f810b0

Browse files
committed
Support custom checkbox size
1 parent 5dd854d commit 5f810b0

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = .engine,
3-
.version = "0.10.4",
3+
.version = "0.10.5",
44
.fingerprint = 0xe8a81a8d0aa558d5,
55
.minimum_zig_version = "0.15.2",
66
.dependencies = .{

src/element.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub const Element = struct {
113113
translated: []const u8 = "",
114114
elements: ArrayListUnmanaged(TextElement) = .empty,
115115
line_height: f32 = 1,
116+
checkbox_size: Size = .{ .width = 0, .height = 0 },
116117
on_texture: ?*Texture = null,
117118
off_texture: ?*Texture = null,
118119
on_change: Callback = .{ .func = null },
@@ -1023,7 +1024,7 @@ pub const Element = struct {
10231024
// the minimum that would be needed.
10241025
self.layout_label(display, parent_inner_width);
10251026
//err("{s} {s} use width {d}", .{ self.name, @tagName(self.type), choose });
1026-
return self.rect.width + self.pad.left + display.checkbox().width;
1027+
return self.rect.width + self.pad.left + self.type.checkbox.checkbox_size.width;
10271028
},
10281029
.fixed => {
10291030
//err("{s} {s} use width {d}", .{ self.name, @tagName(self.type), choose });
@@ -1709,7 +1710,7 @@ pub const Element = struct {
17091710

17101711
/// Draw a radio box combined with a text label.
17111712
inline fn draw_checkbox(element: *Element, display: *Display, _: Vector, _: ?Clip, scroll_offset: Vector) void {
1712-
const checkbox = display.checkbox();
1713+
const checkbox = element.type.checkbox.checkbox_size;
17131714
element.draw_label(display, scroll_offset, null);
17141715
var dest: Rect = .{
17151716
.x = element.rect.x + element.rect.width - checkbox.width - element.pad.left,
@@ -2148,9 +2149,9 @@ pub const Element = struct {
21482149
/// Calculate how many pixels of text we can draw until we must wrap to
21492150
/// the next line. By default the width is whatever the parent element
21502151
/// has room for.
2151-
fn word_wrap_line(element: *Element, display: *Display, max_parent_width: f32) f32 {
2152+
fn word_wrap_line(element: *Element, _: *Display, max_parent_width: f32) f32 {
21522153
var element_padding = element.pad.left + element.pad.right;
2153-
if (element.type == .checkbox) element_padding += display.checkbox().width;
2154+
if (element.type == .checkbox) element_padding += element.type.checkbox.checkbox_size.width;
21542155

21552156
// If a fixed width is specified, clamp to the fixed width
21562157
const wrap = switch (element.layout.x) {

src/engine.zig

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,16 +1299,6 @@ pub const Display = struct {
12991299
_ = sdl.SDL_RenderPresent(display.renderer);
13001300
}
13011301

1302-
/// Reteurn the size of a checkbox button based on the user
1303-
/// selected screen size/scale.
1304-
pub fn checkbox(self: *Display) Size {
1305-
const CHECKBOX_WIDTH: f32 = 72;
1306-
const CHECKBOX_HEIGHT: f32 = 44;
1307-
const screen_height = self.text_height * self.pixel_scale * self.user_scale;
1308-
const screen_width = screen_height * (CHECKBOX_WIDTH / CHECKBOX_HEIGHT);
1309-
return .{ .width = screen_width, .height = screen_height };
1310-
}
1311-
13121302
/// Load and associate a font file with a font name.
13131303
pub fn load_font(
13141304
self: *Display,
@@ -1457,13 +1447,13 @@ pub const Display = struct {
14571447
ti.references -= 1;
14581448
if (ti.references != 0) {
14591449
if (ti.references < 0) {
1460-
err("free texture \"{d}\" (duplicate free)", .{ti.uid});
1450+
err("free texture \"{f}\" (duplicate free)", .{uid_writer(u64, ti.uid)});
14611451
} else {
1462-
trace("free texture \"{d}\" (not yet {d})", .{ ti.uid, ti.references });
1452+
trace("free texture \"{f}\" (not yet {d})", .{ uid_writer(u64, ti.uid), ti.references });
14631453
}
14641454
return;
14651455
}
1466-
trace("free texture \"{d}\" (now)", .{ti.uid});
1456+
trace("free texture \"{f}\" (now)", .{uid_writer(u64, ti.uid)});
14671457
_ = self.textures.remove(ti.uid);
14681458
ti.destroy(allocator);
14691459
}
@@ -2964,7 +2954,12 @@ pub fn setup_checkbox(
29642954
element.pad.bottom = self.text_height * self.scale * 0.3;
29652955
}
29662956

2967-
const size = self.checkbox();
2957+
if (element.type.checkbox.checkbox_size.width == 0 or element.type.checkbox.checkbox_size.height == 0) {
2958+
element.type.checkbox.checkbox_size.width = default_font_size * self.pixel_scale;
2959+
element.type.checkbox.checkbox_size.height = default_font_size * self.pixel_scale;
2960+
}
2961+
2962+
const size = element.type.checkbox.checkbox_size;
29682963
if (element.minimum.height < size.height)
29692964
element.minimum.height = size.height;
29702965

0 commit comments

Comments
 (0)