Skip to content

Commit

Permalink
Fix SubViewport mouse position in Godot 4.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Apr 20, 2024
1 parent fd5d0e2 commit f5276d3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dear ImGui for Godot Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Godot.NET.Sdk/4.2.1">
<Project Sdk="Godot.NET.Sdk/4.2.2">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
Expand Down
17 changes: 9 additions & 8 deletions addons/imgui-godot/ImGuiGodot/Internal/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit f5276d3

Please sign in to comment.