Add sorting the admin player panel by clicking the headers (#7150)
This commit is contained in:
committed by
GitHub
parent
d744729b29
commit
995c02169e
@@ -1,6 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Client.Administration.UI.CustomControls;
|
||||
using Content.Shared.Administration;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.Graphics;
|
||||
@@ -8,16 +5,22 @@ using Robust.Client.Player;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using static Content.Client.Administration.UI.Tabs.PlayerTab.PlayerTabHeader;
|
||||
|
||||
namespace Content.Client.Administration.UI.Tabs.PlayerTab
|
||||
{
|
||||
[GenerateTypedNameReferences]
|
||||
public sealed partial class PlayerTab : Control
|
||||
{
|
||||
private const string ArrowUp = "↑";
|
||||
private const string ArrowDown = "↓";
|
||||
private readonly Color _altColor = Color.FromHex("#292B38");
|
||||
private readonly Color _defaultColor = Color.FromHex("#2F2F3B");
|
||||
private readonly AdminSystem _adminSystem;
|
||||
private readonly List<PlayerTabEntry> _players = new();
|
||||
|
||||
private Header _headerClicked = Header.Username;
|
||||
private bool _ascending = true;
|
||||
|
||||
public event Action<BaseButton.ButtonEventArgs>? OnEntryPressed;
|
||||
|
||||
@@ -29,6 +32,9 @@ namespace Content.Client.Administration.UI.Tabs.PlayerTab
|
||||
_adminSystem.PlayerListChanged += RefreshPlayerList;
|
||||
OverlayButtonOn.OnPressed += _adminSystem.AdminOverlayOn;
|
||||
OverlayButtonOff.OnPressed += _adminSystem.AdminOverlayOff;
|
||||
|
||||
ListHeader.BackgroundColorPanel.PanelOverride = new StyleBoxFlat(_altColor);
|
||||
ListHeader.OnHeaderClicked += HeaderClicked;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
@@ -42,24 +48,20 @@ namespace Content.Client.Administration.UI.Tabs.PlayerTab
|
||||
|
||||
private void RefreshPlayerList(IReadOnlyList<PlayerInfo> players)
|
||||
{
|
||||
PlayerList.RemoveAllChildren();
|
||||
foreach (var control in _players)
|
||||
{
|
||||
PlayerList.RemoveChild(control);
|
||||
}
|
||||
|
||||
_players.Clear();
|
||||
|
||||
var playerManager = IoCManager.Resolve<IPlayerManager>();
|
||||
PlayerCount.Text = $"Players: {playerManager.PlayerCount}";
|
||||
|
||||
var altColor = Color.FromHex("#292B38");
|
||||
var defaultColor = Color.FromHex("#2F2F3B");
|
||||
|
||||
PlayerList.AddChild(new PlayerTabEntry("Username",
|
||||
"Character",
|
||||
"Job",
|
||||
"Antagonist",
|
||||
new StyleBoxFlat(altColor),
|
||||
true));
|
||||
PlayerList.AddChild(new HSeparator());
|
||||
|
||||
// Temporary until we can sort by <whatever>
|
||||
var sortedPlayers = new List<PlayerInfo>(players);
|
||||
sortedPlayers.Sort((x, y) => string.Compare(x.Username, y.Username, StringComparison.Ordinal));
|
||||
sortedPlayers.Sort(Compare);
|
||||
|
||||
UpdateHeaderSymbols();
|
||||
|
||||
var useAltColor = false;
|
||||
foreach (var player in sortedPlayers)
|
||||
@@ -68,14 +70,58 @@ namespace Content.Client.Administration.UI.Tabs.PlayerTab
|
||||
player.CharacterName,
|
||||
player.StartingJob,
|
||||
player.Antag ? "YES" : "NO",
|
||||
new StyleBoxFlat(useAltColor ? altColor : defaultColor),
|
||||
new StyleBoxFlat(useAltColor ? _altColor : _defaultColor),
|
||||
player.Connected);
|
||||
entry.PlayerUid = player.EntityUid;
|
||||
entry.OnPressed += args => OnEntryPressed?.Invoke(args);
|
||||
PlayerList.AddChild(entry);
|
||||
_players.Add(entry);
|
||||
|
||||
useAltColor ^= true;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateHeaderSymbols()
|
||||
{
|
||||
ListHeader.ResetHeaderText();
|
||||
ListHeader.GetHeader(_headerClicked).Text += $" {(_ascending ? ArrowUp : ArrowDown)}";
|
||||
}
|
||||
|
||||
private int Compare(PlayerInfo x, PlayerInfo y)
|
||||
{
|
||||
if (!_ascending)
|
||||
{
|
||||
(x, y) = (y, x);
|
||||
}
|
||||
|
||||
return _headerClicked switch
|
||||
{
|
||||
Header.Username => Compare(x.Username, y.Username),
|
||||
Header.Character => Compare(x.CharacterName, y.CharacterName),
|
||||
Header.Job => Compare(x.StartingJob, y.StartingJob),
|
||||
Header.Antagonist => x.Antag.CompareTo(y.Antag),
|
||||
_ => 1
|
||||
};
|
||||
}
|
||||
|
||||
private int Compare(string x, string y)
|
||||
{
|
||||
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private void HeaderClicked(Header header)
|
||||
{
|
||||
if (_headerClicked == header)
|
||||
{
|
||||
_ascending = !_ascending;
|
||||
}
|
||||
else
|
||||
{
|
||||
_headerClicked = header;
|
||||
_ascending = true;
|
||||
}
|
||||
|
||||
RefreshPlayerList(_adminSystem.PlayerList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user