From 6346872c3931b795fd20bad5326b1d62acff776e Mon Sep 17 00:00:00 2001 From: Pasi4K5 Date: Tue, 15 Aug 2023 23:27:12 +0200 Subject: [PATCH] Improve code readability and add assertion to test scene --- .../Editor/TestSceneSliderReversal.cs | 10 +++++++++- .../Rulesets/Objects/SliderPathExtensions.cs | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/osu.Game.Rulesets.Osu.Tests/Editor/TestSceneSliderReversal.cs b/osu.Game.Rulesets.Osu.Tests/Editor/TestSceneSliderReversal.cs index b804680f0d..9c5eb83e3c 100644 --- a/osu.Game.Rulesets.Osu.Tests/Editor/TestSceneSliderReversal.cs +++ b/osu.Game.Rulesets.Osu.Tests/Editor/TestSceneSliderReversal.cs @@ -48,8 +48,8 @@ namespace osu.Game.Rulesets.Osu.Tests.Editor [TestCase(0, 250)] [TestCase(0, 200)] - [TestCase(1, 80)] [TestCase(1, 120)] + [TestCase(1, 80)] public void TestSliderReversal(int pathIndex, double length) { var controlPoints = paths[pathIndex]; @@ -57,6 +57,7 @@ namespace osu.Game.Rulesets.Osu.Tests.Editor Vector2 oldStartPos = default; Vector2 oldEndPos = default; double oldDistance = default; + var oldControlPointTypes = controlPoints.Select(p => p.Type); AddStep("Add slider", () => { @@ -97,6 +98,13 @@ namespace osu.Game.Rulesets.Osu.Tests.Editor AddAssert("Slider has correct end position", () => Vector2.Distance(selectedSlider.EndPosition, oldStartPos) < 1); + + AddAssert("Control points have correct types", () => + { + var newControlPointTypes = selectedSlider.Path.ControlPoints.Select(p => p.Type).ToArray(); + + return oldControlPointTypes.Take(newControlPointTypes.Length).SequenceEqual(newControlPointTypes); + }); } } } diff --git a/osu.Game/Rulesets/Objects/SliderPathExtensions.cs b/osu.Game/Rulesets/Objects/SliderPathExtensions.cs index 9e9b542961..682e31fa96 100644 --- a/osu.Game/Rulesets/Objects/SliderPathExtensions.cs +++ b/osu.Game/Rulesets/Objects/SliderPathExtensions.cs @@ -3,7 +3,6 @@ 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,12 +29,17 @@ 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; + var inheritedLinearPoints = controlPoints.Where(p => sliderPath.PointsInSegment(p)[0].Type == PathType.Linear && p.Type is null).ToList(); - // 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); + if (controlPoints[0].Type == null) + { + inheritedLinearPoints.Add(controlPoints[0]); + } + + // 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. + inheritedLinearPoints.ForEach(p => p.Type = PathType.Linear); double[] segmentEnds = sliderPath.GetSegmentEnds().ToArray(); double[] distinctSegmentEnds = segmentEnds.Distinct().ToArray(); @@ -62,10 +66,7 @@ namespace osu.Game.Rulesets.Objects } // Restore original control point types. - for (int i = 0; i < controlPoints.Count; i++) - { - controlPoints[i].Type = originalControlPointTypes[i]; - } + inheritedLinearPoints.ForEach(p => p.Type = null); // 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)