GasThermomachineBoundUserInterface.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Content.Shared.Atmos;
  2. using Content.Shared.Atmos.Piping.Unary.Components;
  3. using JetBrains.Annotations;
  4. using Robust.Client.GameObjects;
  5. using Robust.Client.UserInterface;
  6. namespace Content.Client.Atmos.UI
  7. {
  8. /// <summary>
  9. /// Initializes a <see cref="GasThermomachineWindow"/> and updates it when new server messages are received.
  10. /// </summary>
  11. [UsedImplicitly]
  12. public sealed class GasThermomachineBoundUserInterface : BoundUserInterface
  13. {
  14. [ViewVariables]
  15. private GasThermomachineWindow? _window;
  16. [ViewVariables]
  17. private float _minTemp = 0.0f;
  18. [ViewVariables]
  19. private float _maxTemp = 0.0f;
  20. [ViewVariables]
  21. private bool _isHeater = true;
  22. public GasThermomachineBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
  23. {
  24. }
  25. protected override void Open()
  26. {
  27. base.Open();
  28. _window = this.CreateWindow<GasThermomachineWindow>();
  29. _window.ToggleStatusButton.OnPressed += _ => OnToggleStatusButtonPressed();
  30. _window.TemperatureSpinbox.OnValueChanged += _ => OnTemperatureChanged(_window.TemperatureSpinbox.Value);
  31. }
  32. private void OnToggleStatusButtonPressed()
  33. {
  34. if (_window is null) return;
  35. _window.SetActive(!_window.Active);
  36. SendMessage(new GasThermomachineToggleMessage());
  37. }
  38. private void OnTemperatureChanged(float value)
  39. {
  40. var actual = 0f;
  41. if (_isHeater)
  42. actual = Math.Min(value, _maxTemp);
  43. else
  44. actual = Math.Max(value, _minTemp);
  45. actual = Math.Max(actual, Atmospherics.TCMB);
  46. if (!MathHelper.CloseTo(actual, value, 0.09))
  47. {
  48. _window?.SetTemperature(actual);
  49. return;
  50. }
  51. SendMessage(new GasThermomachineChangeTemperatureMessage(actual));
  52. }
  53. /// <summary>
  54. /// Update the UI state based on server-sent info
  55. /// </summary>
  56. /// <param name="state"></param>
  57. protected override void UpdateState(BoundUserInterfaceState state)
  58. {
  59. base.UpdateState(state);
  60. if (_window == null || state is not GasThermomachineBoundUserInterfaceState cast)
  61. return;
  62. _minTemp = cast.MinTemperature;
  63. _maxTemp = cast.MaxTemperature;
  64. _isHeater = cast.IsHeater;
  65. _window.SetTemperature(cast.Temperature);
  66. _window.SetActive(cast.Enabled);
  67. _window.Title = _isHeater switch
  68. {
  69. false => Loc.GetString("comp-gas-thermomachine-ui-title-freezer"),
  70. true => Loc.GetString("comp-gas-thermomachine-ui-title-heater")
  71. };
  72. }
  73. }
  74. }