1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-21 22:27:23 +08:00

Merge pull request #25400 from frenzibyte/unload-main-menu-storyboard

Unload beatmap storyboards when not in main menu screen
This commit is contained in:
Bartłomiej Dach 2023-11-16 16:47:03 +09:00 committed by GitHub
commit d09d58639a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 141 additions and 10 deletions

View File

@ -181,6 +181,54 @@ namespace osu.Game.Tests.Visual.Background
AddStep("restore default beatmap", () => Beatmap.SetDefault());
}
[Test]
public void TestBeatmapBackgroundWithStoryboardUnloadedOnSuspension()
{
BackgroundScreenBeatmap nestedScreen = null;
setSupporter(true);
setSourceMode(BackgroundSource.BeatmapWithStoryboard);
AddStep("change beatmap", () => Beatmap.Value = createTestWorkingBeatmapWithStoryboard());
AddAssert("background changed", () => screen.CheckLastLoadChange() == true);
AddUntilStep("wait for beatmap background to be loaded", () => getCurrentBackground()?.GetType() == typeof(BeatmapBackgroundWithStoryboard));
AddUntilStep("storyboard present", () => screen.ChildrenOfType<DrawableStoryboard>().SingleOrDefault()?.IsLoaded == true);
AddStep("push new background to stack", () => stack.Push(nestedScreen = new BackgroundScreenBeatmap(Beatmap.Value)));
AddUntilStep("wait for screen to load", () => nestedScreen.IsLoaded && nestedScreen.IsCurrentScreen());
AddUntilStep("storyboard unloaded", () => !screen.ChildrenOfType<DrawableStoryboard>().Any());
AddStep("go back", () => screen.MakeCurrent());
AddUntilStep("storyboard reloaded", () => screen.ChildrenOfType<DrawableStoryboard>().SingleOrDefault()?.IsLoaded == true);
}
[Test]
public void TestBeatmapBackgroundWithStoryboardButBeatmapHasNone()
{
BackgroundScreenBeatmap nestedScreen = null;
setSupporter(true);
setSourceMode(BackgroundSource.BeatmapWithStoryboard);
AddStep("change beatmap", () => Beatmap.Value = createTestWorkingBeatmapWithUniqueBackground());
AddAssert("background changed", () => screen.CheckLastLoadChange() == true);
AddUntilStep("wait for beatmap background to be loaded", () => getCurrentBackground()?.GetType() == typeof(BeatmapBackgroundWithStoryboard));
AddUntilStep("no storyboard loaded", () => !screen.ChildrenOfType<DrawableStoryboard>().Any());
AddStep("push new background to stack", () => stack.Push(nestedScreen = new BackgroundScreenBeatmap(Beatmap.Value)));
AddUntilStep("wait for screen to load", () => nestedScreen.IsLoaded && nestedScreen.IsCurrentScreen());
AddUntilStep("still no storyboard", () => !screen.ChildrenOfType<DrawableStoryboard>().Any());
AddStep("go back", () => screen.MakeCurrent());
AddUntilStep("still no storyboard", () => !screen.ChildrenOfType<DrawableStoryboard>().Any());
}
[Test]
public void TestBackgroundTypeSwitch()
{

View File

@ -2,6 +2,8 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -10,6 +12,7 @@ using osu.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Overlays;
using osu.Game.Rulesets.Mods;
using osu.Game.Screens;
using osu.Game.Storyboards.Drawables;
namespace osu.Game.Graphics.Backgrounds
@ -18,6 +21,10 @@ namespace osu.Game.Graphics.Backgrounds
{
private readonly InterpolatingFramedClock storyboardClock;
private AudioContainer storyboardContainer = null!;
private DrawableStoryboard? drawableStoryboard;
private CancellationTokenSource? loadCancellationSource = new CancellationTokenSource();
[Resolved(CanBeNull = true)]
private MusicController? musicController { get; set; }
@ -33,18 +40,59 @@ namespace osu.Game.Graphics.Backgrounds
[BackgroundDependencyLoader]
private void load()
{
if (!Beatmap.Storyboard.HasDrawable)
return;
if (Beatmap.Storyboard.ReplacesBackground)
Sprite.Alpha = 0;
LoadComponentAsync(new AudioContainer
AddInternal(storyboardContainer = new AudioContainer
{
RelativeSizeAxes = Axes.Both,
Volume = { Value = 0 },
Child = new DrawableStoryboard(Beatmap.Storyboard, mods.Value) { Clock = storyboardClock }
}, AddInternal);
});
LoadStoryboard(false);
}
public void LoadStoryboard(bool async = true)
{
Debug.Assert(drawableStoryboard == null);
if (!Beatmap.Storyboard.HasDrawable)
return;
drawableStoryboard = new DrawableStoryboard(Beatmap.Storyboard, mods.Value)
{
Clock = storyboardClock
};
if (async)
LoadComponentAsync(drawableStoryboard, finishLoad, (loadCancellationSource = new CancellationTokenSource()).Token);
else
{
LoadComponent(drawableStoryboard);
finishLoad(drawableStoryboard);
}
void finishLoad(DrawableStoryboard s)
{
if (Beatmap.Storyboard.ReplacesBackground)
Sprite.FadeOut(BackgroundScreen.TRANSITION_LENGTH, Easing.InQuint);
storyboardContainer.FadeInFromZero(BackgroundScreen.TRANSITION_LENGTH, Easing.OutQuint);
storyboardContainer.Add(s);
}
}
public void UnloadStoryboard()
{
if (drawableStoryboard == null)
return;
loadCancellationSource?.Cancel();
loadCancellationSource = null;
// clear is intentionally used here for the storyboard to be disposed asynchronously.
storyboardContainer.Clear();
drawableStoryboard = null;
Sprite.Alpha = 1f;
}
protected override void LoadComplete()

View File

@ -13,7 +13,8 @@ namespace osu.Game.Screens
{
public abstract partial class BackgroundScreen : Screen, IEquatable<BackgroundScreen>
{
protected const float TRANSITION_LENGTH = 500;
public const float TRANSITION_LENGTH = 500;
private const float x_movement_amount = 50;
private readonly bool animateOnEnter;

View File

@ -3,11 +3,14 @@
#nullable disable
using System.Diagnostics;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Screens;
using osu.Framework.Threading;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
@ -34,6 +37,9 @@ namespace osu.Game.Screens.Backgrounds
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; }
[Resolved]
private GameHost gameHost { get; set; }
protected virtual bool AllowStoryboardBackground => true;
public BackgroundScreenDefault(bool animateOnEnter = true)
@ -71,6 +77,34 @@ namespace osu.Game.Screens.Backgrounds
void next() => Next();
}
private ScheduledDelegate storyboardUnloadDelegate;
public override void OnSuspending(ScreenTransitionEvent e)
{
var backgroundScreenStack = Parent as BackgroundScreenStack;
Debug.Assert(backgroundScreenStack != null);
if (background is BeatmapBackgroundWithStoryboard storyboardBackground)
storyboardUnloadDelegate = gameHost.UpdateThread.Scheduler.AddDelayed(storyboardBackground.UnloadStoryboard, TRANSITION_LENGTH);
base.OnSuspending(e);
}
public override void OnResuming(ScreenTransitionEvent e)
{
if (background is BeatmapBackgroundWithStoryboard storyboardBackground)
{
if (storyboardUnloadDelegate?.Completed == false)
storyboardUnloadDelegate.Cancel();
else
storyboardBackground.LoadStoryboard();
storyboardUnloadDelegate = null;
}
base.OnResuming(e);
}
private ScheduledDelegate nextTask;
private CancellationTokenSource cancellationTokenSource;