diff --git a/examples/raylib-resizable-sidebar/build.zig b/examples/raylib-resizable-sidebar/build.zig deleted file mode 100644 index f84b6dd..0000000 --- a/examples/raylib-resizable-sidebar/build.zig +++ /dev/null @@ -1,93 +0,0 @@ -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 = "zclay-example", - .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 raylib_dep = b.dependency("raylib-zig", .{ - .target = target, - .optimize = optimize, - }); - - const raylib = raylib_dep.module("raylib"); - const raylib_artifact = raylib_dep.artifact("raylib"); - - compile_step.linkLibrary(raylib_artifact); - compile_step.root_module.addImport("raylib", raylib); - - const zclay_dep = b.dependency("zclay", .{ - .target = target, - .optimize = optimize, - }); - - const zclay = zclay_dep.module("zclay"); - compile_step.root_module.addImport("zclay", zclay); -} diff --git a/examples/raylib-resizable-sidebar/build.zig.zon b/examples/raylib-resizable-sidebar/build.zig.zon deleted file mode 100644 index daa499a..0000000 --- a/examples/raylib-resizable-sidebar/build.zig.zon +++ /dev/null @@ -1,20 +0,0 @@ -.{ - .name = "zig-exe-template", - .version = "0.0.0", - .dependencies = .{ - .zclay = .{ - .path = "../../", - }, - .@"raylib-zig" = .{ - .url = "https://github.com/johan0A/raylib-zig/archive/db141613a6d5fc7c3c94a52965669c0e86444c50.tar.gz", - .hash = "1220a100a2f60cf4c970110666b9f3607fa388c5544de0311df2c36898b516e424b4", - }, - }, - .paths = .{ - "build.zig", - "build.zig.zon", - "src", - "LICENSE", - "README.md", - }, -} diff --git a/examples/raylib-resizable-sidebar/src/main.zig b/examples/raylib-resizable-sidebar/src/main.zig deleted file mode 100644 index a36ae74..0000000 --- a/examples/raylib-resizable-sidebar/src/main.zig +++ /dev/null @@ -1,179 +0,0 @@ -const std = @import("std"); -const rl = @import("raylib"); -const cl = @import("zclay"); -const renderer = @import("raylib_render_clay.zig"); - -const light_grey: cl.Color = .{ 224, 215, 210, 255 }; -const red: cl.Color = .{ 168, 66, 28, 255 }; -const orange: cl.Color = .{ 225, 138, 50, 255 }; - -const sidebar_item_layout: cl.LayoutConfig = .{ - .size = .{ - .w = cl.sizingGrow(.{}), - .h = cl.sizingFixed(50), - }, -}; - -var side_bar_handle: struct { - position: f32, - dragging: bool, - start_drag_pos: f32, - start_drag_mouse_pos: f32, - - fn tick(self: *@This()) void { - const mouse_pos = rl.getMousePosition(); - if (!rl.isMouseButtonDown(.mouse_button_left)) - self.dragging = false; - - if (self.dragging == true) { - if (!rl.isMouseButtonDown(.mouse_button_left)) - self.dragging = false; - self.position = self.start_drag_pos + mouse_pos.x - self.start_drag_mouse_pos; - self.position = @min(@as(f32, @floatFromInt(rl.getScreenWidth() - 100)), @max(270, self.position)); - } else { - if (rl.isMouseButtonPressed(.mouse_button_left) and cl.pointerOver(cl.ID("ResizeHandle"))) { - self.dragging = true; - self.start_drag_mouse_pos = mouse_pos.x; - self.start_drag_pos = self.position; - } - } - if (cl.pointerOver(cl.ID("ResizeHandle")) or self.dragging) { - rl.setMouseCursor(.mouse_cursor_pointing_hand); - } else { - rl.setMouseCursor(.mouse_cursor_arrow); - } - } -} = .{ - .position = 300, - .dragging = false, - .start_drag_pos = 0, - .start_drag_mouse_pos = 0, -}; - -fn sidebarItemCompoment(index: usize) void { - cl.rectangle(cl.IDI("SidebarBlob", @intCast(index)), cl.layout(sidebar_item_layout), cl.rectangleConfig(.{ .color = orange })); - defer cl.closeParent(); -} - -fn createLayout(profile_picture: *const rl.Texture2D) cl.ClayArray(cl.RenderCommand) { - cl.beginLayout(); - { - cl.rectangle( - cl.ID("OuterContainer"), - cl.layout(.{ .direction = .LEFT_TO_RIGHT, .size = .{ .h = cl.sizingGrow(.{}), .w = cl.sizingGrow(.{}) }, .padding = .{ .x = 16, .y = 16 } }), - cl.rectangleConfig(.{ .color = .{ 250, 250, 255, 255 } }), - ); - defer cl.closeParent(); - - { - cl.rectangle( - cl.ID("SideBar"), - cl.layout(.{ - .direction = .TOP_TO_BOTTOM, - .size = .{ .h = cl.sizingGrow(.{}), .w = cl.sizingFixed(side_bar_handle.position) }, - .padding = .{ .x = 16, .y = 16 }, - .alignment = .{ .x = .CENTER, .y = .TOP }, - .gap = 16, - }), - cl.rectangleConfig(.{ .color = light_grey }), - ); - defer cl.closeParent(); - - { - cl.rectangle( - cl.ID("ProfilePictureOuter"), - cl.layout(.{ .size = .{ .w = cl.sizingGrow(.{}) }, .padding = .{ .x = 16, .y = 16 }, .alignment = .{ .y = .CENTER }, .gap = 16 }), - cl.rectangleConfig(.{ .color = red }), - ); - defer cl.closeParent(); - - cl.image( - cl.ID("ProfilePicture"), - cl.layout(.{ .size = .{ .h = cl.sizingFixed(60), .w = cl.sizingFixed(60) } }), - cl.imageConfig(.{ .source_dimensions = .{ .h = 60, .w = 60 }, .image_data = @ptrCast(@constCast(profile_picture)) }), - ); - cl.closeParent(); - cl.text(cl.ID("profileTitle"), "Clay - UI Library", cl.textConfig(.{ .font_size = 24, .text_color = light_grey })); - } - - for (0..5) |i| { - sidebarItemCompoment(i); - } - } - { - cl.rectangle( - cl.ID("ResizeHandle"), - cl.layout(.{ .padding = .{ .x = 7, .y = 7 }, .size = .{ .h = cl.sizingGrow(.{}), .w = cl.sizingFit(.{}) } }), - cl.rectangleConfig(.{ .color = .{ 0, 0, 0, 0 } }), - ); - defer cl.closeParent(); - cl.rectangle(cl.ID("ResizeHandleInner"), cl.layout(.{ .size = .{ .h = cl.sizingGrow(.{}), .w = cl.sizingFixed(2) } }), cl.rectangleConfig(.{ .color = red })); - defer cl.closeParent(); - } - { - cl.rectangle( - cl.ID("MainContent"), - cl.layout(.{ .size = .{ .h = cl.sizingGrow(.{}), .w = cl.sizingGrow(.{}) } }), - cl.rectangleConfig(.{ .color = light_grey }), - ); - defer cl.closeParent(); - } - } - 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, .texture_filter_bilinear); -} - -pub fn main() anyerror!void { - const allocator = std.heap.page_allocator; - - 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); - - rl.setConfigFlags(.{ - .msaa_4x_hint = true, - .vsync_hint = true, - .window_highdpi = true, - .window_resizable = true, - }); - rl.initWindow(1000, 1000, "Raylib zig Example"); - rl.setTargetFPS(60); - - loadFont(@embedFile("./resources/Roboto-Regular.ttf"), 0, 100); - const profile_picture = rl.loadTextureFromImage(rl.loadImageFromMemory(".png", @embedFile("./resources/profile-picture.png"))); - - var debug_mode_enabled = false; - - while (!rl.windowShouldClose()) { - if (rl.isKeyPressed(.key_d)) { - debug_mode_enabled = !debug_mode_enabled; - cl.setDebugModeEnabled(debug_mode_enabled); - } - - const mouse_pos = rl.getMousePosition(); - cl.setPointerState(.{ - .x = mouse_pos.x, - .y = mouse_pos.y, - }, rl.isMouseButtonDown(.mouse_button_left)); - - cl.setLayoutDimensions(.{ - .w = @floatFromInt(rl.getScreenWidth()), - .h = @floatFromInt(rl.getScreenHeight()), - }); - var render_commands = createLayout(&profile_picture); - - rl.beginDrawing(); - renderer.clayRaylibRender(&render_commands, allocator); - rl.endDrawing(); - - side_bar_handle.tick(); - } -} diff --git a/examples/raylib-resizable-sidebar/src/raylib_render_clay.zig b/examples/raylib-resizable-sidebar/src/raylib_render_clay.zig deleted file mode 100644 index 2ffa7e4..0000000 --- a/examples/raylib-resizable-sidebar/src/raylib_render_clay.zig +++ /dev/null @@ -1,232 +0,0 @@ -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.lineSpacing); - 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.text_color), - ); - }, - .Image => { - const image_texture: *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_spacing = config.lineSpacing; - - 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_spacing)); - } - - if (temp_byte_counter < byte_counter) temp_byte_counter = byte_counter; - } - - if (temp_text_width < text_width) temp_text_width = text_width; - - 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/raylib-resizable-sidebar/src/resources/Roboto-Regular.ttf b/examples/raylib-resizable-sidebar/src/resources/Roboto-Regular.ttf deleted file mode 100644 index ddf4bfa..0000000 Binary files a/examples/raylib-resizable-sidebar/src/resources/Roboto-Regular.ttf and /dev/null differ diff --git a/examples/raylib-resizable-sidebar/src/resources/RobotoMono-Medium.ttf b/examples/raylib-resizable-sidebar/src/resources/RobotoMono-Medium.ttf deleted file mode 100644 index f6c149a..0000000 Binary files a/examples/raylib-resizable-sidebar/src/resources/RobotoMono-Medium.ttf and /dev/null differ diff --git a/examples/raylib-resizable-sidebar/src/resources/profile-picture.png b/examples/raylib-resizable-sidebar/src/resources/profile-picture.png deleted file mode 100644 index 8c4ea3e..0000000 Binary files a/examples/raylib-resizable-sidebar/src/resources/profile-picture.png and /dev/null differ