1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 08:22:56 +08:00
osu-lazer/osu.Game/Rulesets/Objects/SliderPathExtensions.cs
Bartłomiej Dach c09f6ee052
Use slider snapping more liberally to match user expectations
Previously the slider path length would be snapped using the current
beat snap setting on *every* change of the slider path. As it turns out
this is unexpected behaviour in some situations (e.g. when reversing a
path, which is expected to preserve the previous duration, even though
the slider may be technically "unsnapped" at that point in time due to a
different beat snap setting being selected afterwards).
2022-01-07 16:02:04 +01:00

57 lines
2.0 KiB
C#

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Linq;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects.Types;
using osuTK;
#nullable enable
namespace osu.Game.Rulesets.Objects
{
public static class SliderPathExtensions
{
/// <summary>
/// Snaps the provided <paramref name="hitObject"/>'s duration using the <paramref name="snapProvider"/>.
/// </summary>
public static void SnapTo<THitObject>(this THitObject hitObject, IPositionSnapProvider? snapProvider)
where THitObject : HitObject, IHasPath
{
hitObject.Path.ExpectedDistance.Value = snapProvider?.GetSnappedDistanceFromDistance(hitObject, (float)hitObject.Path.CalculatedDistance) ?? hitObject.Path.CalculatedDistance;
}
/// <summary>
/// Reverse the direction of this path.
/// </summary>
/// <param name="sliderPath">The <see cref="SliderPath"/>.</param>
/// <param name="positionalOffset">The positional offset of the resulting path. It should be added to the start position of this path.</param>
public static void Reverse(this SliderPath sliderPath, out Vector2 positionalOffset)
{
var points = sliderPath.ControlPoints.ToArray();
positionalOffset = sliderPath.PositionAt(1);
sliderPath.ControlPoints.Clear();
PathType? lastType = null;
for (int i = 0; i < points.Length; i++)
{
var p = points[i];
p.Position -= positionalOffset;
// propagate types forwards to last null type
if (i == points.Length - 1)
{
p.Type = lastType;
p.Position = Vector2.Zero;
}
else if (p.Type != null)
(p.Type, lastType) = (lastType, p.Type);
sliderPath.ControlPoints.Insert(0, p);
}
}
}
}