Add button state, fix async issues, watch replay method

Most borrowed from `ReplayDownloadButton`
This commit is contained in:
cdwcgt 2022-06-29 22:50:47 +09:00
parent 946178ca41
commit fd0d8b1ce3
No known key found for this signature in database
GPG Key ID: 144396D01095C3A2
3 changed files with 86 additions and 22 deletions

View File

@ -4,6 +4,8 @@
#nullable disable #nullable disable
using System; using System;
using System.Threading.Tasks;
using osu.Game.Scoring;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osuTK; using osuTK;
@ -18,7 +20,7 @@ namespace osu.Game.Screens.Play
{ {
public class FailOverlay : GameplayMenuOverlay public class FailOverlay : GameplayMenuOverlay
{ {
public Action SaveReplay; public Func<Task<ScoreInfo>> SaveReplay;
public override string Header => "failed"; public override string Header => "failed";
public override string Description => "you're dead, try again?"; public override string Description => "you're dead, try again?";
@ -52,10 +54,8 @@ namespace osu.Game.Screens.Play
Direction = FillDirection.Horizontal, Direction = FillDirection.Horizontal,
Children = new Drawable[] Children = new Drawable[]
{ {
new SaveFailedScoreButton() new SaveFailedScoreButton(SaveReplay)
{ {
OnSave = SaveReplay,
RelativeSizeAxes = Axes.Y,
Width = 300 Width = 300
}, },
} }

View File

@ -1043,7 +1043,7 @@ namespace osu.Game.Screens.Play
return base.OnExiting(e); return base.OnExiting(e);
} }
private async void saveFailedReplay() private async Task<ScoreInfo> saveFailedReplay()
{ {
Score.ScoreInfo.Passed = false; Score.ScoreInfo.Passed = false;
Score.ScoreInfo.Rank = ScoreRank.F; Score.ScoreInfo.Rank = ScoreRank.F;
@ -1051,12 +1051,15 @@ namespace osu.Game.Screens.Play
try try
{ {
await ImportScore(scoreCopy).ConfigureAwait(false); await ImportScore(scoreCopy);
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Error(ex, @"Score import failed!"); Logger.Error(ex, @"Score import failed!");
return null;
} }
return scoreCopy.ScoreInfo;
} }
/// <summary> /// <summary>

View File

@ -1,11 +1,16 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
using System.Threading.Tasks;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Threading;
using osu.Game.Scoring;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Online; using osu.Game.Online;
@ -15,14 +20,25 @@ namespace osu.Game.Screens.Play
{ {
public class SaveFailedScoreButton : CompositeDrawable public class SaveFailedScoreButton : CompositeDrawable
{ {
public Action? OnSave; public Func<Task<ScoreInfo>> SaveReplay;
private Task<ScoreInfo> saveReplayAsync;
private ScoreInfo score;
protected readonly Bindable<DownloadState> State = new Bindable<DownloadState>(); private ScheduledDelegate saveScoreDelegate;
protected readonly Bindable<ImportState> State = new Bindable<ImportState>();
private DownloadButton button; private DownloadButton button;
private ShakeContainer shakeContainer; private ShakeContainer shakeContainer;
public SaveFailedScoreButton() public SaveFailedScoreButton(Func<Task<ScoreInfo>> sr)
{
Size = new Vector2(50, 30);
SaveReplay = sr;
}
[BackgroundDependencyLoader(true)]
private void load(OsuGame game)
{ {
InternalChild = shakeContainer = new ShakeContainer InternalChild = shakeContainer = new ShakeContainer
{ {
@ -32,17 +48,16 @@ namespace osu.Game.Screens.Play
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
} }
}; };
Size = new Vector2(50, 30);
}
[BackgroundDependencyLoader]
private void load()
{
button.Action = () => button.Action = () =>
{ {
switch (State.Value) switch (State.Value)
{ {
case DownloadState.LocallyAvailable: case ImportState.Imported:
game?.PresentScore(score, ScorePresentType.Gameplay);
break;
case ImportState.Importing:
shakeContainer.Shake(); shakeContainer.Shake();
break; break;
@ -52,23 +67,61 @@ namespace osu.Game.Screens.Play
} }
}; };
State.BindValueChanged(updateTooltip, true); State.BindValueChanged(updateTooltip, true);
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);
} }
private void saveScore() private void saveScore()
{ {
if (State.Value != DownloadState.LocallyAvailable) State.Value = ImportState.Importing;
OnSave?.Invoke(); saveReplayAsync = Task.Run(SaveReplay);
State.Value = DownloadState.LocallyAvailable; saveScoreDelegate = new ScheduledDelegate(() =>
button.State.Value = DownloadState.LocallyAvailable; {
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<DownloadState> state) private void updateTooltip(ValueChangedEvent<ImportState> state)
{ {
switch (state.NewValue) switch (state.NewValue)
{ {
case DownloadState.LocallyAvailable: case ImportState.Imported:
button.TooltipText = @"Score saved"; button.TooltipText = @"Watch replay";
break;
case ImportState.Importing:
button.TooltipText = @"Importing score";
break;
case ImportState.Failed:
button.State.Value = DownloadState.NotDownloaded;
button.TooltipText = @"Import failed, click button to re-import";
break; break;
default: default:
@ -76,5 +129,13 @@ namespace osu.Game.Screens.Play
break; break;
} }
} }
public enum ImportState
{
NotImported,
Failed,
Importing,
Imported
}
} }
} }