1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-08 04:17:24 +08:00
osu-lazer/osu.Game/Screens/Backgrounds/BackgroundScreenDefault.cs

86 lines
2.5 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2018-04-13 17:19:50 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Allocation;
using osu.Framework.Configuration;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.MathUtils;
2018-04-13 17:19:50 +08:00
using osu.Framework.Threading;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Online.API;
using osu.Game.Skinning;
using osu.Game.Users;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Backgrounds
{
public class BackgroundScreenDefault : BlurrableBackgroundScreen
2018-04-13 17:19:50 +08:00
{
private int currentDisplay;
private const int background_count = 5;
private string backgroundName => $@"Menu/menu-background-{currentDisplay % background_count + 1}";
private Bindable<User> user;
private Bindable<Skin> skin;
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(IAPIProvider api, SkinManager skinManager)
2018-04-13 17:19:50 +08:00
{
user = api.LocalUser.GetBoundCopy();
skin = skinManager.CurrentSkin.GetBoundCopy();
user.ValueChanged += _ => Next();
skin.ValueChanged += _ => Next();
currentDisplay = RNG.Next(0, background_count);
Next();
2018-04-13 17:19:50 +08:00
}
private void display(Background newBackground)
{
Background?.FadeOut(800, Easing.InOutSine);
Background?.Expire();
2018-04-13 17:19:50 +08:00
Add(Background = newBackground);
2018-04-13 17:19:50 +08:00
currentDisplay++;
}
private ScheduledDelegate nextTask;
public void Next()
{
nextTask?.Cancel();
nextTask = Scheduler.AddDelayed(() =>
{
Background background;
if (user.Value?.IsSupporter ?? false)
background = new SkinnedBackground(skin.Value, backgroundName);
else
background = new Background(backgroundName);
background.Depth = currentDisplay;
LoadComponentAsync(background, display);
2018-04-13 17:19:50 +08:00
}, 100);
}
private class SkinnedBackground : Background
{
private readonly Skin skin;
public SkinnedBackground(Skin skin, string fallbackTextureName) : base(fallbackTextureName)
{
this.skin = skin;
}
[BackgroundDependencyLoader]
private void load()
{
Sprite.Texture = skin.GetTexture("menu-background") ?? Sprite.Texture;
}
}
2018-04-13 17:19:50 +08:00
}
}