GasFilterBoundUserInterface.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Content.Client.Atmos.EntitySystems;
  2. using Content.Shared.Atmos;
  3. using Content.Shared.Atmos.Piping.Trinary.Components;
  4. using Content.Shared.Localizations;
  5. using JetBrains.Annotations;
  6. using Robust.Client.UserInterface;
  7. namespace Content.Client.Atmos.UI
  8. {
  9. /// <summary>
  10. /// Initializes a <see cref="GasFilterWindow"/> and updates it when new server messages are received.
  11. /// </summary>
  12. [UsedImplicitly]
  13. public sealed class GasFilterBoundUserInterface : BoundUserInterface
  14. {
  15. [ViewVariables]
  16. private const float MaxTransferRate = Atmospherics.MaxTransferRate;
  17. [ViewVariables]
  18. private GasFilterWindow? _window;
  19. public GasFilterBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
  20. {
  21. }
  22. protected override void Open()
  23. {
  24. base.Open();
  25. var atmosSystem = EntMan.System<AtmosphereSystem>();
  26. _window = this.CreateWindow<GasFilterWindow>();
  27. _window.PopulateGasList(atmosSystem.Gases);
  28. _window.ToggleStatusButtonPressed += OnToggleStatusButtonPressed;
  29. _window.FilterTransferRateChanged += OnFilterTransferRatePressed;
  30. _window.SelectGasPressed += OnSelectGasPressed;
  31. }
  32. private void OnToggleStatusButtonPressed()
  33. {
  34. if (_window is null) return;
  35. SendMessage(new GasFilterToggleStatusMessage(_window.FilterStatus));
  36. }
  37. private void OnFilterTransferRatePressed(string value)
  38. {
  39. var rate = UserInputParser.TryFloat(value, out var parsed) ? parsed : 0f;
  40. SendMessage(new GasFilterChangeRateMessage(rate));
  41. }
  42. private void OnSelectGasPressed()
  43. {
  44. if (_window is null) return;
  45. if (_window.SelectedGas is null)
  46. {
  47. SendMessage(new GasFilterSelectGasMessage(null));
  48. }
  49. else
  50. {
  51. if (!int.TryParse(_window.SelectedGas, out var gas)) return;
  52. SendMessage(new GasFilterSelectGasMessage(gas));
  53. }
  54. }
  55. /// <summary>
  56. /// Update the UI state based on server-sent info
  57. /// </summary>
  58. /// <param name="state"></param>
  59. protected override void UpdateState(BoundUserInterfaceState state)
  60. {
  61. base.UpdateState(state);
  62. if (_window == null || state is not GasFilterBoundUserInterfaceState cast)
  63. return;
  64. _window.Title = (cast.FilterLabel);
  65. _window.SetFilterStatus(cast.Enabled);
  66. _window.SetTransferRate(cast.TransferRate);
  67. if (cast.FilteredGas is not null)
  68. {
  69. var atmos = EntMan.System<AtmosphereSystem>();
  70. var gas = atmos.GetGas((Gas) cast.FilteredGas);
  71. var gasName = Loc.GetString(gas.Name);
  72. _window.SetGasFiltered(gas.ID, gasName);
  73. }
  74. else
  75. {
  76. _window.SetGasFiltered(null, Loc.GetString("comp-gas-filter-ui-filter-gas-none"));
  77. }
  78. }
  79. protected override void Dispose(bool disposing)
  80. {
  81. base.Dispose(disposing);
  82. if (!disposing) return;
  83. _window?.Dispose();
  84. }
  85. }
  86. }