1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 06:23:20 +08:00

Add BeatmapSetDownloadButton.

This commit is contained in:
DrabWeb 2018-06-04 06:08:39 -03:00
parent 92377b5810
commit 7b7236929a

View File

@ -0,0 +1,83 @@
// 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 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)
{
beatmaps.ItemAdded += s =>
{
if (s.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
downloaded.Value = true;
};
beatmaps.ItemRemoved += s =>
{
if (s.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
downloaded.Value = false;
};
// 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 abstract void Enable();
protected abstract void Disable();
protected abstract void AlreadyDownloading();
}
}