1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-30 01:32:55 +08:00

Add better async logic for ScreenWithBeatmapBackground

This commit is contained in:
Dean Herbert 2019-03-12 17:32:40 +09:00
parent 8230d5b52e
commit 3c2d8cad0a
2 changed files with 19 additions and 11 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Threading;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -36,9 +37,13 @@ namespace osu.Game.Screens.Backgrounds
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
backgroundLoaded(new BeatmapBackground(beatmap)); var background = new BeatmapBackground(beatmap);
LoadComponent(background);
switchBackground(background);
} }
private CancellationTokenSource cancellationSource;
public WorkingBeatmap Beatmap public WorkingBeatmap Beatmap
{ {
get => beatmap; get => beatmap;
@ -49,14 +54,18 @@ namespace osu.Game.Screens.Backgrounds
beatmap = value; beatmap = value;
// load will be completed in async load. Schedule(() =>
if (LoadState < LoadState.Ready) return; {
if ((Background as BeatmapBackground)?.Beatmap == beatmap)
return;
Schedule(() => { LoadComponentAsync(new BeatmapBackground(beatmap), b => Schedule(() => backgroundLoaded(b))); }); cancellationSource?.Cancel();
LoadComponentAsync(new BeatmapBackground(beatmap), switchBackground, (cancellationSource = new CancellationTokenSource()).Token);
});
} }
} }
private void backgroundLoaded(BeatmapBackground b) private void switchBackground(BeatmapBackground b)
{ {
float newDepth = 0; float newDepth = 0;
if (Background != null) if (Background != null)
@ -75,25 +84,24 @@ namespace osu.Game.Screens.Backgrounds
public override bool Equals(BackgroundScreen other) public override bool Equals(BackgroundScreen other)
{ {
var otherBeatmapBackground = other as BackgroundScreenBeatmap; if (!(other is BackgroundScreenBeatmap otherBeatmapBackground)) return false;
if (otherBeatmapBackground == null) return false;
return base.Equals(other) && beatmap == otherBeatmapBackground.Beatmap; return base.Equals(other) && beatmap == otherBeatmapBackground.Beatmap;
} }
protected class BeatmapBackground : Background protected class BeatmapBackground : Background
{ {
private readonly WorkingBeatmap beatmap; public readonly WorkingBeatmap Beatmap;
public BeatmapBackground(WorkingBeatmap beatmap) public BeatmapBackground(WorkingBeatmap beatmap)
{ {
this.beatmap = beatmap; Beatmap = beatmap;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(TextureStore textures) private void load(TextureStore textures)
{ {
Sprite.Texture = beatmap?.Background ?? textures.Get(@"Backgrounds/bg1"); Sprite.Texture = Beatmap?.Background ?? textures.Get(@"Backgrounds/bg1");
} }
} }
} }

View File

@ -55,7 +55,7 @@ namespace osu.Game.Screens.Play
/// Called when background elements require updates, usually due to a user changing a setting. /// Called when background elements require updates, usually due to a user changing a setting.
/// </summary> /// </summary>
/// <param name="userChange"></param> /// <param name="userChange"></param>
protected virtual void UpdateBackgroundElements() protected void UpdateBackgroundElements()
{ {
if (!this.IsCurrentScreen()) return; if (!this.IsCurrentScreen()) return;