diff --git a/osu.Game/Rulesets/Objects/SliderPathExtensions.cs b/osu.Game/Rulesets/Objects/SliderPathExtensions.cs index a312f872b8..2c8a6393af 100644 --- a/osu.Game/Rulesets/Objects/SliderPathExtensions.cs +++ b/osu.Game/Rulesets/Objects/SliderPathExtensions.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Utils; using osu.Game.Rulesets.Edit; using osu.Game.Rulesets.Objects.Types; @@ -30,31 +31,40 @@ namespace osu.Game.Rulesets.Objects public static void Reverse(this SliderPath sliderPath, out Vector2 positionalOffset) { var controlPoints = sliderPath.ControlPoints; + var originalControlPointTypes = controlPoints.Select(p => p.Type).ToArray(); + + controlPoints[0].Type ??= PathType.Linear; + + // Inherited points after a linear point should be treated as linear points. + controlPoints.Where(p => sliderPath.PointsInSegment(p)[0].Type == PathType.Linear).ForEach(p => p.Type = PathType.Linear); + double[] segmentEnds = sliderPath.GetSegmentEnds().ToArray(); double[] distinctSegmentEnds = segmentEnds.Distinct().ToArray(); // Remove control points at the end which do not affect the visual slider path ("invisible" control points). - if (segmentEnds[^1] == segmentEnds[^2] && distinctSegmentEnds.Length > 1) + if (Math.Abs(segmentEnds[^1] - segmentEnds[^2]) < 1e-10 && distinctSegmentEnds.Length > 1) { int numVisibleSegments = distinctSegmentEnds.Length - 2; var nonInheritedControlPoints = controlPoints.Where(p => p.Type is not null).ToList(); - var lastVisibleControlPoint = nonInheritedControlPoints[numVisibleSegments]; - int lastVisibleControlPointIndex = controlPoints.IndexOf(lastVisibleControlPoint); + int lastVisibleControlPointIndex = controlPoints.IndexOf(nonInheritedControlPoints[numVisibleSegments]); - if (controlPoints.Count > lastVisibleControlPointIndex + 1) + // Make sure to include all inherited control points directly after the last visible non-inherited control point. + while (lastVisibleControlPointIndex + 1 < controlPoints.Count) { - // Make sure to include all inherited control points directly after the last visible non-inherited control point. - do - { - lastVisibleControlPointIndex++; - } while (lastVisibleControlPointIndex + 1 < controlPoints.Count && controlPoints[lastVisibleControlPointIndex].Type is null); + lastVisibleControlPointIndex++; + + if (controlPoints[lastVisibleControlPointIndex].Type is not null) + break; } // Remove all control points after the first invisible non-inherited control point. controlPoints.RemoveRange(lastVisibleControlPointIndex + 1, controlPoints.Count - lastVisibleControlPointIndex - 1); } + // Restore original control point types. + controlPoints.Zip(originalControlPointTypes).ForEach(x => x.First.Type = x.Second); + // Recalculate perfect curve at the end of the slider path. if (controlPoints.Count >= 3 && controlPoints[^3].Type == PathType.PerfectCurve && controlPoints[^2].Type is null && distinctSegmentEnds.Length > 1) { @@ -75,12 +85,20 @@ namespace osu.Game.Rulesets.Objects controlPoints[^2].Position = newCircleArcPath[newCircleArcPath.Count / 2]; } - // Reverse the control points. + sliderPath.reverseControlPoints(out positionalOffset); + } - var points = controlPoints.ToArray(); + /// + /// Reverses the order of the provided 's s. + /// + /// The . + /// The positional offset of the resulting path. It should be added to the start position of this path. + private static void reverseControlPoints(this SliderPath sliderPath, out Vector2 positionalOffset) + { + var points = sliderPath.ControlPoints.ToArray(); positionalOffset = sliderPath.PositionAt(1); - controlPoints.Clear(); + sliderPath.ControlPoints.Clear(); PathType? lastType = null; @@ -98,7 +116,7 @@ namespace osu.Game.Rulesets.Objects else if (p.Type != null) (p.Type, lastType) = (lastType, p.Type); - controlPoints.Insert(0, p); + sliderPath.ControlPoints.Insert(0, p); } } }