Skip to content

Commit

Permalink
Merge pull request #5 from wlsnmrk/fix-mouse-stretch
Browse files Browse the repository at this point in the history
fix: correct mouse coordinates in stretch mode
  • Loading branch information
jolexxa authored Jul 1, 2024
2 parents a2efb65 + 8fd1aea commit 851532e
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions GodotTestDriver/Input/MouseInputExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}

0 comments on commit 851532e

Please sign in to comment.