1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 10:33:01 +08:00

More tests

This commit is contained in:
smoogipoo 2019-10-11 17:11:37 +09:00
parent 6301f837e0
commit 4d32a8aa6b

View File

@ -5,9 +5,11 @@ using System;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.MathUtils;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Objects;
@ -32,12 +34,13 @@ namespace osu.Game.Rulesets.Osu.Tests
[Cached]
private readonly BindableBeatDivisor beatDivisor = new BindableBeatDivisor();
private OsuBeatSnapGrid grid;
private Drawable cursor;
private TestOsuBeatSnapGrid grid;
public TestSceneOsuBeatSnapGrid()
{
editorBeatmap = new EditorBeatmap<OsuHitObject>(new OsuBeatmap());
createGrid();
}
[SetUp]
@ -45,22 +48,14 @@ namespace osu.Game.Rulesets.Osu.Tests
{
Clear();
editorBeatmap.BeatmapInfo.BaseDifficulty.SliderMultiplier = 1;
editorBeatmap.ControlPointInfo.DifficultyPoints.Clear();
editorBeatmap.ControlPointInfo.TimingPoints.Clear();
editorBeatmap.ControlPointInfo.TimingPoints.Add(new TimingControlPoint { BeatLength = beat_length });
beatDivisor.Value = 1;
});
protected override bool OnMouseMove(MouseMoveEvent e)
{
base.OnMouseMove(e);
if (cursor != null)
cursor.Position = grid?.GetSnapPosition(grid.ToLocalSpace(e.ScreenSpaceMousePosition)) ?? e.ScreenSpaceMousePosition;
return true;
}
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
@ -75,6 +70,80 @@ namespace osu.Game.Rulesets.Osu.Tests
createGrid();
}
[TestCase(100, 100)]
[TestCase(200, 100)]
public void TestBeatLength(float beatLength, float expectedSpacing)
{
AddStep($"set beat length = {beatLength}", () =>
{
editorBeatmap.ControlPointInfo.TimingPoints.Clear();
editorBeatmap.ControlPointInfo.TimingPoints.Add(new TimingControlPoint { BeatLength = beatLength });
});
createGrid();
AddAssert($"spacing = {expectedSpacing}", () => Precision.AlmostEquals(expectedSpacing, grid.DistanceSpacing));
}
[TestCase(0.5f, 50)]
[TestCase(1, 100)]
[TestCase(1.5f, 150)]
public void TestSpeedMultiplier(float multiplier, float expectedSpacing)
{
AddStep($"set speed multiplier = {multiplier}", () =>
{
editorBeatmap.ControlPointInfo.DifficultyPoints.Clear();
editorBeatmap.ControlPointInfo.DifficultyPoints.Add(new DifficultyControlPoint { SpeedMultiplier = multiplier });
});
createGrid();
AddAssert($"spacing = {expectedSpacing}", () => Precision.AlmostEquals(expectedSpacing, grid.DistanceSpacing));
}
[TestCase(0.5f, 50)]
[TestCase(1, 100)]
[TestCase(1.5f, 150)]
public void TestSliderMultiplier(float multiplier, float expectedSpacing)
{
AddStep($"set speed multiplier = {multiplier}", () => editorBeatmap.BeatmapInfo.BaseDifficulty.SliderMultiplier = multiplier);
createGrid();
AddAssert($"spacing = {expectedSpacing}", () => Precision.AlmostEquals(expectedSpacing, grid.DistanceSpacing));
}
[Test]
public void TestCursorInCentre()
{
createGrid();
AddStep("move mouse to centre", () => InputManager.MoveMouseTo(grid.ToScreenSpace(grid_position)));
assertSnappedDistance((float)beat_length);
}
[Test]
public void TestCursorBeforeMovementPoint()
{
createGrid();
AddStep("move mouse to just before movement point", () => InputManager.MoveMouseTo(grid.ToScreenSpace(grid_position + new Vector2((float)beat_length, 0) * 1.49f)));
assertSnappedDistance((float)beat_length);
}
[Test]
public void TestCursorAfterMovementPoint()
{
createGrid();
AddStep("move mouse to just after movement point", () => InputManager.MoveMouseTo(grid.ToScreenSpace(grid_position + new Vector2((float)beat_length, 0) * 1.51f)));
assertSnappedDistance((float)beat_length * 2);
}
private void assertSnappedDistance(float expectedDistance) => AddAssert($"snap distance = {expectedDistance}", () =>
{
Vector2 snappedPosition = grid.GetSnapPosition(grid.ToLocalSpace(InputManager.CurrentState.Mouse.Position));
float distance = Vector2.Distance(snappedPosition, grid_position);
return Precision.AlmostEquals(expectedDistance, distance);
});
private void createGrid()
{
AddStep("create grid", () =>
@ -86,28 +155,77 @@ namespace osu.Game.Rulesets.Osu.Tests
RelativeSizeAxes = Axes.Both,
Colour = Color4.SlateGray
},
grid = new OsuBeatSnapGrid(new HitCircle { Position = grid_position }),
cursor = new Circle
{
Origin = Anchor.Centre,
Size = new Vector2(50),
Colour = Color4.Red
}
grid = new TestOsuBeatSnapGrid(new HitCircle { Position = grid_position }),
new SnappingCursorContainer { GetSnapPosition = v => grid.GetSnapPosition(grid.ToLocalSpace(v)) }
};
});
}
private class SnappingCursorContainer : CompositeDrawable
{
public Func<Vector2, Vector2> GetSnapPosition;
private readonly Drawable cursor;
public SnappingCursorContainer()
{
RelativeSizeAxes = Axes.Both;
InternalChild = cursor = new Circle
{
Origin = Anchor.Centre,
Size = new Vector2(50),
Colour = Color4.Red
};
}
protected override void LoadComplete()
{
base.LoadComplete();
updatePosition(GetContainingInputManager().CurrentState.Mouse.Position);
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
base.OnMouseMove(e);
updatePosition(e.ScreenSpaceMousePosition);
return true;
}
private void updatePosition(Vector2 screenSpacePosition)
{
cursor.Position = GetSnapPosition.Invoke(screenSpacePosition);
}
}
private class TestOsuBeatSnapGrid : OsuBeatSnapGrid
{
public new float DistanceSpacing => base.DistanceSpacing;
public TestOsuBeatSnapGrid(OsuHitObject hitObject)
: base(hitObject)
{
}
}
private abstract class CircularBeatSnapGrid : BeatSnapGrid
{
protected override void CreateGrid(Vector2 startPosition)
protected CircularBeatSnapGrid(HitObject hitObject, Vector2 centrePosition)
: base(hitObject, centrePosition)
{
}
protected override void CreateContent(Vector2 centrePosition)
{
float maxDistance = Math.Max(
Vector2.Distance(startPosition, Vector2.Zero),
Vector2.Distance(centrePosition, Vector2.Zero),
Math.Max(
Vector2.Distance(startPosition, new Vector2(DrawWidth, 0)),
Vector2.Distance(centrePosition, new Vector2(DrawWidth, 0)),
Math.Max(
Vector2.Distance(startPosition, new Vector2(0, DrawHeight)),
Vector2.Distance(startPosition, DrawSize))));
Vector2.Distance(centrePosition, new Vector2(0, DrawHeight)),
Vector2.Distance(centrePosition, DrawSize))));
int requiredCircles = (int)(maxDistance / DistanceSpacing);
@ -118,7 +236,7 @@ namespace osu.Game.Rulesets.Osu.Tests
AddInternal(new CircularProgress
{
Origin = Anchor.Centre,
Position = startPosition,
Position = centrePosition,
Current = { Value = 1 },
Size = new Vector2(radius),
InnerRadius = 4 * 1f / radius,
@ -129,7 +247,7 @@ namespace osu.Game.Rulesets.Osu.Tests
public override Vector2 GetSnapPosition(Vector2 position)
{
Vector2 direction = position - StartPosition;
Vector2 direction = position - CentrePosition;
float distance = direction.Length;
float radius = DistanceSpacing;
@ -140,7 +258,7 @@ namespace osu.Game.Rulesets.Osu.Tests
Vector2 normalisedDirection = direction * new Vector2(1f / distance);
return StartPosition + normalisedDirection * radialCount * radius;
return CentrePosition + normalisedDirection * radialCount * radius;
}
}