AirAlarmWindow.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using Content.Client.Atmos.Monitor.UI.Widgets;
  2. using Content.Client.Message;
  3. using Content.Client.Stylesheets;
  4. using Content.Client.UserInterface.Controls;
  5. using Content.Shared.Atmos;
  6. using Content.Shared.Atmos.Monitor;
  7. using Content.Shared.Atmos.Monitor.Components;
  8. using Content.Shared.Atmos.Piping.Unary.Components;
  9. using Content.Shared.Temperature;
  10. using Robust.Client.AutoGenerated;
  11. using Robust.Client.UserInterface.Controls;
  12. using Robust.Client.UserInterface.XAML;
  13. namespace Content.Client.Atmos.Monitor.UI;
  14. [GenerateTypedNameReferences]
  15. public sealed partial class AirAlarmWindow : FancyWindow
  16. {
  17. public event Action<string, IAtmosDeviceData>? AtmosDeviceDataChanged;
  18. public event Action<IAtmosDeviceData>? AtmosDeviceDataCopied;
  19. public event Action<string, AtmosMonitorThresholdType, AtmosAlarmThreshold, Gas?>? AtmosAlarmThresholdChanged;
  20. public event Action<AirAlarmMode>? AirAlarmModeChanged;
  21. public event Action<bool>? AutoModeChanged;
  22. public event Action? ResyncAllRequested;
  23. private RichTextLabel _address => CDeviceAddress;
  24. private RichTextLabel _deviceTotal => CDeviceTotal;
  25. private RichTextLabel _pressure => CPressureLabel;
  26. private RichTextLabel _temperature => CTemperatureLabel;
  27. private RichTextLabel _alarmState => CStatusLabel;
  28. private TabContainer _tabContainer => CTabContainer;
  29. private BoxContainer _ventDevices => CVentContainer;
  30. private BoxContainer _scrubberDevices => CScrubberContainer;
  31. private Dictionary<string, PumpControl> _pumps = new();
  32. private Dictionary<string, ScrubberControl> _scrubbers = new();
  33. private Dictionary<string, SensorInfo> _sensors = new();
  34. private Button _resyncDevices => CResyncButton;
  35. private Dictionary<Gas, Label> _gasLabels = new();
  36. private OptionButton _modes => CModeButton;
  37. private CheckBox _autoMode => AutoModeCheckBox;
  38. public AirAlarmWindow()
  39. {
  40. RobustXamlLoader.Load(this);
  41. foreach (var mode in Enum.GetValues<AirAlarmMode>())
  42. {
  43. var text = mode switch
  44. {
  45. AirAlarmMode.Filtering => "air-alarm-ui-mode-filtering",
  46. AirAlarmMode.WideFiltering => "air-alarm-ui-mode-wide-filtering",
  47. AirAlarmMode.Fill => "air-alarm-ui-mode-fill",
  48. AirAlarmMode.Panic => "air-alarm-ui-mode-panic",
  49. AirAlarmMode.None => "air-alarm-ui-mode-none",
  50. _ => "error",
  51. };
  52. _modes.AddItem(Loc.GetString(text));
  53. }
  54. _modes.OnItemSelected += args =>
  55. {
  56. _modes.SelectId(args.Id);
  57. AirAlarmModeChanged!.Invoke((AirAlarmMode) args.Id);
  58. };
  59. _autoMode.OnToggled += _ =>
  60. {
  61. AutoModeChanged!.Invoke(_autoMode.Pressed);
  62. };
  63. _tabContainer.SetTabTitle(0, Loc.GetString("air-alarm-ui-window-tab-vents"));
  64. _tabContainer.SetTabTitle(1, Loc.GetString("air-alarm-ui-window-tab-scrubbers"));
  65. _tabContainer.SetTabTitle(2, Loc.GetString("air-alarm-ui-window-tab-sensors"));
  66. _resyncDevices.OnPressed += _ =>
  67. {
  68. _ventDevices.RemoveAllChildren();
  69. _pumps.Clear();
  70. _scrubberDevices.RemoveAllChildren();
  71. _scrubbers.Clear();
  72. CSensorContainer.RemoveAllChildren();
  73. _sensors.Clear();
  74. ResyncAllRequested!.Invoke();
  75. };
  76. }
  77. public void SetEntity(EntityUid uid)
  78. {
  79. EntityView.SetEntity(uid);
  80. }
  81. public void UpdateState(AirAlarmUIState state)
  82. {
  83. _address.SetMarkup(state.Address);
  84. _deviceTotal.SetMarkup($"{state.DeviceCount}");
  85. _pressure.SetMarkup(Loc.GetString("air-alarm-ui-window-pressure", ("pressure", $"{state.PressureAverage:0.##}")));
  86. _temperature.SetMarkup(Loc.GetString("air-alarm-ui-window-temperature", ("tempC", $"{TemperatureHelpers.KelvinToCelsius(state.TemperatureAverage):0.#}"), ("temperature", $"{state.TemperatureAverage:0.##}")));
  87. _alarmState.SetMarkup(Loc.GetString("air-alarm-ui-window-alarm-state",
  88. ("color", ColorForAlarm(state.AlarmType)),
  89. ("state", $"{state.AlarmType}")));
  90. UpdateModeSelector(state.Mode);
  91. UpdateAutoMode(state.AutoMode);
  92. foreach (var (addr, dev) in state.DeviceData)
  93. {
  94. UpdateDeviceData(addr, dev);
  95. }
  96. }
  97. public void UpdateModeSelector(AirAlarmMode mode)
  98. {
  99. _modes.SelectId((int) mode);
  100. }
  101. public void UpdateAutoMode(bool enabled)
  102. {
  103. _autoMode.Pressed = enabled;
  104. }
  105. public void UpdateDeviceData(string addr, IAtmosDeviceData device)
  106. {
  107. switch (device)
  108. {
  109. case GasVentPumpData pump:
  110. if (!_pumps.TryGetValue(addr, out var pumpControl))
  111. {
  112. var control= new PumpControl(pump, addr);
  113. control.PumpDataChanged += AtmosDeviceDataChanged;
  114. control.PumpDataCopied += AtmosDeviceDataCopied;
  115. _pumps.Add(addr, control);
  116. CVentContainer.AddChild(control);
  117. }
  118. else
  119. {
  120. pumpControl.ChangeData(pump);
  121. }
  122. break;
  123. case GasVentScrubberData scrubber:
  124. if (!_scrubbers.TryGetValue(addr, out var scrubberControl))
  125. {
  126. var control = new ScrubberControl(scrubber, addr);
  127. control.ScrubberDataChanged += AtmosDeviceDataChanged;
  128. control.ScrubberDataCopied += AtmosDeviceDataCopied;
  129. _scrubbers.Add(addr, control);
  130. CScrubberContainer.AddChild(control);
  131. }
  132. else
  133. {
  134. scrubberControl.ChangeData(scrubber);
  135. }
  136. break;
  137. case AtmosSensorData sensor:
  138. if (!_sensors.TryGetValue(addr, out var sensorControl))
  139. {
  140. var control = new SensorInfo(sensor, addr);
  141. control.OnThresholdUpdate += AtmosAlarmThresholdChanged;
  142. control.SensorDataCopied += AtmosDeviceDataCopied;
  143. _sensors.Add(addr, control);
  144. CSensorContainer.AddChild(control);
  145. }
  146. else
  147. {
  148. sensorControl.ChangeData(sensor);
  149. }
  150. break;
  151. }
  152. }
  153. public static Color ColorForThreshold(float amount, AtmosAlarmThreshold threshold)
  154. {
  155. threshold.CheckThreshold(amount, out var curAlarm);
  156. return ColorForAlarm(curAlarm);
  157. }
  158. public static Color ColorForAlarm(AtmosAlarmType curAlarm)
  159. {
  160. return curAlarm switch
  161. {
  162. AtmosAlarmType.Danger => StyleNano.DangerousRedFore,
  163. AtmosAlarmType.Warning => StyleNano.ConcerningOrangeFore,
  164. _ => StyleNano.GoodGreenFore,
  165. };
  166. }
  167. }