From 8fd1aea318202e1c5f71aed38cefff4d87426d49 Mon Sep 17 00:00:00 2001 From: Mark Wilson <23439518+wlsnmrk@users.noreply.github.com> Date: Mon, 1 Jul 2024 11:13:12 -0400 Subject: [PATCH] fix: correct mouse coordinates in stretch mode Added a method to transform mouse coordinates for games using fullscreen stretch mode, updated mouse driver extensions to transform coordinates when constructing InputEvents. Fixes mouse clicks not being correctly delivered in fullscreen stretch mode. --- GodotTestDriver/Input/MouseInputExtensions.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/GodotTestDriver/Input/MouseInputExtensions.cs b/GodotTestDriver/Input/MouseInputExtensions.cs index 58461f5..c87ffa0 100644 --- a/GodotTestDriver/Input/MouseInputExtensions.cs +++ b/GodotTestDriver/Input/MouseInputExtensions.cs @@ -29,11 +29,13 @@ public static void ClickMouseAt(this Viewport viewport, Vector2 position, MouseB public static void MoveMouseTo(this Viewport viewport, Vector2 position) { var oldPosition = viewport.GetMousePosition(); + var stretchedPosition = viewport.StretchedPosition(position); + viewport.WarpMouse(position); var inputEvent = new InputEventMouseMotion { - GlobalPosition = position, - Position = position, + GlobalPosition = stretchedPosition, + Position = stretchedPosition, Relative = position - oldPosition }; Input.ParseInputEvent(inputEvent); @@ -88,12 +90,14 @@ public static void ReleaseMouse(this Viewport _, MouseButton button = MouseButto private static void PressMouseAt(this Viewport viewport, Vector2 position, MouseButton button = MouseButton.Left) { viewport.MoveMouseTo(position); + var stretchedPosition = viewport.StretchedPosition(position); var action = new InputEventMouseButton { ButtonIndex = button, Pressed = true, - Position = position + Position = stretchedPosition, + GlobalPosition = stretchedPosition }; Input.ParseInputEvent(action); Input.FlushBufferedEvents(); @@ -102,14 +106,24 @@ private static void PressMouseAt(this Viewport viewport, Vector2 position, Mouse private static void ReleaseMouseAt(this Viewport viewport, Vector2 position, MouseButton button = MouseButton.Left) { viewport.MoveMouseTo(position); + var stretchedPosition = viewport.StretchedPosition(position); var action = new InputEventMouseButton { ButtonIndex = button, Pressed = false, - Position = position + Position = stretchedPosition, + GlobalPosition = stretchedPosition }; Input.ParseInputEvent(action); Input.FlushBufferedEvents(); } + + // Correct for viewport's stretch transform + // see https://docs.godotengine.org/en/stable/tutorials/2d/2d_transforms.html#stretch-transform + private static Vector2 StretchedPosition(this Viewport viewport, Vector2 position) + { + var screenTransform = viewport.GetFinalTransform(); + return screenTransform.BasisXform(position) + screenTransform.Origin; + } }