1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 15:17:28 +08:00
osu-lazer/osu.Game/Overlays/BeatmapSet/Buttons/HeaderDownloadButton.cs

170 lines
6.0 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
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-07-31 13:50:57 +08:00
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Online;
using osu.Game.Online.API;
using osu.Game.Overlays.BeatmapListing.Panels;
using osu.Game.Users;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.BeatmapSet.Buttons
2018-04-13 17:19:50 +08:00
{
public class HeaderDownloadButton : BeatmapDownloadTrackingComposite, IHasTooltip
2018-04-13 17:19:50 +08:00
{
private const int text_size = 12;
private readonly bool noVideo;
public LocalisableString TooltipText => button.Enabled.Value ? "download this beatmap" : "login to download";
2018-07-31 13:50:57 +08:00
private readonly IBindable<User> localUser = new Bindable<User>();
private ShakeContainer shakeContainer;
private HeaderButton button;
public HeaderDownloadButton(BeatmapSetInfo beatmapSet, bool noVideo = false)
: base(beatmapSet)
2018-04-13 17:19:50 +08:00
{
this.noVideo = noVideo;
2018-04-13 17:19:50 +08:00
Width = 120;
RelativeSizeAxes = Axes.Y;
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(IAPIProvider api, BeatmapManager beatmaps)
{
FillFlowContainer textSprites;
AddInternal(shakeContainer = new ShakeContainer
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
Masking = true,
CornerRadius = 5,
Child = button = new HeaderButton { RelativeSizeAxes = Axes.Both },
});
button.AddRange(new Drawable[]
{
new Container
2018-04-13 17:19:50 +08:00
{
Padding = new MarginPadding { Horizontal = 10 },
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
textSprites = new FillFlowContainer
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Both,
AutoSizeDuration = 500,
AutoSizeEasing = Easing.OutQuint,
Direction = FillDirection.Vertical,
2018-04-13 17:19:50 +08:00
},
new SpriteIcon
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Icon = FontAwesome.Solid.Download,
Size = new Vector2(18),
},
}
},
new DownloadProgressBar(BeatmapSet.Value)
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
},
2018-04-13 17:19:50 +08:00
});
button.Action = () =>
{
2019-01-31 18:15:04 +08:00
if (State.Value != DownloadState.NotDownloaded)
{
shakeContainer.Shake();
return;
}
2019-02-21 17:56:34 +08:00
beatmaps.Download(BeatmapSet.Value, noVideo);
};
localUser.BindTo(api.LocalUser);
localUser.BindValueChanged(userChanged, true);
button.Enabled.BindValueChanged(enabledChanged, true);
State.BindValueChanged(state =>
{
switch (state.NewValue)
{
case DownloadState.Downloading:
textSprites.Children = new Drawable[]
{
new OsuSpriteText
{
Text = "Downloading...",
Font = OsuFont.GetFont(size: text_size, weight: FontWeight.Bold)
},
};
break;
2019-04-01 11:44:46 +08:00
case DownloadState.Importing:
2019-01-31 18:09:04 +08:00
textSprites.Children = new Drawable[]
{
new OsuSpriteText
{
Text = "Importing...",
Font = OsuFont.GetFont(size: text_size, weight: FontWeight.Bold)
2019-01-31 18:09:04 +08:00
},
};
break;
2019-04-01 11:44:46 +08:00
2019-01-31 18:09:04 +08:00
case DownloadState.LocallyAvailable:
this.FadeOut(200);
break;
2019-04-01 11:44:46 +08:00
case DownloadState.NotDownloaded:
textSprites.Children = new Drawable[]
{
new OsuSpriteText
{
Text = "Download",
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);
}
2019-02-21 17:56:34 +08:00
private void userChanged(ValueChangedEvent<User> e) => button.Enabled.Value = !(e.NewValue is GuestUser);
2019-02-21 17:56:34 +08:00
private void enabledChanged(ValueChangedEvent<bool> e) => this.FadeColour(e.NewValue ? Color4.White : Color4.Gray, 200, Easing.OutQuint);
private string getVideoSuffixText()
{
if (!BeatmapSet.Value.OnlineInfo.HasVideo)
return string.Empty;
return noVideo ? "without Video" : "with Video";
}
2018-04-13 17:19:50 +08:00
}
}