apprt/gtk: imgui widget calls callback to populate imgui frame

This commit is contained in:
Mitchell Hashimoto
2023-10-18 17:24:03 -07:00
parent c0ace7a29e
commit f3d99efcd8

View File

@ -11,13 +11,18 @@ const input = @import("../../input.zig");
const log = std.log.scoped(.gtk_imgui_widget); const log = std.log.scoped(.gtk_imgui_widget);
/// This is called every frame to populate the ImGui frame.
render_callback: ?*const fn (?*anyopaque) void = null,
render_userdata: ?*anyopaque = null,
/// Our OpenGL widget /// Our OpenGL widget
gl_area: *c.GtkGLArea, gl_area: *c.GtkGLArea,
im_context: *c.GtkIMContext, im_context: *c.GtkIMContext,
/// ImGui Context
ig_ctx: *cimgui.c.ImGuiContext, ig_ctx: *cimgui.c.ImGuiContext,
/// Our previous instant used to calculate delta time. /// Our previous instant used to calculate delta time for animations.
instant: ?std.time.Instant = null, instant: ?std.time.Instant = null,
/// Initialize the widget. This must have a stable pointer for events. /// Initialize the widget. This must have a stable pointer for events.
@ -105,6 +110,12 @@ pub fn deinit(self: *ImguiWidget) void {
cimgui.c.igDestroyContext(self.ig_ctx); cimgui.c.igDestroyContext(self.ig_ctx);
} }
/// This should be called anytime the underlying data for the UI changes
/// so that the UI can be refreshed.
pub fn queueRender(self: *ImguiWidget) void {
c.gtk_gl_area_queue_render(self.gl_area);
}
/// Initialize the frame. Expects that the context is already current. /// Initialize the frame. Expects that the context is already current.
fn newFrame(self: *ImguiWidget) !void { fn newFrame(self: *ImguiWidget) !void {
const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO(); const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO();
@ -119,10 +130,6 @@ fn newFrame(self: *ImguiWidget) !void {
self.instant = now; self.instant = now;
} }
fn queueRender(self: *ImguiWidget) void {
c.gtk_gl_area_queue_render(self.gl_area);
}
fn translateMouseButton(button: c.guint) ?c_int { fn translateMouseButton(button: c.guint) ?c_int {
return switch (button) { return switch (button) {
1 => cimgui.c.ImGuiMouseButton_Left, 1 => cimgui.c.ImGuiMouseButton_Left,
@ -204,6 +211,7 @@ fn gtkRender(area: *c.GtkGLArea, ctx: *c.GdkGLContext, ud: ?*anyopaque) callconv
// Build our UI // Build our UI
var show: bool = true; var show: bool = true;
cimgui.c.igShowDemoWindow(&show); cimgui.c.igShowDemoWindow(&show);
if (self.render_callback) |cb| cb(self.render_userdata);
// Render // Render
cimgui.c.igRender(); cimgui.c.igRender();