2020-03-23 10:03:42 +00: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 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-03-23 10:18:56 +00:00
|
|
|
using System;
|
2020-03-23 10:03:42 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-10-22 05:54:27 +00:00
|
|
|
using osu.Framework.Allocation;
|
2020-03-23 10:03:42 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Input;
|
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
using osu.Framework.Input.Events;
|
2020-10-22 05:54:27 +00:00
|
|
|
using osu.Game.Online.Spectator;
|
2020-03-23 10:03:42 +00:00
|
|
|
using osu.Game.Rulesets.Replays;
|
2020-12-14 07:52:14 +00:00
|
|
|
using osu.Game.Scoring;
|
2020-03-23 10:18:56 +00:00
|
|
|
using osuTK;
|
2020-03-23 10:03:42 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI
|
|
|
|
{
|
|
|
|
public abstract partial class ReplayRecorder<T> : ReplayRecorder, IKeyBindingHandler<T>
|
|
|
|
where T : struct
|
|
|
|
{
|
2020-12-14 07:52:14 +00:00
|
|
|
private readonly Score target;
|
2020-03-23 10:03:42 +00:00
|
|
|
|
|
|
|
private readonly List<T> pressedActions = new List<T>();
|
|
|
|
|
|
|
|
private InputManager inputManager;
|
|
|
|
|
|
|
|
public int RecordFrameRate = 60;
|
|
|
|
|
2022-02-10 05:18:29 +00:00
|
|
|
[Resolved]
|
2021-05-20 06:55:07 +00:00
|
|
|
private SpectatorClient spectatorClient { get; set; }
|
2020-10-22 06:26:57 +00:00
|
|
|
|
2020-12-14 07:52:14 +00:00
|
|
|
protected ReplayRecorder(Score target)
|
2020-03-23 10:03:42 +00:00
|
|
|
{
|
|
|
|
this.target = target;
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
Depth = float.MinValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
inputManager = GetContainingInputManager();
|
|
|
|
}
|
|
|
|
|
2021-04-16 10:40:56 +00:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
recordFrame(false);
|
|
|
|
}
|
|
|
|
|
2020-03-23 10:03:42 +00:00
|
|
|
protected override bool OnMouseMove(MouseMoveEvent e)
|
|
|
|
{
|
|
|
|
recordFrame(false);
|
|
|
|
return base.OnMouseMove(e);
|
|
|
|
}
|
|
|
|
|
2021-09-16 09:26:12 +00:00
|
|
|
public bool OnPressed(KeyBindingPressEvent<T> e)
|
2020-03-23 10:03:42 +00:00
|
|
|
{
|
2021-09-16 09:26:12 +00:00
|
|
|
pressedActions.Add(e.Action);
|
2020-03-23 10:03:42 +00:00
|
|
|
recordFrame(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-16 09:26:12 +00:00
|
|
|
public void OnReleased(KeyBindingReleaseEvent<T> e)
|
2020-03-23 10:03:42 +00:00
|
|
|
{
|
2021-09-16 09:26:12 +00:00
|
|
|
pressedActions.Remove(e.Action);
|
2020-03-23 10:03:42 +00:00
|
|
|
recordFrame(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void recordFrame(bool important)
|
|
|
|
{
|
2020-12-14 07:52:14 +00:00
|
|
|
var last = target.Replay.Frames.LastOrDefault();
|
2020-03-23 10:03:42 +00:00
|
|
|
|
|
|
|
if (!important && last != null && Time.Current - last.Time < (1000d / RecordFrameRate))
|
|
|
|
return;
|
|
|
|
|
2020-03-23 10:18:56 +00:00
|
|
|
var position = ScreenSpaceToGamefield?.Invoke(inputManager.CurrentState.Mouse.Position) ?? inputManager.CurrentState.Mouse.Position;
|
|
|
|
|
|
|
|
var frame = HandleFrame(position, pressedActions, last);
|
2020-03-23 10:03:42 +00:00
|
|
|
|
|
|
|
if (frame != null)
|
2020-10-22 05:54:27 +00:00
|
|
|
{
|
2020-12-14 07:52:14 +00:00
|
|
|
target.Replay.Frames.Add(frame);
|
2020-10-22 05:54:27 +00:00
|
|
|
|
2021-05-20 06:55:07 +00:00
|
|
|
spectatorClient?.HandleFrame(frame);
|
2020-10-22 05:54:27 +00:00
|
|
|
}
|
2020-03-23 10:03:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 06:38:54 +00:00
|
|
|
protected abstract ReplayFrame HandleFrame(Vector2 mousePosition, List<T> actions, ReplayFrame previousFrame);
|
2020-03-23 10:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract partial class ReplayRecorder : Component
|
|
|
|
{
|
2020-03-23 10:18:56 +00:00
|
|
|
public Func<Vector2, Vector2> ScreenSpaceToGamefield;
|
2020-03-23 10:03:42 +00:00
|
|
|
}
|
|
|
|
}
|