From f6a86434e28bc420ba8437319253794f9313c90c Mon Sep 17 00:00:00 2001 From: johan0A Date: Mon, 13 Jan 2025 00:06:27 +0100 Subject: [PATCH] first pass with unsolved text wrapping bug --- examples/bug/build.zig | 86 +++++ examples/bug/build.zig.zon | 20 ++ examples/bug/src/main.zig | 154 +++++++++ examples/bug/src/raylib_render_clay.zig | 234 ++++++++++++++ .../bug/src/resources/Quicksand-Semibold.ttf | Bin 0 -> 18980 bytes examples/clay-official-website/src/main.zig | 298 ++++++++---------- src/root.zig | 56 +++- test.zig | 16 + 8 files changed, 679 insertions(+), 185 deletions(-) create mode 100644 examples/bug/build.zig create mode 100644 examples/bug/build.zig.zon create mode 100644 examples/bug/src/main.zig create mode 100644 examples/bug/src/raylib_render_clay.zig create mode 100644 examples/bug/src/resources/Quicksand-Semibold.ttf create mode 100644 test.zig diff --git a/examples/bug/build.zig b/examples/bug/build.zig new file mode 100644 index 0000000..464f450 --- /dev/null +++ b/examples/bug/build.zig @@ -0,0 +1,86 @@ +const std = @import("std"); +const B = std.Build; + +pub fn build(b: *B) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + { + const exe = b.addExecutable(.{ + .name = "debug", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + addDependencies(exe, b, target, optimize); + + b.installArtifact(exe); + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + } + + { + const exe_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + addDependencies(exe_unit_tests, b, target, optimize); + + const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_exe_unit_tests.step); + } + + { + const exe_check = b.addExecutable(.{ + .name = "check", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + addDependencies(exe_check, b, target, optimize); + + const tests_check = b.addTest(.{ + .name = "check", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + addDependencies(tests_check, b, target, optimize); + + const check = b.step("check", "Check if exe and tests compile"); + check.dependOn(&exe_check.step); + check.dependOn(&tests_check.step); + } +} + +fn addDependencies( + compile_step: *B.Step.Compile, + b: *B, + target: B.ResolvedTarget, + optimize: std.builtin.OptimizeMode, +) void { + const zclay_dep = b.dependency("zclay", .{ + .target = target, + .optimize = optimize, + }); + const zclay = zclay_dep.module("zclay"); + compile_step.root_module.addImport("zclay", zclay); + + const raylib_dep = b.dependency("raylib-zig", .{ + .target = target, + .optimize = optimize, + }); + const raylib = raylib_dep.module("raylib"); + compile_step.root_module.addImport("raylib", raylib); + const raylib_artifact = raylib_dep.artifact("raylib"); + compile_step.linkLibrary(raylib_artifact); +} diff --git a/examples/bug/build.zig.zon b/examples/bug/build.zig.zon new file mode 100644 index 0000000..68acf3f --- /dev/null +++ b/examples/bug/build.zig.zon @@ -0,0 +1,20 @@ +.{ + .name = "zig-exe-template", + .version = "0.0.0", + .dependencies = .{ + .zclay = .{ + .path = "../../", + }, + .@"raylib-zig" = .{ + .url = "https://github.com/Not-Nik/raylib-zig/archive/35332edacf2e03236220f12a8dd3410b1c39d9eb.tar.gz", + .hash = "1220c34fe472645e173a7168eb513ca8343af3c3f61b618daedda30ec3932b002637", + }, + }, + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + "LICENSE", + "README.md", + }, +} diff --git a/examples/bug/src/main.zig b/examples/bug/src/main.zig new file mode 100644 index 0000000..177a72d --- /dev/null +++ b/examples/bug/src/main.zig @@ -0,0 +1,154 @@ +const std = @import("std"); +const rl = @import("raylib"); +const cl = @import("zclay"); +const renderer = @import("raylib_render_clay.zig"); + +const FONT_ID_BODY_30 = 6; + +const COLOR_LIGHT = cl.Color{ 244, 235, 230, 255 }; +const COLOR_LIGHT_HOVER = cl.Color{ 224, 215, 210, 255 }; +const COLOR_BUTTON_HOVER = cl.Color{ 238, 227, 225, 255 }; +const COLOR_BROWN = cl.Color{ 61, 26, 5, 255 }; +const COLOR_RED = cl.Color{ 168, 66, 28, 255 }; +const COLOR_RED_HOVER = cl.Color{ 148, 46, 8, 255 }; +const COLOR_ORANGE = cl.Color{ 225, 138, 50, 255 }; +const COLOR_BLUE = cl.Color{ 111, 173, 162, 255 }; +const COLOR_TEAL = cl.Color{ 111, 173, 162, 255 }; +const COLOR_BLUE_DARK = cl.Color{ 2, 32, 82, 255 }; +const COLOR_ZIG_LOGO = cl.Color{ 247, 164, 29, 255 }; + +// Colors for top stripe +const COLORS_TOP_BORDER = [_]cl.Color{ + .{ 240, 213, 137, 255 }, + .{ 236, 189, 80, 255 }, + .{ 225, 138, 50, 255 }, + .{ 223, 110, 44, 255 }, + .{ 168, 66, 28, 255 }, +}; + +const border_data = cl.BorderData{ .width = 2, .color = COLOR_RED }; + +var window_height: isize = 0; +var window_width: isize = 0; + +fn LandingPageBlob_(index: u32, font_size: u16, font_id: u16, color: cl.Color, max_width: f32, text: []const u8) void { + // std.debug.print("\nBLOB START\n", .{}); + if (cl.OPEN(&.{ + .IDI("HeroBlob", index), + .layout(.{ .sizing = .{ .w = .growMinMax(.{ .max = max_width }) }, .padding = .all(16), .child_gap = 16, .child_alignment = .{ .y = .CENTER } }), + .border(.outside(color, 2, 0)), + })) { + defer cl.CLOSE(); + cl.text(text, cl.Config.text(.{ .font_size = font_size, .font_id = font_id, .color = color })); + } + // std.debug.print("BLOB END\n\n", .{}); +} + +fn LandingPageBlob(index: u32, font_size: u16, font_id: u16, color: cl.Color, max_width: f32, text: []const u8) void { + // std.debug.print("\nBLOB START\n", .{}); + cl.UI(&.{ + .IDI("HeroBlob", index), + .layout(.{ .sizing = .{ .w = .growMinMax(.{ .max = max_width }) }, .padding = .all(16), .child_gap = 16, .child_alignment = .{ .y = .CENTER } }), + .border(.outside(color, 2, 0)), + })({ + cl.text(text, cl.Config.text(.{ .font_size = font_size, .font_id = font_id, .color = color })); + }); + // std.debug.print("BLOB END\n\n", .{}); +} + +fn createLayout() cl.ClayArray(cl.RenderCommand) { + cl.beginLayout(); + // cl.UI(&.{ + // .ID("ScrollContainerBackgroundRectangle"), + // .scroll(.{ .vertical = true }), + // .layout(.{ .sizing = .grow, .direction = .TOP_TO_BOTTOM, .child_gap = 10 }), + // .rectangle(.{ .color = COLOR_LIGHT }), + // })({ + // LandingPageBlob(1, 30, FONT_ID_BODY_30, COLOR_ZIG_LOGO, 510, "The official Clay website recreated with zclay: clay-zig-bindings"); + // @call(.always_inline, LandingPageBlob, .{ 2, 30, FONT_ID_BODY_30, COLOR_ZIG_LOGO, 510, "The official Clay website recreated with zclay: clay-zig-bindings" }); + // }); + + if (cl.OPEN(&.{ + .ID("ScrollContainerBackgroundRectangle"), + .scroll(.{ .vertical = true }), + .layout(.{ .sizing = .grow, .direction = .TOP_TO_BOTTOM, .child_gap = 10 }), + .rectangle(.{ .color = COLOR_LIGHT }), + })) { + defer cl.CLOSE(); + // LandingPageBlob_(1, 30, FONT_ID_BODY_30, COLOR_ZIG_LOGO, 510, "The official Clay website recreated with zclay: clay-zig-bindings"); + // std.debug.print("\n==\n\n", .{}); + @call(.always_inline, LandingPageBlob_, .{ 2, 30, FONT_ID_BODY_30, COLOR_ZIG_LOGO, 510, "The official Clay website recreated with zclay: clay-zig-bindings" }); + } + + return cl.endLayout(); +} + +fn loadFont(file_data: ?[]const u8, font_id: u16, font_size: i32) void { + renderer.raylib_fonts[font_id] = rl.loadFontFromMemory(".ttf", file_data, font_size * 2, null); + rl.setTextureFilter(renderer.raylib_fonts[font_id].?.texture, .bilinear); +} + +fn loadImage(comptime path: [:0]const u8) rl.Texture2D { + const texture = rl.loadTextureFromImage(rl.loadImageFromMemory(@ptrCast(std.fs.path.extension(path)), @embedFile(path))); + rl.setTextureFilter(texture, .bilinear); + return texture; +} + +pub fn main() anyerror!void { + const allocator = std.heap.page_allocator; + + // init clay + const min_memory_size: u32 = cl.minMemorySize(); + const memory = try allocator.alloc(u8, min_memory_size); + defer allocator.free(memory); + const arena: cl.Arena = cl.createArenaWithCapacityAndMemory(min_memory_size, @ptrCast(memory)); + cl.initialize(arena, .{ .h = 1000, .w = 1000 }); + cl.setMeasureTextFunction(renderer.measureText); + + // init raylib + rl.setConfigFlags(.{ + .msaa_4x_hint = true, + .window_resizable = true, + }); + rl.initWindow(1000, 1000, "Raylib zig Example"); + rl.setWindowMinSize(300, 100); + rl.setTargetFPS(60); + + // load assets + loadFont(@embedFile("resources/Quicksand-Semibold.ttf"), FONT_ID_BODY_30, 30); + + var debug_mode_enabled = false; + while (!rl.windowShouldClose()) { + if (rl.isKeyPressed(.d)) { + debug_mode_enabled = !debug_mode_enabled; + cl.setDebugModeEnabled(debug_mode_enabled); + } + + window_width = rl.getScreenWidth(); + window_height = rl.getScreenHeight(); + + const mouse_pos = rl.getMousePosition(); + cl.setPointerState(.{ + .x = mouse_pos.x, + .y = mouse_pos.y, + }, rl.isMouseButtonDown(.left)); + + const scroll_delta = rl.getMouseWheelMoveV().multiply(.{ .x = 6, .y = 6 }); + cl.updateScrollContainers( + false, + .{ .x = scroll_delta.x, .y = scroll_delta.y }, + rl.getFrameTime(), + ); + + cl.setLayoutDimensions(.{ + .w = @floatFromInt(window_width), + .h = @floatFromInt(window_height), + }); + var render_commands = createLayout(); + + rl.beginDrawing(); + renderer.clayRaylibRender(&render_commands, allocator); + rl.endDrawing(); + // @panic(""); + } +} diff --git a/examples/bug/src/raylib_render_clay.zig b/examples/bug/src/raylib_render_clay.zig new file mode 100644 index 0000000..cb91142 --- /dev/null +++ b/examples/bug/src/raylib_render_clay.zig @@ -0,0 +1,234 @@ +const std = @import("std"); +const rl = @import("raylib"); +const cl = @import("zclay"); +const math = std.math; + +pub fn clayColorToRaylibColor(color: cl.Color) rl.Color { + return rl.Color{ + .r = @intFromFloat(color[0]), + .g = @intFromFloat(color[1]), + .b = @intFromFloat(color[2]), + .a = @intFromFloat(color[3]), + }; +} + +pub var raylib_fonts: [10]?rl.Font = .{null} ** 10; + +pub fn clayRaylibRender(render_commands: *cl.ClayArray(cl.RenderCommand), allocator: std.mem.Allocator) void { + var i: usize = 0; + while (i < render_commands.length) : (i += 1) { + const render_command = cl.renderCommandArrayGet(render_commands, @intCast(i)); + const bounding_box = render_command.bounding_box; + switch (render_command.command_type) { + .None => {}, + .Text => { + const text = render_command.text.chars[0..@intCast(render_command.text.length)]; + const cloned = allocator.dupeZ(c_char, text) catch unreachable; + defer allocator.free(cloned); + const fontToUse: rl.Font = raylib_fonts[render_command.config.text_element_config.font_id].?; + rl.setTextLineSpacing(render_command.config.text_element_config.line_height); + rl.drawTextEx( + fontToUse, + @ptrCast(@alignCast(cloned.ptr)), + rl.Vector2{ .x = bounding_box.x, .y = bounding_box.y }, + @floatFromInt(render_command.config.text_element_config.font_size), + @floatFromInt(render_command.config.text_element_config.letter_spacing), + clayColorToRaylibColor(render_command.config.text_element_config.color), + ); + }, + .Image => { + const image_texture: *const rl.Texture2D = @ptrCast( + @alignCast(render_command.config.image_element_config.image_data), + ); + rl.drawTextureEx( + image_texture.*, + rl.Vector2{ .x = bounding_box.x, .y = bounding_box.y }, + 0, + bounding_box.width / @as(f32, @floatFromInt(image_texture.width)), + rl.Color.white, + ); + }, + .ScissorStart => { + rl.beginScissorMode( + @intFromFloat(math.round(bounding_box.x)), + @intFromFloat(math.round(bounding_box.y)), + @intFromFloat(math.round(bounding_box.width)), + @intFromFloat(math.round(bounding_box.height)), + ); + }, + .ScissorEnd => rl.endScissorMode(), + .Rectangle => { + const config = render_command.config.rectangle_element_config; + if (config.corner_radius.top_left > 0) { + const radius: f32 = (config.corner_radius.top_left * 2) / @min(bounding_box.width, bounding_box.height); + rl.drawRectangleRounded( + rl.Rectangle{ + .x = bounding_box.x, + .y = bounding_box.y, + .width = bounding_box.width, + .height = bounding_box.height, + }, + radius, + 8, + clayColorToRaylibColor(config.color), + ); + } else { + rl.drawRectangle( + @intFromFloat(bounding_box.x), + @intFromFloat(bounding_box.y), + @intFromFloat(bounding_box.width), + @intFromFloat(bounding_box.height), + clayColorToRaylibColor(config.color), + ); + } + }, + .Border => { + const config = render_command.config.border_element_config; + if (config.left.width > 0) { + rl.drawRectangle( + @intFromFloat(math.round(bounding_box.x)), + @intFromFloat(math.round(bounding_box.y + config.corner_radius.top_left)), + @intCast(config.left.width), + @intFromFloat(math.round(bounding_box.height - config.corner_radius.top_left - config.corner_radius.bottom_left)), + clayColorToRaylibColor(config.left.color), + ); + } + if (config.right.width > 0) { + rl.drawRectangle( + @intFromFloat(math.round(bounding_box.x + bounding_box.width - @as(f32, @floatFromInt(config.right.width)))), + @intFromFloat(math.round(bounding_box.y + config.corner_radius.top_right)), + @intCast(config.right.width), + @intFromFloat(math.round(bounding_box.height - config.corner_radius.top_right - config.corner_radius.bottom_right)), + clayColorToRaylibColor(config.right.color), + ); + } + if (config.top.width > 0) { + rl.drawRectangle( + @intFromFloat(math.round(bounding_box.x + config.corner_radius.top_left)), + @intFromFloat(math.round(bounding_box.y)), + @intFromFloat(math.round(bounding_box.width - config.corner_radius.top_left - config.corner_radius.top_right)), + @intCast(config.top.width), + clayColorToRaylibColor(config.top.color), + ); + } + if (config.bottom.width > 0) { + rl.drawRectangle( + @intFromFloat(math.round(bounding_box.x + config.corner_radius.bottom_left)), + @intFromFloat(math.round(bounding_box.y + bounding_box.height - @as(f32, @floatFromInt(config.bottom.width)))), + @intFromFloat(math.round(bounding_box.width - config.corner_radius.bottom_left - config.corner_radius.bottom_right)), + @intCast(config.bottom.width), + clayColorToRaylibColor(config.bottom.color), + ); + } + + if (config.corner_radius.top_left > 0) { + rl.drawRing( + rl.Vector2{ + .x = math.round(bounding_box.x + config.corner_radius.top_left), + .y = math.round(bounding_box.y + config.corner_radius.top_left), + }, + math.round(config.corner_radius.top_left - @as(f32, @floatFromInt(config.top.width))), + config.corner_radius.top_left, + 180, + 270, + 10, + clayColorToRaylibColor(config.top.color), + ); + } + if (config.corner_radius.top_right > 0) { + rl.drawRing( + rl.Vector2{ + .x = math.round(bounding_box.x + bounding_box.width - config.corner_radius.top_right), + .y = math.round(bounding_box.y + config.corner_radius.top_right), + }, + math.round(config.corner_radius.top_right - @as(f32, @floatFromInt(config.top.width))), + config.corner_radius.top_right, + 270, + 360, + 10, + clayColorToRaylibColor(config.top.color), + ); + } + if (config.corner_radius.bottom_left > 0) { + rl.drawRing( + rl.Vector2{ + .x = math.round(bounding_box.x + config.corner_radius.bottom_left), + .y = math.round(bounding_box.y + bounding_box.height - config.corner_radius.bottom_left), + }, + math.round(config.corner_radius.bottom_left - @as(f32, @floatFromInt(config.top.width))), + config.corner_radius.bottom_left, + 90, + 180, + 10, + clayColorToRaylibColor(config.bottom.color), + ); + } + if (config.corner_radius.bottom_right > 0) { + rl.drawRing( + rl.Vector2{ + .x = math.round(bounding_box.x + bounding_box.width - config.corner_radius.bottom_right), + .y = math.round(bounding_box.y + bounding_box.height - config.corner_radius.bottom_right), + }, + math.round(config.corner_radius.bottom_right - @as(f32, @floatFromInt(config.top.width))), + config.corner_radius.bottom_right, + 0.1, + 90, + 10, + clayColorToRaylibColor(config.bottom.color), + ); + } + }, + .Custom => { + // Implement custom element rendering here + }, + } + } +} + +pub fn measureText(clay_text: []const u8, config: *cl.TextElementConfig) cl.Dimensions { + const font = raylib_fonts[config.font_id].?; + const text: []const u8 = clay_text; + const font_size: f32 = @floatFromInt(config.font_size); + const letter_spacing: f32 = @floatFromInt(config.letter_spacing); + const line_height = config.line_height; + + var temp_byte_counter: usize = 0; + var byte_counter: usize = 0; + var text_width: f32 = 0.0; + var temp_text_width: f32 = 0.0; + var text_height: f32 = font_size; + const scale_factor: f32 = font_size / @as(f32, @floatFromInt(font.baseSize)); + + var utf8 = std.unicode.Utf8View.initUnchecked(text).iterator(); + + while (utf8.nextCodepoint()) |codepoint| { + byte_counter += std.unicode.utf8CodepointSequenceLength(codepoint) catch 1; + const index: usize = @intCast( + rl.getGlyphIndex(font, @as(i32, @intCast(codepoint))), + ); + + if (codepoint != '\n') { + if (font.glyphs[index].advanceX != 0) { + text_width += @floatFromInt(font.glyphs[index].advanceX); + } else { + text_width += font.recs[index].width + @as(f32, @floatFromInt(font.glyphs[index].offsetX)); + } + } else { + if (temp_text_width < text_width) temp_text_width = text_width; + byte_counter = 0; + text_width = 0; + text_height += font_size + @as(f32, @floatFromInt(line_height)); + } + + if (temp_byte_counter < byte_counter) temp_byte_counter = byte_counter; + } + + if (temp_text_width < text_width) temp_text_width = text_width; + + std.debug.print("\"{s}\" => .w = {d}\n", .{ clay_text, temp_text_width * scale_factor + (@as(f32, @floatFromInt(temp_byte_counter)) - 1) * letter_spacing }); + + return cl.Dimensions{ + .h = text_height, + .w = temp_text_width * scale_factor + (@as(f32, @floatFromInt(temp_byte_counter)) - 1) * letter_spacing, + }; +} diff --git a/examples/bug/src/resources/Quicksand-Semibold.ttf b/examples/bug/src/resources/Quicksand-Semibold.ttf new file mode 100644 index 0000000000000000000000000000000000000000..27106d00af73ab0dce2ed14694f8a218788aafeb GIT binary patch literal 18980 zcmb_^34CN#neRFGR^3~B-CK97y((3es_d0ilB%S$bdt`}Yr4}3bVDz6x@kHMz0nI> zw++Lh=%}OPIwQ(^z)ul%o{lq$Oar2b_*7&D6dV;5K@gcilvPO8`=5KOlJug_H}h_& zyPSK@cfRxO-*;|9C?Q0`$wtV;=-4=&q=yOVy&0`t6C2h~U+{_5AHnk}LcIStF@4^M z@A&i2pywe%xSQ8c=L9d5adIQf*yY|l>ay{q#9wEgI zc>c<{VCZ$d(|QG5w@VhRX8ON%XMk)xGcNdfckVDAqd-hF6Jc<)Vk@4&N2I3iWv<}T+RAYl?GWSAF(bSj(c zDk;UHQP2%V*Hp8G=Sy`4UGqyJ=+|}1m&J(W4-0SEVgY|h_|>hgG$M1$=AV%QAw_g@ zm&gGckmhf9x%7zQ=R)OUJ9ZGBOaZ4|+$qdePdYGLfk79}<;~jyyUo%Yw_emdJ;>Fq8*OPAS)Z*CEgsi?|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`onoF^Ab{4?zIC9O}-!)`2a?NYV-;zi1_dHg`dNI9W+zyy> z&7nTPvg#ycmz^8&w$_W)RN0&?53jW~VC>?P0R3e>b}wy%?yzwK$aq;@maWQ#3I{4j zn3E#MWUvUdkd553(B4#S_n4z6P}{NkSs1gK7}P1$&6qD_t6)F21L81sS%ti&pmn`~ x$X2t=yf&h0z}D4=bJ|=fTOmaYJ2MEZKF7{c!d OPEN ELEMENT\n", .{}); cdefs.Clay__OpenElement(); - nesting_level += 1; for (configs) |config| { switch (config) { - .Layout => |layoutConf| cdefs.Clay__AttachLayoutConfig(layoutConf), + .Layout => |layoutConf| { + std.debug.print("LAYOUT CONF: {}\n", .{layoutConf}); + cdefs.Clay__AttachLayoutConfig(layoutConf); + }, .id => |id| cdefs.Clay__AttachId(id), - inline else => |elem_config| cdefs.Clay__AttachElementConfig(@ptrCast(elem_config), config), + inline else => |elem_config| { + std.debug.print("elem_config CONF: {}\n", .{elem_config}); + cdefs.Clay__AttachElementConfig(@ptrCast(elem_config), config); + }, } } + std.debug.print("-> POST ELEMENT\n", .{}); + cdefs.Clay__ElementPostConfiguration(); + return struct { + fn f(_: void) void { + std.time.sleep(1000); + std.debug.print("-> CLOSE ELEMENT\n", .{}); + cdefs.Clay__CloseElement(); + } + }.f; +} + +pub fn OPEN(configs: []const Config) bool { + std.debug.print("-> OPEN ELEMENT\n", .{}); + cdefs.Clay__OpenElement(); + for (configs) |config| { + switch (config) { + .Layout => |layoutConf| { + std.debug.print("LAYOUT CONF: {}\n", .{layoutConf}); + cdefs.Clay__AttachLayoutConfig(layoutConf); + }, + .id => |id| cdefs.Clay__AttachId(id), + inline else => |elem_config| { + std.debug.print("elem_config CONF: {}\n", .{elem_config}); + cdefs.Clay__AttachElementConfig(@ptrCast(elem_config), config); + }, + } + } + std.debug.print("-> POST ELEMENT\n", .{}); cdefs.Clay__ElementPostConfiguration(); return true; } pub fn CLOSE() void { - nesting_level -= 1; - if (nesting_level < 0) { - std.debug.panic("clay.CLOSE() was called but no clay element is currently open", .{}); - } + std.debug.print("-> CLOSE ELEMENT\n", .{}); cdefs.Clay__CloseElement(); } @@ -434,12 +463,6 @@ pub fn singleElem(configs: []const Config) void { } pub fn endLayout() ClayArray(RenderCommand) { - if (nesting_level > 0) { - std.debug.panic( - \\clay.endLayout() was called but {} clay elements are still currently open. - \\Make sure every call to clay.OPEN() is matched with a call to clay.CLOSE() before the end of the layout. - , .{nesting_level}); - } return cdefs.Clay_EndLayout(); } @@ -467,6 +490,7 @@ pub fn makeClayString(string: []const u8) String { } pub fn text(string: []const u8, config: Config) void { + // std.debug.print("TEXT 2 conf: {}\n", .{config.Text}); cdefs.Clay__OpenTextElement(makeClayString(string), config.Text); } diff --git a/test.zig b/test.zig new file mode 100644 index 0000000..2e46214 --- /dev/null +++ b/test.zig @@ -0,0 +1,16 @@ +const std = @import("std"); + +fn CLOSE(_: void) void { + std.debug.print("CLOSE\n", .{}); +} + +inline fn UI() fn (void) void { + std.debug.print("OPEN\n", .{}); + return CLOSE; +} + +pub fn main() !void { + UI()({ + std.debug.print("INSIDE\n", .{}); + }); +}