1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-25 23:02:59 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/ToolbarUserButton.cs

138 lines
4.6 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osu.Game.Localisation;
2018-04-13 17:19:50 +08:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Users.Drawables;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Toolbar
{
2022-11-24 13:32:20 +08:00
public partial class ToolbarUserButton : ToolbarOverlayToggleButton
2018-04-13 17:19:50 +08:00
{
private UpdateableAvatar avatar = null!;
private IBindable<APIUser> localUser = null!;
private LoadingSpinner spinner = null!;
2018-04-13 17:19:50 +08:00
private SpriteIcon failingIcon = null!;
private IBindable<APIState> apiState = null!;
2018-04-13 17:19:50 +08:00
public ToolbarUserButton()
{
ButtonContent.AutoSizeAxes = Axes.X;
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours, IAPIProvider api, LoginOverlay? login)
{
Flow.Add(new Container
2018-04-13 17:19:50 +08:00
{
Masking = true,
CornerRadius = 4,
2018-04-13 17:19:50 +08:00
Size = new Vector2(32),
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Radius = 4,
Colour = Color4.Black.Opacity(0.1f),
},
Children = new Drawable[]
{
avatar = new UpdateableAvatar(isInteractive: false)
{
RelativeSizeAxes = Axes.Both,
},
spinner = new LoadingLayer(dimBackground: true, withBox: false, blockInput: false)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
},
failingIcon = new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Alpha = 0,
Size = new Vector2(0.3f),
Icon = FontAwesome.Solid.ExclamationTriangle,
RelativeSizeAxes = Axes.Both,
Colour = colours.YellowLight,
},
2018-04-13 17:19:50 +08:00
}
});
Flow.Add(new TransientUserStatisticsUpdateDisplay
{
Alpha = 0
});
Flow.AutoSizeEasing = Easing.OutQuint;
Flow.AutoSizeDuration = 250;
apiState = api.State.GetBoundCopy();
apiState.BindValueChanged(onlineStateChanged, true);
2019-03-28 13:01:06 +08:00
localUser = api.LocalUser.GetBoundCopy();
localUser.BindValueChanged(userChanged, true);
2019-03-28 13:01:06 +08:00
StateContainer = login;
2018-04-13 17:19:50 +08:00
}
private void userChanged(ValueChangedEvent<APIUser> user) => Schedule(() =>
{
Text = user.NewValue.Username;
avatar.User = user.NewValue;
});
2020-10-22 13:49:48 +08:00
private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() =>
2018-04-13 17:19:50 +08:00
{
failingIcon.FadeTo(state.NewValue == APIState.Failing || state.NewValue == APIState.RequiresSecondFactorAuth ? 1 : 0, 200, Easing.OutQuint);
switch (state.NewValue)
2018-04-13 17:19:50 +08:00
{
case APIState.Connecting:
TooltipText = ToolbarStrings.Connecting;
spinner.Show();
break;
case APIState.Failing:
TooltipText = ToolbarStrings.AttemptingToReconnect;
spinner.Show();
failingIcon.Icon = FontAwesome.Solid.ExclamationTriangle;
break;
case APIState.RequiresSecondFactorAuth:
TooltipText = ToolbarStrings.VerificationRequired;
spinner.Show();
failingIcon.Icon = FontAwesome.Solid.Key;
2018-04-13 17:19:50 +08:00
break;
2019-04-01 11:44:46 +08:00
case APIState.Offline:
2018-04-13 17:19:50 +08:00
case APIState.Online:
TooltipText = string.Empty;
spinner.Hide();
2018-04-13 17:19:50 +08:00
break;
default:
throw new ArgumentOutOfRangeException();
2018-04-13 17:19:50 +08:00
}
2020-10-22 13:49:48 +08:00
});
2018-04-13 17:19:50 +08:00
}
}