Skip to content

Commit 3a72637

Browse files
author
Jay
committed
Lables and other text elements can now specify font name
1 parent dde4840 commit 3a72637

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
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.2.8",
3+
.version = "0.2.9",
44
.fingerprint = 0xe8a81a8d0aa558d5,
55
.minimum_zig_version = "0.14.1",
66
.dependencies = .{

src/engine.zig

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ pub const Element = struct {
308308
update: ?*const fn (display: *Display, element: *Element) void = null,
309309
},
310310
label: struct {
311+
font: *FontInfo = undefined,
312+
font_name: ?[]const u8 = null,
311313
text: []const u8 = "",
312314
translated: []const u8 = "",
313315
elements: ArrayListUnmanaged(TextElement) = .empty,
@@ -317,6 +319,8 @@ pub const Element = struct {
317319
on_click: ?*const fn (display: *Display, element: *Element) Allocator.Error!void = null,
318320
},
319321
checkbox: struct {
322+
font: *FontInfo = undefined,
323+
font_name: ?[]const u8 = null,
320324
checked: bool = false,
321325
translated: []const u8 = "",
322326
text: []const u8 = "",
@@ -329,6 +333,8 @@ pub const Element = struct {
329333
on_change: ?*const fn (display: *Display, element: *Element) Allocator.Error!void = null,
330334
},
331335
text_input: struct {
336+
font: *FontInfo = undefined,
337+
font_name: ?[]const u8 = null,
332338
texture: ?*sdl.SDL_Texture = null,
333339
initial_text: ?[]const u8 = "",
334340
icon_texture_name: ?[]const u8 = "",
@@ -347,6 +353,8 @@ pub const Element = struct {
347353
style: ThemeColour = .normal,
348354
},
349355
button: struct {
356+
font: *FontInfo = undefined,
357+
font_name: ?[]const u8 = null,
350358
text: []const u8 = "",
351359
translated: []const u8 = "",
352360
text_texture: ?*sdl.SDL_Texture = null,
@@ -751,7 +759,7 @@ pub const Element = struct {
751759
}
752760
if (text.len == 0) return;
753761
self.type.text_input.placeholder_text = text;
754-
if (display.generate_text_texture(self.type.text_input.placeholder_text.?)) |texture| {
762+
if (display.generate_text_texture(self.type.text_input.placeholder_text.?, self.type.text_input.font)) |texture| {
755763
self.type.text_input.placeholder_texture = texture;
756764
}
757765
},
@@ -817,7 +825,7 @@ pub const Element = struct {
817825
if (new_text.len > 0) {
818826
try self.type.text_input.text.appendSlice(allocator, new_text);
819827
self.text_data_to_runes(allocator);
820-
if (display.generate_text_texture(self.type.text_input.text.items)) |texture| {
828+
if (display.generate_text_texture(self.type.text_input.text.items, self.type.text_input.font)) |texture| {
821829
self.type.text_input.cursor_pixels = text_size(display, texture, .normal).width;
822830
self.type.text_input.texture = texture;
823831
self.type.text_input.cursor_character = self.type.text_input.runes.items.len;
@@ -841,7 +849,7 @@ pub const Element = struct {
841849
if (self.type.label.translated.len > 0) {
842850
var data = Chunker.init(self.type.label.translated);
843851
while (data.next()) |text| {
844-
if (display.generate_text_texture(text)) |texture| {
852+
if (display.generate_text_texture(text, self.type.label.font)) |texture| {
845853
try self.type.label.elements.append(allocator, .{
846854
.text = text,
847855
.width = @floatFromInt(texture.*.w),
@@ -869,7 +877,7 @@ pub const Element = struct {
869877
self.type.checkbox.elements.clearRetainingCapacity();
870878
var data = Chunker.init(self.type.checkbox.translated);
871879
while (data.next()) |text| {
872-
if (display.generate_text_texture(text)) |texture| {
880+
if (display.generate_text_texture(text, self.type.checkbox.font)) |texture| {
873881
try self.type.checkbox.elements.append(allocator, .{
874882
.text = text,
875883
.width = @floatFromInt(texture.*.w),
@@ -895,7 +903,7 @@ pub const Element = struct {
895903
self.type.button.text = new_text;
896904
self.type.button.translated = new_translated;
897905
if (new_translated.len > 0) {
898-
if (display.generate_text_texture(self.type.button.translated)) |texture| {
906+
if (display.generate_text_texture(self.type.button.translated, self.type.button.font)) |texture| {
899907
self.type.button.text_texture = texture;
900908
}
901909
}
@@ -1709,7 +1717,7 @@ pub const Element = struct {
17091717
self.type.text_input.texture = null;
17101718
}
17111719
if (self.type.text_input.text.items.len > 0) {
1712-
if (display.generate_text_texture(self.type.text_input.text.items)) |texture| {
1720+
if (display.generate_text_texture(self.type.text_input.text.items, self.type.text_input.font)) |texture| {
17131721
self.type.text_input.texture = texture;
17141722
// For now, the cursor position is simply the end of the text.
17151723
self.type.text_input.cursor_pixels = text_size(display, texture, .normal).width;
@@ -3329,15 +3337,14 @@ pub const Display = struct {
33293337

33303338
/// Convert a text string into an image that is sent as a texture to
33313339
/// the graphics card.
3332-
fn generate_text_texture(self: *Display, text: []const u8) ?*sdl.SDL_Texture {
3333-
const myfont = self.fonts.items[0].font;
3340+
fn generate_text_texture(self: *Display, text: []const u8, myfont: *FontInfo) ?*sdl.SDL_Texture {
33343341

33353342
// Step 1: Create a surface (a bitmap) that holds the text.
33363343
//
33373344
// The text colour is set to white, so that it can be tinted to
33383345
// match the current theme.
33393346
const surface = sdl.TTF_RenderText_Blended(
3340-
myfont,
3347+
myfont.font,
33413348
text.ptr,
33423349
text.len,
33433350
.{ .r = 255, .g = 255, .b = 255, .a = 255 },
@@ -4614,6 +4621,7 @@ pub fn setup_checkbox(
46144621
element.background_texture = null;
46154622
element.type.checkbox.translated = "";
46164623
element.type.checkbox.elements = .empty;
4624+
element.type.checkbox.font = select_font(self.fonts.items, element.type.checkbox.font_name);
46174625

46184626
if (element.focus == .unspecified)
46194627
element.focus = .can_focus;
@@ -4667,6 +4675,7 @@ pub fn setup_label(
46674675
element.background_texture = null;
46684676
element.type.label.translated = "";
46694677
element.type.label.elements = .empty;
4678+
element.type.label.font = select_font(self.fonts.items, element.type.label.font_name);
46704679

46714680
if (element.focus == .unspecified) {
46724681
if (element.type.label.on_click != null)
@@ -4698,6 +4707,7 @@ pub fn setup_text_input(
46984707
if (element.focus == .unspecified) {
46994708
element.focus = .can_focus;
47004709
}
4710+
element.type.text_input.font = select_font(self.fonts.items, element.type.text_input.font_name);
47014711

47024712
if (element.type.text_input.icon_texture_name) |icon| {
47034713
if (try self.load_texture_resource(allocator, icon)) |texture| {
@@ -4775,6 +4785,17 @@ pub fn setup_sprite(
47754785
}
47764786
}
47774787

4788+
fn select_font(fonts: []*FontInfo, name: ?[]const u8) *FontInfo {
4789+
if (name) |font_name| {
4790+
for (fonts) |font| {
4791+
if (std.mem.eql(u8, font.name, font_name)) {
4792+
return font;
4793+
}
4794+
}
4795+
}
4796+
return fonts[0];
4797+
}
4798+
47784799
pub fn setup_button(
47794800
display: *Display,
47804801
allocator: Allocator,
@@ -4787,6 +4808,7 @@ pub fn setup_button(
47874808
element.type.button.icon_hover = null;
47884809
element.type.button.background_pressed = null;
47894810
element.type.button.background_hover = null;
4811+
element.type.button.font = select_font(display.fonts.items, element.type.button.font_name);
47904812

47914813
if (element.focus == .unspecified)
47924814
element.focus = .can_focus;

0 commit comments

Comments
 (0)