From 6ac4529f3bec08db1d946d6d174194c386f7c573 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Wed, 30 Aug 2023 05:21:33 -0500 Subject: [PATCH] possibly fix HiDPI input problem on macOS/Wayland issue #1425 --- .../backends/imgui_impl_sdl2.cpp | 21 +++++-------------- extern/imgui_patched/imgui.cpp | 1 + extern/imgui_patched/imgui.h | 1 + src/gui/gui.cpp | 2 ++ 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/extern/imgui_patched/backends/imgui_impl_sdl2.cpp b/extern/imgui_patched/backends/imgui_impl_sdl2.cpp index 919f490e..ddbad7be 100644 --- a/extern/imgui_patched/backends/imgui_impl_sdl2.cpp +++ b/extern/imgui_patched/backends/imgui_impl_sdl2.cpp @@ -322,14 +322,8 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event) mouse_pos.y += window_y; } // Fix for high DPI mac/idevice/wayland - ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); - if (!platform_io.Monitors.empty() && platform_io.Monitors[0].DpiScale > 1.0f) - { - // The Framebuffer is scaled by an integer ceiling of the actual ratio, so 2.0 not 1.685 on Mac! - //printf("multiply by %f\n",platform_io.Monitors[0].DpiScale); - mouse_pos.x *= std::ceil(platform_io.Monitors[0].DpiScale); - mouse_pos.y *= std::ceil(platform_io.Monitors[0].DpiScale); - } + mouse_pos.x *= io.InputScale; + mouse_pos.y *= io.InputScale; io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); io.AddMousePosEvent(mouse_pos.x, mouse_pos.y); return true; @@ -627,13 +621,8 @@ static void ImGui_ImplSDL2_UpdateMouseData() mouse_y -= window_y; } // Fix for high DPI mac/idevice/wayland - ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); - if (!platform_io.Monitors.empty() && platform_io.Monitors[0].DpiScale > 1.0f) - { - // The Framebuffer is scaled by an integer ceiling of the actual ratio, so 2.0 not 1.685 on Mac! - mouse_x *= std::ceil(platform_io.Monitors[0].DpiScale); - mouse_y *= std::ceil(platform_io.Monitors[0].DpiScale); - } + mouse_x *= io.InputScale; + mouse_y *= io.InputScale; io.AddMousePosEvent((float)mouse_x, (float)mouse_y); } } @@ -793,7 +782,7 @@ void ImGui_ImplSDL2_NewFrame() io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f); io.DisplaySize = ImVec2((float)display_w, (float)display_h); //printf("write %d/%d to DpiScale\n",display_w,w); - platform_io.Monitors[0].DpiScale=(float)display_w/(float)w; + io.InputScale=(float)display_w/(float)w; } // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution) diff --git a/extern/imgui_patched/imgui.cpp b/extern/imgui_patched/imgui.cpp index 7d5313b8..71aee935 100644 --- a/extern/imgui_patched/imgui.cpp +++ b/extern/imgui_patched/imgui.cpp @@ -1273,6 +1273,7 @@ ImGuiIO::ImGuiIO() DisplaySize = ImVec2(-1.0f, -1.0f); DeltaTime = 1.0f / 60.0f; IniSavingRate = 5.0f; + InputScale = 1.0f; IniFilename = "imgui.ini"; // Important: "imgui.ini" is relative to current working dir, most apps will want to lock this to an absolute path (e.g. same path as executables). LogFilename = "imgui_log.txt"; #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO diff --git a/extern/imgui_patched/imgui.h b/extern/imgui_patched/imgui.h index 2acd2532..e7966cab 100644 --- a/extern/imgui_patched/imgui.h +++ b/extern/imgui_patched/imgui.h @@ -2060,6 +2060,7 @@ struct ImGuiIO ImVec2 DisplaySize; // // Main display size, in pixels (generally == GetMainViewport()->Size). May change every frame. float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. May change every frame. float IniSavingRate; // = 5.0f // Minimum time between saving positions/sizes to .ini file, in seconds. + float InputScale; // = 1.0f // Used by backend in system managed scale situations. const char* IniFilename; // = "imgui.ini" // Path to .ini file (important: default "imgui.ini" is relative to current working dir!). Set NULL to disable automatic .ini loading/saving or if you want to manually call LoadIniSettingsXXX() / SaveIniSettingsXXX() functions. const char* LogFilename; // = "imgui_log.txt"// Path to .log file (default parameter to ImGui::LogToFile when no file is specified). void* UserData; // = NULL // Store your own data. diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index f9c76d12..45adf116 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -3628,6 +3628,8 @@ bool FurnaceGUI::loop() { if (prevScrW!=scrW || prevScrH!=scrH) { logV("size change 2: %dx%d (from %dx%d)",scrW,scrH,prevScrW,prevScrH); } + + ImGui::GetIO().InputScale=(float)canvasW/(float)scrW; } wantCaptureKeyboard=ImGui::GetIO().WantTextInput;