2020-01-14 04:12:19 +08:00
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
using osu.Framework.Allocation;
|
2020-10-22 13:19:12 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2020-01-09 00:41:44 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-01-20 03:24:46 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2020-01-09 00:41:44 +08:00
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
using osu.Game.Online.Placeholders;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-02-12 01:10:46 +08:00
|
|
|
|
/// A <see cref="Container"/> for displaying online content which require a local user to be logged in.
|
2020-01-09 00:41:44 +08:00
|
|
|
|
/// Shows its children only when the local user is logged in and supports displaying a placeholder if not.
|
|
|
|
|
/// </summary>
|
2021-02-18 16:45:58 +08:00
|
|
|
|
public class OnlineViewContainer : Container
|
2020-01-09 00:41:44 +08:00
|
|
|
|
{
|
2020-02-21 14:33:31 +08:00
|
|
|
|
protected LoadingSpinner LoadingSpinner { get; private set; }
|
2020-01-09 00:41:44 +08:00
|
|
|
|
|
2020-02-14 14:41:46 +08:00
|
|
|
|
protected override Container<Drawable> Content { get; } = new Container { RelativeSizeAxes = Axes.Both };
|
|
|
|
|
|
|
|
|
|
private readonly string placeholderMessage;
|
2020-01-09 00:41:44 +08:00
|
|
|
|
|
2021-04-06 15:17:20 +08:00
|
|
|
|
private Drawable placeholder;
|
2020-02-14 14:41:46 +08:00
|
|
|
|
|
|
|
|
|
private const double transform_duration = 300;
|
2020-01-09 00:41:44 +08:00
|
|
|
|
|
2020-01-18 01:53:17 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
protected IAPIProvider API { get; private set; }
|
|
|
|
|
|
2021-04-06 15:17:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Construct a new instance of an online view container.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="placeholderMessage">The message to display when not logged in. If empty, no button will display.</param>
|
2021-02-18 16:45:58 +08:00
|
|
|
|
public OnlineViewContainer(string placeholderMessage)
|
2020-02-14 14:41:46 +08:00
|
|
|
|
{
|
|
|
|
|
this.placeholderMessage = placeholderMessage;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 13:19:12 +08:00
|
|
|
|
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
|
|
|
|
|
|
2020-02-14 14:41:46 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2020-10-22 13:19:12 +08:00
|
|
|
|
private void load(IAPIProvider api)
|
2020-01-09 00:41:44 +08:00
|
|
|
|
{
|
2021-04-06 15:17:20 +08:00
|
|
|
|
InternalChildren = new[]
|
2020-01-09 00:41:44 +08:00
|
|
|
|
{
|
2020-02-14 14:41:46 +08:00
|
|
|
|
Content,
|
2021-04-06 15:17:20 +08:00
|
|
|
|
placeholder = string.IsNullOrEmpty(placeholderMessage) ? Empty() : new LoginPlaceholder(placeholderMessage),
|
2020-02-21 14:33:31 +08:00
|
|
|
|
LoadingSpinner = new LoadingSpinner
|
2020-01-20 03:24:46 +08:00
|
|
|
|
{
|
|
|
|
|
Alpha = 0,
|
|
|
|
|
}
|
2020-01-09 00:41:44 +08:00
|
|
|
|
};
|
2020-10-22 13:19:12 +08:00
|
|
|
|
|
|
|
|
|
apiState.BindTo(api.State);
|
|
|
|
|
apiState.BindValueChanged(onlineStateChanged, true);
|
2020-01-09 00:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 13:19:12 +08:00
|
|
|
|
private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() =>
|
2020-01-09 00:41:44 +08:00
|
|
|
|
{
|
2020-10-22 13:19:12 +08:00
|
|
|
|
switch (state.NewValue)
|
2020-01-09 00:41:44 +08:00
|
|
|
|
{
|
2020-01-20 03:24:46 +08:00
|
|
|
|
case APIState.Offline:
|
2020-02-14 14:29:50 +08:00
|
|
|
|
PopContentOut(Content);
|
2020-02-14 14:38:47 +08:00
|
|
|
|
placeholder.ScaleTo(0.8f).Then().ScaleTo(1, 3 * transform_duration, Easing.OutQuint);
|
|
|
|
|
placeholder.FadeInFromZero(2 * transform_duration, Easing.OutQuint);
|
2020-02-21 14:33:31 +08:00
|
|
|
|
LoadingSpinner.Hide();
|
2020-01-20 03:24:46 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2020-01-25 04:10:31 +08:00
|
|
|
|
case APIState.Online:
|
2020-02-14 14:29:50 +08:00
|
|
|
|
PopContentIn(Content);
|
2020-02-14 14:38:47 +08:00
|
|
|
|
placeholder.FadeOut(transform_duration / 2, Easing.OutQuint);
|
2020-02-21 14:33:31 +08:00
|
|
|
|
LoadingSpinner.Hide();
|
2020-01-20 03:24:46 +08:00
|
|
|
|
break;
|
|
|
|
|
|
2020-01-25 04:10:31 +08:00
|
|
|
|
case APIState.Failing:
|
|
|
|
|
case APIState.Connecting:
|
2020-02-14 14:29:50 +08:00
|
|
|
|
PopContentOut(Content);
|
2020-02-21 14:33:31 +08:00
|
|
|
|
LoadingSpinner.Show();
|
2020-02-14 14:38:47 +08:00
|
|
|
|
placeholder.FadeOut(transform_duration / 2, Easing.OutQuint);
|
2020-01-20 03:24:46 +08:00
|
|
|
|
break;
|
2020-01-09 00:41:44 +08:00
|
|
|
|
}
|
2020-10-22 13:19:12 +08:00
|
|
|
|
});
|
2020-01-09 00:41:44 +08:00
|
|
|
|
|
2020-02-14 14:34:46 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Applies a transform to the online content to make it hidden.
|
|
|
|
|
/// </summary>
|
2020-02-14 14:38:47 +08:00
|
|
|
|
protected virtual void PopContentOut(Drawable content) => content.FadeOut(transform_duration / 2, Easing.OutQuint);
|
2020-02-05 22:04:10 +08:00
|
|
|
|
|
2020-02-14 14:34:46 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Applies a transform to the online content to make it visible.
|
|
|
|
|
/// </summary>
|
2020-02-14 14:38:47 +08:00
|
|
|
|
protected virtual void PopContentIn(Drawable content) => content.FadeIn(transform_duration, Easing.OutQuint);
|
2020-01-09 00:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|