2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-08-09 14:34:45 +08:00
|
|
|
|
using System;
|
2016-12-01 15:05:54 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2020-10-22 13:19:12 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2017-03-15 14:22:28 +08:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2016-12-01 15:05:54 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2022-08-09 14:34:45 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-04-02 13:51:28 +08:00
|
|
|
|
using osu.Framework.Graphics.Effects;
|
2022-08-09 14:34:45 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2019-02-12 12:04:46 +08:00
|
|
|
|
using osu.Game.Graphics;
|
2022-08-09 14:34:45 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Localisation;
|
2016-12-01 15:05:54 +08:00
|
|
|
|
using osu.Game.Online.API;
|
2021-11-04 17:02:44 +08:00
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2019-06-18 02:55:07 +08:00
|
|
|
|
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
|
|
|
|
|
2016-12-01 15:05:54 +08:00
|
|
|
|
namespace osu.Game.Overlays.Toolbar
|
|
|
|
|
{
|
2020-10-22 13:19:12 +08:00
|
|
|
|
public partial class ToolbarUserButton : ToolbarOverlayToggleButton
|
2016-12-01 15:05:54 +08:00
|
|
|
|
{
|
2022-08-09 14:34:45 +08:00
|
|
|
|
private UpdateableAvatar avatar = null!;
|
|
|
|
|
|
|
|
|
|
private IBindable<APIUser> localUser = null!;
|
|
|
|
|
|
|
|
|
|
private LoadingSpinner spinner = null!;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-08-09 14:34:45 +08:00
|
|
|
|
private SpriteIcon failingIcon = null!;
|
2020-10-22 13:19:12 +08:00
|
|
|
|
|
2022-08-09 14:34:45 +08:00
|
|
|
|
private IBindable<APIState> apiState = null!;
|
2020-10-22 13:19:12 +08:00
|
|
|
|
|
2016-12-01 15:05:54 +08:00
|
|
|
|
public ToolbarUserButton()
|
|
|
|
|
{
|
2017-02-08 19:14:17 +08:00
|
|
|
|
AutoSizeAxes = Axes.X;
|
2022-08-09 14:34:45 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-08-09 14:34:45 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours, IAPIProvider api, LoginOverlay? login)
|
|
|
|
|
{
|
2016-12-01 17:53:13 +08:00
|
|
|
|
Add(new OpaqueBackground { Depth = 1 });
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-08-09 14:34:45 +08:00
|
|
|
|
Flow.Add(new Container
|
2017-03-15 14:22:28 +08:00
|
|
|
|
{
|
|
|
|
|
Masking = true,
|
2022-08-09 14:34:45 +08:00
|
|
|
|
CornerRadius = 4,
|
2017-03-15 14:22:28 +08:00
|
|
|
|
Size = new Vector2(32),
|
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
|
Origin = Anchor.CentreLeft,
|
2017-06-12 11:48:47 +08:00
|
|
|
|
EdgeEffect = new EdgeEffectParameters
|
2017-03-15 14:22:28 +08:00
|
|
|
|
{
|
|
|
|
|
Type = EdgeEffectType.Shadow,
|
|
|
|
|
Radius = 4,
|
|
|
|
|
Colour = Color4.Black.Opacity(0.1f),
|
2022-08-09 14:34:45 +08:00
|
|
|
|
},
|
|
|
|
|
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,
|
|
|
|
|
},
|
2017-03-15 14:22:28 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-08-09 14:34:45 +08:00
|
|
|
|
apiState = api.State.GetBoundCopy();
|
2020-10-22 13:19:12 +08:00
|
|
|
|
apiState.BindValueChanged(onlineStateChanged, true);
|
2019-03-28 13:01:06 +08:00
|
|
|
|
|
2022-08-09 14:34:45 +08:00
|
|
|
|
localUser = api.LocalUser.GetBoundCopy();
|
|
|
|
|
localUser.BindValueChanged(userChanged, true);
|
|
|
|
|
|
2019-03-28 13:01:06 +08:00
|
|
|
|
StateContainer = login;
|
2016-12-01 15:05:54 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-08-09 15:01:04 +08:00
|
|
|
|
private void userChanged(ValueChangedEvent<APIUser> user) => Schedule(() =>
|
2022-08-09 14:34:45 +08:00
|
|
|
|
{
|
|
|
|
|
Text = user.NewValue.Username;
|
|
|
|
|
avatar.User = user.NewValue;
|
2022-08-09 15:01:04 +08:00
|
|
|
|
});
|
2022-08-09 14:34:45 +08:00
|
|
|
|
|
2020-10-22 13:49:48 +08:00
|
|
|
|
private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() =>
|
2016-12-01 15:05:54 +08:00
|
|
|
|
{
|
2022-08-09 14:34:45 +08:00
|
|
|
|
failingIcon.FadeTo(state.NewValue == APIState.Failing ? 1 : 0, 200, Easing.OutQuint);
|
|
|
|
|
|
2020-10-22 13:19:12 +08:00
|
|
|
|
switch (state.NewValue)
|
2016-12-01 15:05:54 +08:00
|
|
|
|
{
|
2022-08-09 14:34:45 +08:00
|
|
|
|
case APIState.Connecting:
|
2022-08-09 14:43:26 +08:00
|
|
|
|
TooltipText = ToolbarStrings.Connecting;
|
|
|
|
|
spinner.Show();
|
|
|
|
|
break;
|
|
|
|
|
|
2022-08-09 14:34:45 +08:00
|
|
|
|
case APIState.Failing:
|
2022-08-09 14:57:16 +08:00
|
|
|
|
TooltipText = ToolbarStrings.AttemptingToReconnect;
|
2022-08-09 14:34:45 +08:00
|
|
|
|
spinner.Show();
|
2016-12-01 15:05:54 +08:00
|
|
|
|
break;
|
2019-04-01 11:44:46 +08:00
|
|
|
|
|
2022-08-09 14:34:45 +08:00
|
|
|
|
case APIState.Offline:
|
2016-12-01 15:05:54 +08:00
|
|
|
|
case APIState.Online:
|
2022-08-09 14:43:26 +08:00
|
|
|
|
TooltipText = string.Empty;
|
2022-08-09 14:34:45 +08:00
|
|
|
|
spinner.Hide();
|
2016-12-01 15:05:54 +08:00
|
|
|
|
break;
|
2022-08-09 14:34:45 +08:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
2016-12-01 15:05:54 +08:00
|
|
|
|
}
|
2020-10-22 13:49:48 +08:00
|
|
|
|
});
|
2016-12-01 15:05:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|