osu/osu.Game/Screens/Play/SaveFailedScoreButton.cs

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

141 lines
4.1 KiB
C#
Raw Normal View History

// 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.
#nullable disable
using System;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2022-06-21 16:19:20 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Threading;
using osu.Game.Scoring;
2022-06-21 16:19:20 +00:00
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online;
2022-06-21 16:19:20 +00:00
using osuTK;
namespace osu.Game.Screens.Play
{
2022-06-21 16:19:20 +00:00
public class SaveFailedScoreButton : CompositeDrawable
{
public Func<Task<ScoreInfo>> SaveReplay;
private Task<ScoreInfo> saveReplayAsync;
private ScoreInfo score;
private ScheduledDelegate saveScoreDelegate;
protected readonly Bindable<ImportState> State = new Bindable<ImportState>();
2022-06-21 16:19:20 +00:00
private DownloadButton button;
private ShakeContainer shakeContainer;
public SaveFailedScoreButton(Func<Task<ScoreInfo>> sr)
{
Size = new Vector2(50, 30);
SaveReplay = sr;
}
[BackgroundDependencyLoader(true)]
private void load(OsuGame game)
2022-06-21 16:19:20 +00:00
{
InternalChild = shakeContainer = new ShakeContainer
{
RelativeSizeAxes = Axes.Both,
Child = button = new DownloadButton
{
RelativeSizeAxes = Axes.Both,
}
};
button.Action = () =>
{
switch (State.Value)
{
case ImportState.Imported:
game?.PresentScore(score, ScorePresentType.Gameplay);
break;
case ImportState.Importing:
2022-06-21 16:19:20 +00:00
shakeContainer.Shake();
break;
default:
saveScore();
break;
}
};
State.BindValueChanged(state =>
{
switch (state.NewValue)
{
case ImportState.Imported:
button.State.Value = DownloadState.LocallyAvailable;
break;
case ImportState.Importing:
button.State.Value = DownloadState.Importing;
break;
case ImportState.Failed:
button.State.Value = DownloadState.NotDownloaded;
break;
}
}, true);
2022-06-30 10:51:58 +00:00
State.BindValueChanged(updateTooltip, true);
}
private void saveScore()
{
State.Value = ImportState.Importing;
saveReplayAsync = Task.Run(SaveReplay);
saveScoreDelegate = new ScheduledDelegate(() =>
{
if (saveReplayAsync?.IsCompleted != true)
// If the asynchronous preparation has not completed, keep repeating this delegate.
return;
saveScoreDelegate?.Cancel();
score = saveReplayAsync.GetAwaiter().GetResult();
State.Value = score != null ? ImportState.Imported : ImportState.Failed;
}, Time.Current, 50);
Scheduler.Add(saveScoreDelegate);
}
private void updateTooltip(ValueChangedEvent<ImportState> state)
{
switch (state.NewValue)
{
case ImportState.Imported:
button.TooltipText = @"Watch replay";
break;
case ImportState.Importing:
button.TooltipText = @"Importing score";
break;
case ImportState.Failed:
button.TooltipText = @"Import failed, click button to re-import";
break;
default:
2022-06-21 16:19:20 +00:00
button.TooltipText = @"Save score";
break;
}
}
public enum ImportState
{
NotImported,
Failed,
Importing,
Imported
}
}
}