Skip to content

Commit

Permalink
Merge pull request #18234 from peppy/distance-snap-no-snap-to-zero
Browse files Browse the repository at this point in the history
Fix distance snap providing zero-distance snaps incorrectly
  • Loading branch information
smoogipoo authored May 12, 2022
2 parents 26d4237 + c54ca93 commit 7ab31b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ public void TestDistanceSpacing(float multiplier)
public void TestCursorInCentre()
{
AddStep("move mouse to centre", () => InputManager.MoveMouseTo(grid.ToScreenSpace(grid_position)));
assertSnappedDistance(0);
assertSnappedDistance(beat_length);
}

[Test]
public void TestCursorAlmostInCentre()
{
AddStep("move mouse to almost centre", () => InputManager.MoveMouseTo(grid.ToScreenSpace(grid_position) + new Vector2(1)));
assertSnappedDistance(beat_length);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@ public override (Vector2 position, double time) GetSnappedPosition(Vector2 posit

Vector2 travelVector = (position - StartPosition);

// We need a non-zero travel vector in order to find a valid direction.
if (travelVector == Vector2.Zero)
return (StartPosition, StartTime);
travelVector = new Vector2(0, -1);

float travelLength = travelVector.Length;

// FindSnappedDistance will always round down, but we want to potentially round upwards.
travelLength += DistanceBetweenTicks / 2;

// We never want to snap towards zero.
if (travelLength < DistanceBetweenTicks)
travelLength = DistanceBetweenTicks;

// When interacting with the resolved snap provider, the distance spacing multiplier should first be removed
// to allow for snapping at a non-multiplied ratio.
float snappedDistance = SnapProvider.FindSnappedDistance(ReferenceObject, travelLength / distanceSpacingMultiplier);
Expand Down

0 comments on commit 7ab31b8

Please sign in to comment.