2022-06-21 19:06:38 +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.
|
|
|
|
|
|
|
|
|
|
using System;
|
2022-06-29 21:50:47 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2022-06-21 19:06:38 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Bindables;
|
2022-06-22 00:19:20 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-06-29 21:50:47 +08:00
|
|
|
|
using osu.Game.Scoring;
|
2022-06-21 19:06:38 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Online;
|
2022-06-22 00:19:20 +08:00
|
|
|
|
using osuTK;
|
2022-06-21 19:06:38 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
|
{
|
2022-06-22 00:19:20 +08:00
|
|
|
|
public class SaveFailedScoreButton : CompositeDrawable
|
2022-06-21 19:06:38 +08:00
|
|
|
|
{
|
2022-07-15 17:54:19 +08:00
|
|
|
|
private readonly Bindable<DownloadState> state = new Bindable<DownloadState>();
|
|
|
|
|
|
|
|
|
|
private readonly Func<Task<ScoreInfo>> importFailedScore;
|
|
|
|
|
|
|
|
|
|
private Task<ScoreInfo>? saveFailedScoreTask;
|
2022-06-29 21:50:47 +08:00
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
private ScoreInfo? score;
|
2022-06-22 00:19:20 +08:00
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
private DownloadButton button = null!;
|
2022-06-22 00:19:20 +08:00
|
|
|
|
|
2022-07-08 17:36:46 +08:00
|
|
|
|
public SaveFailedScoreButton(Func<Task<ScoreInfo>> requestImportFailedScore)
|
2022-06-29 21:50:47 +08:00
|
|
|
|
{
|
|
|
|
|
Size = new Vector2(50, 30);
|
2022-07-15 17:54:19 +08:00
|
|
|
|
importFailedScore = requestImportFailedScore;
|
2022-06-29 21:50:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuGame? game)
|
2022-06-22 00:19:20 +08:00
|
|
|
|
{
|
2022-07-08 20:42:34 +08:00
|
|
|
|
InternalChild = button = new DownloadButton
|
2022-06-22 00:19:20 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
button.Action = () =>
|
|
|
|
|
{
|
2022-07-15 17:54:19 +08:00
|
|
|
|
switch (state.Value)
|
2022-06-22 00:19:20 +08:00
|
|
|
|
{
|
2022-07-15 17:54:19 +08:00
|
|
|
|
case DownloadState.LocallyAvailable:
|
2022-06-29 21:50:47 +08:00
|
|
|
|
game?.PresentScore(score, ScorePresentType.Gameplay);
|
|
|
|
|
break;
|
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
case DownloadState.Importing:
|
2022-06-22 00:19:20 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
saveScore();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-07-15 17:54:19 +08:00
|
|
|
|
state.BindValueChanged(state =>
|
2022-06-29 21:50:47 +08:00
|
|
|
|
{
|
|
|
|
|
switch (state.NewValue)
|
|
|
|
|
{
|
2022-07-15 17:54:19 +08:00
|
|
|
|
case DownloadState.LocallyAvailable:
|
2022-06-29 21:50:47 +08:00
|
|
|
|
button.State.Value = DownloadState.LocallyAvailable;
|
|
|
|
|
break;
|
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
case DownloadState.Importing:
|
2022-06-29 21:50:47 +08:00
|
|
|
|
button.State.Value = DownloadState.Importing;
|
|
|
|
|
break;
|
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
case DownloadState.NotDownloaded:
|
2022-06-29 21:50:47 +08:00
|
|
|
|
button.State.Value = DownloadState.NotDownloaded;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}, true);
|
2022-07-15 17:54:19 +08:00
|
|
|
|
state.BindValueChanged(updateState, true);
|
2022-06-21 19:06:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveScore()
|
|
|
|
|
{
|
2022-07-08 20:31:35 +08:00
|
|
|
|
if (saveFailedScoreTask != null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
state.Value = DownloadState.Importing;
|
2022-07-08 17:36:46 +08:00
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
saveFailedScoreTask = Task.Run(importFailedScore);
|
2022-07-08 20:31:35 +08:00
|
|
|
|
saveFailedScoreTask.ContinueWith(s => Schedule(() =>
|
2022-06-29 21:50:47 +08:00
|
|
|
|
{
|
2022-07-08 20:31:35 +08:00
|
|
|
|
score = s.GetAwaiter().GetResult();
|
2022-07-15 17:54:19 +08:00
|
|
|
|
state.Value = score != null ? DownloadState.LocallyAvailable : DownloadState.NotDownloaded;
|
2022-07-08 20:31:35 +08:00
|
|
|
|
}));
|
2022-06-21 19:06:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
private void updateState(ValueChangedEvent<DownloadState> state)
|
2022-06-21 19:06:38 +08:00
|
|
|
|
{
|
|
|
|
|
switch (state.NewValue)
|
|
|
|
|
{
|
2022-07-15 17:54:19 +08:00
|
|
|
|
case DownloadState.LocallyAvailable:
|
2022-06-29 21:50:47 +08:00
|
|
|
|
button.TooltipText = @"Watch replay";
|
2022-07-08 20:42:34 +08:00
|
|
|
|
button.Enabled.Value = true;
|
2022-06-29 21:50:47 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
case DownloadState.Importing:
|
2022-06-29 21:50:47 +08:00
|
|
|
|
button.TooltipText = @"Importing score";
|
2022-07-08 20:42:34 +08:00
|
|
|
|
button.Enabled.Value = false;
|
2022-06-29 21:50:47 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2022-06-21 19:06:38 +08:00
|
|
|
|
default:
|
2022-06-22 00:19:20 +08:00
|
|
|
|
button.TooltipText = @"Save score";
|
2022-07-08 20:42:34 +08:00
|
|
|
|
button.Enabled.Value = true;
|
2022-06-21 19:06:38 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|