1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 01:27:29 +08:00
osu-lazer/osu.Game/Online/OnlineViewContainer.cs

116 lines
3.8 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>
/// 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-09 00:41:44 +08:00
private const int transform_time = 300;
private readonly Container content;
2020-01-09 00:41:44 +08:00
protected override Container<Drawable> Content => content;
2020-01-18 01:53:17 +08:00
[Resolved]
protected IAPIProvider API { get; private set; }
2020-01-14 04:12:19 +08:00
public OnlineViewContainer(string placeholderMessage)
2020-01-09 00:41:44 +08:00
{
InternalChildren = new Drawable[]
{
content = new Container
{
RelativeSizeAxes = Axes.Both,
},
placeholderContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
2020-01-24 02:09:34 +08:00
Child = placeholder = new LoginPlaceholder(placeholderMessage)
2020-01-09 00:41:44 +08:00
},
loading = new LoadingAnimation
{
Alpha = 0,
}
2020-01-09 00:41:44 +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.Failing:
2020-01-09 00:41:44 +08:00
case APIState.Connecting:
Schedule(() => UpdatePlaceholderVisibility(PlaceholderStatus.Connecting));
2020-01-09 00:41:44 +08:00
break;
case APIState.Offline:
Schedule(() => UpdatePlaceholderVisibility(PlaceholderStatus.Offline));
break;
case APIState.Online:
Schedule(() => UpdatePlaceholderVisibility(PlaceholderStatus.Online));
2020-01-09 00:41:44 +08:00
break;
}
}
protected void UpdatePlaceholderVisibility(PlaceholderStatus status)
2020-01-09 00:41:44 +08:00
{
switch (status)
2020-01-09 00:41:44 +08: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-09 00:41:44 +08:00
}
}
2020-01-18 01:53:17 +08:00
protected override void LoadComplete()
{
API?.Register(this);
base.LoadComplete();
}
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
}
protected enum PlaceholderStatus
{
Offline,
Online,
Connecting,
}
2020-01-09 00:41:44 +08:00
}
}