Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct mouse coordinates in stretch mode #5

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading