changes text size func to one function that includes a max witdth

This commit is contained in:
2026-05-10 00:54:16 +12:00
parent 058c3c4156
commit c349a7ca82

View File

@@ -22,18 +22,20 @@ const Spaceing = 4;
pub fn TextType(
comptime FontType: type,
comptime getTextWidthFunc: fn (string: []const u8, FontType) Real,
comptime getTextHeightFunc: fn (string: []const u8, FontType) Real,
comptime getTextSizeFunc: fn (string: []const u8, FontType, max_width: ?Real) Rect,
) type {
return struct {
const Optional = struct {
font: FontType,
};
// const Optional = struct {
// font: FontType,
// };
const Self = @This();
string: []const u8,
colour: Colour,
background: Colour,
font: FontType,
/// this lib just cares about rendering some text
/// text wrapping is your job SDL3 ttf does it :D
max_width: ?Real = null,
pub fn init(string: []const u8, font: FontType, colour: Colour) Self {
return Self{
@@ -55,12 +57,8 @@ pub fn TextType(
return s;
}
pub fn getTextWidth(self: Self) Real {
return getTextWidthFunc(self.string, self.font);
}
pub fn getTextHeight(self: Self) Real {
return getTextHeightFunc(self.string, self.font);
pub fn getTextSize(self: Self) Rect {
return getTextSizeFunc(self.string, self.font, self.max_width);
}
};
}
@@ -419,9 +417,11 @@ pub fn Shoots(
}
},
.text => {
parent.element.rect.h += node.text.getTextHeight();
const text_size: Rect = node.text.getTextSize();
parent.element.rect.h += text_size.h;
// std.debug.print("parent.element.rect.h : {}\n", .{parent.element.rect.h});
parent.element.rect.w += node.text.getTextWidth();
parent.element.rect.w += text_size.w;
// std.debug.print("parent.element.rect.w : {}\n", .{parent.element.rect.w});
},
.texture => {