1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 14:32:55 +08:00
osu-lazer/osu.Game/Online/OnlineViewContainer.cs

101 lines
3.4 KiB
C#
Raw Normal View History

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-01-09 00:41:44 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
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>
2020-01-25 04:10:31 +08:00
public abstract class OnlineViewContainer : Container, IOnlineComponent
2020-01-09 00:41:44 +08:00
{
protected LoadingSpinner LoadingSpinner { get; private set; }
2020-01-09 00:41:44 +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
private Placeholder placeholder;
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; }
protected OnlineViewContainer(string placeholderMessage)
{
this.placeholderMessage = placeholderMessage;
}
[BackgroundDependencyLoader]
private void load()
2020-01-09 00:41:44 +08:00
{
InternalChildren = new Drawable[]
{
Content,
placeholder = new LoginPlaceholder(placeholderMessage),
LoadingSpinner = new LoadingSpinner
{
Alpha = 0,
}
2020-01-09 00:41:44 +08:00
};
}
2020-02-14 14:35:39 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
2020-02-14 14:38:47 +08:00
API.Register(this);
2020-02-14 14:35:39 +08:00
}
2020-01-18 01:53:17 +08:00
public virtual void APIStateChanged(IAPIProvider api, APIState state)
2020-01-09 00:41:44 +08:00
{
switch (state)
{
case APIState.Offline:
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);
LoadingSpinner.Hide();
break;
2020-01-25 04:10:31 +08:00
case APIState.Online:
PopContentIn(Content);
2020-02-14 14:38:47 +08:00
placeholder.FadeOut(transform_duration / 2, Easing.OutQuint);
LoadingSpinner.Hide();
break;
2020-01-25 04:10:31 +08:00
case APIState.Failing:
case APIState.Connecting:
PopContentOut(Content);
LoadingSpinner.Show();
2020-02-14 14:38:47 +08:00
placeholder.FadeOut(transform_duration / 2, Easing.OutQuint);
break;
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-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-18 01:53:17 +08:00
protected override void Dispose(bool isDisposing)
2020-01-09 00:41:44 +08:00
{
2020-01-18 01:53:17 +08:00
API?.Unregister(this);
base.Dispose(isDisposing);
2020-01-09 00:41:44 +08:00
}
}
}