1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 02:11:05 +08:00
osu-lazer/osu.Game.Rulesets.Osu/UI/ReplayAnalysisOverlay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

129 lines
4.8 KiB
C#
Raw Normal View History

2024-09-03 16:49:50 +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.
2024-09-05 15:01:28 +08:00
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2024-09-05 15:01:28 +08:00
using osu.Framework.Caching;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Replays;
using osu.Game.Rulesets.Osu.Configuration;
using osu.Game.Rulesets.Osu.Replays;
using osu.Game.Rulesets.Osu.UI.ReplayAnalysis;
namespace osu.Game.Rulesets.Osu.UI
{
2024-09-04 17:43:33 +08:00
public partial class ReplayAnalysisOverlay : CompositeDrawable
{
private BindableBool showClickMarkers { get; } = new BindableBool();
private BindableBool showFrameMarkers { get; } = new BindableBool();
private BindableBool showCursorPath { get; } = new BindableBool();
private BindableInt displayLength { get; } = new BindableInt();
2024-09-05 15:01:28 +08:00
protected ClickMarkerContainer? ClickMarkers;
protected FrameMarkerContainer? FrameMarkers;
protected CursorPathContainer? CursorPath;
private readonly Replay replay;
public ReplayAnalysisOverlay(Replay replay)
{
RelativeSizeAxes = Axes.Both;
this.replay = replay;
}
private bool requireDisplay => showClickMarkers.Value || showFrameMarkers.Value || showCursorPath.Value;
[BackgroundDependencyLoader]
private void load(OsuRulesetConfigManager config)
{
config.BindWith(OsuRulesetSetting.ReplayClickMarkersEnabled, showClickMarkers);
config.BindWith(OsuRulesetSetting.ReplayFrameMarkersEnabled, showFrameMarkers);
config.BindWith(OsuRulesetSetting.ReplayCursorPathEnabled, showCursorPath);
config.BindWith(OsuRulesetSetting.ReplayAnalysisDisplayLength, displayLength);
}
protected override void LoadComplete()
{
base.LoadComplete();
displayLength.BindValueChanged(_ =>
{
2024-09-05 15:01:28 +08:00
// Need to fully reload to make this work.
loaded.Invalidate();
}, true);
}
2024-09-05 15:01:28 +08:00
private readonly Cached loaded = new Cached();
private CancellationTokenSource? generationCancellationSource;
protected override void Update()
{
base.Update();
if (requireDisplay)
initialise();
2024-09-05 15:45:37 +08:00
if (ClickMarkers != null) ClickMarkers.Alpha = showClickMarkers.Value ? 1 : 0;
if (FrameMarkers != null) FrameMarkers.Alpha = showFrameMarkers.Value ? 1 : 0;
if (CursorPath != null) CursorPath.Alpha = showCursorPath.Value ? 1 : 0;
2024-09-05 15:01:28 +08:00
}
private void initialise()
{
2024-09-05 15:01:28 +08:00
if (loaded.IsValid)
return;
2024-09-05 15:01:28 +08:00
loaded.Validate();
2024-09-05 15:01:28 +08:00
generationCancellationSource?.Cancel();
generationCancellationSource = new CancellationTokenSource();
// It's faster to reinitialise the whole drawable stack than use `Clear` on `PooledDrawableWithLifetimeContainer`
2024-09-05 15:01:28 +08:00
var newDrawables = new Drawable[]
{
CursorPath = new CursorPathContainer(),
ClickMarkers = new ClickMarkerContainer(),
FrameMarkers = new FrameMarkerContainer(),
};
bool leftHeld = false;
bool rightHeld = false;
2024-09-05 15:01:28 +08:00
// This should probably be async as well, but it's a bit of a pain to debounce and everything.
// Let's address concerns when they are raised.
foreach (var frame in replay.Frames)
{
var osuFrame = (OsuReplayFrame)frame;
bool leftButton = osuFrame.Actions.Contains(OsuAction.LeftButton);
bool rightButton = osuFrame.Actions.Contains(OsuAction.RightButton);
if (leftHeld && !leftButton)
leftHeld = false;
else if (!leftHeld && leftButton)
{
leftHeld = true;
ClickMarkers.Add(new AnalysisFrameEntry(osuFrame.Time, displayLength.Value, osuFrame.Position, OsuAction.LeftButton));
}
if (rightHeld && !rightButton)
rightHeld = false;
else if (!rightHeld && rightButton)
{
rightHeld = true;
ClickMarkers.Add(new AnalysisFrameEntry(osuFrame.Time, displayLength.Value, osuFrame.Position, OsuAction.RightButton));
}
2024-09-04 20:04:59 +08:00
FrameMarkers.Add(new AnalysisFrameEntry(osuFrame.Time, displayLength.Value, osuFrame.Position, osuFrame.Actions.ToArray()));
CursorPath.Add(new AnalysisFrameEntry(osuFrame.Time, displayLength.Value, osuFrame.Position));
}
2024-09-05 15:01:28 +08:00
LoadComponentsAsync(newDrawables, drawables => InternalChildrenEnumerable = drawables, generationCancellationSource.Token);
}
}
}