osu/osu.Game/Online/OnlineViewContainer.cs

116 lines
3.8 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.
using osu.Framework.Allocation;
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>
/// A <see cref="Container"/> for dislaying online content who require a local user to be logged in.
/// Shows its children only when the local user is logged in and supports displaying a placeholder if not.
/// </summary>
public class OnlineViewContainer : Container, IOnlineComponent
{
private readonly Container placeholderContainer;
private readonly Placeholder placeholder;
private readonly LoadingAnimation loading;
2020-01-08 16:41:44 +00:00
private const int transform_time = 300;
private readonly Container content;
2020-01-08 16:41:44 +00:00
protected override Container<Drawable> Content => content;
2020-01-17 17:53:17 +00:00
[Resolved]
protected IAPIProvider API { get; private set; }
2020-01-13 20:12:19 +00:00
public OnlineViewContainer(string placeholderMessage)
2020-01-08 16:41:44 +00:00
{
InternalChildren = new Drawable[]
{
content = new Container
{
RelativeSizeAxes = Axes.Both,
},
placeholderContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
2020-01-17 17:53:17 +00:00
Child = placeholder = new LoginPlaceholder($@"Please sign in to {placeholderMessage}")
2020-01-08 16:41:44 +00:00
},
loading = new LoadingAnimation
{
Alpha = 0,
}
2020-01-08 16:41:44 +00:00
};
}
2020-01-17 17:53:17 +00:00
public virtual void APIStateChanged(IAPIProvider api, APIState state)
2020-01-08 16:41:44 +00:00
{
switch (state)
{
case APIState.Failing:
2020-01-08 16:41:44 +00:00
case APIState.Connecting:
Schedule(() => UpdatePlaceholderVisibility(PlaceholderStatus.Connecting));
2020-01-08 16:41:44 +00:00
break;
case APIState.Offline:
Schedule(() => UpdatePlaceholderVisibility(PlaceholderStatus.Offline));
break;
case APIState.Online:
Schedule(() => UpdatePlaceholderVisibility(PlaceholderStatus.Online));
2020-01-08 16:41:44 +00:00
break;
}
}
protected void UpdatePlaceholderVisibility(PlaceholderStatus status)
2020-01-08 16:41:44 +00:00
{
switch (status)
2020-01-08 16:41:44 +00:00
{
case PlaceholderStatus.Offline:
Content.FadeOut(150, Easing.OutQuint);
placeholder.ScaleTo(0.8f).Then().ScaleTo(1, 3 * transform_time, Easing.OutQuint);
placeholderContainer.FadeInFromZero(2 * transform_time, Easing.OutQuint);
loading.Hide();
break;
case PlaceholderStatus.Online:
placeholderContainer.FadeOut(150, Easing.OutQuint);
Content.FadeIn(transform_time, Easing.OutQuint);
loading.Hide();
break;
case PlaceholderStatus.Connecting:
loading.Show();
placeholderContainer.FadeOut(150, Easing.OutQuint);
Content.FadeOut(150, Easing.OutQuint);
break;
2020-01-08 16:41:44 +00:00
}
}
2020-01-17 17:53:17 +00:00
protected override void LoadComplete()
{
API?.Register(this);
base.LoadComplete();
}
protected override void Dispose(bool isDisposing)
2020-01-08 16:41:44 +00:00
{
2020-01-17 17:53:17 +00:00
API?.Unregister(this);
base.Dispose(isDisposing);
2020-01-08 16:41:44 +00:00
}
protected enum PlaceholderStatus
{
Offline,
Online,
Connecting,
}
2020-01-08 16:41:44 +00:00
}
}