1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 18:53:51 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/ToolbarUserButton.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

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;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
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;
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
{
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!;
public ToolbarUserButton()
{
AutoSizeAxes = Axes.X;
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours, IAPIProvider api, LoginOverlay? login)
{
Add(new OpaqueBackground { Depth = 1 });
2018-04-13 17:19:50 +08:00
Flow.Add(new Container
{
Masking = true,
CornerRadius = 4,
Size = new Vector2(32),
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
2017-06-12 11:48:47 +08:00
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(() =>
{
failingIcon.FadeTo(state.NewValue == APIState.Failing ? 1 : 0, 200, Easing.OutQuint);
switch (state.NewValue)
{
case APIState.Connecting:
TooltipText = ToolbarStrings.Connecting;
spinner.Show();
break;
case APIState.Failing:
TooltipText = ToolbarStrings.AttemptingToReconnect;
spinner.Show();
break;
2019-04-01 11:44:46 +08:00
case APIState.Offline:
case APIState.Online:
TooltipText = string.Empty;
spinner.Hide();
break;
default:
throw new ArgumentOutOfRangeException();
}
2020-10-22 13:49:48 +08:00
});
}
}