1
0

SetOutfitMenu.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Client.UserInterface.Controls;
  4. using Content.Shared.Preferences.Loadouts;
  5. using Content.Shared.Roles;
  6. using Robust.Client.AutoGenerated;
  7. using Robust.Client.Console;
  8. using Robust.Client.UserInterface.Controls;
  9. using Robust.Client.UserInterface.CustomControls;
  10. using Robust.Client.UserInterface.XAML;
  11. using Robust.Shared.Prototypes;
  12. namespace Content.Client.Administration.UI.SetOutfit
  13. {
  14. [GenerateTypedNameReferences]
  15. public sealed partial class SetOutfitMenu : DefaultWindow
  16. {
  17. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  18. [Dependency] private readonly IClientConsoleHost _consoleHost = default!;
  19. public NetEntity? TargetEntityId { get; set; }
  20. private StartingGearPrototype? _selectedOutfit;
  21. public SetOutfitMenu()
  22. {
  23. MinSize = SetSize = new Vector2(250, 320);
  24. IoCManager.InjectDependencies(this);
  25. RobustXamlLoader.Load(this);
  26. Title = Loc.GetString("set-outfit-menu-title");
  27. ConfirmButton.Text = Loc.GetString("set-outfit-menu-confirm-button");
  28. ConfirmButton.OnPressed += ConfirmButtonOnOnPressed;
  29. SearchBar.OnTextChanged += SearchBarOnOnTextChanged;
  30. OutfitList.OnItemSelected += OutfitListOnOnItemSelected;
  31. OutfitList.OnItemDeselected += OutfitListOnOnItemDeselected;
  32. PopulateList();
  33. }
  34. private void ConfirmButtonOnOnPressed(BaseButton.ButtonEventArgs obj)
  35. {
  36. if (TargetEntityId == null || _selectedOutfit == null)
  37. return;
  38. var command = $"setoutfit {TargetEntityId} {_selectedOutfit.ID}";
  39. _consoleHost.ExecuteCommand(command);
  40. Close();
  41. }
  42. private void OutfitListOnOnItemSelected(ItemList.ItemListSelectedEventArgs obj)
  43. {
  44. _selectedOutfit = (StartingGearPrototype) obj.ItemList[obj.ItemIndex].Metadata!;
  45. ConfirmButton.Disabled = false;
  46. }
  47. private void OutfitListOnOnItemDeselected(ItemList.ItemListDeselectedEventArgs obj)
  48. {
  49. _selectedOutfit = null;
  50. ConfirmButton.Disabled = true;
  51. }
  52. private void SearchBarOnOnTextChanged(LineEdit.LineEditEventArgs obj)
  53. {
  54. PopulateByFilter(SearchBar.Text);
  55. }
  56. private IEnumerable<StartingGearPrototype> GetPrototypes()
  57. {
  58. // Filter out any StartingGearPrototypes that belong to loadouts
  59. var loadouts = _prototypeManager.EnumeratePrototypes<LoadoutPrototype>();
  60. var loadoutGears = loadouts.Select(l => l.StartingGear);
  61. return _prototypeManager.EnumeratePrototypes<StartingGearPrototype>()
  62. .Where(p => !loadoutGears.Contains(p.ID));
  63. }
  64. private void PopulateList()
  65. {
  66. foreach (var gear in GetPrototypes())
  67. {
  68. OutfitList.Add(GetItem(gear, OutfitList));
  69. }
  70. }
  71. private void PopulateByFilter(string filter)
  72. {
  73. OutfitList.Clear();
  74. foreach (var gear in GetPrototypes())
  75. {
  76. if (!string.IsNullOrEmpty(filter) &&
  77. gear.ID.ToLowerInvariant().Contains(filter.Trim().ToLowerInvariant()))
  78. {
  79. OutfitList.Add(GetItem(gear, OutfitList));
  80. }
  81. }
  82. }
  83. private static ItemList.Item GetItem(StartingGearPrototype gear, ItemList itemList)
  84. {
  85. return new(itemList)
  86. {
  87. Metadata = gear,
  88. Text = gear.ID
  89. };
  90. }
  91. }
  92. }