refactor BeatmapBackgroundWithStoryboard to reduce overhead

This avoids loading the sprite if its not needed and instead of hiding it, it is removed when the storyboard replaces the background or there is a video.

This also only initializes DrawableStoryboard if there are any elements in any layer.
This commit is contained in:
Nathan Alo
2021-06-02 20:27:12 +08:00
parent 3c3ef13632
commit 277545bb06
2 changed files with 23 additions and 17 deletions

View File

@ -13,16 +13,21 @@ namespace osu.Game.Graphics.Backgrounds
private readonly string fallbackTextureName; private readonly string fallbackTextureName;
[Resolved]
private LargeTextureStore textures { get; set; }
public BeatmapBackground(WorkingBeatmap beatmap, string fallbackTextureName = @"Backgrounds/bg1") public BeatmapBackground(WorkingBeatmap beatmap, string fallbackTextureName = @"Backgrounds/bg1")
{ {
Beatmap = beatmap; Beatmap = beatmap;
this.fallbackTextureName = fallbackTextureName; this.fallbackTextureName = fallbackTextureName;
} }
[BackgroundDependencyLoader] protected override void LoadComplete()
private void load(LargeTextureStore textures)
{ {
Sprite.Texture = Beatmap?.Background ?? textures.Get(fallbackTextureName); base.LoadComplete();
Initialize();
} }
protected virtual void Initialize() => Sprite.Texture = Beatmap?.Background ?? textures.Get(fallbackTextureName);
} }
} }

View File

@ -1,8 +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 osu.Framework.Allocation; using System.Linq;
using osu.Framework.Graphics;
using osu.Framework.Timing; using osu.Framework.Timing;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Storyboards.Drawables; using osu.Game.Storyboards.Drawables;
@ -16,19 +15,21 @@ namespace osu.Game.Graphics.Backgrounds
{ {
} }
[BackgroundDependencyLoader] protected override void Initialize()
private void load()
{ {
LoadComponentAsync(new DrawableStoryboard(Beatmap.Storyboard) if (Beatmap.Storyboard.HasDrawable)
{ {
Clock = new InterpolatingFramedClock(Beatmap.Track), LoadComponentAsync(new DrawableStoryboard(Beatmap.Storyboard) { Clock = new InterpolatingFramedClock(Beatmap.Track) }, AddInternal);
}, }
loaded =>
{ if (Beatmap.Storyboard.ReplacesBackground || Beatmap.Storyboard.Layers.First(l => l.Name == "Video").Elements.Any())
AddInternal(loaded); {
if (Beatmap.Storyboard.ReplacesBackground) Sprite.Expire();
Sprite.FadeOut(300, Easing.OutQuint); }
}); else
{
base.Initialize();
}
} }
} }
} }