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

94 lines
2.6 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.
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.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;
2021-08-20 09:14:12 +00:00
#nullable enable
2021-08-19 10:10:54 +00:00
namespace osu.Game.Screens.OnlinePlay.Components
{
2021-08-20 09:14:12 +00:00
public abstract class OnlinePlayBackgroundScreen : BackgroundScreen
2021-08-19 10:10:54 +00:00
{
private CancellationTokenSource? cancellationSource;
private PlaylistItemBackground? background;
2021-08-20 09:14:12 +00:00
protected OnlinePlayBackgroundScreen()
2021-08-20 08:05:46 +00:00
: base(false)
2021-08-19 10:10:54 +00:00
{
}
2021-08-20 08:05:46 +00:00
[BackgroundDependencyLoader]
private void load()
{
2021-08-20 09:14:12 +00:00
switchBackground(new PlaylistItemBackground(playlistItem));
2021-08-20 08:05:46 +00:00
}
2021-08-20 09:14:12 +00:00
private PlaylistItem? playlistItem;
2021-08-19 10:10:54 +00:00
2021-08-20 09:14:12 +00:00
protected PlaylistItem? PlaylistItem
2021-08-19 10:10:54 +00:00
{
2021-08-20 09:14:12 +00:00
get => playlistItem;
2021-08-19 10:10:54 +00:00
set
{
2021-08-20 09:14:12 +00:00
if (playlistItem == value)
2021-08-19 10:10:54 +00:00
return;
2021-08-20 09:14:12 +00:00
playlistItem = value;
2021-08-19 10:10:54 +00:00
2021-08-20 09:14:12 +00:00
if (LoadState > LoadState.Ready)
updateBackground();
2021-08-19 10:10:54 +00:00
}
}
private void updateBackground()
{
Schedule(() =>
{
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
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
}
}