Avoid reloading background unnecessariyl when not yet loaded

This commit is contained in:
Dean Herbert 2022-07-08 01:39:09 +09:00
parent b83073c2e9
commit 789904ccd1

View File

@ -7,6 +7,7 @@ using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Logging;
using osu.Framework.Threading;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
@ -26,7 +27,7 @@ namespace osu.Game.Screens.Backgrounds
private const int background_count = 7;
private IBindable<APIUser> user;
private Bindable<Skin> skin;
private Bindable<BackgroundSource> mode;
private Bindable<BackgroundSource> source;
private Bindable<IntroSequence> introSequence;
private readonly SeasonalBackgroundLoader seasonalBackgroundLoader = new SeasonalBackgroundLoader();
@ -45,14 +46,14 @@ namespace osu.Game.Screens.Backgrounds
{
user = api.LocalUser.GetBoundCopy();
skin = skinManager.CurrentSkin.GetBoundCopy();
mode = config.GetBindable<BackgroundSource>(OsuSetting.MenuBackgroundSource);
source = config.GetBindable<BackgroundSource>(OsuSetting.MenuBackgroundSource);
introSequence = config.GetBindable<IntroSequence>(OsuSetting.IntroSequence);
AddInternal(seasonalBackgroundLoader);
user.ValueChanged += _ => Scheduler.AddOnce(loadNextIfRequired);
skin.ValueChanged += _ => Scheduler.AddOnce(loadNextIfRequired);
mode.ValueChanged += _ => Scheduler.AddOnce(loadNextIfRequired);
source.ValueChanged += _ => Scheduler.AddOnce(loadNextIfRequired);
beatmap.ValueChanged += _ => Scheduler.AddOnce(loadNextIfRequired);
introSequence.ValueChanged += _ => Scheduler.AddOnce(loadNextIfRequired);
seasonalBackgroundLoader.SeasonalBackgroundChanged += () => Scheduler.AddOnce(loadNextIfRequired);
@ -62,7 +63,13 @@ namespace osu.Game.Screens.Backgrounds
Next();
// helper function required for AddOnce usage.
void loadNextIfRequired() => Next();
void loadNextIfRequired()
{
if (!IsLoaded)
return;
Next();
}
}
private ScheduledDelegate nextTask;
@ -80,6 +87,8 @@ namespace osu.Game.Screens.Backgrounds
if (nextBackground == background)
return false;
Logger.Log("🌅 Background change queued");
cancellationTokenSource?.Cancel();
cancellationTokenSource = new CancellationTokenSource();
@ -108,12 +117,12 @@ namespace osu.Game.Screens.Backgrounds
if (newBackground == null && user.Value?.IsSupporter == true)
{
switch (mode.Value)
switch (source.Value)
{
case BackgroundSource.Beatmap:
case BackgroundSource.BeatmapWithStoryboard:
{
if (mode.Value == BackgroundSource.BeatmapWithStoryboard && AllowStoryboardBackground)
if (source.Value == BackgroundSource.BeatmapWithStoryboard && AllowStoryboardBackground)
newBackground = new BeatmapBackgroundWithStoryboard(beatmap.Value, getBackgroundTextureName());
newBackground ??= new BeatmapBackground(beatmap.Value, getBackgroundTextureName());