2022-06-21 11:06:38 +00: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.
|
|
|
|
|
|
2022-06-29 13:50:47 +00:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2022-06-21 11:06:38 +00:00
|
|
|
|
using System;
|
2022-06-29 13:50:47 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2022-06-21 11:06:38 +00:00
|
|
|
|
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;
|
2022-06-29 13:50:47 +00:00
|
|
|
|
using osu.Game.Scoring;
|
2022-06-21 11:06:38 +00:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Online;
|
2022-06-21 16:19:20 +00:00
|
|
|
|
using osuTK;
|
2022-06-21 11:06:38 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
|
{
|
2022-06-21 16:19:20 +00:00
|
|
|
|
public class SaveFailedScoreButton : CompositeDrawable
|
2022-06-21 11:06:38 +00:00
|
|
|
|
{
|
2022-07-08 09:36:46 +00:00
|
|
|
|
public Func<Task<ScoreInfo>> ImportFailedScore;
|
|
|
|
|
private Task<ScoreInfo> saveFailedScoreTask;
|
2022-06-29 13:50:47 +00:00
|
|
|
|
private ScoreInfo score;
|
|
|
|
|
|
|
|
|
|
protected readonly Bindable<ImportState> State = new Bindable<ImportState>();
|
2022-06-21 16:19:20 +00:00
|
|
|
|
|
|
|
|
|
private DownloadButton button;
|
|
|
|
|
|
2022-07-08 09:36:46 +00:00
|
|
|
|
public SaveFailedScoreButton(Func<Task<ScoreInfo>> requestImportFailedScore)
|
2022-06-29 13:50:47 +00:00
|
|
|
|
{
|
|
|
|
|
Size = new Vector2(50, 30);
|
2022-07-08 09:36:46 +00:00
|
|
|
|
ImportFailedScore = requestImportFailedScore;
|
2022-06-29 13:50:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
|
private void load(OsuGame game)
|
2022-06-21 16:19:20 +00:00
|
|
|
|
{
|
2022-07-08 12:42:34 +00:00
|
|
|
|
InternalChild = button = new DownloadButton
|
2022-06-21 16:19:20 +00:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
button.Action = () =>
|
|
|
|
|
{
|
|
|
|
|
switch (State.Value)
|
|
|
|
|
{
|
2022-06-29 13:50:47 +00:00
|
|
|
|
case ImportState.Imported:
|
|
|
|
|
game?.PresentScore(score, ScorePresentType.Gameplay);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ImportState.Importing:
|
2022-06-21 16:19:20 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
saveScore();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-06-29 13:50:47 +00:00
|
|
|
|
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-07-08 15:25:11 +00:00
|
|
|
|
State.BindValueChanged(updateState, true);
|
2022-06-21 11:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveScore()
|
|
|
|
|
{
|
2022-07-08 12:31:35 +00:00
|
|
|
|
if (saveFailedScoreTask != null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 13:50:47 +00:00
|
|
|
|
State.Value = ImportState.Importing;
|
2022-07-08 09:36:46 +00:00
|
|
|
|
|
|
|
|
|
saveFailedScoreTask = Task.Run(ImportFailedScore);
|
2022-07-08 12:31:35 +00:00
|
|
|
|
saveFailedScoreTask.ContinueWith(s => Schedule(() =>
|
2022-06-29 13:50:47 +00:00
|
|
|
|
{
|
2022-07-08 12:31:35 +00:00
|
|
|
|
score = s.GetAwaiter().GetResult();
|
2022-06-29 13:50:47 +00:00
|
|
|
|
State.Value = score != null ? ImportState.Imported : ImportState.Failed;
|
2022-07-08 12:31:35 +00:00
|
|
|
|
}));
|
2022-06-21 11:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 15:25:11 +00:00
|
|
|
|
private void updateState(ValueChangedEvent<ImportState> state)
|
2022-06-21 11:06:38 +00:00
|
|
|
|
{
|
|
|
|
|
switch (state.NewValue)
|
|
|
|
|
{
|
2022-06-29 13:50:47 +00:00
|
|
|
|
case ImportState.Imported:
|
|
|
|
|
button.TooltipText = @"Watch replay";
|
2022-07-08 12:42:34 +00:00
|
|
|
|
button.Enabled.Value = true;
|
2022-06-29 13:50:47 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ImportState.Importing:
|
|
|
|
|
button.TooltipText = @"Importing score";
|
2022-07-08 12:42:34 +00:00
|
|
|
|
button.Enabled.Value = false;
|
2022-06-29 13:50:47 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ImportState.Failed:
|
|
|
|
|
button.TooltipText = @"Import failed, click button to re-import";
|
2022-07-08 12:42:34 +00:00
|
|
|
|
button.Enabled.Value = true;
|
2022-06-21 11:06:38 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2022-06-21 16:19:20 +00:00
|
|
|
|
button.TooltipText = @"Save score";
|
2022-07-08 12:42:34 +00:00
|
|
|
|
button.Enabled.Value = true;
|
2022-06-21 11:06:38 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-29 13:50:47 +00:00
|
|
|
|
|
|
|
|
|
public enum ImportState
|
|
|
|
|
{
|
|
|
|
|
NotImported,
|
|
|
|
|
Failed,
|
|
|
|
|
Importing,
|
|
|
|
|
Imported
|
|
|
|
|
}
|
2022-06-21 11:06:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|