1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 20:25:39 +08:00

Rewrite CircularDistanceSnapGrid snapping implementation to use snap provider

This commit is contained in:
Dean Herbert 2022-05-05 17:50:33 +09:00
parent 947a68006a
commit b2e9be70a5

View File

@ -68,19 +68,29 @@ namespace osu.Game.Screens.Edit.Compose.Components
if (MaxIntervals == 0) if (MaxIntervals == 0)
return (StartPosition, StartTime); return (StartPosition, StartTime);
Vector2 direction = position - StartPosition; // This grid implementation factors in the user's distance spacing specification,
if (direction == Vector2.Zero) // which is usually not considered by an `IDistanceSnapProvider`.
direction = new Vector2(0.001f, 0.001f); float distanceSpacing = (float)DistanceSpacingMultiplier.Value;
float distance = direction.Length; Vector2 travelVector = (position - StartPosition);
float radius = DistanceBetweenTick; if (travelVector == Vector2.Zero)
int radialCount = Math.Clamp((int)MathF.Round(distance / radius), 1, MaxIntervals); return (StartPosition, StartTime);
Vector2 normalisedDirection = direction * new Vector2(1f / distance); float travelLength = travelVector.Length;
Vector2 snappedPosition = StartPosition + normalisedDirection * radialCount * radius;
return (snappedPosition, StartTime + SnapProvider.FindSnappedDuration(ReferenceObject, (float)((snappedPosition - StartPosition).Length / DistanceSpacingMultiplier.Value))); // FindSnappedDistance will always round down, but we want to potentially round upwards.
travelLength += DistanceBetweenTick / 2;
// 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 / distanceSpacing);
double snappedTime = StartTime + SnapProvider.DistanceToDuration(ReferenceObject, snappedDistance);
// The multiplier can then be reapplied to the final position.
Vector2 snappedPosition = StartPosition + travelVector.Normalized() * snappedDistance * distanceSpacing;
return (snappedPosition, snappedTime);
} }
} }
} }