2021-07-22 07:14:43 +00:00
|
|
|
// 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.
|
|
|
|
|
2023-08-12 18:30:48 +00:00
|
|
|
using System.Collections.Generic;
|
2021-07-22 07:14:43 +00:00
|
|
|
using System.Linq;
|
2022-01-07 14:11:38 +00:00
|
|
|
using osu.Game.Rulesets.Edit;
|
2021-07-22 07:14:43 +00:00
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Objects
|
|
|
|
{
|
|
|
|
public static class SliderPathExtensions
|
|
|
|
{
|
2022-01-07 14:11:38 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Snaps the provided <paramref name="hitObject"/>'s duration using the <paramref name="snapProvider"/>.
|
|
|
|
/// </summary>
|
2022-04-28 02:48:45 +00:00
|
|
|
public static void SnapTo<THitObject>(this THitObject hitObject, IDistanceSnapProvider? snapProvider)
|
2022-01-07 14:11:38 +00:00
|
|
|
where THitObject : HitObject, IHasPath
|
|
|
|
{
|
2022-05-05 07:25:05 +00:00
|
|
|
hitObject.Path.ExpectedDistance.Value = snapProvider?.FindSnappedDistance(hitObject, (float)hitObject.Path.CalculatedDistance) ?? hitObject.Path.CalculatedDistance;
|
2022-01-07 14:11:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:14:43 +00:00
|
|
|
/// <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)
|
|
|
|
{
|
2023-08-12 18:30:48 +00:00
|
|
|
var controlPoints = sliderPath.ControlPoints;
|
2023-08-14 12:08:02 +00:00
|
|
|
|
2023-11-08 10:43:54 +00:00
|
|
|
var inheritedLinearPoints = controlPoints.Where(p => sliderPath.PointsInSegment(p)[0].Type == PathType.LINEAR && p.Type is null).ToList();
|
2023-08-14 12:08:02 +00:00
|
|
|
|
2023-08-15 21:27:12 +00:00
|
|
|
// Inherited points after a linear point, as well as the first control point if it inherited,
|
|
|
|
// should be treated as linear points, so their types are temporarily changed to linear.
|
2023-11-08 10:43:54 +00:00
|
|
|
inheritedLinearPoints.ForEach(p => p.Type = PathType.LINEAR);
|
2023-08-14 12:08:02 +00:00
|
|
|
|
2023-08-12 18:30:48 +00:00
|
|
|
double[] segmentEnds = sliderPath.GetSegmentEnds().ToArray();
|
|
|
|
|
2023-08-19 00:40:18 +00:00
|
|
|
// Remove segments after the end of the slider.
|
|
|
|
for (int numSegmentsToRemove = segmentEnds.Count(se => se >= 1) - 1; numSegmentsToRemove > 0 && controlPoints.Count > 0;)
|
2023-08-12 18:30:48 +00:00
|
|
|
{
|
2023-08-19 00:40:18 +00:00
|
|
|
if (controlPoints.Last().Type is not null)
|
2023-08-12 18:30:48 +00:00
|
|
|
{
|
2023-08-19 00:40:18 +00:00
|
|
|
numSegmentsToRemove--;
|
|
|
|
segmentEnds = segmentEnds[..^1];
|
2023-08-12 18:30:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 00:40:18 +00:00
|
|
|
controlPoints.RemoveAt(controlPoints.Count - 1);
|
2023-08-12 18:30:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 12:08:02 +00:00
|
|
|
// Restore original control point types.
|
2023-08-15 21:27:12 +00:00
|
|
|
inheritedLinearPoints.ForEach(p => p.Type = null);
|
2023-08-14 12:08:02 +00:00
|
|
|
|
2023-08-19 00:40:18 +00:00
|
|
|
// Recalculate middle perfect curve control points at the end of the slider path.
|
2023-11-13 07:24:09 +00:00
|
|
|
if (controlPoints.Count >= 3 && controlPoints[^3].Type == PathType.PERFECT_CURVE && controlPoints[^2].Type is null && segmentEnds.Any())
|
2023-08-12 18:30:48 +00:00
|
|
|
{
|
2023-08-19 00:40:18 +00:00
|
|
|
double lastSegmentStart = segmentEnds.Length > 1 ? segmentEnds[^2] : 0;
|
|
|
|
double lastSegmentEnd = segmentEnds[^1];
|
2023-08-12 18:30:48 +00:00
|
|
|
|
2023-08-14 19:09:58 +00:00
|
|
|
var circleArcPath = new List<Vector2>();
|
|
|
|
sliderPath.GetPathToProgress(circleArcPath, lastSegmentStart / lastSegmentEnd, 1);
|
2023-08-12 18:30:48 +00:00
|
|
|
|
2023-08-14 19:09:58 +00:00
|
|
|
controlPoints[^2].Position = circleArcPath[circleArcPath.Count / 2];
|
2023-08-12 18:30:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 12:08:02 +00:00
|
|
|
sliderPath.reverseControlPoints(out positionalOffset);
|
|
|
|
}
|
2023-08-12 18:30:48 +00:00
|
|
|
|
2023-08-14 12:08:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Reverses the order of the provided <see cref="SliderPath"/>'s <see cref="PathControlPoint"/>s.
|
|
|
|
/// </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>
|
|
|
|
private static void reverseControlPoints(this SliderPath sliderPath, out Vector2 positionalOffset)
|
|
|
|
{
|
|
|
|
var points = sliderPath.ControlPoints.ToArray();
|
2021-10-26 04:05:46 +00:00
|
|
|
positionalOffset = sliderPath.PositionAt(1);
|
2021-07-22 07:14:43 +00:00
|
|
|
|
2023-08-14 12:08:02 +00:00
|
|
|
sliderPath.ControlPoints.Clear();
|
2021-07-22 07:14:43 +00:00
|
|
|
|
|
|
|
PathType? lastType = null;
|
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
for (int i = 0; i < points.Length; i++)
|
2021-07-22 07:14:43 +00:00
|
|
|
{
|
|
|
|
var p = points[i];
|
2021-08-25 16:42:57 +00:00
|
|
|
p.Position -= positionalOffset;
|
2021-07-22 07:14:43 +00:00
|
|
|
|
|
|
|
// propagate types forwards to last null type
|
|
|
|
if (i == points.Length - 1)
|
2021-10-26 04:05:46 +00:00
|
|
|
{
|
2021-08-25 16:42:57 +00:00
|
|
|
p.Type = lastType;
|
2021-10-26 04:05:46 +00:00
|
|
|
p.Position = Vector2.Zero;
|
|
|
|
}
|
2021-08-25 16:42:57 +00:00
|
|
|
else if (p.Type != null)
|
|
|
|
(p.Type, lastType) = (lastType, p.Type);
|
2021-07-22 07:14:43 +00:00
|
|
|
|
2023-08-14 12:08:02 +00:00
|
|
|
sliderPath.ControlPoints.Insert(0, p);
|
2021-07-22 07:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|