1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 09:42:54 +08:00
osu-lazer/osu.Game/Beatmaps/Drawables/BeatmapDownloadButton.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

125 lines
4.1 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2020-04-12 21:00:05 +08:00
using System;
using osu.Framework.Allocation;
2020-04-13 02:57:35 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-04-11 19:08:16 +08:00
using osu.Game.Configuration;
using osu.Game.Extensions;
using osu.Game.Graphics.Containers;
2018-07-13 15:28:18 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Online;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Resources.Localisation.Web;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Beatmaps.Drawables
{
public partial class BeatmapDownloadButton : CompositeDrawable
{
2019-06-27 12:38:21 +08:00
protected bool DownloadEnabled => button.Enabled.Value;
/// <summary>
/// Currently selected beatmap. Used to present the correct difficulty after completing a download.
/// </summary>
public readonly IBindable<APIBeatmap> SelectedBeatmap = new Bindable<APIBeatmap>();
2020-04-12 21:00:05 +08:00
private readonly ShakeContainer shakeContainer;
private readonly DownloadButton button;
private Bindable<bool> noVideoSetting;
protected readonly BeatmapDownloadTracker DownloadTracker;
protected readonly Bindable<DownloadState> State = new Bindable<DownloadState>();
private readonly IBeatmapSetInfo beatmapSet;
public BeatmapDownloadButton(IBeatmapSetInfo beatmapSet)
{
this.beatmapSet = beatmapSet;
2021-10-27 20:26:26 +08:00
InternalChildren = new Drawable[]
{
shakeContainer = new ShakeContainer
{
RelativeSizeAxes = Axes.Both,
Child = button = new DownloadButton
{
RelativeSizeAxes = Axes.Both,
2021-10-27 20:26:26 +08:00
State = { BindTarget = State }
},
},
DownloadTracker = new BeatmapDownloadTracker(beatmapSet)
{
State = { BindTarget = State }
}
};
button.Add(new DownloadProgressBar(beatmapSet)
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Depth = -1,
});
}
[BackgroundDependencyLoader(true)]
private void load(OsuGame game, BeatmapModelDownloader beatmaps, OsuConfigManager osuConfig)
{
noVideoSetting = osuConfig.GetBindable<bool>(OsuSetting.PreferNoVideo);
2020-04-13 02:57:35 +08:00
button.Action = () =>
2018-07-17 20:36:33 +08:00
{
switch (DownloadTracker.State.Value)
2018-07-17 20:36:33 +08:00
{
case DownloadState.Downloading:
case DownloadState.Importing:
shakeContainer.Shake();
2018-07-17 20:36:33 +08:00
break;
2019-04-01 11:44:46 +08:00
case DownloadState.LocallyAvailable:
2020-04-12 21:00:05 +08:00
Predicate<BeatmapInfo> findPredicate = null;
if (SelectedBeatmap.Value != null)
findPredicate = b => b.MatchesOnlineID(SelectedBeatmap.Value);
2020-04-12 21:00:05 +08:00
game?.PresentBeatmap(beatmapSet, findPredicate);
2018-07-17 20:36:33 +08:00
break;
2019-04-01 11:44:46 +08:00
2018-07-17 20:36:33 +08:00
default:
beatmaps.Download(beatmapSet, noVideoSetting.Value);
2018-07-17 20:36:33 +08:00
break;
}
};
2021-10-27 20:26:26 +08:00
State.BindValueChanged(state =>
{
switch (state.NewValue)
{
case DownloadState.LocallyAvailable:
button.Enabled.Value = true;
button.TooltipText = "Go to beatmap";
break;
default:
if ((beatmapSet as IBeatmapSetOnlineInfo)?.Availability.DownloadDisabled == true)
{
button.Enabled.Value = false;
button.TooltipText = BeatmapsetsStrings.AvailabilityDisabled;
}
break;
}
}, true);
}
2021-10-27 20:26:26 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
FinishTransforms(true);
}
}
2018-01-05 19:21:19 +08:00
}