Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make beatmap overlay download buttons work #1506

Merged
merged 6 commits into from Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions osu.Game/Beatmaps/BeatmapManager.cs
Expand Up @@ -18,6 +18,7 @@
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.IO;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.IO;
using osu.Game.IPC;
using osu.Game.Online.API;
Expand Down Expand Up @@ -52,6 +53,11 @@ public class BeatmapManager
/// </summary>
public event Action<BeatmapInfo> BeatmapRestored;

/// <summary>
/// Fired when a beatmap download begins.
/// </summary>
public event Action<DownloadBeatmapSetRequest> BeatmapDownloadBegan;

/// <summary>
/// A default representation of a WorkingBeatmap to use when no beatmap is available.
/// </summary>
Expand Down Expand Up @@ -221,21 +227,29 @@ public void Import(BeatmapSetInfo beatmapSetInfo)
/// Downloads a beatmap.
/// </summary>
/// <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>
public DownloadBeatmapSetRequest Download(BeatmapSetInfo beatmapSetInfo)
/// <param name="noVideo">Whether the beatmap should be downloaded without video. Defaults to false.</param>
public void Download(BeatmapSetInfo beatmapSetInfo, bool noVideo = false)
{
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
{
Text = $"Downloading {beatmapSetInfo.Metadata.Artist} - {beatmapSetInfo.Metadata.Title}",
};

var request = new DownloadBeatmapSetRequest(beatmapSetInfo);
var request = new DownloadBeatmapSetRequest(beatmapSetInfo, noVideo);

request.DownloadProgressed += progress =>
{
Expand Down Expand Up @@ -280,8 +294,7 @@ public DownloadBeatmapSetRequest Download(BeatmapSetInfo beatmapSetInfo)

// don't run in the main api queue as this is a long-running task.
Task.Factory.StartNew(() => request.Perform(api), TaskCreationOptions.LongRunning);

return request;
BeatmapDownloadBegan?.Invoke(request);
}

/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions osu.Game/Online/API/Requests/DownloadBeatmapSetRequest.cs
Expand Up @@ -12,13 +12,16 @@ public class DownloadBeatmapSetRequest : APIDownloadRequest

public Action<float> DownloadProgressed;

public DownloadBeatmapSetRequest(BeatmapSetInfo set)
private readonly bool noVideo;

public DownloadBeatmapSetRequest(BeatmapSetInfo set, bool noVideo)
{
this.noVideo = noVideo;
BeatmapSet = set;

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" : "")}";
}
}
53 changes: 48 additions & 5 deletions osu.Game/Overlays/BeatmapSet/Header.cs
Expand Up @@ -29,8 +29,11 @@ public class Header : Container
private readonly Container noVideoButtons;
private readonly FillFlowContainer videoButtons;
private readonly AuthorInfo author;
private readonly Container downloadButtonsContainer;
public Details Details;

private BeatmapManager beatmaps;

private DelayedLoadWrapper cover;

public readonly BeatmapPicker Picker;
Expand All @@ -48,6 +51,7 @@ public BeatmapSetInfo BeatmapSet
title.Text = BeatmapSet.Metadata.Title;
artist.Text = BeatmapSet.Metadata.Artist;

downloadButtonsContainer.FadeIn();
noVideoButtons.FadeTo(BeatmapSet.OnlineInfo.HasVideo ? 0 : 1, transition_duration);
videoButtons.FadeTo(BeatmapSet.OnlineInfo.HasVideo ? 1 : 0, transition_duration);

Expand Down Expand Up @@ -164,7 +168,7 @@ public Header()
Children = new Drawable[]
{
new FavouriteButton(),
new Container
downloadButtonsContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Left = buttons_height + buttons_spacing },
Expand All @@ -174,7 +178,10 @@ public Header()
{
RelativeSizeAxes = Axes.Both,
Alpha = 0f,
Child = new DownloadButton("Download", @""),
Child = new DownloadButton("Download", @"")
{
Action = () => download(false),
},
},
videoButtons = new FillFlowContainer
{
Expand All @@ -183,8 +190,14 @@ public Header()
Alpha = 0f,
Children = new[]
{
new DownloadButton("Download", "with Video"),
new DownloadButton("Download", "without Video"),
new DownloadButton("Download", "with Video")
{
Action = () => download(false),
},
new DownloadButton("Download", "without Video")
{
Action = () => download(true),
},
},
},
},
Expand All @@ -208,9 +221,39 @@ public Header()
}

[BackgroundDependencyLoader]
private void load(OsuColour colours)
private void load(OsuColour colours, BeatmapManager beatmaps)
{
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);

This comment was marked as off-topic.

}

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);
}
}
}
33 changes: 13 additions & 20 deletions osu.Game/Overlays/Direct/DirectPanel.cs
Expand Up @@ -16,9 +16,7 @@
using OpenTK.Graphics;
using osu.Framework.Input;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Framework.Logging;
using osu.Game.Overlays.Notifications;
using osu.Game.Online.API.Requests;
using osu.Framework.Configuration;
using osu.Framework.Audio.Track;
Expand All @@ -35,10 +33,8 @@ public abstract class DirectPanel : Container

private Container content;

private APIAccess api;
private ProgressBar progressBar;
private BeatmapManager beatmaps;
private NotificationOverlay notifications;
private BeatmapSetOverlay beatmapSetOverlay;

public Track Preview => PlayButton.Preview;
Expand Down Expand Up @@ -71,11 +67,9 @@ protected DirectPanel(BeatmapSetInfo setInfo)


[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.notifications = notifications;
this.beatmapSetOverlay = beatmapSetOverlay;

AddInternal(content = new Container
Expand Down Expand Up @@ -109,6 +103,14 @@ private void load(APIAccess api, BeatmapManager beatmaps, OsuColour colours, Not

if (downloadRequest != null)
attachDownload(downloadRequest);

beatmaps.BeatmapDownloadBegan += attachDownload;

This comment was marked as off-topic.

}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
beatmaps.BeatmapDownloadBegan -= attachDownload;
}

protected override void Update()
Expand Down Expand Up @@ -151,16 +153,6 @@ protected override bool OnClick(InputState state)

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)
{
// we already have an active download running.
Expand All @@ -172,13 +164,14 @@ protected void StartDownload()
return;
}

var request = beatmaps.Download(SetInfo);

attachDownload(request);
beatmaps.Download(SetInfo);
}

private void attachDownload(DownloadBeatmapSetRequest request)
{
if (request.BeatmapSet.OnlineBeatmapSetID != SetInfo.OnlineBeatmapSetID)
return;

progressBar.FadeIn(400, Easing.OutQuint);
progressBar.ResizeHeightTo(4, 400, Easing.OutQuint);

Expand Down