2020-03-23 18:03:42 +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.
|
|
|
|
|
2020-03-23 18:18:56 +08:00
|
|
|
using System;
|
2020-03-23 18:03:42 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-10-22 13:54:27 +08:00
|
|
|
using osu.Framework.Allocation;
|
2020-03-23 18:03:42 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Input;
|
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
using osu.Framework.Input.Events;
|
2020-10-22 13:54:27 +08:00
|
|
|
using osu.Game.Online.Spectator;
|
2020-03-23 18:03:42 +08:00
|
|
|
using osu.Game.Replays;
|
|
|
|
using osu.Game.Rulesets.Replays;
|
2020-10-27 07:05:03 +08:00
|
|
|
using osu.Game.Screens.Play;
|
2020-03-23 18:18:56 +08:00
|
|
|
using osuTK;
|
2020-03-23 18:03:42 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI
|
|
|
|
{
|
|
|
|
public abstract class ReplayRecorder<T> : ReplayRecorder, IKeyBindingHandler<T>
|
|
|
|
where T : struct
|
|
|
|
{
|
|
|
|
private readonly Replay target;
|
|
|
|
|
|
|
|
private readonly List<T> pressedActions = new List<T>();
|
|
|
|
|
|
|
|
private InputManager inputManager;
|
|
|
|
|
|
|
|
public int RecordFrameRate = 60;
|
|
|
|
|
2020-10-22 14:26:57 +08:00
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
private SpectatorStreamingClient spectatorStreaming { get; set; }
|
|
|
|
|
|
|
|
[Resolved]
|
2020-10-27 07:05:03 +08:00
|
|
|
private GameplayBeatmap gameplayBeatmap { get; set; }
|
2020-10-22 14:26:57 +08:00
|
|
|
|
2020-03-23 18:03:42 +08:00
|
|
|
protected ReplayRecorder(Replay target)
|
|
|
|
{
|
|
|
|
this.target = target;
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
Depth = float.MinValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
inputManager = GetContainingInputManager();
|
2020-10-22 14:26:57 +08:00
|
|
|
|
2020-10-27 07:05:03 +08:00
|
|
|
spectatorStreaming?.BeginPlaying(gameplayBeatmap);
|
2020-10-22 14:26:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
2020-10-22 16:29:38 +08:00
|
|
|
spectatorStreaming?.EndPlaying();
|
2020-03-23 18:03:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnMouseMove(MouseMoveEvent e)
|
|
|
|
{
|
|
|
|
recordFrame(false);
|
|
|
|
return base.OnMouseMove(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool OnPressed(T action)
|
|
|
|
{
|
|
|
|
pressedActions.Add(action);
|
|
|
|
recordFrame(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnReleased(T action)
|
|
|
|
{
|
|
|
|
pressedActions.Remove(action);
|
|
|
|
recordFrame(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void recordFrame(bool important)
|
|
|
|
{
|
|
|
|
var last = target.Frames.LastOrDefault();
|
|
|
|
|
|
|
|
if (!important && last != null && Time.Current - last.Time < (1000d / RecordFrameRate))
|
|
|
|
return;
|
|
|
|
|
2020-03-23 18:18:56 +08:00
|
|
|
var position = ScreenSpaceToGamefield?.Invoke(inputManager.CurrentState.Mouse.Position) ?? inputManager.CurrentState.Mouse.Position;
|
|
|
|
|
|
|
|
var frame = HandleFrame(position, pressedActions, last);
|
2020-03-23 18:03:42 +08:00
|
|
|
|
|
|
|
if (frame != null)
|
2020-10-22 13:54:27 +08:00
|
|
|
{
|
2020-03-23 18:03:42 +08:00
|
|
|
target.Frames.Add(frame);
|
2020-10-22 13:54:27 +08:00
|
|
|
|
|
|
|
spectatorStreaming?.HandleFrame(frame);
|
|
|
|
}
|
2020-03-23 18:03:42 +08:00
|
|
|
}
|
|
|
|
|
2020-03-24 14:38:54 +08:00
|
|
|
protected abstract ReplayFrame HandleFrame(Vector2 mousePosition, List<T> actions, ReplayFrame previousFrame);
|
2020-03-23 18:03:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class ReplayRecorder : Component
|
|
|
|
{
|
2020-03-23 18:18:56 +08:00
|
|
|
public Func<Vector2, Vector2> ScreenSpaceToGamefield;
|
2020-03-23 18:03:42 +08:00
|
|
|
}
|
|
|
|
}
|