1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 19:27:31 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Components/RoomBackgroundScreen.cs

91 lines
2.5 KiB
C#
Raw Normal View History

2021-08-19 18:10:54 +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.
#nullable enable
using System.Linq;
using System.Threading;
2021-08-20 16:05:46 +08:00
using osu.Framework.Allocation;
2021-08-19 18:10:54 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Online.Rooms;
using osuTK;
namespace osu.Game.Screens.OnlinePlay.Components
{
public class RoomBackgroundScreen : BackgroundScreen
{
private CancellationTokenSource? cancellationSource;
private PlaylistItemBackground? background;
private readonly BindableList<PlaylistItem> playlist = new BindableList<PlaylistItem>();
public RoomBackgroundScreen()
2021-08-20 16:05:46 +08:00
: base(false)
2021-08-19 18:10:54 +08:00
{
playlist.BindCollectionChanged((_, __) => updateBackground());
}
2021-08-20 16:05:46 +08:00
[BackgroundDependencyLoader]
private void load()
{
switchBackground(new PlaylistItemBackground(null));
}
2021-08-19 18:10:54 +08:00
private Room? room;
public Room? Room
{
get => room;
set
{
if (room == value)
return;
if (room != null)
playlist.UnbindFrom(room.Playlist);
room = value;
if (room != null)
playlist.BindTo(room.Playlist);
else
playlist.Clear();
}
}
private void updateBackground()
{
Schedule(() =>
{
var playlistItem = playlist.FirstOrDefault();
var beatmap = playlistItem?.Beatmap.Value;
if (background?.BeatmapInfo?.BeatmapSet?.OnlineInfo?.Covers?.Cover == beatmap?.BeatmapSet?.OnlineInfo?.Covers?.Cover)
return;
cancellationSource?.Cancel();
LoadComponentAsync(new PlaylistItemBackground(playlistItem), switchBackground, (cancellationSource = new CancellationTokenSource()).Token);
});
}
private void switchBackground(PlaylistItemBackground newBackground)
{
float newDepth = 0;
if (background != null)
{
newDepth = background.Depth + 1;
background.FinishTransforms();
background.FadeOut(250);
background.Expire();
}
newBackground.Depth = newDepth;
newBackground.BlurTo(new Vector2(10));
AddInternal(background = newBackground);
}
}
}