1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 10:22:56 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/BeatmapSetDownloadButton.cs

102 lines
2.8 KiB
C#

// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Input;
using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers;
using osu.Game.Online.API;
namespace osu.Game.Graphics.UserInterface
{
public abstract class BeatmapSetDownloadButton : OsuClickableContainer
{
private readonly BeatmapSetInfo set;
private readonly bool noVideo;
private readonly BindableBool downloaded = new BindableBool();
private BeatmapManager beatmaps;
private Action action;
public Action Action
{
get => action;
set => action = value;
}
protected BeatmapSetDownloadButton(BeatmapSetInfo set, bool noVideo = false)
{
this.set = set;
this.noVideo = noVideo;
downloaded.ValueChanged += e =>
{
if (e)
Disable();
else
Enable();
};
}
[BackgroundDependencyLoader]
private void load(BeatmapManager beatmaps, APIAccess api)
{
this.beatmaps = beatmaps;
beatmaps.ItemAdded += setAdded;
beatmaps.ItemRemoved += setRemoved;
// initial downloaded value
downloaded.Value = beatmaps.QueryBeatmapSets(s => s.OnlineBeatmapSetID == set.OnlineBeatmapSetID && !s.DeletePending).Count() != 0;
Action = () =>
{
if (beatmaps.GetExistingDownload(set) != null)
{
AlreadyDownloading();
return;
}
beatmaps.Download(set, noVideo);
};
}
protected override bool OnClick(InputState state)
{
if (!downloaded.Value)
Action?.Invoke();
return true;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (beatmaps != null)
{
beatmaps.ItemAdded -= setAdded;
beatmaps.ItemRemoved -= setRemoved;
}
}
protected abstract void Enable();
protected abstract void Disable();
protected abstract void AlreadyDownloading();
private void setAdded(BeatmapSetInfo s)
{
if (s.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
downloaded.Value = true;
}
private void setRemoved(BeatmapSetInfo s)
{
if (s.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
downloaded.Value = false;
}
}
}