1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 15:27:26 +08:00
osu-lazer/osu.Game/Screens/Play/SaveFailedScoreButton.cs

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

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