1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-05 15:53:21 +08:00

Fix linear sliders sometimes being reversed incorrectly

Extract control point reversing to separate method
This commit is contained in:
Pasi4K5 2023-08-14 14:08:02 +02:00
parent 8912a0e91e
commit f42b3603b3

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Rulesets.Edit; using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects.Types; 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) public static void Reverse(this SliderPath sliderPath, out Vector2 positionalOffset)
{ {
var controlPoints = sliderPath.ControlPoints; 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[] segmentEnds = sliderPath.GetSegmentEnds().ToArray();
double[] distinctSegmentEnds = segmentEnds.Distinct().ToArray(); double[] distinctSegmentEnds = segmentEnds.Distinct().ToArray();
// Remove control points at the end which do not affect the visual slider path ("invisible" control points). // 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; int numVisibleSegments = distinctSegmentEnds.Length - 2;
var nonInheritedControlPoints = controlPoints.Where(p => p.Type is not null).ToList(); var nonInheritedControlPoints = controlPoints.Where(p => p.Type is not null).ToList();
var lastVisibleControlPoint = nonInheritedControlPoints[numVisibleSegments]; int lastVisibleControlPointIndex = controlPoints.IndexOf(nonInheritedControlPoints[numVisibleSegments]);
int lastVisibleControlPointIndex = controlPoints.IndexOf(lastVisibleControlPoint);
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. lastVisibleControlPointIndex++;
do
{ if (controlPoints[lastVisibleControlPointIndex].Type is not null)
lastVisibleControlPointIndex++; break;
} while (lastVisibleControlPointIndex + 1 < controlPoints.Count && controlPoints[lastVisibleControlPointIndex].Type is null);
} }
// Remove all control points after the first invisible non-inherited control point. // Remove all control points after the first invisible non-inherited control point.
controlPoints.RemoveRange(lastVisibleControlPointIndex + 1, controlPoints.Count - lastVisibleControlPointIndex - 1); 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. // 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) 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]; controlPoints[^2].Position = newCircleArcPath[newCircleArcPath.Count / 2];
} }
// Reverse the control points. sliderPath.reverseControlPoints(out positionalOffset);
}
var points = controlPoints.ToArray(); /// <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();
positionalOffset = sliderPath.PositionAt(1); positionalOffset = sliderPath.PositionAt(1);
controlPoints.Clear(); sliderPath.ControlPoints.Clear();
PathType? lastType = null; PathType? lastType = null;
@ -98,7 +116,7 @@ namespace osu.Game.Rulesets.Objects
else if (p.Type != null) else if (p.Type != null)
(p.Type, lastType) = (lastType, p.Type); (p.Type, lastType) = (lastType, p.Type);
controlPoints.Insert(0, p); sliderPath.ControlPoints.Insert(0, p);
} }
} }
} }