osu/osu.Game/Beatmaps/Drawables/UpdateableBeatmapBackground...

63 lines
2.3 KiB
C#
Raw Normal View History

// 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 08:59:28 +00:00
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-12-25 08:59:28 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
namespace osu.Game.Beatmaps.Drawables
{
/// <summary>
2019-03-05 09:59:25 +00:00
/// Display a baetmap background from a local source, but fallback to online source if not available.
2018-12-25 08:59:28 +00:00
/// </summary>
public class UpdateableBeatmapBackgroundSprite : ModelBackedDrawable<BeatmapInfo>
{
2019-02-11 10:11:34 +00:00
public readonly Bindable<BeatmapInfo> Beatmap = new Bindable<BeatmapInfo>();
2018-12-25 08:59:28 +00:00
[Resolved]
private BeatmapManager beatmaps { get; set; }
private readonly BeatmapSetCoverType beatmapSetCoverType;
2019-03-05 09:59:25 +00:00
public UpdateableBeatmapBackgroundSprite(BeatmapSetCoverType beatmapSetCoverType = BeatmapSetCoverType.Cover)
2018-12-25 08:59:28 +00:00
{
Beatmap.BindValueChanged(b => Model = b.NewValue);
this.beatmapSetCoverType = beatmapSetCoverType;
2018-12-25 08:59:28 +00:00
}
protected override Drawable CreateDrawable(BeatmapInfo model)
{
2019-02-28 04:31:40 +00:00
return new DelayedLoadUnloadWrapper(() =>
{
2019-03-05 06:51:43 +00:00
Drawable drawable;
var localBeatmap = beatmaps.GetWorkingBeatmap(model);
2019-03-05 06:51:43 +00:00
if (model?.BeatmapSet?.OnlineInfo != null)
drawable = new BeatmapSetCover(model.BeatmapSet, beatmapSetCoverType);
2019-03-05 09:59:25 +00:00
else if (localBeatmap.BeatmapInfo.ID != 0)
{
// Fall back to local background if one exists
2019-03-05 07:59:02 +00:00
drawable = new BeatmapBackgroundSprite(localBeatmap);
}
2019-03-05 06:51:43 +00:00
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);
2019-03-05 06:51:43 +00:00
}
2018-12-25 08:59:28 +00:00
drawable.RelativeSizeAxes = Axes.Both;
drawable.Anchor = Anchor.Centre;
drawable.Origin = Anchor.Centre;
drawable.FillMode = FillMode.Fill;
drawable.OnLoadComplete = d => d.FadeInFromZero(400);
2018-12-25 08:59:28 +00:00
return drawable;
}, 500, 10000);
2018-12-25 08:59:28 +00:00
}
protected override double FadeDuration => 0;
2018-12-25 08:59:28 +00:00
}
}