forked from ppy/osu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpectatorList.cs
More file actions
300 lines (242 loc) · 10.8 KB
/
Copy pathSpectatorList.cs
File metadata and controls
300 lines (242 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// 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.
using System;
using System.Collections.Specialized;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Pooling;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Localisation.HUD;
using osu.Game.Online.Chat;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Spectator;
using osu.Game.Skinning;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Play.HUD
{
public partial class SpectatorList : CompositeDrawable, ISerialisableDrawable
{
private const int max_spectators_displayed = 10;
public Bindable<Typeface> HeaderFont { get; } = new Bindable<Typeface>(Typeface.Torus);
public BindableColour4 HeaderColour { get; } = new BindableColour4(Colour4.White);
private IBindableList<SpectatorUser> watchingUsers { get; } = new BindableList<SpectatorUser>();
private IBindableList<int> multiplayerPlayers { get; } = new BindableList<int>();
private BindableList<SpectatorUser> actualSpectators { get; } = new BindableList<SpectatorUser>();
private Bindable<LocalUserPlayingState> userPlayingState { get; } = new Bindable<LocalUserPlayingState>();
private OsuSpriteText header = null!;
private FillFlowContainer mainFlow = null!;
private FillFlowContainer<SpectatorListEntry> spectatorsFlow = null!;
private DrawablePool<SpectatorListEntry> pool = null!;
[Resolved]
private SpectatorClient client { get; set; } = null!;
[Resolved(CanBeNull = true)]
private GameplayState gameplayState { get; set; } = null!;
[Resolved]
private MultiplayerClient multiplayerClient { get; set; } = null!;
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
AutoSizeAxes = Axes.Y;
InternalChildren = new[]
{
Empty().With(t => t.Size = new Vector2(100, 50)),
mainFlow = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
header = new OsuSpriteText
{
Colour = colours.Blue0,
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
},
spectatorsFlow = new FillFlowContainer<SpectatorListEntry>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
}
}
},
pool = new DrawablePool<SpectatorListEntry>(max_spectators_displayed),
};
HeaderColour.Value = header.Colour;
}
protected override void LoadComplete()
{
base.LoadComplete();
if (gameplayState != null) ((IBindable<LocalUserPlayingState>)userPlayingState).BindTo(gameplayState.PlayingState);
multiplayerPlayers.BindTo(multiplayerClient.CurrentMatchPlayingUserIds);
multiplayerPlayers.BindCollectionChanged((_, _) => removePlayersFromMultiplayerRoom());
watchingUsers.BindTo(client.WatchingUsers);
watchingUsers.BindCollectionChanged(onWatchingUsersChanged, true);
actualSpectators.BindCollectionChanged(onSpectatorsChanged, true);
userPlayingState.BindValueChanged(_ => updateVisibility());
HeaderFont.BindValueChanged(_ => updateAppearance());
HeaderColour.BindValueChanged(_ => updateAppearance(), true);
FinishTransforms(true);
this.FadeInFromZero(200, Easing.OutQuint);
}
private void onWatchingUsersChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
{
for (int i = 0; i < e.NewItems!.Count; i++)
actualSpectators.Add((SpectatorUser)e.NewItems![i]!);
break;
}
case NotifyCollectionChangedAction.Remove:
{
for (int i = 0; i < e.OldItems!.Count; i++)
actualSpectators.Remove((SpectatorUser)e.OldItems![i]!);
break;
}
case NotifyCollectionChangedAction.Reset:
{
actualSpectators.Clear();
break;
}
default:
throw new NotSupportedException();
}
removePlayersFromMultiplayerRoom();
}
private void removePlayersFromMultiplayerRoom()
{
// the multiplayer gameplay leaderboard relies on calling `SpectatorClient.WatchUser()` to get updates on users' total scores.
// this has an unfortunate side effect of other players showing up in `SpectatorClient.WatchingUsers`.
//
// we do not generally wish to display other players in the room as spectators due to that implementation detail,
// therefore this code is intended to filter out those players on the client side.
actualSpectators.RemoveAll(s => multiplayerPlayers.Contains(s.OnlineID));
}
private void onSpectatorsChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
{
for (int i = 0; i < e.NewItems!.Count; i++)
{
var spectator = (SpectatorUser)e.NewItems![i]!;
int index = Math.Max(e.NewStartingIndex, 0) + i;
if (index >= max_spectators_displayed)
break;
addNewSpectatorToList(index, spectator);
}
break;
}
case NotifyCollectionChangedAction.Remove:
{
spectatorsFlow.RemoveAll(entry => e.OldItems!.Contains(entry.Current.Value), false);
for (int i = 0; i < spectatorsFlow.Count; i++)
spectatorsFlow.SetLayoutPosition(spectatorsFlow[i], i);
if (actualSpectators.Count >= max_spectators_displayed && spectatorsFlow.Count < max_spectators_displayed)
{
for (int i = spectatorsFlow.Count; i < max_spectators_displayed; i++)
addNewSpectatorToList(i, actualSpectators[i]);
}
break;
}
case NotifyCollectionChangedAction.Reset:
{
spectatorsFlow.Clear(false);
break;
}
default:
throw new NotSupportedException();
}
header.Text = SpectatorListStrings.SpectatorCount(actualSpectators.Count).ToUpper();
updateVisibility();
for (int i = 0; i < spectatorsFlow.Count; i++)
{
spectatorsFlow[i].Colour = i < max_spectators_displayed - 1
? Color4.White
: ColourInfo.GradientVertical(Color4.White, Color4.White.Opacity(0));
}
}
private void addNewSpectatorToList(int i, SpectatorUser spectator)
{
var entry = pool.Get(entry =>
{
entry.Current.Value = spectator;
entry.UserPlayingState = userPlayingState;
});
spectatorsFlow.Insert(i, entry);
}
private void updateVisibility()
{
// We don't want to show spectators when we are watching a replay.
mainFlow.FadeTo(actualSpectators.Count > 0 && userPlayingState.Value != LocalUserPlayingState.NotPlaying ? 1 : 0, 250, Easing.OutQuint);
}
private void updateAppearance()
{
header.Font = OsuFont.GetFont(HeaderFont.Value, 12, FontWeight.Bold);
header.Colour = HeaderColour.Value;
Width = header.DrawWidth;
}
private partial class SpectatorListEntry : PoolableDrawable
{
public Bindable<SpectatorUser> Current { get; } = new Bindable<SpectatorUser>();
private readonly BindableWithCurrent<LocalUserPlayingState> current = new BindableWithCurrent<LocalUserPlayingState>();
public Bindable<LocalUserPlayingState> UserPlayingState
{
get => current.Current;
set => current.Current = value;
}
private OsuSpriteText username = null!;
private DrawableLinkCompiler? linkCompiler;
[Resolved]
private OsuGame? game { get; set; }
[BackgroundDependencyLoader]
private void load()
{
AutoSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
username = new OsuSpriteText(),
};
}
protected override void LoadComplete()
{
base.LoadComplete();
UserPlayingState.BindValueChanged(_ => updateEnabledState());
Current.BindValueChanged(_ => updateState(), true);
}
protected override void PrepareForUse()
{
base.PrepareForUse();
username.MoveToX(10)
.Then()
.MoveToX(0, 400, Easing.OutQuint);
this.FadeInFromZero(400, Easing.OutQuint);
}
private void updateState()
{
username.Text = Current.Value.Username;
linkCompiler?.Expire();
AddInternal(linkCompiler = new DrawableLinkCompiler([username])
{
IdleColour = Colour4.White,
Action = () => game?.HandleLink(new LinkDetails(LinkAction.OpenUserProfile, Current.Value)),
});
updateEnabledState();
}
private void updateEnabledState()
{
linkCompiler?.Enabled.Value = UserPlayingState.Value != LocalUserPlayingState.Playing;
}
}
public bool UsesFixedAnchor { get; set; }
}
}