1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 13:47:38 +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.

92 lines
3.2 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-07-15 18:02:12 +08:00
using osu.Framework.Extensions;
2022-06-22 00:19:20 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Database;
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 partial class SaveFailedScoreButton : CompositeDrawable
{
private readonly Bindable<DownloadState> state = new Bindable<DownloadState>();
private readonly Func<Task<ScoreInfo>> importFailedScore;
private ScoreInfo? importedScore;
2022-06-22 00:19:20 +08:00
private DownloadButton button = null!;
2022-06-22 00:19:20 +08:00
2022-07-15 18:02:12 +08:00
public SaveFailedScoreButton(Func<Task<ScoreInfo>> importFailedScore)
{
Size = new Vector2(50, 30);
2022-07-15 18:02:12 +08:00
this.importFailedScore = importFailedScore;
}
[BackgroundDependencyLoader]
private void load(OsuGame? game, Player? player, RealmAccess realm)
2022-06-22 00:19:20 +08:00
{
InternalChild = button = new DownloadButton
2022-06-22 00:19:20 +08:00
{
RelativeSizeAxes = Axes.Both,
2022-07-15 18:02:12 +08:00
State = { BindTarget = state },
Action = () =>
2022-06-22 00:19:20 +08:00
{
2022-07-15 18:02:12 +08:00
switch (state.Value)
{
case DownloadState.LocallyAvailable:
game?.PresentScore(importedScore, ScorePresentType.Gameplay);
2022-07-15 18:02:12 +08:00
break;
case DownloadState.NotDownloaded:
state.Value = DownloadState.Importing;
Task.Run(importFailedScore).ContinueWith(t =>
2022-07-15 18:02:12 +08:00
{
importedScore = realm.Run(r => r.Find<ScoreInfo>(t.GetResultSafely().ID)?.Detach());
Schedule(() => state.Value = importedScore != null ? DownloadState.LocallyAvailable : DownloadState.NotDownloaded);
});
2022-07-15 18:02:12 +08:00
break;
}
2022-06-22 00:19:20 +08:00
}
};
2022-07-15 18:02:12 +08:00
if (player != null)
{
importedScore = realm.Run(r => r.Find<ScoreInfo>(player.Score.ScoreInfo.ID)?.Detach());
state.Value = importedScore != null ? DownloadState.LocallyAvailable : DownloadState.NotDownloaded;
}
state.BindValueChanged(state =>
{
switch (state.NewValue)
{
case DownloadState.LocallyAvailable:
2022-07-15 18:02:12 +08:00
button.TooltipText = @"watch replay";
button.Enabled.Value = true;
break;
case DownloadState.Importing:
2022-07-15 18:02:12 +08:00
button.TooltipText = @"importing score";
button.Enabled.Value = false;
break;
2022-07-15 18:02:12 +08:00
default:
button.TooltipText = @"save score";
button.Enabled.Value = true;
break;
}
}, true);
}
}
}