AddReagentWindow.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Content.Shared.Chemistry.Reagent;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.Console;
  4. using Robust.Client.UserInterface.Controls;
  5. using Robust.Client.UserInterface.CustomControls;
  6. using Robust.Client.UserInterface.XAML;
  7. using Robust.Shared.Prototypes;
  8. namespace Content.Client.Administration.UI.ManageSolutions
  9. {
  10. /// <summary>
  11. /// A debug window that allows you to add a reagent to a solution. Intended to be used with <see
  12. /// cref="EditSolutionsWindow"/>
  13. /// </summary>
  14. [GenerateTypedNameReferences]
  15. public sealed partial class AddReagentWindow : DefaultWindow
  16. {
  17. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  18. [Dependency] private readonly IClientConsoleHost _consoleHost = default!;
  19. private readonly NetEntity _targetEntity;
  20. private string _targetSolution;
  21. private ReagentPrototype? _selectedReagent;
  22. // FloatSpinBox does not (yet?) play nice with xaml
  23. private FloatSpinBox _quantitySpin = new(1, 2) { Value = 10, HorizontalExpand = true};
  24. public AddReagentWindow(NetEntity targetEntity, string targetSolution)
  25. {
  26. IoCManager.InjectDependencies(this);
  27. RobustXamlLoader.Load(this);
  28. Title = Loc.GetString("admin-add-reagent-window-title", ("solution", targetSolution));
  29. _targetEntity = targetEntity;
  30. _targetSolution = targetSolution;
  31. QuantityBox.AddChild(_quantitySpin);
  32. ReagentList.OnItemSelected += ReagentListSelected;
  33. ReagentList.OnItemDeselected += ReagentListDeselected;
  34. SearchBar.OnTextChanged += (_) => UpdateReagentPrototypes(SearchBar.Text);
  35. _quantitySpin.OnValueChanged += (_) => UpdateAddButton();
  36. AddButton.OnPressed += AddReagent;
  37. UpdateReagentPrototypes();
  38. UpdateAddButton();
  39. }
  40. /// <summary>
  41. /// Execute a console command that asks the server to add the selected reagent.
  42. /// </summary>
  43. private void AddReagent(BaseButton.ButtonEventArgs obj)
  44. {
  45. if (_selectedReagent == null)
  46. return;
  47. var quantity = _quantitySpin.Value.ToString("F2");
  48. var command = $"addreagent {_targetEntity} {_targetSolution} {_selectedReagent.ID} {quantity}";
  49. _consoleHost.ExecuteCommand(command);
  50. }
  51. private void ReagentListSelected(ItemList.ItemListSelectedEventArgs obj)
  52. {
  53. _selectedReagent = (ReagentPrototype) obj.ItemList[obj.ItemIndex].Metadata!;
  54. UpdateAddButton();
  55. }
  56. private void ReagentListDeselected(ItemList.ItemListDeselectedEventArgs obj)
  57. {
  58. _selectedReagent = null;
  59. UpdateAddButton();
  60. }
  61. public void UpdateSolution(string? selectedSolution)
  62. {
  63. if (selectedSolution == null)
  64. {
  65. Close();
  66. Dispose();
  67. return;
  68. }
  69. _targetSolution = selectedSolution;
  70. Title = Loc.GetString("admin-add-reagent-window-title", ("solution", _targetSolution));
  71. UpdateAddButton();
  72. }
  73. /// <summary>
  74. /// Set the Text and enabled/disabled status of the button that actually adds the reagent.
  75. /// </summary>
  76. private void UpdateAddButton()
  77. {
  78. AddButton.Disabled = true;
  79. if (_selectedReagent == null)
  80. {
  81. AddButton.Text = Loc.GetString("admin-add-reagent-window-add-invalid-reagent");
  82. return;
  83. }
  84. AddButton.Text = Loc.GetString("admin-add-reagent-window-add",
  85. ("quantity", _quantitySpin.Value.ToString("F2")),
  86. ("reagent", _selectedReagent.ID));
  87. AddButton.Disabled = false;
  88. }
  89. /// <summary>
  90. /// Get a list of all reagent prototypes and show them in an item list.
  91. /// </summary>
  92. private void UpdateReagentPrototypes(string? filter = null)
  93. {
  94. ReagentList.Clear();
  95. foreach (var reagent in _prototypeManager.EnumeratePrototypes<ReagentPrototype>())
  96. {
  97. if (!string.IsNullOrEmpty(filter) &&
  98. !reagent.ID.ToLowerInvariant().Contains(filter.Trim().ToLowerInvariant()))
  99. {
  100. continue;
  101. }
  102. ItemList.Item regentItem = new(ReagentList)
  103. {
  104. Metadata = reagent,
  105. Text = reagent.ID
  106. };
  107. ReagentList.Add(regentItem);
  108. }
  109. }
  110. }
  111. }