2019-12-05 15:45:02 +08: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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2019-12-05 15:45:02 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Lines;
|
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual.Gameplay
|
|
|
|
{
|
2019-12-05 16:49:32 +08:00
|
|
|
public partial class TestSceneSliderPath : OsuTestScene
|
2019-12-05 15:45:02 +08:00
|
|
|
{
|
|
|
|
private readonly SmoothPath drawablePath;
|
2019-12-05 16:49:32 +08:00
|
|
|
private SliderPath path;
|
2019-12-05 15:45:02 +08:00
|
|
|
|
2019-12-05 16:49:32 +08:00
|
|
|
public TestSceneSliderPath()
|
2019-12-05 15:45:02 +08:00
|
|
|
{
|
|
|
|
Child = drawablePath = new SmoothPath
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup() => Schedule(() =>
|
|
|
|
{
|
2019-12-05 16:49:32 +08:00
|
|
|
path = new SliderPath();
|
2019-12-05 15:45:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
if (path != null)
|
|
|
|
{
|
|
|
|
List<Vector2> vertices = new List<Vector2>();
|
|
|
|
path.GetPathToProgress(vertices, 0, 1);
|
|
|
|
|
|
|
|
drawablePath.Vertices = vertices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestEmptyPath()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:43:54 +08:00
|
|
|
[TestCase(SplineType.Linear, null)]
|
|
|
|
[TestCase(SplineType.BSpline, null)]
|
|
|
|
[TestCase(SplineType.BSpline, 3)]
|
|
|
|
[TestCase(SplineType.Catmull, null)]
|
|
|
|
[TestCase(SplineType.PerfectCurve, null)]
|
|
|
|
public void TestSingleSegment(SplineType splineType, int? degree)
|
|
|
|
=> AddStep("create path", () => path.ControlPoints.AddRange(createSegment(
|
2023-11-11 22:02:06 +08:00
|
|
|
new PathType { Type = splineType, Degree = degree },
|
2023-11-08 18:43:54 +08:00
|
|
|
Vector2.Zero,
|
|
|
|
new Vector2(0, 100),
|
|
|
|
new Vector2(100),
|
|
|
|
new Vector2(0, 200),
|
|
|
|
new Vector2(200)
|
|
|
|
)));
|
|
|
|
|
|
|
|
[TestCase(SplineType.Linear, null)]
|
|
|
|
[TestCase(SplineType.BSpline, null)]
|
|
|
|
[TestCase(SplineType.BSpline, 3)]
|
|
|
|
[TestCase(SplineType.Catmull, null)]
|
|
|
|
[TestCase(SplineType.PerfectCurve, null)]
|
|
|
|
public void TestMultipleSegment(SplineType splineType, int? degree)
|
2019-12-05 15:45:02 +08:00
|
|
|
{
|
|
|
|
AddStep("create path", () =>
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero));
|
2023-11-11 22:02:06 +08:00
|
|
|
path.ControlPoints.AddRange(createSegment(new PathType { Type = splineType, Degree = degree }, new Vector2(0, 100), new Vector2(100), Vector2.Zero));
|
2019-12-05 15:45:02 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestAddControlPoint()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100))));
|
2021-08-26 00:42:57 +08:00
|
|
|
AddStep("add point", () => path.ControlPoints.Add(new PathControlPoint { Position = new Vector2(100) }));
|
2019-12-05 15:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestInsertControlPoint()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(100))));
|
2021-08-26 00:42:57 +08:00
|
|
|
AddStep("insert point", () => path.ControlPoints.Insert(1, new PathControlPoint { Position = new Vector2(0, 100) }));
|
2019-12-05 15:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestRemoveControlPoint()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100))));
|
2019-12-05 15:45:02 +08:00
|
|
|
AddStep("remove second point", () => path.ControlPoints.RemoveAt(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestChangePathType()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100))));
|
|
|
|
AddStep("change type to bezier", () => path.ControlPoints[0].Type = PathType.BEZIER);
|
2019-12-05 15:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestAddSegmentByChangingType()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100), new Vector2(100, 0))));
|
|
|
|
AddStep("change second point type to bezier", () => path.ControlPoints[1].Type = PathType.BEZIER);
|
2019-12-05 15:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestRemoveSegmentByChangingType()
|
|
|
|
{
|
|
|
|
AddStep("create path", () =>
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100), new Vector2(100, 0)));
|
|
|
|
path.ControlPoints[1].Type = PathType.BEZIER;
|
2019-12-05 15:45:02 +08:00
|
|
|
});
|
|
|
|
|
2021-08-26 00:42:57 +08:00
|
|
|
AddStep("change second point type to null", () => path.ControlPoints[1].Type = null);
|
2019-12-05 15:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestRemoveSegmentByRemovingControlPoint()
|
|
|
|
{
|
|
|
|
AddStep("create path", () =>
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100), new Vector2(100, 0)));
|
|
|
|
path.ControlPoints[1].Type = PathType.BEZIER;
|
2019-12-05 15:45:02 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
AddStep("remove second point", () => path.ControlPoints.RemoveAt(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase(2)]
|
|
|
|
[TestCase(4)]
|
|
|
|
public void TestPerfectCurveFallbackScenarios(int points)
|
|
|
|
{
|
|
|
|
AddStep("create path", () =>
|
|
|
|
{
|
|
|
|
switch (points)
|
|
|
|
{
|
|
|
|
case 2:
|
2023-11-13 15:24:09 +08:00
|
|
|
path.ControlPoints.AddRange(createSegment(PathType.PERFECT_CURVE, Vector2.Zero, new Vector2(0, 100)));
|
2019-12-05 15:45:02 +08:00
|
|
|
break;
|
2019-12-09 15:44:19 +08:00
|
|
|
|
2019-12-05 15:45:02 +08:00
|
|
|
case 4:
|
2023-11-13 15:24:09 +08:00
|
|
|
path.ControlPoints.AddRange(createSegment(PathType.PERFECT_CURVE, Vector2.Zero, new Vector2(0, 100), new Vector2(100), new Vector2(100, 0)));
|
2019-12-05 15:45:02 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2019-12-06 14:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestLengthenLastSegment()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100))));
|
2019-12-06 14:37:00 +08:00
|
|
|
AddStep("lengthen last segment", () => path.ExpectedDistance.Value = 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestShortenLastSegment()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100))));
|
2019-12-06 14:37:00 +08:00
|
|
|
AddStep("shorten last segment", () => path.ExpectedDistance.Value = 150);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestShortenFirstSegment()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100))));
|
2019-12-06 14:37:00 +08:00
|
|
|
AddStep("shorten first segment", () => path.ExpectedDistance.Value = 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestShortenToZeroLength()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100))));
|
2019-12-06 14:37:00 +08:00
|
|
|
AddStep("shorten to 0 length", () => path.ExpectedDistance.Value = 0);
|
|
|
|
}
|
2019-12-05 15:45:02 +08:00
|
|
|
|
2019-12-06 14:37:00 +08:00
|
|
|
[Test]
|
|
|
|
public void TestShortenToNegativeLength()
|
|
|
|
{
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(createSegment(PathType.LINEAR, Vector2.Zero, new Vector2(0, 100), new Vector2(100))));
|
2019-12-06 14:37:00 +08:00
|
|
|
AddStep("shorten to -10 length", () => path.ExpectedDistance.Value = -10);
|
2019-12-05 15:45:02 +08:00
|
|
|
}
|
|
|
|
|
2023-08-18 18:36:34 +08:00
|
|
|
[Test]
|
|
|
|
public void TestGetSegmentEnds()
|
|
|
|
{
|
|
|
|
var positions = new[]
|
|
|
|
{
|
|
|
|
Vector2.Zero,
|
|
|
|
new Vector2(100, 0),
|
|
|
|
new Vector2(100),
|
|
|
|
new Vector2(200, 100),
|
|
|
|
};
|
|
|
|
double[] distances = { 100d, 200d, 300d };
|
|
|
|
|
2023-11-08 18:43:54 +08:00
|
|
|
AddStep("create path", () => path.ControlPoints.AddRange(positions.Select(p => new PathControlPoint(p, PathType.LINEAR))));
|
2023-08-19 20:33:26 +08:00
|
|
|
AddAssert("segment ends are correct", () => path.GetSegmentEnds(), () => Is.EqualTo(distances.Select(d => d / 300)));
|
|
|
|
AddAssert("segment end positions recovered", () => path.GetSegmentEnds().Select(p => path.PositionAt(p)), () => Is.EqualTo(positions.Skip(1)));
|
|
|
|
|
2023-08-18 18:36:34 +08:00
|
|
|
AddStep("lengthen last segment", () => path.ExpectedDistance.Value = 400);
|
2023-08-19 20:33:26 +08:00
|
|
|
AddAssert("segment ends are correct", () => path.GetSegmentEnds(), () => Is.EqualTo(distances.Select(d => d / 400)));
|
|
|
|
AddAssert("segment end positions recovered", () => path.GetSegmentEnds().Select(p => path.PositionAt(p)), () => Is.EqualTo(positions.Skip(1)));
|
|
|
|
|
2023-08-18 18:36:34 +08:00
|
|
|
AddStep("shorten last segment", () => path.ExpectedDistance.Value = 150);
|
2023-08-19 20:33:26 +08:00
|
|
|
AddAssert("segment ends are correct", () => path.GetSegmentEnds(), () => Is.EqualTo(distances.Select(d => d / 150)));
|
2023-08-21 14:27:08 +08:00
|
|
|
// see remarks in `GetSegmentEnds()` xmldoc (`SliderPath.PositionAt()` clamps progress to [0,1]).
|
2023-08-19 20:33:26 +08:00
|
|
|
AddAssert("segment end positions not recovered", () => path.GetSegmentEnds().Select(p => path.PositionAt(p)), () => Is.EqualTo(new[]
|
2023-08-18 18:36:34 +08:00
|
|
|
{
|
|
|
|
positions[1],
|
|
|
|
new Vector2(100, 50),
|
|
|
|
new Vector2(100, 50),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-12-05 15:45:02 +08:00
|
|
|
private List<PathControlPoint> createSegment(PathType type, params Vector2[] controlPoints)
|
|
|
|
{
|
2021-08-26 00:42:57 +08:00
|
|
|
var points = controlPoints.Select(p => new PathControlPoint { Position = p }).ToList();
|
|
|
|
points[0].Type = type;
|
2019-12-05 15:45:02 +08:00
|
|
|
return points;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|