mirror of
https://github.com/ppy/osu.git
synced 2025-03-15 13:27:20 +08:00
Merge pull request #4419 from nekodex/fix-duplicate-bg-sprites
Fix UpdateableBeatmapBackgroundSprite not disposing of previously loaded sprites
This commit is contained in:
commit
3a634b7d72
@ -16,7 +16,7 @@ namespace osu.Game.Tests.Visual
|
||||
{
|
||||
public class TestCaseUpdateableBeatmapBackgroundSprite : OsuTestCase
|
||||
{
|
||||
private UpdateableBeatmapBackgroundSprite backgroundSprite;
|
||||
private TestUpdateableBeatmapBackgroundSprite backgroundSprite;
|
||||
|
||||
[Resolved]
|
||||
private BeatmapManager beatmaps { get; set; }
|
||||
@ -28,30 +28,36 @@ namespace osu.Game.Tests.Visual
|
||||
|
||||
var imported = ImportBeatmapTest.LoadOszIntoOsu(osu);
|
||||
|
||||
Child = backgroundSprite = new UpdateableBeatmapBackgroundSprite { RelativeSizeAxes = Axes.Both };
|
||||
Child = backgroundSprite = new TestUpdateableBeatmapBackgroundSprite { RelativeSizeAxes = Axes.Both };
|
||||
|
||||
backgroundSprite.Beatmap.BindTo(beatmapBindable);
|
||||
|
||||
var req = new GetBeatmapSetRequest(1);
|
||||
api.Queue(req);
|
||||
|
||||
AddStep("null", () => beatmapBindable.Value = null);
|
||||
|
||||
AddStep("imported", () => beatmapBindable.Value = imported.Beatmaps.First());
|
||||
AddStep("load null beatmap", () => beatmapBindable.Value = null);
|
||||
AddUntilStep(() => backgroundSprite.ChildCount == 1, "wait for cleanup...");
|
||||
AddStep("load imported beatmap", () => beatmapBindable.Value = imported.Beatmaps.First());
|
||||
AddUntilStep(() => backgroundSprite.ChildCount == 1, "wait for cleanup...");
|
||||
|
||||
if (api.IsLoggedIn)
|
||||
{
|
||||
AddUntilStep(() => req.Result != null, "wait for api response");
|
||||
|
||||
AddStep("online", () => beatmapBindable.Value = new BeatmapInfo
|
||||
AddStep("load online beatmap", () => beatmapBindable.Value = new BeatmapInfo
|
||||
{
|
||||
BeatmapSet = req.Result?.ToBeatmapSet(rulesets)
|
||||
});
|
||||
AddUntilStep(() => backgroundSprite.ChildCount == 1, "wait for cleanup...");
|
||||
}
|
||||
else
|
||||
{
|
||||
AddStep("online (login first)", () => { });
|
||||
}
|
||||
}
|
||||
|
||||
private class TestUpdateableBeatmapBackgroundSprite : UpdateableBeatmapBackgroundSprite
|
||||
{
|
||||
public int ChildCount => InternalChildren.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ using osu.Framework.Graphics.Containers;
|
||||
namespace osu.Game.Beatmaps.Drawables
|
||||
{
|
||||
/// <summary>
|
||||
/// Display a baetmap background from a local source, but fallback to online source if not available.
|
||||
/// Display a beatmap background from a local source, but fallback to online source if not available.
|
||||
/// </summary>
|
||||
public class UpdateableBeatmapBackgroundSprite : ModelBackedDrawable<BeatmapInfo>
|
||||
{
|
||||
@ -26,37 +26,51 @@ namespace osu.Game.Beatmaps.Drawables
|
||||
this.beatmapSetCoverType = beatmapSetCoverType;
|
||||
}
|
||||
|
||||
protected override Drawable CreateDrawable(BeatmapInfo model)
|
||||
private BeatmapInfo lastModel;
|
||||
|
||||
protected override DelayedLoadWrapper CreateDelayedLoadWrapper(Drawable content, double timeBeforeLoad)
|
||||
{
|
||||
return new DelayedLoadUnloadWrapper(() =>
|
||||
{
|
||||
Drawable drawable;
|
||||
// If DelayedLoadUnloadWrapper is attempting to RELOAD the same content (Beatmap), that means that it was
|
||||
// previously UNLOADED and thus its children have been disposed of, so we need to recreate them here.
|
||||
if (lastModel == Beatmap.Value && Beatmap.Value != null)
|
||||
return CreateDrawable(Beatmap.Value);
|
||||
|
||||
var localBeatmap = beatmaps.GetWorkingBeatmap(model);
|
||||
|
||||
if (model?.BeatmapSet?.OnlineInfo != null)
|
||||
drawable = new BeatmapSetCover(model.BeatmapSet, beatmapSetCoverType);
|
||||
else if (localBeatmap.BeatmapInfo.ID != 0)
|
||||
{
|
||||
// Fall back to local background if one exists
|
||||
drawable = new BeatmapBackgroundSprite(localBeatmap);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use the default background if somehow an online set does not exist and we don't have a local copy.
|
||||
drawable = new BeatmapBackgroundSprite(beatmaps.DefaultBeatmap);
|
||||
}
|
||||
|
||||
drawable.RelativeSizeAxes = Axes.Both;
|
||||
drawable.Anchor = Anchor.Centre;
|
||||
drawable.Origin = Anchor.Centre;
|
||||
drawable.FillMode = FillMode.Fill;
|
||||
drawable.OnLoadComplete = d => d.FadeInFromZero(400);
|
||||
|
||||
return drawable;
|
||||
}, 500, 10000);
|
||||
// If the model has changed since the previous unload (or if there was no load), then we can safely use the given content
|
||||
lastModel = Beatmap.Value;
|
||||
return content;
|
||||
}, timeBeforeLoad, 10000);
|
||||
}
|
||||
|
||||
protected override double FadeDuration => 0;
|
||||
protected override Drawable CreateDrawable(BeatmapInfo model)
|
||||
{
|
||||
Drawable drawable;
|
||||
|
||||
var localBeatmap = beatmaps.GetWorkingBeatmap(model);
|
||||
|
||||
if (model?.BeatmapSet?.OnlineInfo != null)
|
||||
{
|
||||
drawable = new BeatmapSetCover(model.BeatmapSet, beatmapSetCoverType);
|
||||
}
|
||||
else if (localBeatmap.BeatmapInfo.ID != 0)
|
||||
{
|
||||
// Fall back to local background if one exists
|
||||
drawable = new BeatmapBackgroundSprite(localBeatmap);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use the default background if somehow an online set does not exist and we don't have a local copy.
|
||||
drawable = new BeatmapBackgroundSprite(beatmaps.DefaultBeatmap);
|
||||
}
|
||||
|
||||
drawable.RelativeSizeAxes = Axes.Both;
|
||||
drawable.Anchor = Anchor.Centre;
|
||||
drawable.Origin = Anchor.Centre;
|
||||
drawable.FillMode = FillMode.Fill;
|
||||
drawable.OnLoadComplete = d => d.FadeInFromZero(400);
|
||||
|
||||
return drawable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user