1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 14:23:20 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Multiplayer/Match/MultiplayerSpectateButton.cs

83 lines
2.4 KiB
C#
Raw Normal View History

2021-04-06 20:37:21 +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.
2022-06-17 15:37:17 +08:00
#nullable disable
2021-04-06 20:37:21 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterfaceV2;
2021-04-06 20:37:21 +08:00
using osu.Game.Online.Multiplayer;
using osuTK;
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
{
2022-11-24 13:32:20 +08:00
public partial class MultiplayerSpectateButton : MultiplayerRoomComposite
2021-04-06 20:37:21 +08:00
{
[Resolved]
private OngoingOperationTracker ongoingOperationTracker { get; set; }
[Resolved]
private OsuColour colours { get; set; }
private IBindable<bool> operationInProgress;
private readonly RoundedButton button;
2021-04-06 20:37:21 +08:00
public MultiplayerSpectateButton()
{
InternalChild = button = new RoundedButton
2021-04-06 20:37:21 +08:00
{
RelativeSizeAxes = Axes.Both,
Size = Vector2.One,
Enabled = { Value = true },
Action = onClick
2021-04-06 20:37:21 +08:00
};
}
private void onClick()
{
var clickOperation = ongoingOperationTracker.BeginOperation();
2022-06-24 20:25:23 +08:00
Client.ToggleSpectate().ContinueWith(_ => endOperation());
void endOperation() => clickOperation?.Dispose();
}
2021-04-06 20:37:21 +08:00
[BackgroundDependencyLoader]
private void load()
{
operationInProgress = ongoingOperationTracker.InProgress.GetBoundCopy();
operationInProgress.BindValueChanged(_ => updateState());
}
protected override void OnRoomUpdated()
{
base.OnRoomUpdated();
updateState();
}
private void updateState()
{
switch (Client.LocalUser?.State)
2021-04-06 20:37:21 +08:00
{
default:
button.Text = "Spectate";
button.BackgroundColour = colours.BlueDark;
2021-04-06 20:37:21 +08:00
break;
case MultiplayerUserState.Spectating:
button.Text = "Stop spectating";
button.BackgroundColour = colours.Gray4;
2021-04-06 20:37:21 +08:00
break;
}
button.Enabled.Value = Client.Room != null
&& Client.Room.State != MultiplayerRoomState.Closed
&& !operationInProgress.Value;
2021-04-06 20:37:21 +08:00
}
}
}