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

89 lines
2.9 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
{
private readonly Placeholder placeholder;
2020-01-25 04:10:31 +08:00
protected readonly LoadingAnimation LoadingAnimation;
2020-01-09 00:41:44 +08:00
protected const double TRANSFORM_TIME = 300.0;
2020-01-09 00:41:44 +08:00
2020-02-14 03:08:14 +08:00
private Container viewContent;
protected override Container<Drawable> Content => viewContent;
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)
2020-01-09 00:41:44 +08:00
{
InternalChildren = new Drawable[]
{
2020-02-14 03:08:14 +08:00
viewContent = new Container
2020-01-09 00:41:44 +08:00
{
RelativeSizeAxes = Axes.Both,
},
placeholder = new LoginPlaceholder(placeholderMessage),
2020-01-25 04:10:31 +08:00
LoadingAnimation = 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.Offline:
2020-02-14 03:08:14 +08:00
PopContentOut(viewContent);
placeholder.ScaleTo(0.8f).Then().ScaleTo(1, 3 * TRANSFORM_TIME, Easing.OutQuint);
placeholder.FadeInFromZero(2 * TRANSFORM_TIME, Easing.OutQuint);
2020-01-25 04:10:31 +08:00
LoadingAnimation.Hide();
break;
2020-01-25 04:10:31 +08:00
case APIState.Online:
2020-02-14 03:08:14 +08:00
PopContentIn(viewContent);
placeholder.FadeOut(TRANSFORM_TIME / 2, Easing.OutQuint);
2020-01-25 04:10:31 +08:00
LoadingAnimation.Hide();
break;
2020-01-25 04:10:31 +08:00
case APIState.Failing:
case APIState.Connecting:
2020-02-14 03:08:14 +08:00
PopContentOut(viewContent);
2020-01-25 04:10:31 +08:00
LoadingAnimation.Show();
placeholder.FadeOut(TRANSFORM_TIME / 2, Easing.OutQuint);
break;
2020-01-09 00:41:44 +08:00
}
}
2020-02-12 01:10:46 +08:00
protected abstract void PopContentOut(Drawable content);
2020-02-12 01:10:46 +08:00
protected abstract void PopContentIn(Drawable content);
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
}
}
}