From 597c1a17aec5b3081ef2275651fa1c4b52f2912d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 21 Oct 2023 15:13:27 -0700 Subject: [PATCH] apprt/embedded: render in native dpi --- src/Inspector.zig | 2 +- src/apprt/embedded.zig | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Inspector.zig b/src/Inspector.zig index 0a5f71072..dfbe593d7 100644 --- a/src/Inspector.zig +++ b/src/Inspector.zig @@ -29,7 +29,7 @@ pub fn setup() void { io.Fonts, @constCast(@ptrCast(Surface.face_ttf)), Surface.face_ttf.len, - 24, + 32, font_config, null, ); diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 35ba871df..8d01db594 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -841,6 +841,7 @@ pub const Inspector = struct { ig_ctx: *cimgui.c.ImGuiContext, backend: ?Backend = null, keymap_state: input.Keymap.State = .{}, + content_scale: f64 = 1, /// Our previous instant used to calculate delta time for animations. instant: ?std.time.Instant = null, @@ -952,23 +953,24 @@ pub const Inspector = struct { } pub fn updateContentScale(self: *Inspector, x: f64, y: f64) void { + _ = y; cimgui.c.igSetCurrentContext(self.ig_ctx); - const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO(); - io.DisplayFramebufferScale = .{ - .x = @floatCast(x), - .y = @floatCast(y), - }; + + // Cache our scale because we use it for cursor position calculations. + self.content_scale = x; + + // Setup a new style and scale it appropriately. + const style = cimgui.c.ImGuiStyle_ImGuiStyle(); + defer cimgui.c.ImGuiStyle_destroy(style); + cimgui.c.ImGuiStyle_ScaleAllSizes(style, @floatCast(x)); + const active_style = cimgui.c.igGetStyle(); + active_style.* = style.*; } pub fn updateSize(self: *Inspector, width: u32, height: u32) void { cimgui.c.igSetCurrentContext(self.ig_ctx); const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO(); - const x_scale: u32 = @intFromFloat(io.DisplayFramebufferScale.x); - const y_scale: u32 = @intFromFloat(io.DisplayFramebufferScale.y); - io.DisplaySize = .{ - .x = @floatFromInt(@divFloor(width, x_scale)), - .y = @floatFromInt(@divFloor(height, y_scale)), - }; + io.DisplaySize = .{ .x = @floatFromInt(width), .y = @floatFromInt(height) }; } pub fn mouseButtonCallback( @@ -1015,7 +1017,11 @@ pub const Inspector = struct { self.queueRender(); cimgui.c.igSetCurrentContext(self.ig_ctx); const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO(); - cimgui.c.ImGuiIO_AddMousePosEvent(io, @floatCast(x), @floatCast(y)); + cimgui.c.ImGuiIO_AddMousePosEvent( + io, + @floatCast(x * self.content_scale), + @floatCast(y * self.content_scale), + ); } pub fn focusCallback(self: *Inspector, focused: bool) void {