1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 14:52:55 +08:00
osu-lazer/osu.Game/Screens/Backgrounds/BackgroundScreenDefault.cs

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

207 lines
7.4 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Diagnostics;
using System.Threading;
2016-11-09 07:13:20 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2017-06-26 18:06:08 +08:00
using osu.Framework.Graphics;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Screens;
using osu.Framework.Threading;
using osu.Framework.Utils;
2019-09-24 17:42:06 +08:00
using osu.Game.Beatmaps;
using osu.Game.Configuration;
2016-11-23 10:59:50 +08:00
using osu.Game.Graphics.Backgrounds;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Skinning;
2018-04-13 17:19:50 +08:00
2016-11-14 16:23:33 +08:00
namespace osu.Game.Screens.Backgrounds
{
public partial class BackgroundScreenDefault : BackgroundScreen
{
private Background background;
2017-06-26 18:06:08 +08:00
private int currentDisplay;
2024-01-29 18:33:07 +08:00
private const int background_count = 8;
private IBindable<APIUser> user;
private Bindable<Skin> skin;
private Bindable<BackgroundSource> source;
private Bindable<IntroSequence> introSequence;
private readonly SeasonalBackgroundLoader seasonalBackgroundLoader = new SeasonalBackgroundLoader();
2019-09-24 17:42:06 +08:00
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; }
[Resolved]
private GameHost gameHost { get; set; }
protected virtual bool AllowStoryboardBackground => true;
2019-08-09 19:05:28 +08:00
public BackgroundScreenDefault(bool animateOnEnter = true)
: base(animateOnEnter)
{
}
[BackgroundDependencyLoader]
2019-09-24 17:42:06 +08:00
private void load(IAPIProvider api, SkinManager skinManager, OsuConfigManager config)
{
user = api.LocalUser.GetBoundCopy();
skin = skinManager.CurrentSkin.GetBoundCopy();
source = config.GetBindable<BackgroundSource>(OsuSetting.MenuBackgroundSource);
introSequence = config.GetBindable<IntroSequence>(OsuSetting.IntroSequence);
AddInternal(seasonalBackgroundLoader);
2022-07-08 14:09:16 +08:00
}
2022-07-08 14:09:16 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
user.ValueChanged += _ => Scheduler.AddOnce(next);
skin.ValueChanged += _ => Scheduler.AddOnce(next);
source.ValueChanged += _ => Scheduler.AddOnce(next);
beatmap.ValueChanged += _ => Scheduler.AddOnce(next);
introSequence.ValueChanged += _ => Scheduler.AddOnce(next);
seasonalBackgroundLoader.SeasonalBackgroundChanged += () => Scheduler.AddOnce(next);
currentDisplay = RNG.Next(0, background_count);
Next();
2022-07-08 14:09:16 +08:00
// helper function required for AddOnce usage.
void next() => Next();
2017-06-26 18:06:08 +08:00
}
2018-04-13 17:19:50 +08:00
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;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Request loading the next background.
/// </summary>
/// <returns>Whether a new background was queued for load. May return false if the current background is still valid.</returns>
public virtual bool Next()
2017-06-26 18:06:08 +08:00
{
var nextBackground = createBackground();
// in the case that the background hasn't changed, we want to avoid cancelling any tasks that could still be loading.
if (nextBackground == background)
return false;
Logger.Log(@"🌅 Global background change queued");
cancellationTokenSource?.Cancel();
cancellationTokenSource = new CancellationTokenSource();
nextTask?.Cancel();
nextTask = Scheduler.AddDelayed(() =>
{
Logger.Log(@"🌅 Global background loading");
LoadComponentAsync(nextBackground, displayNext, cancellationTokenSource.Token);
}, 500);
return true;
}
private void displayNext(Background newBackground)
{
background?.FadeOut(800, Easing.OutQuint);
background?.Expire();
AddInternal(background = newBackground);
currentDisplay++;
}
private Background createBackground()
{
// seasonal background loading gets highest priority.
Background newBackground = seasonalBackgroundLoader.LoadNextBackground();
if (newBackground == null && user.Value?.IsSupporter == true)
2019-09-24 17:42:06 +08:00
{
switch (source.Value)
2019-09-24 17:42:06 +08:00
{
2019-11-22 01:38:31 +08:00
case BackgroundSource.Beatmap:
case BackgroundSource.BeatmapWithStoryboard:
{
if (source.Value == BackgroundSource.BeatmapWithStoryboard && AllowStoryboardBackground)
newBackground = new BeatmapBackgroundWithStoryboard(beatmap.Value, getBackgroundTextureName());
newBackground ??= new BeatmapBackground(beatmap.Value, getBackgroundTextureName());
2019-09-24 17:42:06 +08:00
break;
}
2021-06-09 04:04:59 +08:00
case BackgroundSource.Skin:
2022-09-15 15:02:57 +08:00
switch (skin.Value)
{
case TrianglesSkin:
case ArgonSkin:
case DefaultLegacySkin:
// default skins should use the default background rotation, which won't be the case if a SkinBackground is created for them.
break;
default:
newBackground = new SkinBackground(skin.Value, getBackgroundTextureName());
break;
}
2021-06-09 04:04:59 +08:00
break;
2019-09-24 17:42:06 +08:00
}
}
// this method is called in many cases where the background might not necessarily need to change.
// if an equivalent background is currently being shown, we don't want to load it again.
if (newBackground?.Equals(background) == true)
return background;
newBackground ??= new Background(getBackgroundTextureName());
2019-03-20 14:13:59 +08:00
newBackground.Depth = currentDisplay;
2019-03-20 14:13:59 +08:00
return newBackground;
}
private string getBackgroundTextureName()
{
switch (introSequence.Value)
{
case IntroSequence.Welcome:
return @"Intro/Welcome/menu-background";
default:
return $@"Menu/menu-background-{currentDisplay % background_count + 1}";
}
}
}
2017-06-26 22:05:35 +08:00
}