osu/osu.Game/Overlays/BeatmapSet/Buttons/HeaderDownloadButton.cs

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

183 lines
6.5 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 09:19:50 +00:00
2022-06-17 07:37:17 +00:00
#nullable disable
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2017-09-13 02:41:10 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-07-31 05:50:57 +00:00
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
2017-09-13 02:41:10 +00:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2017-09-13 02:41:10 +00:00
using osu.Game.Graphics.Sprites;
using osu.Game.Online;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
2021-08-07 16:27:55 +00:00
using osu.Game.Resources.Localisation.Web;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
using APIUser = osu.Game.Online.API.Requests.Responses.APIUser;
using CommonStrings = osu.Game.Localisation.CommonStrings;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Overlays.BeatmapSet.Buttons
2017-09-13 02:41:10 +00:00
{
public class HeaderDownloadButton : CompositeDrawable, IHasTooltip
2017-09-13 02:41:10 +00:00
{
private const int text_size = 12;
private readonly bool noVideo;
public LocalisableString TooltipText => BeatmapsetsStrings.ShowDetailsDownloadDefault;
2018-07-31 05:50:57 +00:00
private readonly IBindable<APIUser> localUser = new Bindable<APIUser>();
private ShakeContainer shakeContainer;
private HeaderButton button;
private BeatmapDownloadTracker downloadTracker;
private readonly APIBeatmapSet beatmapSet;
public HeaderDownloadButton(APIBeatmapSet beatmapSet, bool noVideo = false)
2017-09-13 02:41:10 +00:00
{
this.beatmapSet = beatmapSet;
this.noVideo = noVideo;
2017-09-13 02:41:10 +00:00
Width = 120;
RelativeSizeAxes = Axes.Y;
}
2018-04-13 09:19:50 +00:00
[BackgroundDependencyLoader]
private void load(IAPIProvider api, BeatmapModelDownloader beatmaps)
{
FillFlowContainer textSprites;
InternalChildren = new Drawable[]
2017-09-13 02:41:10 +00:00
{
shakeContainer = new ShakeContainer
{
RelativeSizeAxes = Axes.Both,
Masking = true,
CornerRadius = 5,
Child = button = new HeaderButton { RelativeSizeAxes = Axes.Both },
},
downloadTracker = new BeatmapDownloadTracker(beatmapSet),
};
button.AddRange(new Drawable[]
{
new Container
2017-09-13 02:41:10 +00:00
{
Padding = new MarginPadding { Horizontal = 10 },
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2017-09-13 02:41:10 +00:00
{
textSprites = new FillFlowContainer
2017-09-13 02:41:10 +00:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both,
AutoSizeDuration = 500,
AutoSizeEasing = Easing.OutQuint,
Direction = FillDirection.Vertical,
2017-09-13 02:41:10 +00:00
},
new SpriteIcon
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Icon = FontAwesome.Solid.Download,
Size = new Vector2(18),
},
}
},
new DownloadProgressBar(beatmapSet)
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
},
});
button.Action = () =>
{
if (downloadTracker.State.Value != DownloadState.NotDownloaded)
{
shakeContainer.Shake();
return;
}
beatmaps.Download(beatmapSet, noVideo);
};
localUser.BindTo(api.LocalUser);
localUser.BindValueChanged(userChanged, true);
button.Enabled.BindValueChanged(enabledChanged, true);
downloadTracker.State.BindValueChanged(state =>
{
switch (state.NewValue)
{
case DownloadState.Downloading:
textSprites.Children = new Drawable[]
{
new OsuSpriteText
{
Text = CommonStrings.Downloading,
Font = OsuFont.GetFont(size: text_size, weight: FontWeight.Bold)
},
};
break;
2019-04-01 03:44:46 +00:00
case DownloadState.Importing:
2019-01-31 10:09:04 +00:00
textSprites.Children = new Drawable[]
{
new OsuSpriteText
{
Text = CommonStrings.Importing,
Font = OsuFont.GetFont(size: text_size, weight: FontWeight.Bold)
2019-01-31 10:09:04 +00:00
},
};
break;
2019-04-01 03:44:46 +00:00
2019-01-31 10:09:04 +00:00
case DownloadState.LocallyAvailable:
this.FadeOut(200);
break;
2019-04-01 03:44:46 +00:00
case DownloadState.NotDownloaded:
textSprites.Children = new Drawable[]
{
new OsuSpriteText
{
2021-08-19 17:02:04 +00:00
Text = BeatmapsetsStrings.ShowDetailsDownloadDefault,
Font = OsuFont.GetFont(size: text_size, weight: FontWeight.Bold)
},
new OsuSpriteText
{
Text = getVideoSuffixText(),
Font = OsuFont.GetFont(size: text_size - 2, weight: FontWeight.Bold)
},
};
this.FadeIn(200);
break;
}
}, true);
}
private void userChanged(ValueChangedEvent<APIUser> e) => button.Enabled.Value = !(e.NewValue is GuestUser);
2019-02-21 09:56:34 +00:00
private void enabledChanged(ValueChangedEvent<bool> e) => this.FadeColour(e.NewValue ? Color4.White : Color4.Gray, 200, Easing.OutQuint);
2021-08-07 16:27:55 +00:00
private LocalisableString getVideoSuffixText()
{
if (!beatmapSet.HasVideo)
return string.Empty;
2021-08-07 16:27:55 +00:00
return noVideo ? BeatmapsetsStrings.ShowDetailsDownloadNoVideo : BeatmapsetsStrings.ShowDetailsDownloadVideo;
}
2017-09-13 02:41:10 +00:00
}
}