// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Graphics.UserInterface; using osu.Game.Online.API; using osu.Game.Online.Placeholders; namespace osu.Game.Online { /// /// A for displaying online content which 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. /// public abstract class OnlineViewContainer : Container, IOnlineComponent { protected LoadingSpinner LoadingSpinner { get; private set; } protected override Container Content { get; } = new Container { RelativeSizeAxes = Axes.Both }; private readonly string placeholderMessage; private Placeholder placeholder; private const double transform_duration = 300; [Resolved] protected IAPIProvider API { get; private set; } protected OnlineViewContainer(string placeholderMessage) { this.placeholderMessage = placeholderMessage; } [BackgroundDependencyLoader] private void load() { InternalChildren = new Drawable[] { Content, placeholder = new LoginPlaceholder(placeholderMessage), LoadingSpinner = new LoadingSpinner { Alpha = 0, } }; } protected override void LoadComplete() { base.LoadComplete(); API.Register(this); } public virtual void APIStateChanged(IAPIProvider api, APIState state) { switch (state) { case APIState.Offline: PopContentOut(Content); placeholder.ScaleTo(0.8f).Then().ScaleTo(1, 3 * transform_duration, Easing.OutQuint); placeholder.FadeInFromZero(2 * transform_duration, Easing.OutQuint); LoadingSpinner.Hide(); break; case APIState.Online: PopContentIn(Content); placeholder.FadeOut(transform_duration / 2, Easing.OutQuint); LoadingSpinner.Hide(); break; case APIState.Failing: case APIState.Connecting: PopContentOut(Content); LoadingSpinner.Show(); placeholder.FadeOut(transform_duration / 2, Easing.OutQuint); break; } } /// /// Applies a transform to the online content to make it hidden. /// protected virtual void PopContentOut(Drawable content) => content.FadeOut(transform_duration / 2, Easing.OutQuint); /// /// Applies a transform to the online content to make it visible. /// protected virtual void PopContentIn(Drawable content) => content.FadeIn(transform_duration, Easing.OutQuint); protected override void Dispose(bool isDisposing) { API?.Unregister(this); base.Dispose(isDisposing); } } }