From a25844b948844380a4e706c12b4bf5996ea8dce9 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Sat, 13 Jan 2024 15:07:23 +0100 Subject: [PATCH] feat: implement CharType treevents and fix font stuff --- build.zig.zon | 4 ++-- example/main.zig | 35 ++++++----------------------------- src/Sdl2Font.zig | 16 ++++++++++++---- src/Sdl2Platform.zig | 19 ++++++++++++++++--- 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index b06d143..7618930 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -10,8 +10,8 @@ .dependencies = .{ .zenolith = .{ //.path = "../zenolith", - .url = "git+https://git.mzte.de/zenolith/zenolith.git#d55439e94ebd67b764dc2906ea5f2dee520a3956", - .hash = "12208b7dd8669c55155d4e39b016b2836adb321b4dc2be30ddb3eb6888e0b6a82ae4", + .url = "git+https://git.mzte.de/zenolith/zenolith.git#f84d6be6be10724f8baba1cf51da8fd03701d84c", + .hash = "12207777d676529c333c17b8d6909e325b7205220b8f4057e48bfa3ccd71c84a9d6d", }, }, } diff --git a/example/main.zig b/example/main.zig index 09c9bf3..2c150c1 100644 --- a/example/main.zig +++ b/example/main.zig @@ -17,7 +17,7 @@ pub fn main() !void { defer platform.deinit(); var font = zenolith.text.Font.create(try platform.createFont(.{ - .source = .{ .path = "/usr/share/fonts/noto/NotoSans-Regular.ttf" }, + .source = .{ .path = "/usr/share/fonts/liberation/LiberationSans-Regular.ttf" }, }), {}); defer font.deinit(); var zplatform = zenolith.platform.Platform.create(platform, .{}); @@ -30,24 +30,7 @@ pub fn main() !void { errdefer attrs.deinit(alloc); (try attrs.mod(alloc, zenolith.attreebute.CurrentFont)).* = .{ .font = &font }; - (try attrs.mod(alloc, zenolith.attreebute.ButtonStyle)).* = .{ - .background = .{ - .stroked = .{ - .stroke = zenolith.Color.fromInt(0xeba0acff), - .fill = zenolith.Color.fromInt(0x1e1e2eff), - .width = 4, - }, - }, - .background_hovered = .{ - .stroked = .{ - .stroke = zenolith.Color.fromInt(0xeba0acff), - .fill = zenolith.Color.fromInt(0x313244ff), - .width = 3, - }, - }, - .padding = 10, - .font_style = .{}, - }; + try zenolith.Theme.catppuccin_mocha.apply(alloc, &attrs); root.data.attreebutes = attrs; } @@ -56,24 +39,18 @@ pub fn main() !void { try root.downcast(zenolith.widget.Box).?.addChildPositioned( root, null, - try zenolith.widget.Label.init(alloc, .{ - .font = &font, - .text = "Hello, Zenolith!", - .style = .{ .size = 64 }, - }), + try zenolith.widget.Label.init(alloc, "Hello, Zenolith!"), .center, ); - try root.addChild(null, try zenolith.widget.Label.init(alloc, .{ - .font = &font, - .text = "Labels!", - .style = .{ .size = 32 }, - })); + try root.addChild(null, try zenolith.widget.Label.init(alloc, "Labels!")); try root.addChild(null, try zenolith.widget.Button.init(alloc, "Button 1")); try root.addChild(null, try zenolith.widget.Button.init(alloc, "Button 2")); try root.addChild(null, try zenolith.widget.Button.init(alloc, "Button 3")); + try root.addChild(null, try zenolith.widget.Spacer.init(alloc, .{.flex = 1})); + { var chunk = zenolith.text.Chunk.init(alloc); errdefer chunk.deinit(); diff --git a/src/Sdl2Font.zig b/src/Sdl2Font.zig index 1e403fc..d5222a6 100644 --- a/src/Sdl2Font.zig +++ b/src/Sdl2Font.zig @@ -66,11 +66,19 @@ pub fn getGlyph(self: *Sdl2Font, codepoint: u21, style: zenolith.text.Style) !ze } pub fn yOffset(self: *Sdl2Font, size: u31) u31 { - if (c.FT_Set_Pixel_Sizes(self.face, 0, @intCast(size)) != 0) - // TODO: wonk - @panic("Unable to FT_Set_Pixel_Sizes for determining y offset"); + _ = self; + // What is supposed to work: + //if (c.FT_Set_Pixel_Sizes(self.face, 0, @intCast(size)) != 0) + // @panic("Unable to FT_Set_Pixel_Sizes for determining y offset"); - return @intCast(self.face.*.size.*.metrics.height >> 6); + // All these produce equally nonsensical results and there seems to be no consensus which one is actually correct: + //return @intCast(self.face.*.size.*.metrics.height >> 6); + //return @intCast((self.face.*.size.*.metrics.ascender - self.face.*.size.*.metrics.descender) >> 6); + //return @intCast((c.FT_MulFix(self.face.*.bbox.yMax, self.face.*.size.*.metrics.y_scale) >> 6) - + // (c.FT_MulFix(self.face.*.bbox.yMin, self.face.*.size.*.metrics.y_scale) >> 6)); + + // What actually works: + return size; } fn getSize(self: *Sdl2Font) zenolith.layout.Size { diff --git a/src/Sdl2Platform.zig b/src/Sdl2Platform.zig index b5ffd99..cd8bc72 100644 --- a/src/Sdl2Platform.zig +++ b/src/Sdl2Platform.zig @@ -224,7 +224,7 @@ pub fn run( switch (ev.type) { c.SDL_KEYUP => { - try zenolith.treevent.ptrFire(root, zenolith.treevent.KeyPress{ + try zenolith.treevent.fire(root, zenolith.treevent.KeyPress{ .scancode = sc, .modifiers = mods, .action = .up, @@ -232,14 +232,14 @@ pub fn run( }, c.SDL_KEYDOWN => { if (ev.key.repeat == 0) { - try zenolith.treevent.ptrFire(root, zenolith.treevent.KeyPress{ + try zenolith.treevent.fire(root, zenolith.treevent.KeyPress{ .scancode = sc, .modifiers = mods, .action = .down, }); } - try zenolith.treevent.ptrFire(root, zenolith.treevent.KeyPress{ + try zenolith.treevent.fire(root, zenolith.treevent.KeyPress{ .scancode = sc, .modifiers = mods, .action = .press, @@ -252,6 +252,19 @@ pub fn run( } }, + c.SDL_TEXTINPUT => { + var iter = std.unicode.Utf8Iterator{ + .bytes = std.mem.sliceTo(&ev.text.text, 0), + .i = 0, + }; + + while (iter.nextCodepoint()) |codepoint| { + try zenolith.treevent.fire(root, zenolith.treevent.CharType{ + .codepoint = codepoint, + }); + } + }, + else => {}, }