mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 20:59:35 +08:00
Add button state, fix async issues, watch replay method
Most borrowed from `ReplayDownloadButton`
This commit is contained in:
parent
946178ca41
commit
fd0d8b1ce3
@ -4,6 +4,8 @@
|
||||
#nullable disable
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using osu.Game.Scoring;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using osuTK;
|
||||
@ -18,7 +20,7 @@ namespace osu.Game.Screens.Play
|
||||
{
|
||||
public class FailOverlay : GameplayMenuOverlay
|
||||
{
|
||||
public Action SaveReplay;
|
||||
public Func<Task<ScoreInfo>> SaveReplay;
|
||||
public override string Header => "failed";
|
||||
public override string Description => "you're dead, try again?";
|
||||
|
||||
@ -52,10 +54,8 @@ namespace osu.Game.Screens.Play
|
||||
Direction = FillDirection.Horizontal,
|
||||
Children = new Drawable[]
|
||||
{
|
||||
new SaveFailedScoreButton()
|
||||
new SaveFailedScoreButton(SaveReplay)
|
||||
{
|
||||
OnSave = SaveReplay,
|
||||
RelativeSizeAxes = Axes.Y,
|
||||
Width = 300
|
||||
},
|
||||
}
|
||||
|
@ -1043,7 +1043,7 @@ namespace osu.Game.Screens.Play
|
||||
return base.OnExiting(e);
|
||||
}
|
||||
|
||||
private async void saveFailedReplay()
|
||||
private async Task<ScoreInfo> saveFailedReplay()
|
||||
{
|
||||
Score.ScoreInfo.Passed = false;
|
||||
Score.ScoreInfo.Rank = ScoreRank.F;
|
||||
@ -1051,12 +1051,15 @@ namespace osu.Game.Screens.Play
|
||||
|
||||
try
|
||||
{
|
||||
await ImportScore(scoreCopy).ConfigureAwait(false);
|
||||
await ImportScore(scoreCopy);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, @"Score import failed!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return scoreCopy.ScoreInfo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -1,11 +1,16 @@
|
||||
// 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;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Threading;
|
||||
using osu.Game.Scoring;
|
||||
using osu.Game.Graphics.Containers;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using osu.Game.Online;
|
||||
@ -15,14 +20,25 @@ namespace osu.Game.Screens.Play
|
||||
{
|
||||
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 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
|
||||
{
|
||||
@ -32,17 +48,16 @@ namespace osu.Game.Screens.Play
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
}
|
||||
};
|
||||
Size = new Vector2(50, 30);
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load()
|
||||
{
|
||||
button.Action = () =>
|
||||
{
|
||||
switch (State.Value)
|
||||
{
|
||||
case DownloadState.LocallyAvailable:
|
||||
case ImportState.Imported:
|
||||
game?.PresentScore(score, ScorePresentType.Gameplay);
|
||||
break;
|
||||
|
||||
case ImportState.Importing:
|
||||
shakeContainer.Shake();
|
||||
break;
|
||||
|
||||
@ -52,23 +67,61 @@ namespace osu.Game.Screens.Play
|
||||
}
|
||||
};
|
||||
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()
|
||||
{
|
||||
if (State.Value != DownloadState.LocallyAvailable)
|
||||
OnSave?.Invoke();
|
||||
State.Value = ImportState.Importing;
|
||||
saveReplayAsync = Task.Run(SaveReplay);
|
||||
|
||||
State.Value = DownloadState.LocallyAvailable;
|
||||
button.State.Value = DownloadState.LocallyAvailable;
|
||||
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<DownloadState> state)
|
||||
private void updateTooltip(ValueChangedEvent<ImportState> state)
|
||||
{
|
||||
switch (state.NewValue)
|
||||
{
|
||||
case DownloadState.LocallyAvailable:
|
||||
button.TooltipText = @"Score saved";
|
||||
case ImportState.Imported:
|
||||
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;
|
||||
|
||||
default:
|
||||
@ -76,5 +129,13 @@ namespace osu.Game.Screens.Play
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public enum ImportState
|
||||
{
|
||||
NotImported,
|
||||
Failed,
|
||||
Importing,
|
||||
Imported
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user