|
| 1 | +# zgui |
| 2 | + |
| 3 | +Zig build package and bindings for [Dear Imgui](https://github.com/ocornut/imgui) & extras. |
| 4 | + |
| 5 | +Easy to use, hand-crafted API with default arguments, named parameters and Zig style text formatting. [Here](https://github.com/michal-z/zig-gamedev/tree/main/samples/minimal_zgpu_zgui) is a simple sample application, and [here](https://github.com/michal-z/zig-gamedev/tree/main/samples/gui_test_wgpu) is a full one. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +* Most public dear imgui API exposed |
| 10 | +* All memory allocations go through user provided Zig allocator |
| 11 | +* [DrawList API](#drawlist-api) for vector graphics, text rendering and custom widgets |
| 12 | +* [Test engine API](#test-engine-api) for automatic testing |
| 13 | +* [Plot API](#plot-api) for advanced data visualizations |
| 14 | +* [Gizmo API](#gizmo-api) for gizmo |
| 15 | +* [Node editor API](#node-editor-api) for node based stuff |
| 16 | + |
| 17 | +## Versions |
| 18 | + |
| 19 | +* [ImGui](https://github.com/ocornut/imgui/tree/v1.91.0-docking) `1.91.0-docking` |
| 20 | +* [ImGui test engine](https://github.com/ocornut/imgui_test_engine/tree/v1.91.0) `1.91.0` |
| 21 | +* [ImPlot](https://github.com/epezent/implot) `O.17` |
| 22 | +* [ImGuizmo](https://github.com/CedricGuillemet/ImGuizmo) `1.89 WIP` |
| 23 | +* [ImGuiNodeEditor](https://github.com/thedmd/imgui-node-editor/tree/v0.9.3) `O.9.3` |
| 24 | + |
| 25 | +## Getting started |
| 26 | + |
| 27 | +Copy `zgui` to a subdirectory in your project and add the following to your `build.zig.zon` .dependencies: |
| 28 | +```zig |
| 29 | + .zgui = .{ .path = "libs/zgui" }, |
| 30 | +``` |
| 31 | + |
| 32 | +To get glfw/wgpu rendering backend working also copy `zglfw`, `system-sdk`, `zgpu` and `zpool` folders and add the depenency paths (see [zgpu](https://github.com/zig-gamedev/zig-gamedev/tree/main/libs/zgpu) for the details). |
| 33 | + |
| 34 | +Then in your `build.zig` add: |
| 35 | +```zig |
| 36 | +
|
| 37 | +pub fn build(b: *std.Build) void { |
| 38 | + const exe = b.addExecutable(.{ ... }); |
| 39 | +
|
| 40 | + const zgui = b.dependency("zgui", .{ |
| 41 | + .shared = false, |
| 42 | + .with_implot = true, |
| 43 | + }); |
| 44 | + exe.root_module.addImport("zgui", zgui.module("root")); |
| 45 | + exe.linkLibrary(zgui.artifact("imgui")); |
| 46 | + |
| 47 | + { // Needed for glfw/wgpu rendering backend |
| 48 | + const zglfw = b.dependency("zglfw", .{}); |
| 49 | + exe.root_module.addImport("zglfw", zglfw.module("root")); |
| 50 | + exe.linkLibrary(zglfw.artifact("glfw")); |
| 51 | +
|
| 52 | + const zpool = b.dependency("zpool", .{}); |
| 53 | + exe.root_module.addImport("zpool", zpool.module("root")); |
| 54 | +
|
| 55 | + const zgpu = b.dependency("zgpu", .{}); |
| 56 | + exe.root_module.addImport("zgpu", zgpu.module("root")); |
| 57 | + exe.linkLibrary(zgpu.artifact("zdawn")); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +Now in your code you may import and use `zgui`: |
| 63 | + |
| 64 | +```zig |
| 65 | +const zgui = @import("zgui"); |
| 66 | +
|
| 67 | +zgui.init(allocator); |
| 68 | +
|
| 69 | +_ = zgui.io.addFontFromFile(content_dir ++ "Roboto-Medium.ttf", 16.0); |
| 70 | +
|
| 71 | +zgui.backend.init( |
| 72 | + window, |
| 73 | + demo.gctx.device, |
| 74 | + @enumToInt(swapchain_format), |
| 75 | + @enumToInt(depth_format), |
| 76 | +); |
| 77 | +``` |
| 78 | + |
| 79 | +```zig |
| 80 | +// Main loop |
| 81 | +while (...) { |
| 82 | + zgui.backend.newFrame(framebuffer_width, framebuffer_height); |
| 83 | +
|
| 84 | + zgui.bulletText( |
| 85 | + "Average : {d:.3} ms/frame ({d:.1} fps)", |
| 86 | + .{ demo.gctx.stats.average_cpu_time, demo.gctx.stats.fps }, |
| 87 | + ); |
| 88 | + zgui.bulletText("W, A, S, D : move camera", .{}); |
| 89 | + zgui.spacing(); |
| 90 | +
|
| 91 | + if (zgui.button("Setup Scene", .{})) { |
| 92 | + // Button pressed. |
| 93 | + } |
| 94 | +
|
| 95 | + if (zgui.dragFloat("Drag 1", .{ .v = &value0 })) { |
| 96 | + // value0 has changed |
| 97 | + } |
| 98 | +
|
| 99 | + if (zgui.dragFloat("Drag 2", .{ .v = &value0, .min = -1.0, .max = 1.0 })) { |
| 100 | + // value1 has changed |
| 101 | + } |
| 102 | +
|
| 103 | + // Setup wgpu render pass here |
| 104 | +
|
| 105 | + zgui.backend.draw(pass); |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### Building a shared library |
| 110 | + |
| 111 | +If your project spans multiple zig modules that both use ImGui, such as an exe paired with a dll, you may want to build the `zgui` dependencies (`zgui_pkg.zgui_c_cpp`) as a shared library. This can be enabled with the `shared` build option. Then, in `build.zig`, use `zgui_pkg.link` to link `zgui` to all the modules that use ImGui. |
| 112 | + |
| 113 | +When built this way, the ImGui context will be located in the shared library. However, the `zgui` zig code (which is compiled separately into each module) requires its own memory buffer which has to be initialized separately with `initNoContext`. |
| 114 | + |
| 115 | +In your executable: |
| 116 | +```zig |
| 117 | +const zgui = @import("zgui"); |
| 118 | +zgui.init(allocator); |
| 119 | +defer zgui.deinit(); |
| 120 | +``` |
| 121 | + |
| 122 | +In your shared library: |
| 123 | +```zig |
| 124 | +const zgui = @import("zgui"); |
| 125 | +zgui.initNoContext(allocator); |
| 126 | +defer zgui.deinitNoContxt(); |
| 127 | +``` |
| 128 | + |
| 129 | +### DrawList API |
| 130 | + |
| 131 | +```zig |
| 132 | +draw_list.addQuad(.{ |
| 133 | + .p1 = .{ 170, 420 }, |
| 134 | + .p2 = .{ 270, 420 }, |
| 135 | + .p3 = .{ 220, 520 }, |
| 136 | + .p4 = .{ 120, 520 }, |
| 137 | + .col = 0xff_00_00_ff, |
| 138 | + .thickness = 3.0, |
| 139 | +}); |
| 140 | +draw_list.addText(.{ 130, 130 }, 0xff_00_00_ff, "The number is: {}", .{7}); |
| 141 | +draw_list.addCircleFilled(.{ .p = .{ 200, 600 }, .r = 50, .col = 0xff_ff_ff_ff }); |
| 142 | +draw_list.addCircle(.{ .p = .{ 200, 600 }, .r = 30, .col = 0xff_00_00_ff, .thickness = 11 }); |
| 143 | +draw_list.addPolyline( |
| 144 | + &.{ .{ 100, 700 }, .{ 200, 600 }, .{ 300, 700 }, .{ 400, 600 } }, |
| 145 | + .{ .col = 0xff_00_aa_11, .thickness = 7 }, |
| 146 | +); |
| 147 | +``` |
| 148 | + |
| 149 | +### Test Engine API |
| 150 | +Zig wraper for [ImGUI test engine](https://github.com/ocornut/imgui_test_engine). |
| 151 | + |
| 152 | +```zig |
| 153 | +var check_b = false; |
| 154 | +var _te: *zgui.te.TestEngine = zgui.te.getTestEngine().?; |
| 155 | +fn registerTests() void { |
| 156 | + _ = _te.registerTest( |
| 157 | + "Awesome", |
| 158 | + "should_do_some_another_magic", |
| 159 | + @src(), |
| 160 | + struct { |
| 161 | + pub fn gui(ctx: *zgui.te.TestContext) !void { |
| 162 | + _ = ctx; // autofix |
| 163 | + _ = zgui.begin("Test Window", .{ .flags = .{ .no_saved_settings = true } }); |
| 164 | + defer zgui.end(); |
| 165 | +
|
| 166 | + zgui.text("Hello, automation world", .{}); |
| 167 | + _ = zgui.button("Click Me", .{}); |
| 168 | + if (zgui.treeNode("Node")) { |
| 169 | + defer zgui.treePop(); |
| 170 | +
|
| 171 | + _ = zgui.checkbox("Checkbox", .{ .v = &check_b }); |
| 172 | + } |
| 173 | + } |
| 174 | +
|
| 175 | + pub fn run(ctx: *zgui.te.TestContext) !void { |
| 176 | + ctx.setRef("/Test Window"); |
| 177 | + ctx.windowFocus(""); |
| 178 | +
|
| 179 | + ctx.itemAction(.click, "Click Me", .{}, null); |
| 180 | + ctx.itemAction(.open, "Node", .{}, null); |
| 181 | + ctx.itemAction(.check, "Node/Checkbox", .{}, null); |
| 182 | + ctx.itemAction(.uncheck, "Node/Checkbox", .{}, null); |
| 183 | +
|
| 184 | + std.testing.expect(true) catch |err| { |
| 185 | + zgui.te.checkTestError(@src(), err); |
| 186 | + return; |
| 187 | + }; |
| 188 | + } |
| 189 | + }, |
| 190 | + ); |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +### Plot API |
| 195 | +```zig |
| 196 | +if (zgui.plot.beginPlot("Line Plot", .{ .h = -1.0 })) { |
| 197 | + zgui.plot.setupAxis(.x1, .{ .label = "xaxis" }); |
| 198 | + zgui.plot.setupAxisLimits(.x1, .{ .min = 0, .max = 5 }); |
| 199 | + zgui.plot.setupLegend(.{ .south = true, .west = true }, .{}); |
| 200 | + zgui.plot.setupFinish(); |
| 201 | + zgui.plot.plotLineValues("y data", i32, .{ .v = &.{ 0, 1, 0, 1, 0, 1 } }); |
| 202 | + zgui.plot.plotLine("xy data", f32, .{ |
| 203 | + .xv = &.{ 0.1, 0.2, 0.5, 2.5 }, |
| 204 | + .yv = &.{ 0.1, 0.3, 0.5, 0.9 }, |
| 205 | + }); |
| 206 | + zgui.plot.endPlot(); |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | +### Gizmo API |
| 211 | + |
| 212 | +Zig wraper for [ImGuizmo](https://github.com/CedricGuillemet/ImGuizmo). |
| 213 | + |
| 214 | + |
| 215 | +### Node editor API |
| 216 | + |
| 217 | +Zig wraper for [ImGuiNodeEditor](https://github.com/thedmd/imgui-node-editor). |
| 218 | + |
| 219 | +```zig |
| 220 | +var node_editor = zgui.node_editor.EditorContext.create(.{ .enable_smooth_zoom = true }), |
| 221 | +
|
| 222 | +zgui.node_editor.setCurrentEditor(node_editor); |
| 223 | +defer zgui.node_editor.setCurrentEditor(null); |
| 224 | +{ |
| 225 | + zgui.node_editor.begin("NodeEditor", .{ 0, 0 }); |
| 226 | + defer zgui.node_editor.end(); |
| 227 | +
|
| 228 | + zgui.node_editor.beginNode(1); |
| 229 | + { |
| 230 | + defer zgui.node_editor.endNode(); |
| 231 | +
|
| 232 | + zgui.textUnformatted("Node A"); |
| 233 | +
|
| 234 | + zgui.node_editor.beginPin(1, .input); |
| 235 | + { |
| 236 | + defer zgui.node_editor.endPin(); |
| 237 | + zgui.textUnformatted("-> In"); |
| 238 | + } |
| 239 | +
|
| 240 | + zgui.sameLine(.{}); |
| 241 | +
|
| 242 | + zgui.node_editor.beginPin(2, .output); |
| 243 | + { |
| 244 | + defer zgui.node_editor.endPin(); |
| 245 | + zgui.textUnformatted("Out ->"); |
| 246 | + } |
| 247 | + } |
| 248 | +} |
| 249 | +``` |
0 commit comments