ChameleonMenu.xaml.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Client.Clothing.Systems;
  4. using Content.Client.Stylesheets;
  5. using Robust.Client.AutoGenerated;
  6. using Robust.Client.GameObjects;
  7. using Robust.Client.UserInterface.Controls;
  8. using Robust.Client.UserInterface.CustomControls;
  9. using Robust.Client.UserInterface.XAML;
  10. using Robust.Shared.Prototypes;
  11. namespace Content.Client.Clothing.UI;
  12. [GenerateTypedNameReferences]
  13. public sealed partial class ChameleonMenu : DefaultWindow
  14. {
  15. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  16. [Dependency] private readonly IEntityManager _entityManager = default!;
  17. private readonly SpriteSystem _sprite;
  18. public event Action<string>? OnIdSelected;
  19. private IEnumerable<string> _possibleIds = Enumerable.Empty<string>();
  20. private string? _selectedId;
  21. private string _searchFilter = "";
  22. public ChameleonMenu()
  23. {
  24. RobustXamlLoader.Load(this);
  25. IoCManager.InjectDependencies(this);
  26. _sprite = _entityManager.System<SpriteSystem>();
  27. Search.OnTextChanged += OnSearchEntered;
  28. }
  29. public void UpdateState(IEnumerable<string> possibleIds, string? selectedId)
  30. {
  31. _possibleIds = possibleIds;
  32. _selectedId = selectedId;
  33. UpdateGrid();
  34. }
  35. private void OnSearchEntered(LineEdit.LineEditEventArgs obj)
  36. {
  37. _searchFilter = obj.Text;
  38. UpdateGrid();
  39. }
  40. private void UpdateGrid()
  41. {
  42. ClearGrid();
  43. var group = new ButtonGroup();
  44. var searchFilterLow = _searchFilter.ToLowerInvariant();
  45. foreach (var id in _possibleIds)
  46. {
  47. if (!_prototypeManager.TryIndex(id, out EntityPrototype? proto))
  48. continue;
  49. var lowId = id.ToLowerInvariant();
  50. var lowName = proto.Name.ToLowerInvariant();
  51. if (!lowId.Contains(searchFilterLow) && !lowName.Contains(_searchFilter))
  52. continue;
  53. var button = new Button
  54. {
  55. MinSize = new Vector2(48, 48),
  56. HorizontalExpand = true,
  57. Group = group,
  58. StyleClasses = {StyleBase.ButtonSquare},
  59. ToggleMode = true,
  60. Pressed = _selectedId == id,
  61. ToolTip = proto.Name
  62. };
  63. button.OnPressed += _ => OnIdSelected?.Invoke(id);
  64. Grid.AddChild(button);
  65. var entityPrototypeView = new EntityPrototypeView();
  66. button.AddChild(entityPrototypeView);
  67. entityPrototypeView.SetPrototype(proto);
  68. }
  69. }
  70. private void ClearGrid()
  71. {
  72. Grid.RemoveAllChildren();
  73. }
  74. }