2019-01-24 16:43:03 +08: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.
|
2018-12-25 16:59:28 +08:00
|
|
|
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2018-12-25 16:59:28 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Drawables
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-03-06 17:18:47 +08:00
|
|
|
/// Display a beatmap background from a local source, but fallback to online source if not available.
|
2018-12-25 16:59:28 +08:00
|
|
|
/// </summary>
|
|
|
|
public class UpdateableBeatmapBackgroundSprite : ModelBackedDrawable<BeatmapInfo>
|
|
|
|
{
|
2019-02-11 18:11:34 +08:00
|
|
|
public readonly Bindable<BeatmapInfo> Beatmap = new Bindable<BeatmapInfo>();
|
2018-12-25 16:59:28 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private BeatmapManager beatmaps { get; set; }
|
|
|
|
|
2019-03-05 11:08:14 +08:00
|
|
|
private readonly BeatmapSetCoverType beatmapSetCoverType;
|
|
|
|
|
2019-03-05 17:59:25 +08:00
|
|
|
public UpdateableBeatmapBackgroundSprite(BeatmapSetCoverType beatmapSetCoverType = BeatmapSetCoverType.Cover)
|
2018-12-25 16:59:28 +08:00
|
|
|
{
|
2019-02-22 19:13:38 +08:00
|
|
|
Beatmap.BindValueChanged(b => Model = b.NewValue);
|
2019-03-05 11:08:14 +08:00
|
|
|
this.beatmapSetCoverType = beatmapSetCoverType;
|
2018-12-25 16:59:28 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 16:24:05 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Delay before the background is unloaded while off-screen.
|
|
|
|
/// </summary>
|
|
|
|
protected virtual double UnloadDelay => 10000;
|
|
|
|
|
2019-03-06 17:18:47 +08:00
|
|
|
private BeatmapInfo lastModel;
|
2019-05-23 16:01:13 +08:00
|
|
|
private bool firstLoad = true;
|
2019-03-06 17:18:47 +08:00
|
|
|
|
|
|
|
protected override DelayedLoadWrapper CreateDelayedLoadWrapper(Drawable content, double timeBeforeLoad)
|
2018-12-25 16:59:28 +08:00
|
|
|
{
|
2019-02-28 12:31:40 +08:00
|
|
|
return new DelayedLoadUnloadWrapper(() =>
|
|
|
|
{
|
2019-03-06 17:18:47 +08:00
|
|
|
// 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.
|
2019-05-23 16:01:13 +08:00
|
|
|
if (!firstLoad && lastModel == Beatmap.Value)
|
2019-03-06 17:18:47 +08:00
|
|
|
return CreateDrawable(Beatmap.Value);
|
|
|
|
|
|
|
|
// 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;
|
2019-05-23 16:01:13 +08:00
|
|
|
firstLoad = false;
|
2019-03-06 17:18:47 +08:00
|
|
|
return content;
|
2019-05-07 16:24:05 +08:00
|
|
|
}, timeBeforeLoad, UnloadDelay);
|
2018-12-25 16:59:28 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 17:18:47 +08:00
|
|
|
protected override Drawable CreateDrawable(BeatmapInfo model)
|
|
|
|
{
|
2019-03-11 17:45:30 +08:00
|
|
|
Drawable drawable = getDrawableForModel(model);
|
2019-03-06 17:18:47 +08:00
|
|
|
|
|
|
|
drawable.RelativeSizeAxes = Axes.Both;
|
|
|
|
drawable.Anchor = Anchor.Centre;
|
|
|
|
drawable.Origin = Anchor.Centre;
|
|
|
|
drawable.FillMode = FillMode.Fill;
|
2019-03-17 12:43:23 +08:00
|
|
|
drawable.OnLoadComplete += d => d.FadeInFromZero(400);
|
2019-03-06 17:18:47 +08:00
|
|
|
|
|
|
|
return drawable;
|
|
|
|
}
|
2019-03-11 17:45:30 +08:00
|
|
|
|
|
|
|
private Drawable getDrawableForModel(BeatmapInfo model)
|
|
|
|
{
|
|
|
|
// prefer online cover where available.
|
|
|
|
if (model?.BeatmapSet?.OnlineInfo != null)
|
|
|
|
return new BeatmapSetCover(model.BeatmapSet, beatmapSetCoverType);
|
|
|
|
|
|
|
|
return model?.ID > 0
|
|
|
|
? new BeatmapBackgroundSprite(beatmaps.GetWorkingBeatmap(model))
|
|
|
|
: new BeatmapBackgroundSprite(beatmaps.DefaultBeatmap);
|
|
|
|
}
|
2018-12-25 16:59:28 +08:00
|
|
|
}
|
|
|
|
}
|