1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 14:43:21 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/ToolbarUserButton.cs

126 lines
4.1 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
{
public 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()
{
AutoSizeAxes = Axes.X;
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours, IAPIProvider api, LoginOverlay? login)
{
2018-04-13 17:19:50 +08:00
Add(new OpaqueBackground { Depth = 1 });
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
}
});
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 ? 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();
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
}
}