BeatmapSetDownloadButton -> BeatmapSetDownloader

Allows it to integrate better with existing buttons, like HeaderButton.
This commit is contained in:
DrabWeb 2018-06-04 20:12:43 -03:00
parent 4bdc1fb781
commit 698a42f145
2 changed files with 26 additions and 27 deletions

View File

@ -1,25 +1,27 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>. // Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Linq; using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Input; using osu.Framework.Graphics;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
{ {
public abstract class BeatmapSetDownloadButton : OsuClickableContainer public class BeatmapSetDownloader : Drawable
{ {
private readonly BeatmapSetInfo set; private readonly BeatmapSetInfo set;
private readonly bool noVideo; private readonly bool noVideo;
private BeatmapManager beatmaps; private BeatmapManager beatmaps;
protected readonly BindableBool Downloaded = new BindableBool(); public readonly BindableBool Downloaded = new BindableBool();
protected BeatmapSetDownloadButton(BeatmapSetInfo set, bool noVideo = false) public event Action OnAlreadyDownloading;
public BeatmapSetDownloader(BeatmapSetInfo set, bool noVideo = false)
{ {
this.set = set; this.set = set;
this.noVideo = noVideo; this.noVideo = noVideo;
@ -35,24 +37,6 @@ private void load(BeatmapManager beatmaps)
// initial value // initial value
Downloaded.Value = beatmaps.QueryBeatmapSets(s => s.OnlineBeatmapSetID == set.OnlineBeatmapSetID && !s.DeletePending).Count() != 0; 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 (Enabled.Value && !Downloaded.Value)
Action?.Invoke();
return true;
} }
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
@ -66,8 +50,18 @@ protected override void Dispose(bool isDisposing)
} }
} }
protected virtual void AlreadyDownloading() public void Download()
{ {
if (Downloaded.Value)
return;
if (beatmaps.GetExistingDownload(set) != null)
{
OnAlreadyDownloading?.Invoke();
return;
}
beatmaps.Download(set, noVideo);
} }
private void setAdded(BeatmapSetInfo s) private void setAdded(BeatmapSetInfo s)

View File

@ -5,19 +5,22 @@
using osu.Framework.Input; using osu.Framework.Input;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using OpenTK; using OpenTK;
namespace osu.Game.Overlays.Direct namespace osu.Game.Overlays.Direct
{ {
public class DownloadButton : BeatmapSetDownloadButton public class DownloadButton : OsuClickableContainer
{ {
private readonly SpriteIcon icon; private readonly SpriteIcon icon;
public DownloadButton(BeatmapSetInfo set, bool noVideo = false) : base(set, noVideo) public DownloadButton(BeatmapSetInfo set, bool noVideo = false)
{ {
BeatmapSetDownloader downloader;
Children = new Drawable[] Children = new Drawable[]
{ {
downloader = new BeatmapSetDownloader(set, noVideo),
icon = new SpriteIcon icon = new SpriteIcon
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
@ -27,7 +30,9 @@ public DownloadButton(BeatmapSetInfo set, bool noVideo = false) : base(set, noVi
}, },
}; };
Downloaded.ValueChanged += e => Action = downloader.Download;
downloader.Downloaded.ValueChanged += e =>
{ {
if (e) if (e)
this.FadeOut(200); this.FadeOut(200);