1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 20:13:02 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestSceneReplayRecording.cs

226 lines
7.9 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.
2021-10-02 01:22:23 +08:00
using System;
2020-03-23 16:33:02 +08:00
using System.Collections.Generic;
using osu.Framework.Allocation;
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;
using osu.Game.Beatmaps;
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;
2021-10-02 01:22:23 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu;
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.Scoring;
using osu.Game.Screens.Play;
2020-03-23 16:33:02 +08:00
using osu.Game.Tests.Visual.UserInterface;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Tests.Visual.Gameplay
2020-03-23 16:33:02 +08:00
{
public class TestSceneReplayRecording : OsuTestScene
{
2020-03-23 17:50:16 +08:00
private readonly TestRulesetInputManager playbackManager;
private readonly TestRulesetInputManager recordingManager;
[Cached]
2021-10-02 01:22:23 +08:00
private GameplayState gameplayState = new GameplayState(new Beatmap(), new OsuRuleset(), Array.Empty<Mod>());
2020-03-23 16:33:02 +08:00
public TestSceneReplayRecording()
{
2020-03-23 17:50:16 +08:00
Replay replay = new Replay();
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[]
{
recordingManager = new TestRulesetInputManager(new TestSceneModSettings.TestRulesetInfo(), 0, SimultaneousBindingMode.Unique)
2020-03-23 17:50:16 +08:00
{
2021-09-15 12:18:46 +08:00
Recorder = new TestReplayRecorder(new Score
{
Replay = replay,
2021-10-02 01:22:23 +08:00
ScoreInfo = { Beatmap = gameplayState.Beatmap.BeatmapInfo }
2021-09-15 12:18:46 +08:00
})
{
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,
},
new TestConsumer()
}
},
}
},
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,
},
new TestConsumer()
}
},
}
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-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-23 16:33:02 +08:00
2020-03-23 17:50:16 +08:00
playbackManager.ReplayInputHandler.SetFrameFromTime(Time.Current - 500);
}
}
2020-03-23 16:33:02 +08:00
2020-03-23 17:50:16 +08:00
public class TestFramedReplayInputHandler : FramedReplayInputHandler<TestReplayFrame>
{
public TestFramedReplayInputHandler(Replay replay)
: base(replay)
{
}
2020-07-22 18:53:45 +08:00
public override void CollectPendingInputs(List<IInput> inputs)
2020-03-23 17:50:16 +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-23 17:50:16 +08:00
}
}
2020-03-23 16:33:02 +08:00
2020-03-23 17:50:16 +08:00
public class TestConsumer : CompositeDrawable, IKeyBindingHandler<TestAction>
{
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => Parent.ReceivePositionalInputAt(screenSpacePos);
2020-03-23 16:33:02 +08:00
2020-03-23 17:50:16 +08:00
private readonly Box box;
2020-03-23 16:33:02 +08:00
2020-03-23 17:50:16 +08:00
public TestConsumer()
{
Size = new Vector2(30);
2020-03-23 16:33:02 +08:00
2020-03-23 17:50:16 +08:00
Origin = Anchor.Centre;
2020-03-23 16:33:02 +08:00
2020-03-23 17:50:16 +08:00
InternalChildren = new Drawable[]
2020-03-23 16:33:02 +08:00
{
2020-03-23 17:50:16 +08:00
box = new Box
{
Colour = Color4.Black,
RelativeSizeAxes = Axes.Both,
},
};
2020-03-23 16:33:02 +08:00
}
2020-03-23 17:50:16 +08:00
protected override bool OnMouseMove(MouseMoveEvent e)
2020-03-23 16:33:02 +08:00
{
2020-03-23 17:50:16 +08:00
Position = e.MousePosition;
return base.OnMouseMove(e);
}
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<TestAction> e)
2020-03-23 17:50:16 +08:00
{
box.Colour = Color4.White;
return true;
}
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<TestAction> e)
2020-03-23 17:50:16 +08:00
{
box.Colour = Color4.Black;
}
}
public class TestRulesetInputManager : RulesetInputManager<TestAction>
{
public TestRulesetInputManager(RulesetInfo ruleset, int variant, SimultaneousBindingMode unique)
: base(ruleset, variant, unique)
{
}
2020-03-23 16:33:02 +08:00
2020-03-23 17:50:16 +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-23 17:50:16 +08:00
internal class TestKeyBindingContainer : KeyBindingContainer<TestAction>
{
public override IEnumerable<IKeyBinding> DefaultKeyBindings => new[]
2020-03-23 16:33:02 +08:00
{
2020-03-23 17:50:16 +08:00
new KeyBinding(InputKey.MouseLeft, TestAction.Down),
};
}
}
public class TestReplayFrame : ReplayFrame
{
public Vector2 Position;
public List<TestAction> Actions = new List<TestAction>();
public TestReplayFrame(double time, Vector2 position, params TestAction[] actions)
: base(time)
{
Position = position;
Actions.AddRange(actions);
}
}
public enum TestAction
{
Down,
}
internal class TestReplayRecorder : ReplayRecorder<TestAction>
{
public TestReplayRecorder(Score target)
2020-03-23 17:50:16 +08:00
: base(target)
{
}
protected override ReplayFrame HandleFrame(Vector2 mousePosition, List<TestAction> actions, ReplayFrame previousFrame) =>
new TestReplayFrame(Time.Current, mousePosition, actions.ToArray());
2020-03-23 17:50:16 +08:00
}
2020-03-23 16:33:02 +08:00
}