osu/osu.Game/Screens/OnlinePlay/Components/RoomBackgroundScreen.cs

113 lines
3.2 KiB
C#
Raw Normal View History

2021-08-19 10:10:54 +00: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 08:05:46 +00:00
using osu.Framework.Allocation;
2021-08-19 10:10:54 +00:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
2021-08-20 08:50:49 +00:00
using osu.Framework.Screens;
2021-08-19 10:10:54 +00:00
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 08:05:46 +00:00
: base(false)
2021-08-19 10:10:54 +00:00
{
playlist.BindCollectionChanged((_, __) => updateBackground());
}
2021-08-20 08:05:46 +00:00
[BackgroundDependencyLoader]
private void load()
{
switchBackground(new PlaylistItemBackground(null));
}
2021-08-19 10:10:54 +00: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);
}
2021-08-20 08:50:49 +00:00
protected override void Update()
{
base.Update();
// This is a static screen, so override the scale set in base.Update(), but also the scale set by the screen stack.
Scale = new Vector2(1f / BackgroundScreenStack.BACKGROUND_SCALE);
}
public override void OnSuspending(IScreen next)
{
base.OnSuspending(next);
this.MoveToX(0, TRANSITION_LENGTH);
}
public override bool OnExiting(IScreen next)
{
var result = base.OnExiting(next);
this.MoveToX(0);
return result;
}
2021-08-19 10:10:54 +00:00
}
}