1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 03:55:30 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestSceneReplayRecorder.cs

273 lines
10 KiB
C#
Raw Normal View History

2020-03-23 16:33: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.
using System.Collections.Generic;
2020-03-24 15:22:54 +08:00
using System.Linq;
using NUnit.Framework;
2020-03-23 16:33:02 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
2020-03-23 17:50:16 +08:00
using osu.Framework.Input.StateChanges;
2020-03-24 15:22:54 +08:00
using osu.Framework.Testing;
using osu.Framework.Threading;
2020-03-23 17:50:16 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Replays;
2020-03-23 16:33:02 +08:00
using osu.Game.Rulesets;
2020-03-23 17:50:16 +08:00
using osu.Game.Rulesets.Replays;
2020-03-23 16:33:02 +08:00
using osu.Game.Rulesets.UI;
using osu.Game.Tests.Visual.UserInterface;
using osuTK;
using osuTK.Graphics;
2020-03-24 15:22:54 +08:00
using osuTK.Input;
2020-03-23 16:33:02 +08:00
namespace osu.Game.Tests.Visual.Gameplay
2020-03-23 16:33:02 +08:00
{
2020-03-24 15:22:54 +08:00
public class TestSceneReplayRecorder : OsuManualInputManagerTestScene
2020-03-23 16:33:02 +08:00
{
2020-03-24 15:22:54 +08:00
private TestRulesetInputManager playbackManager;
private TestRulesetInputManager recordingManager;
2020-03-23 17:50:16 +08:00
2020-03-24 15:22:54 +08:00
private Replay replay;
private TestReplayRecorder recorder;
[SetUp]
public void SetUp() => Schedule(() =>
2020-03-23 16:33:02 +08:00
{
2020-03-24 15:22:54 +08:00
replay = new Replay();
2020-03-23 17:50:16 +08:00
Add(new GridContainer
2020-03-23 16:33:02 +08:00
{
2020-03-23 17:50:16 +08:00
RelativeSizeAxes = Axes.Both,
Content = new[]
2020-03-23 16:33:02 +08:00
{
2020-03-23 17:50:16 +08:00
new Drawable[]
{
2020-03-24 15:22:54 +08:00
recordingManager = new TestRulesetInputManager(new TestSceneModSettings.TestRulesetInfo(), 0, SimultaneousBindingMode.Unique)
2020-03-23 17:50:16 +08:00
{
Recorder = recorder = new TestReplayRecorder(replay)
{
ScreenSpaceToGamefield = pos => recordingManager.ToLocalSpace(pos),
},
2020-03-23 17:50:16 +08:00
Child = new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
Colour = Color4.Brown,
RelativeSizeAxes = Axes.Both,
},
new OsuSpriteText
{
Text = "Recording",
Scale = new Vector2(3),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
2020-03-24 14:54:04 +08:00
new TestInputConsumer()
2020-03-23 17:50:16 +08:00
}
},
}
},
new Drawable[]
2020-03-23 16:33:02 +08:00
{
2020-03-23 17:50:16 +08:00
playbackManager = new TestRulesetInputManager(new TestSceneModSettings.TestRulesetInfo(), 0, SimultaneousBindingMode.Unique)
2020-03-23 16:33:02 +08:00
{
2020-03-23 17:50:16 +08:00
ReplayInputHandler = new TestFramedReplayInputHandler(replay)
{
GamefieldToScreenSpace = pos => playbackManager.ToScreenSpace(pos),
},
Child = new Container
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
Colour = Color4.DarkBlue,
RelativeSizeAxes = Axes.Both,
},
new OsuSpriteText
{
Text = "Playback",
Scale = new Vector2(3),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
2020-03-24 14:54:04 +08:00
new TestInputConsumer()
2020-03-23 17:50:16 +08:00
}
},
}
2020-03-23 16:33:02 +08:00
}
2020-03-23 17:50:16 +08:00
}
2020-03-23 16:33:02 +08:00
});
2020-03-24 15:22:54 +08:00
});
[Test]
public void TestBasic()
{
AddStep("move to center", () => InputManager.MoveMouseTo(recordingManager.ScreenSpaceDrawQuad.Centre));
AddUntilStep("one frame recorded", () => replay.Frames.Count == 1);
AddAssert("position matches", () => playbackManager.ChildrenOfType<Box>().First().Position == recordingManager.ChildrenOfType<Box>().First().Position);
}
[Test]
public void TestHighFrameRate()
{
ScheduledDelegate moveFunction = null;
AddStep("move to center", () => InputManager.MoveMouseTo(recordingManager.ScreenSpaceDrawQuad.Centre));
AddStep("much move", () => moveFunction = Scheduler.AddDelayed(() =>
InputManager.MoveMouseTo(InputManager.CurrentState.Mouse.Position + new Vector2(-1, 0)), 10, true));
AddWaitStep("move", 10);
AddStep("stop move", () => moveFunction.Cancel());
AddAssert("at least 60 frames recorded", () => replay.Frames.Count > 60);
}
[Test]
public void TestLimitedFrameRate()
{
ScheduledDelegate moveFunction = null;
AddStep("lower rate", () => recorder.RecordFrameRate = 2);
AddStep("move to center", () => InputManager.MoveMouseTo(recordingManager.ScreenSpaceDrawQuad.Centre));
AddStep("much move", () => moveFunction = Scheduler.AddDelayed(() =>
InputManager.MoveMouseTo(InputManager.CurrentState.Mouse.Position + new Vector2(-1, 0)), 10, true));
AddWaitStep("move", 10);
AddStep("stop move", () => moveFunction.Cancel());
AddAssert("less than 10 frames recorded", () => replay.Frames.Count < 10);
}
[Test]
public void TestLimitedFrameRateWithImportantFrames()
{
ScheduledDelegate moveFunction = null;
AddStep("lower rate", () => recorder.RecordFrameRate = 2);
AddStep("move to center", () => InputManager.MoveMouseTo(recordingManager.ScreenSpaceDrawQuad.Centre));
AddStep("much move with press", () => moveFunction = Scheduler.AddDelayed(() =>
{
InputManager.MoveMouseTo(InputManager.CurrentState.Mouse.Position + new Vector2(-1, 0));
InputManager.PressButton(MouseButton.Left);
InputManager.ReleaseButton(MouseButton.Left);
}, 10, true));
AddWaitStep("move", 10);
AddStep("stop move", () => moveFunction.Cancel());
AddAssert("at least 60 frames recorded", () => replay.Frames.Count > 60);
2020-03-23 16:33:02 +08:00
}
2020-03-23 17:50:16 +08:00
protected override void Update()
2020-03-23 16:33:02 +08:00
{
2020-03-23 17:50:16 +08:00
base.Update();
2020-03-24 15:22:54 +08:00
playbackManager?.ReplayInputHandler.SetFrameFromTime(Time.Current - 100);
2020-03-23 17:50:16 +08:00
}
2020-03-23 16:33:02 +08:00
2020-03-24 14:54:04 +08:00
public class TestFramedReplayInputHandler : FramedReplayInputHandler<TestReplayFrame>
2020-03-23 17:50:16 +08:00
{
2020-03-24 14:54:04 +08:00
public TestFramedReplayInputHandler(Replay replay)
: base(replay)
{
}
2020-03-23 17:50:16 +08:00
2020-07-22 18:53:45 +08:00
public override void CollectPendingInputs(List<IInput> inputs)
2020-03-23 16:33:02 +08:00
{
inputs.Add(new MousePositionAbsoluteInput { Position = GamefieldToScreenSpace(CurrentFrame?.Position ?? Vector2.Zero) });
inputs.Add(new ReplayState<TestAction> { PressedActions = CurrentFrame?.Actions ?? new List<TestAction>() });
2020-03-24 14:54:04 +08:00
}
2020-03-23 17:50:16 +08:00
}
2020-03-23 16:33:02 +08:00
2020-03-24 14:54:04 +08:00
public class TestInputConsumer : CompositeDrawable, IKeyBindingHandler<TestAction>
2020-03-23 17:50:16 +08:00
{
2020-03-24 14:54:04 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => Parent.ReceivePositionalInputAt(screenSpacePos);
2020-03-23 16:33:02 +08:00
2020-03-24 14:54:04 +08:00
private readonly Box box;
2020-03-23 16:33:02 +08:00
2020-03-24 14:54:04 +08:00
public TestInputConsumer()
2020-03-23 16:33:02 +08:00
{
2020-03-24 14:54:04 +08:00
Size = new Vector2(30);
Origin = Anchor.Centre;
InternalChildren = new Drawable[]
2020-03-23 17:50:16 +08:00
{
2020-03-24 14:54:04 +08:00
box = new Box
{
Colour = Color4.Black,
RelativeSizeAxes = Axes.Both,
},
};
}
2020-03-23 16:33:02 +08:00
2020-03-24 14:54:04 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
{
Position = e.MousePosition;
return base.OnMouseMove(e);
}
2020-03-23 17:50:16 +08:00
2020-03-24 14:54:04 +08:00
public bool OnPressed(TestAction action)
{
box.Colour = Color4.White;
return true;
}
2020-03-23 17:50:16 +08:00
2020-03-24 14:54:04 +08:00
public void OnReleased(TestAction action)
{
box.Colour = Color4.Black;
}
2020-03-23 17:50:16 +08:00
}
2020-03-24 14:54:04 +08:00
public class TestRulesetInputManager : RulesetInputManager<TestAction>
2020-03-23 17:50:16 +08:00
{
2020-03-24 14:54:04 +08:00
public TestRulesetInputManager(RulesetInfo ruleset, int variant, SimultaneousBindingMode unique)
: base(ruleset, variant, unique)
{
}
2020-03-23 16:33:02 +08:00
2020-03-24 14:54:04 +08:00
protected override KeyBindingContainer<TestAction> CreateKeyBindingContainer(RulesetInfo ruleset, int variant, SimultaneousBindingMode unique)
=> new TestKeyBindingContainer();
2020-03-23 16:33:02 +08:00
2020-03-24 14:54:04 +08:00
internal class TestKeyBindingContainer : KeyBindingContainer<TestAction>
2020-03-23 16:33:02 +08:00
{
2020-03-24 14:54:04 +08:00
public override IEnumerable<KeyBinding> DefaultKeyBindings => new[]
{
new KeyBinding(InputKey.MouseLeft, TestAction.Down),
};
}
2020-03-23 17:50:16 +08:00
}
2020-03-24 14:54:04 +08:00
public class TestReplayFrame : ReplayFrame
{
public Vector2 Position;
2020-03-23 17:50:16 +08:00
2020-03-24 14:54:04 +08:00
public List<TestAction> Actions = new List<TestAction>();
2020-03-23 17:50:16 +08:00
2020-03-24 14:54:04 +08:00
public TestReplayFrame(double time, Vector2 position, params TestAction[] actions)
: base(time)
{
Position = position;
Actions.AddRange(actions);
}
2020-03-23 17:50:16 +08:00
}
2020-03-24 14:54:04 +08:00
public enum TestAction
2020-03-23 17:50:16 +08:00
{
2020-03-24 14:54:04 +08:00
Down,
2020-03-23 17:50:16 +08:00
}
2020-03-24 14:54:04 +08:00
internal class TestReplayRecorder : ReplayRecorder<TestAction>
{
public TestReplayRecorder(Replay target)
: base(target)
{
}
protected override ReplayFrame HandleFrame(Vector2 mousePosition, List<TestAction> actions, ReplayFrame previousFrame)
=> new TestReplayFrame(Time.Current, mousePosition, actions.ToArray());
2020-03-24 14:54:04 +08:00
}
2020-03-23 17:50:16 +08:00
}
2020-03-23 16:33:02 +08:00
}