diff --git a/README.md b/README.md index c55ab19..1c5ff6b 100644 --- a/README.md +++ b/README.md @@ -24,24 +24,28 @@ const zglfw = @import("libs/zglfw/build.zig"); const zgpu = @import("libs/zgpu/build.zig"); const zpool = @import("libs/zpool/build.zig"); -pub fn build(b: *std.build.Builder) void { +pub fn build(b: *std.Build) void { ... - const zgui_options = zgui.BuildOptionsStep.init(b, .{ .backend = .glfw_wgpu }); - const zgui_pkg = zgui.getPkg(&.{zgui_options.getPkg()}); + const zgui_pkg = zgui.package(b, .{ + .options = .{ .backend = .glfw_wgpu }, + }); - exe.addPackage(zgui_pkg); + exe.addModule("zgui", zgui_pkg.module); - zgui.link(exe, zgui_options); + zgui.link(exe, zgui_pkg.options); // Needed for glfw/wgpu rendering backend - const zgpu_options = zgpu.BuildOptionsStep.init(b, .{}); - const zgpu_pkg = zgpu.getPkg(&.{ zgpu_options.getPkg(), zpool.pkg, zglfw.pkg }); + const zglfw_pkg = zglfw.package(b, .{}); + const zpool_pkg = zpool.package(b, .{}); + const zgpu_pkg = zgpu.package(b, .{ + .deps = .{ .zpool = zpool_pkg.module, .zglfw = zglfw_pkg.module }, + }); - exe.addPackage(zglfw.pkg); - exe.addPackage(zgpu_pkg); + exe.addModule("zgpu", zgpu_pkg.module); + exe.addModule("zglfw", zglfw_pkg.module); zglfw.link(exe); - zgpu.link(exe, zgpu_options); + zgpu.link(exe); } ``` Now in your code you may import and use `zgui`: diff --git a/build.zig b/build.zig index c5ec3b8..b78d460 100644 --- a/build.zig +++ b/build.zig @@ -6,43 +6,44 @@ pub const Backend = enum { win32_dx12, }; -pub const BuildOptions = struct { +pub const Options = struct { backend: Backend, }; -pub const BuildOptionsStep = struct { - options: BuildOptions, - step: *std.build.OptionsStep, - - pub fn init(b: *std.build.Builder, options: BuildOptions) BuildOptionsStep { - const bos = .{ - .options = options, - .step = b.addOptions(), - }; - bos.step.addOption(Backend, "backend", bos.options.backend); - return bos; - } - - pub fn getPkg(bos: BuildOptionsStep) std.build.Pkg { - return bos.step.getPackage("zgui_options"); - } - - fn addTo(bos: BuildOptionsStep, target_step: *std.build.LibExeObjStep) void { - target_step.addOptions("zgui_options", bos.step); - } +pub const Package = struct { + module: *std.Build.Module, + options: Options, + options_module: *std.Build.Module, }; -pub fn getPkg(dependencies: []const std.build.Pkg) std.build.Pkg { +pub fn package( + b: *std.Build, + args: struct { + options: Options, + }, +) Package { + const step = b.addOptions(); + step.addOption(Backend, "backend", args.options.backend); + + const options_module = step.createModule(); + + const module = b.createModule(.{ + .source_file = .{ .path = thisDir() ++ "/src/main.zig" }, + .dependencies = &.{ + .{ .name = "zgui_options", .module = options_module }, + }, + }); + return .{ - .name = "zgui", - .source = .{ .path = thisDir() ++ "/src/main.zig" }, - .dependencies = dependencies, + .module = module, + .options = args.options, + .options_module = options_module, }; } -pub fn link(exe: *std.build.LibExeObjStep, bos: BuildOptionsStep) void { - bos.addTo(exe); +pub fn build(_: *std.Build) void {} +pub fn link(exe: *std.Build.CompileStep, options: Options) void { exe.addIncludePath(thisDir() ++ "/libs"); exe.addIncludePath(thisDir() ++ "/libs/imgui"); @@ -63,7 +64,7 @@ pub fn link(exe: *std.build.LibExeObjStep, bos: BuildOptionsStep) void { exe.addCSourceFile(thisDir() ++ "/libs/imgui/implot.cpp", cflags); exe.addCSourceFile(thisDir() ++ "/libs/imgui/implot_items.cpp", cflags); - switch (bos.options.backend) { + switch (options.backend) { .glfw_wgpu => { exe.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_glfw.cpp", cflags); exe.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_wgpu.cpp", cflags); diff --git a/src/gui.zig b/src/gui.zig index bd5c85f..afa09f4 100644 --- a/src/gui.zig +++ b/src/gui.zig @@ -252,6 +252,16 @@ pub const io = struct { pub const addCharacterEvent = zguiIoAddCharacterEvent; extern fn zguiIoAddCharacterEvent(char: i32) void; }; + +pub fn setClipboardText(value: [:0]const u8) void { + zguiSetClipboardText(value.ptr); +} +pub fn getClipboardText() [:0]const u8 { + const value = zguiGetClipboardText(); + return std.mem.span(value); +} +extern fn zguiSetClipboardText(text: [*:0]const u8) void; +extern fn zguiGetClipboardText() [*:0]const u8; //-------------------------------------------------------------------------------------------------- const Context = *opaque {}; pub const DrawData = *extern struct { @@ -943,9 +953,12 @@ pub const pushItemWidth = zguiPushItemWidth; pub const popItemWidth = zguiPopItemWidth; /// `void setNextItemWidth(item_width: f32) void` pub const setNextItemWidth = zguiSetNextItemWidth; +/// `void setItemDefaultFocus() void` +pub const setItemDefaultFocus = zguiSetItemDefaultFocus; extern fn zguiPushItemWidth(item_width: f32) void; extern fn zguiPopItemWidth() void; extern fn zguiSetNextItemWidth(item_width: f32) void; +extern fn zguiSetItemDefaultFocus() void; //-------------------------------------------------------------------------------------------------- /// `pub fn getFont() Font` pub const getFont = zguiGetFont; @@ -3292,6 +3305,14 @@ pub const DrawList = *opaque { } extern fn zguiDrawList_ResetForNewFrame(draw_list: DrawList) void; + pub fn clearMemory(draw_list: DrawList) void { + if (draw_list.getOwnerName()) |owner| { + @panic(format("zgui: illegally clearing memory DrawList of {s}", .{owner})); + } + zguiDrawList_ClearFreeMemory(draw_list); + } + extern fn zguiDrawList_ClearFreeMemory(draw_list: DrawList) void; + //---------------------------------------------------------------------------------------------- pub const getVertexBufferLength = zguiDrawList_GetVertexBufferLength; extern fn zguiDrawList_GetVertexBufferLength(draw_list: DrawList) i32; @@ -3829,7 +3850,7 @@ pub const DrawList = *opaque { uvmax: *const [2]f32, col: u32, rounding: f32, - flags: u32, + flags: DrawFlags, ) void; //---------------------------------------------------------------------------------------------- pub const pathClear = zguiDrawList_PathClear; @@ -3923,7 +3944,7 @@ pub const DrawList = *opaque { p3: [2]f32, num_segments: u32 = 0, }; - pub fn pathPathBezierQuadraticCurveTo(draw_list: DrawList, args: PathBezierQuadraticCurveTo) void { + pub fn pathBezierQuadraticCurveTo(draw_list: DrawList, args: PathBezierQuadraticCurveTo) void { zguiDrawList_PathBezierQuadraticCurveTo(draw_list, &args.p2, &args.p3, args.num_segments); } extern fn zguiDrawList_PathBezierQuadraticCurveTo( @@ -3950,4 +3971,95 @@ pub const DrawList = *opaque { flags: DrawFlags, ) void; //---------------------------------------------------------------------------------------------- + pub const primReserve = zguiDrawList_PrimReserve; + pub const primUnreserve = zguiDrawList_PrimUnreserve; + pub fn primRect( + draw_list: DrawList, + a: [2]f32, + b: [2]f32, + col: u32, + ) void { + return zguiDrawList_PrimRect(draw_list, &a, &b, col); + } + pub fn primRectUV( + draw_list: DrawList, + a: [2]f32, + b: [2]f32, + uv_a: [2]f32, + uv_b: [2]f32, + col: u32, + ) void { + return zguiDrawList_PrimRectUV(draw_list, &a, &b, &uv_a, &uv_b, col); + } + pub fn primQuadUV( + draw_list: DrawList, + a: [2]f32, + b: [2]f32, + c: [2]f32, + d: [2]f32, + uv_a: [2]f32, + uv_b: [2]f32, + uv_c: [2]f32, + uv_d: [2]f32, + col: u32, + ) void { + return zguiDrawList_PrimQuadUV(draw_list, &a, &b, &c, &d, &uv_a, &uv_b, &uv_c, &uv_d, col); + } + pub fn primWriteVtx( + draw_list: DrawList, + pos: [2]f32, + uv: [2]f32, + col: u32, + ) void { + return zguiDrawList_PrimWriteVtx(draw_list, &pos, &uv, col); + } + pub const primWriteIdx = zguiDrawList_PrimWriteIdx; + + extern fn zguiDrawList_PrimReserve( + draw_list: DrawList, + idx_count: i32, + vtx_count: i32, + ) void; + extern fn zguiDrawList_PrimUnreserve( + draw_list: DrawList, + idx_count: i32, + vtx_count: i32, + ) void; + extern fn zguiDrawList_PrimRect( + draw_list: DrawList, + a: *const [2]f32, + b: *const [2]f32, + col: u32, + ) void; + extern fn zguiDrawList_PrimRectUV( + draw_list: DrawList, + a: *const [2]f32, + b: *const [2]f32, + uv_a: *const [2]f32, + uv_b: *const [2]f32, + col: u32, + ) void; + extern fn zguiDrawList_PrimQuadUV( + draw_list: DrawList, + a: *const [2]f32, + b: *const [2]f32, + c: *const [2]f32, + d: *const [2]f32, + uv_a: *const [2]f32, + uv_b: *const [2]f32, + uv_c: *const [2]f32, + uv_d: *const [2]f32, + col: u32, + ) void; + extern fn zguiDrawList_PrimWriteVtx( + draw_list: DrawList, + pos: *const [2]f32, + uv: *const [2]f32, + col: u32, + ) void; + extern fn zguiDrawList_PrimWriteIdx( + draw_list: DrawList, + idx: DrawIdx, + ) void; + //---------------------------------------------------------------------------------------------- }; diff --git a/src/zgui.cpp b/src/zgui.cpp index 45774f9..af71e0b 100644 --- a/src/zgui.cpp +++ b/src/zgui.cpp @@ -1040,6 +1040,10 @@ ZGUI_API void zguiSetNextItemWidth(float item_width) { ImGui::SetNextItemWidth(item_width); } +ZGUI_API void zguiSetItemDefaultFocus(void) { + ImGui::SetItemDefaultFocus(); +} + ZGUI_API ImFont* zguiGetFont(void) { return ImGui::GetFont(); } @@ -1148,6 +1152,14 @@ ZGUI_API ImGuiID zguiGetPtrId(const void* ptr_id) { return ImGui::GetID(ptr_id); } +ZGUI_API void zguiSetClipboardText(const char* text) { + ImGui::SetClipboardText(text); +} + +ZGUI_API const char* zguiGetClipboardText(void) { + return ImGui::GetClipboardText(); +} + ZGUI_API ImFont* zguiIoAddFontFromFileWithConfig( const char* filename, float size_pixels, @@ -1572,6 +1584,10 @@ ZGUI_API void zguiDrawList_ResetForNewFrame(ImDrawList *draw_list) { draw_list->_ResetForNewFrame(); } +ZGUI_API void zguiDrawList_ClearFreeMemory(ImDrawList *draw_list) { + draw_list->_ClearFreeMemory(); +} + ZGUI_API int zguiDrawList_GetVertexBufferLength(ImDrawList *draw_list) { return draw_list->VtxBuffer.size(); } @@ -1976,6 +1992,66 @@ ZGUI_API void zguiDrawList_PathRect( ) { draw_list->PathRect({ rect_min[0], rect_min[1] }, { rect_max[0], rect_max[1] }, rounding, flags); } + +ZGUI_API void zguiDrawList_PrimReserve( ImDrawList* draw_list, int idx_count, int vtx_count) { + draw_list->PrimReserve(idx_count, vtx_count); +} + +ZGUI_API void zguiDrawList_PrimUnreserve( ImDrawList* draw_list, int idx_count, int vtx_count) { + draw_list->PrimUnreserve(idx_count, vtx_count); +} + +ZGUI_API void zguiDrawList_PrimRect( + ImDrawList* draw_list, + const float a[2], + const float b[2], + unsigned int col +) { + draw_list->PrimRect({ a[0], a[1] }, { b[0], b[1] }, col); +} + +ZGUI_API void zguiDrawList_PrimRectUV( + ImDrawList* draw_list, + const float a[2], + const float b[2], + const float uv_a[2], + const float uv_b[2], + unsigned int col +) { + draw_list->PrimRectUV({ a[0], a[1] }, { b[0], b[1] }, { uv_a[0], uv_a[1] }, { uv_b[0], uv_b[1] }, col); +} + +ZGUI_API void zguiDrawList_PrimQuadUV( + ImDrawList* draw_list, + const float a[2], + const float b[2], + const float c[2], + const float d[2], + const float uv_a[2], + const float uv_b[2], + const float uv_c[2], + const float uv_d[2], + unsigned int col +) { + draw_list->PrimQuadUV( + { a[0], a[1] }, { b[0], b[1] }, { c[0], c[1] }, { d[0], d[1] }, + { uv_a[0], uv_a[1] }, { uv_b[0], uv_b[1] }, { uv_c[0], uv_c[1] }, { uv_d[0], uv_d[1] }, + col + ); +} + +ZGUI_API void zguiDrawList_PrimWriteVtx( + ImDrawList* draw_list, + const float pos[2], + const float uv[2], + unsigned int col +) { + draw_list->PrimWriteVtx({ pos[0], pos[1] }, { uv[0], uv[1] }, col); +} + +ZGUI_API void zguiDrawList_PrimWriteIdx( ImDrawList* draw_list, ImDrawIdx idx) { + draw_list->PrimWriteIdx(idx); +} //-------------------------------------------------------------------------------------------------- // // Viewport