1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:07:44 +08:00

Added tests

This commit is contained in:
OliBomby 2022-10-26 19:30:42 +02:00
parent 77dcd0fae2
commit 86d5fcc26d
2 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,172 @@
// 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.
#nullable disable
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Lines;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osuTK;
namespace osu.Game.Tests.Visual.Gameplay
{
public class TestSceneBezierConverter : OsuTestScene
{
private readonly SmoothPath drawablePath;
private readonly SmoothPath controlPointDrawablePath;
private readonly SmoothPath convertedDrawablePath;
private readonly SmoothPath convertedControlPointDrawablePath;
private SliderPath path;
private SliderPath convertedPath;
public TestSceneBezierConverter()
{
Children = new Drawable[]
{
new Container
{
Children =
new Drawable[]
{
drawablePath = new SmoothPath(),
controlPointDrawablePath = new SmoothPath
{
Colour = Colour4.Magenta,
PathRadius = 1f
}
},
Position = new Vector2(100)
},
new Container
{
Children =
new Drawable[]
{
convertedDrawablePath = new SmoothPath(),
convertedControlPointDrawablePath = new SmoothPath
{
Colour = Colour4.Magenta,
PathRadius = 1f
}
},
Position = new Vector2(100, 300)
}
};
}
[SetUp]
public void Setup() => Schedule(() =>
{
path = new SliderPath();
convertedPath = new SliderPath();
path.Version.ValueChanged += getConvertedControlPoints;
});
private void getConvertedControlPoints(ValueChangedEvent<int> obj)
{
convertedPath.ControlPoints.Clear();
convertedPath.ControlPoints.AddRange(BezierConverter.ConvertToModernBezier(path.ControlPoints));
}
protected override void Update()
{
base.Update();
if (path != null)
{
List<Vector2> vertices = new List<Vector2>();
path.GetPathToProgress(vertices, 0, 1);
drawablePath.Vertices = vertices;
controlPointDrawablePath.Vertices = path.ControlPoints.Select(o => o.Position).ToList();
if (controlPointDrawablePath.Vertices.Count > 0)
controlPointDrawablePath.Position = drawablePath.PositionInBoundingBox(drawablePath.Vertices[0]) - controlPointDrawablePath.PositionInBoundingBox(controlPointDrawablePath.Vertices[0]);
}
if (convertedPath != null)
{
List<Vector2> vertices = new List<Vector2>();
convertedPath.GetPathToProgress(vertices, 0, 1);
convertedDrawablePath.Vertices = vertices;
convertedControlPointDrawablePath.Vertices = convertedPath.ControlPoints.Select(o => o.Position).ToList();
if (convertedControlPointDrawablePath.Vertices.Count > 0)
convertedControlPointDrawablePath.Position = convertedDrawablePath.PositionInBoundingBox(convertedDrawablePath.Vertices[0]) - convertedControlPointDrawablePath.PositionInBoundingBox(convertedControlPointDrawablePath.Vertices[0]);
}
}
[Test]
public void TestEmptyPath()
{
}
[TestCase(PathType.Linear)]
[TestCase(PathType.Bezier)]
[TestCase(PathType.Catmull)]
[TestCase(PathType.PerfectCurve)]
public void TestSingleSegment(PathType type)
=> AddStep("create path", () => path.ControlPoints.AddRange(createSegment(type, Vector2.Zero, new Vector2(0, 100), new Vector2(100))));
[TestCase(PathType.Linear)]
[TestCase(PathType.Bezier)]
[TestCase(PathType.Catmull)]
[TestCase(PathType.PerfectCurve)]
public void TestMultipleSegment(PathType type)
{
AddStep("create path", () =>
{
path.ControlPoints.AddRange(createSegment(PathType.Linear, Vector2.Zero));
path.ControlPoints.AddRange(createSegment(type, new Vector2(0, 100), new Vector2(100), Vector2.Zero));
});
}
[TestCase(0, 100)]
[TestCase(1, 100)]
[TestCase(5, 100)]
[TestCase(10, 100)]
[TestCase(30, 100)]
[TestCase(50, 100)]
[TestCase(100, 100)]
[TestCase(100, 1)]
public void TestPerfectCurveAngles(float height, float width)
{
AddStep("create path", () =>
{
path.ControlPoints.AddRange(createSegment(PathType.PerfectCurve, Vector2.Zero, new Vector2(width / 2, height), new Vector2(width, 0)));
});
}
[TestCase(2)]
[TestCase(4)]
public void TestPerfectCurveFallbackScenarios(int points)
{
AddStep("create path", () =>
{
switch (points)
{
case 2:
path.ControlPoints.AddRange(createSegment(PathType.PerfectCurve, Vector2.Zero, new Vector2(0, 100)));
break;
case 4:
path.ControlPoints.AddRange(createSegment(PathType.PerfectCurve, Vector2.Zero, new Vector2(0, 100), new Vector2(100), new Vector2(100, 0)));
break;
}
});
}
private List<PathControlPoint> createSegment(PathType type, params Vector2[] controlPoints)
{
var points = controlPoints.Select(p => new PathControlPoint { Position = p }).ToList();
points[0].Type = type;
return points;
}
}
}

View File

@ -245,6 +245,9 @@ namespace osu.Game.Rulesets.Objects
/// <param name="controlPoints">The control point positions to convert.</param> /// <param name="controlPoints">The control point positions to convert.</param>
public static Vector2[] ConvertCircleToBezierAnchors(ReadOnlySpan<Vector2> controlPoints) public static Vector2[] ConvertCircleToBezierAnchors(ReadOnlySpan<Vector2> controlPoints)
{ {
if (controlPoints.Length != 3)
return controlPoints.ToArray();
var pr = circularArcProperties(controlPoints); var pr = circularArcProperties(controlPoints);
if (!pr.IsValid) if (!pr.IsValid)
return controlPoints.ToArray(); return controlPoints.ToArray();