Files
tbd-station-14/Content.Client/Administration/UI/CustomControls/PlayerListControl.xaml.cs
2022-01-03 01:54:44 +01:00

86 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Content.Shared.Administration;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Client.Administration.UI.CustomControls
{
[GenerateTypedNameReferences]
public partial class PlayerListControl : BoxContainer
{
private readonly AdminSystem _adminSystem;
public event Action<PlayerInfo?>? OnSelectionChanged;
public Action<PlayerInfo, ItemList.Item>? DecoratePlayer;
public Func<PlayerInfo, int>? SortKey;
public PlayerListControl()
{
_adminSystem = EntitySystem.Get<AdminSystem>();
IoCManager.InjectDependencies(this);
RobustXamlLoader.Load(this);
}
protected override void EnteredTree()
{
// Fill the Option data
PopulateList();
PlayerItemList.OnItemSelected += PlayerItemListOnOnItemSelected;
PlayerItemList.OnItemDeselected += PlayerItemListOnOnItemDeselected;
FilterLineEdit.OnTextChanged += FilterLineEditOnOnTextEntered;
_adminSystem.PlayerListChanged += PopulateList;
}
private void FilterLineEditOnOnTextEntered(LineEdit.LineEditEventArgs obj)
{
PopulateList();
}
private void PlayerItemListOnOnItemSelected(ItemList.ItemListSelectedEventArgs obj)
{
var selectedPlayer = (PlayerInfo) obj.ItemList[obj.ItemIndex].Metadata!;
OnSelectionChanged?.Invoke(selectedPlayer);
}
private void PlayerItemListOnOnItemDeselected(ItemList.ItemListDeselectedEventArgs obj)
{
OnSelectionChanged?.Invoke(null);
}
public void Refresh() => PopulateList();
private void PopulateList(IReadOnlyList<PlayerInfo> _ = null!)
{
PlayerItemList.Clear();
IEnumerable<PlayerInfo> iter = _adminSystem.PlayerList;
if (SortKey is not null)
iter = _adminSystem.PlayerList.OrderByDescending(SortKey);
foreach (var info in iter)
{
var displayName = $"{info.CharacterName} ({info.Username})";
if (!string.IsNullOrEmpty(FilterLineEdit.Text) &&
!displayName.ToLowerInvariant().Contains(FilterLineEdit.Text.Trim().ToLowerInvariant()))
{
continue;
}
var item = new ItemList.Item(PlayerItemList)
{
Metadata = info,
Text = displayName
};
DecoratePlayer?.Invoke(info, item);
PlayerItemList.Add(item);
}
}
}
}