osu/osu.Game/Online/OnlineViewContainer.cs

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

100 lines
3.6 KiB
C#
Raw Normal View History

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