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-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;
|
2023-06-19 19:43:33 +08:00
|
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
|
using osu.Framework.Input.Events;
|
2022-07-15 19:45:37 +08:00
|
|
|
|
using osu.Game.Database;
|
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;
|
2023-06-19 19:43:33 +08:00
|
|
|
|
using osu.Game.Input.Bindings;
|
2022-06-21 19:06:38 +08:00
|
|
|
|
using osu.Game.Online;
|
2023-06-21 16:48:55 +08:00
|
|
|
|
using osu.Game.Online.Multiplayer;
|
2022-06-22 00:19:20 +08:00
|
|
|
|
using osuTK;
|
2022-06-21 19:06:38 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
|
{
|
2023-06-19 19:43:33 +08:00
|
|
|
|
public partial class SaveFailedScoreButton : CompositeDrawable, IKeyBindingHandler<GlobalAction>
|
2022-06-21 19:06:38 +08:00
|
|
|
|
{
|
2023-06-20 15:38:30 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private RealmAccess realm { get; set; } = null!;
|
|
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
|
private ScoreManager scoreManager { get; set; } = null!;
|
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
private readonly Bindable<DownloadState> state = new Bindable<DownloadState>();
|
|
|
|
|
|
2024-02-28 15:24:04 +08:00
|
|
|
|
private readonly Func<Task<ScoreInfo>>? importFailedScore;
|
2022-07-15 17:54:19 +08:00
|
|
|
|
|
2022-07-16 04:39:04 +08:00
|
|
|
|
private ScoreInfo? importedScore;
|
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
|
|
|
|
|
2024-02-28 15:24:04 +08:00
|
|
|
|
public SaveFailedScoreButton(Func<Task<ScoreInfo>>? importFailedScore)
|
2022-06-29 21:50:47 +08:00
|
|
|
|
{
|
|
|
|
|
Size = new Vector2(50, 30);
|
2022-07-15 18:02:12 +08:00
|
|
|
|
|
|
|
|
|
this.importFailedScore = importFailedScore;
|
2022-06-29 21:50:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 17:54:19 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2023-06-20 15:38:30 +08:00
|
|
|
|
private void load(OsuGame? game, Player? player)
|
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,
|
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:
|
2022-07-16 04:39:04 +08:00
|
|
|
|
game?.PresentScore(importedScore, ScorePresentType.Gameplay);
|
2022-07-15 18:02:12 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case DownloadState.NotDownloaded:
|
|
|
|
|
state.Value = DownloadState.Importing;
|
2024-02-28 15:24:04 +08:00
|
|
|
|
|
|
|
|
|
if (importFailedScore != null)
|
2022-07-15 18:02:12 +08:00
|
|
|
|
{
|
2024-02-28 15:24:04 +08:00
|
|
|
|
Task.Run(importFailedScore).ContinueWith(t =>
|
|
|
|
|
{
|
|
|
|
|
importedScore = realm.Run(r => r.Find<ScoreInfo>(t.GetResultSafely().ID)?.Detach());
|
|
|
|
|
Schedule(() => state.Value = importedScore != null ? DownloadState.LocallyAvailable : DownloadState.NotDownloaded);
|
|
|
|
|
}).FireAndForget();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2022-07-15 19:45:37 +08:00
|
|
|
|
if (player != null)
|
|
|
|
|
{
|
2022-07-16 04:39:04 +08:00
|
|
|
|
importedScore = realm.Run(r => r.Find<ScoreInfo>(player.Score.ScoreInfo.ID)?.Detach());
|
2022-09-08 15:48:43 +08:00
|
|
|
|
state.Value = importedScore != null ? DownloadState.LocallyAvailable : DownloadState.NotDownloaded;
|
2022-07-15 19:45:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
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-07-15 18:02:12 +08:00
|
|
|
|
button.TooltipText = @"watch replay";
|
|
|
|
|
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-07-15 18:02:12 +08:00
|
|
|
|
button.TooltipText = @"importing score";
|
|
|
|
|
button.Enabled.Value = false;
|
2022-06-29 21:50:47 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2022-07-15 18:02:12 +08:00
|
|
|
|
default:
|
|
|
|
|
button.TooltipText = @"save score";
|
|
|
|
|
button.Enabled.Value = true;
|
2022-06-29 21:50:47 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}, true);
|
2022-06-21 19:06:38 +08:00
|
|
|
|
}
|
2023-06-19 19:43:33 +08:00
|
|
|
|
|
2023-06-21 16:48:12 +08:00
|
|
|
|
#region Export via hotkey logic (also in ReplayDownloadButton)
|
|
|
|
|
|
2023-06-19 19:43:33 +08:00
|
|
|
|
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
|
|
|
|
|
{
|
2023-12-20 15:36:57 +08:00
|
|
|
|
if (e.Repeat)
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-06-19 19:43:33 +08:00
|
|
|
|
switch (e.Action)
|
|
|
|
|
{
|
|
|
|
|
case GlobalAction.SaveReplay:
|
|
|
|
|
button.TriggerClick();
|
|
|
|
|
return true;
|
2023-06-20 15:38:30 +08:00
|
|
|
|
|
|
|
|
|
case GlobalAction.ExportReplay:
|
2023-06-21 16:48:12 +08:00
|
|
|
|
state.BindValueChanged(exportWhenReady, true);
|
|
|
|
|
|
|
|
|
|
// start the import via button
|
|
|
|
|
if (state.Value != DownloadState.LocallyAvailable)
|
|
|
|
|
button.TriggerClick();
|
|
|
|
|
|
2023-06-20 15:38:30 +08:00
|
|
|
|
return true;
|
2023-06-19 19:43:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
|
|
|
|
|
{
|
|
|
|
|
}
|
2023-06-21 16:48:12 +08:00
|
|
|
|
|
|
|
|
|
private void exportWhenReady(ValueChangedEvent<DownloadState> state)
|
|
|
|
|
{
|
|
|
|
|
if (state.NewValue != DownloadState.LocallyAvailable) return;
|
|
|
|
|
|
|
|
|
|
scoreManager.Export(importedScore);
|
|
|
|
|
|
|
|
|
|
this.state.ValueChanged -= exportWhenReady;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2022-06-21 19:06:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|