Files
tbd-station-14/Content.Client/Administration/UI/CustomControls/PlayerListControl.xaml.cs
Paul Ritter e74301a856 refactors adminmenu a timid amount (#5095)
Co-authored-by: Paul <ritter.paul1+git@googlemail.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
2021-11-10 23:26:25 +11:00

78 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Content.Shared.Administration;
using Content.Shared.Administration.Events;
using Robust.Client.AutoGenerated;
using Robust.Client.Player;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Players;
namespace Content.Client.Administration.UI.CustomControls
{
[GenerateTypedNameReferences]
public partial class PlayerListControl : BoxContainer
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
private readonly AdminSystem _adminSystem;
public event Action<ICommonSession?>? OnSelectionChanged;
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 = (ICommonSession) obj.ItemList[obj.ItemIndex].Metadata!;
OnSelectionChanged?.Invoke(selectedPlayer);
}
private void PlayerItemListOnOnItemDeselected(ItemList.ItemListDeselectedEventArgs obj)
{
OnSelectionChanged?.Invoke(null);
}
private void PopulateList(IReadOnlyList<PlayerInfo> _ = null!)
{
PlayerItemList.Clear();
foreach (var info in _adminSystem.PlayerList)
{
var displayName = $"{info.CharacterName} ({info.Username})";
if (!string.IsNullOrEmpty(FilterLineEdit.Text) &&
!displayName.ToLowerInvariant().Contains(FilterLineEdit.Text.Trim().ToLowerInvariant()))
{
continue;
}
PlayerItemList.Add(new ItemList.Item(PlayerItemList)
{
Metadata = _playerManager.SessionsDict[info.SessionId],
Text = displayName
});
}
}
}
}