Merge pull request #1506 from naoey/beatmap-overlay-download

Make beatmap overlay download buttons work
This commit is contained in:
Dean Herbert
2017-11-23 17:18:33 +09:00
committed by GitHub
4 changed files with 86 additions and 34 deletions

View File

@ -18,6 +18,7 @@ using osu.Framework.Platform;
using osu.Game.Beatmaps.Formats; using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.IO; using osu.Game.Beatmaps.IO;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.IO; using osu.Game.IO;
using osu.Game.IPC; using osu.Game.IPC;
using osu.Game.Online.API; using osu.Game.Online.API;
@ -52,6 +53,11 @@ namespace osu.Game.Beatmaps
/// </summary> /// </summary>
public event Action<BeatmapInfo> BeatmapRestored; public event Action<BeatmapInfo> BeatmapRestored;
/// <summary>
/// Fired when a beatmap download begins.
/// </summary>
public event Action<DownloadBeatmapSetRequest> BeatmapDownloadBegan;
/// <summary> /// <summary>
/// A default representation of a WorkingBeatmap to use when no beatmap is available. /// A default representation of a WorkingBeatmap to use when no beatmap is available.
/// </summary> /// </summary>
@ -221,21 +227,29 @@ namespace osu.Game.Beatmaps
/// Downloads a beatmap. /// Downloads a beatmap.
/// </summary> /// </summary>
/// <param name="beatmapSetInfo">The <see cref="BeatmapSetInfo"/> to be downloaded.</param> /// <param name="beatmapSetInfo">The <see cref="BeatmapSetInfo"/> to be downloaded.</param>
/// <returns>A new <see cref="DownloadBeatmapSetRequest"/>, or an existing one if a download is already in progress.</returns> /// <param name="noVideo">Whether the beatmap should be downloaded without video. Defaults to false.</param>
public DownloadBeatmapSetRequest Download(BeatmapSetInfo beatmapSetInfo) public void Download(BeatmapSetInfo beatmapSetInfo, bool noVideo = false)
{ {
var existing = GetExistingDownload(beatmapSetInfo); var existing = GetExistingDownload(beatmapSetInfo);
if (existing != null) return existing; if (existing != null || api == null) return;
if (api == null) return null; if (!api.LocalUser.Value.IsSupporter)
{
PostNotification?.Invoke(new SimpleNotification
{
Icon = FontAwesome.fa_superpowers,
Text = "You gotta be a supporter to download for now 'yo"
});
return;
}
ProgressNotification downloadNotification = new ProgressNotification ProgressNotification downloadNotification = new ProgressNotification
{ {
Text = $"Downloading {beatmapSetInfo.Metadata.Artist} - {beatmapSetInfo.Metadata.Title}", Text = $"Downloading {beatmapSetInfo.Metadata.Artist} - {beatmapSetInfo.Metadata.Title}",
}; };
var request = new DownloadBeatmapSetRequest(beatmapSetInfo); var request = new DownloadBeatmapSetRequest(beatmapSetInfo, noVideo);
request.DownloadProgressed += progress => request.DownloadProgressed += progress =>
{ {
@ -280,8 +294,7 @@ namespace osu.Game.Beatmaps
// don't run in the main api queue as this is a long-running task. // don't run in the main api queue as this is a long-running task.
Task.Factory.StartNew(() => request.Perform(api), TaskCreationOptions.LongRunning); Task.Factory.StartNew(() => request.Perform(api), TaskCreationOptions.LongRunning);
BeatmapDownloadBegan?.Invoke(request);
return request;
} }
/// <summary> /// <summary>

View File

@ -12,13 +12,16 @@ namespace osu.Game.Online.API.Requests
public Action<float> DownloadProgressed; public Action<float> DownloadProgressed;
public DownloadBeatmapSetRequest(BeatmapSetInfo set) private readonly bool noVideo;
public DownloadBeatmapSetRequest(BeatmapSetInfo set, bool noVideo)
{ {
this.noVideo = noVideo;
BeatmapSet = set; BeatmapSet = set;
Progress += (current, total) => DownloadProgressed?.Invoke((float) current / total); Progress += (current, total) => DownloadProgressed?.Invoke((float) current / total);
} }
protected override string Target => $@"beatmapsets/{BeatmapSet.OnlineBeatmapSetID}/download"; protected override string Target => $@"beatmapsets/{BeatmapSet.OnlineBeatmapSetID}/download{(noVideo ? "?noVideo=1" : "")}";
} }
} }

View File

@ -29,8 +29,11 @@ namespace osu.Game.Overlays.BeatmapSet
private readonly Container noVideoButtons; private readonly Container noVideoButtons;
private readonly FillFlowContainer videoButtons; private readonly FillFlowContainer videoButtons;
private readonly AuthorInfo author; private readonly AuthorInfo author;
private readonly Container downloadButtonsContainer;
public Details Details; public Details Details;
private BeatmapManager beatmaps;
private DelayedLoadWrapper cover; private DelayedLoadWrapper cover;
public readonly BeatmapPicker Picker; public readonly BeatmapPicker Picker;
@ -48,6 +51,7 @@ namespace osu.Game.Overlays.BeatmapSet
title.Text = BeatmapSet.Metadata.Title; title.Text = BeatmapSet.Metadata.Title;
artist.Text = BeatmapSet.Metadata.Artist; artist.Text = BeatmapSet.Metadata.Artist;
downloadButtonsContainer.FadeIn();
noVideoButtons.FadeTo(BeatmapSet.OnlineInfo.HasVideo ? 0 : 1, transition_duration); noVideoButtons.FadeTo(BeatmapSet.OnlineInfo.HasVideo ? 0 : 1, transition_duration);
videoButtons.FadeTo(BeatmapSet.OnlineInfo.HasVideo ? 1 : 0, transition_duration); videoButtons.FadeTo(BeatmapSet.OnlineInfo.HasVideo ? 1 : 0, transition_duration);
@ -164,7 +168,7 @@ namespace osu.Game.Overlays.BeatmapSet
Children = new Drawable[] Children = new Drawable[]
{ {
new FavouriteButton(), new FavouriteButton(),
new Container downloadButtonsContainer = new Container
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Left = buttons_height + buttons_spacing }, Padding = new MarginPadding { Left = buttons_height + buttons_spacing },
@ -174,7 +178,10 @@ namespace osu.Game.Overlays.BeatmapSet
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Alpha = 0f, Alpha = 0f,
Child = new DownloadButton("Download", @""), Child = new DownloadButton("Download", @"")
{
Action = () => download(false),
},
}, },
videoButtons = new FillFlowContainer videoButtons = new FillFlowContainer
{ {
@ -183,8 +190,14 @@ namespace osu.Game.Overlays.BeatmapSet
Alpha = 0f, Alpha = 0f,
Children = new[] Children = new[]
{ {
new DownloadButton("Download", "with Video"), new DownloadButton("Download", "with Video")
new DownloadButton("Download", "without Video"), {
Action = () => download(false),
},
new DownloadButton("Download", "without Video")
{
Action = () => download(true),
},
}, },
}, },
}, },
@ -208,9 +221,39 @@ namespace osu.Game.Overlays.BeatmapSet
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours) private void load(OsuColour colours, BeatmapManager beatmaps)
{ {
tabsBg.Colour = colours.Gray3; tabsBg.Colour = colours.Gray3;
this.beatmaps = beatmaps;
beatmaps.BeatmapSetAdded += handleBeatmapAdd;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
beatmaps.BeatmapSetAdded -= handleBeatmapAdd;
}
private void handleBeatmapAdd(BeatmapSetInfo beatmap)
{
if (beatmap.OnlineBeatmapSetID == BeatmapSet.OnlineBeatmapSetID)
downloadButtonsContainer.FadeOut(transition_duration);
}
private void download(bool noVideo)
{
if (beatmaps.GetExistingDownload(BeatmapSet) != null)
{
downloadButtonsContainer.MoveToX(-5, 50, Easing.OutSine).Then()
.MoveToX(5, 100, Easing.InOutSine).Then()
.MoveToX(-5, 100, Easing.InOutSine).Then()
.MoveToX(0, 50, Easing.InSine).Then();
return;
}
beatmaps.Download(BeatmapSet, noVideo);
} }
} }
} }

View File

@ -16,9 +16,7 @@ using osu.Game.Graphics.Sprites;
using OpenTK.Graphics; using OpenTK.Graphics;
using osu.Framework.Input; using osu.Framework.Input;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Game.Overlays.Notifications;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Audio.Track; using osu.Framework.Audio.Track;
@ -35,10 +33,8 @@ namespace osu.Game.Overlays.Direct
private Container content; private Container content;
private APIAccess api;
private ProgressBar progressBar; private ProgressBar progressBar;
private BeatmapManager beatmaps; private BeatmapManager beatmaps;
private NotificationOverlay notifications;
private BeatmapSetOverlay beatmapSetOverlay; private BeatmapSetOverlay beatmapSetOverlay;
public Track Preview => PlayButton.Preview; public Track Preview => PlayButton.Preview;
@ -71,11 +67,9 @@ namespace osu.Game.Overlays.Direct
[BackgroundDependencyLoader(permitNulls: true)] [BackgroundDependencyLoader(permitNulls: true)]
private void load(APIAccess api, BeatmapManager beatmaps, OsuColour colours, NotificationOverlay notifications, BeatmapSetOverlay beatmapSetOverlay) private void load(BeatmapManager beatmaps, OsuColour colours, BeatmapSetOverlay beatmapSetOverlay)
{ {
this.api = api;
this.beatmaps = beatmaps; this.beatmaps = beatmaps;
this.notifications = notifications;
this.beatmapSetOverlay = beatmapSetOverlay; this.beatmapSetOverlay = beatmapSetOverlay;
AddInternal(content = new Container AddInternal(content = new Container
@ -109,6 +103,14 @@ namespace osu.Game.Overlays.Direct
if (downloadRequest != null) if (downloadRequest != null)
attachDownload(downloadRequest); attachDownload(downloadRequest);
beatmaps.BeatmapDownloadBegan += attachDownload;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
beatmaps.BeatmapDownloadBegan -= attachDownload;
} }
protected override void Update() protected override void Update()
@ -151,16 +153,6 @@ namespace osu.Game.Overlays.Direct
protected void StartDownload() protected void StartDownload()
{ {
if (!api.LocalUser.Value.IsSupporter)
{
notifications.Post(new SimpleNotification
{
Icon = FontAwesome.fa_superpowers,
Text = "You gotta be a supporter to download for now 'yo"
});
return;
}
if (beatmaps.GetExistingDownload(SetInfo) != null) if (beatmaps.GetExistingDownload(SetInfo) != null)
{ {
// we already have an active download running. // we already have an active download running.
@ -172,13 +164,14 @@ namespace osu.Game.Overlays.Direct
return; return;
} }
var request = beatmaps.Download(SetInfo); beatmaps.Download(SetInfo);
attachDownload(request);
} }
private void attachDownload(DownloadBeatmapSetRequest request) private void attachDownload(DownloadBeatmapSetRequest request)
{ {
if (request.BeatmapSet.OnlineBeatmapSetID != SetInfo.OnlineBeatmapSetID)
return;
progressBar.FadeIn(400, Easing.OutQuint); progressBar.FadeIn(400, Easing.OutQuint);
progressBar.ResizeHeightTo(4, 400, Easing.OutQuint); progressBar.ResizeHeightTo(4, 400, Easing.OutQuint);