From f5276d34d2c03dbc35c40860832f224cd11f67a4 Mon Sep 17 00:00:00 2001 From: Patrick Dawson Date: Sun, 21 Apr 2024 01:30:02 +0200 Subject: [PATCH] Fix SubViewport mouse position in Godot 4.2.2 --- Dear ImGui for Godot Demo.csproj | 2 +- addons/imgui-godot/ImGuiGodot/Internal/Input.cs | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Dear ImGui for Godot Demo.csproj b/Dear ImGui for Godot Demo.csproj index 3bfb6090..a9cb7ce0 100644 --- a/Dear ImGui for Godot Demo.csproj +++ b/Dear ImGui for Godot Demo.csproj @@ -1,4 +1,4 @@ - + net7.0 true diff --git a/addons/imgui-godot/ImGuiGodot/Internal/Input.cs b/addons/imgui-godot/ImGuiGodot/Internal/Input.cs index 8e34a2be..42c2655f 100644 --- a/addons/imgui-godot/ImGuiGodot/Internal/Input.cs +++ b/addons/imgui-godot/ImGuiGodot/Internal/Input.cs @@ -94,19 +94,20 @@ public void Update(ImGuiIOPtr io) public bool ProcessInput(InputEvent evt, Window window) { var io = ImGui.GetIO(); - bool viewportsEnable = io.ConfigFlags.HasFlag(ImGuiConfigFlags.ViewportsEnable); - - var windowPos = Vector2I.Zero; - if (viewportsEnable) - windowPos = window.Position; if (CurrentSubViewport != null) { var vpEvent = evt.Duplicate() as InputEvent; if (vpEvent is InputEventMouse mouseEvent) { - mouseEvent.Position = new Vector2(windowPos.X + mouseEvent.GlobalPosition.X - CurrentSubViewportPos.X, - windowPos.Y + mouseEvent.GlobalPosition.Y - CurrentSubViewportPos.Y) + var mousePos = DisplayServer.MouseGetPosition(); + var windowPos = Vector2I.Zero; + if (!io.ConfigFlags.HasFlag(ImGuiConfigFlags.ViewportsEnable)) + windowPos = window.Position; + + mouseEvent.Position = new Vector2( + mousePos.X - windowPos.X - CurrentSubViewportPos.X, + mousePos.Y - windowPos.Y - CurrentSubViewportPos.Y) .Clamp(Vector2.Zero, CurrentSubViewport.Size); } CurrentSubViewport.PushInput(vpEvent, true); @@ -133,7 +134,7 @@ public bool ProcessInput(InputEvent evt, Window window) io.AddMouseButtonEvent((int)ImGuiMouseButton.Left, mb.Pressed); #if GODOT_WINDOWS && !GODOT4_1_OR_GREATER // if the left mouse button is released, the mouse almost certainly should not be captured - if (viewportsEnable && !mb.Pressed) + if (io.ConfigFlags.HasFlag(ImGuiConfigFlags.ViewportsEnable) && !mb.Pressed) Viewports.MouseCaptureWorkaround(); #endif break;