1
0

SensorInfo.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Content.Client.Message;
  2. using Content.Shared.Atmos;
  3. using Content.Shared.Atmos.Monitor;
  4. using Content.Shared.Temperature;
  5. using Robust.Client.AutoGenerated;
  6. using Robust.Client.UserInterface.Controls;
  7. using Robust.Client.UserInterface.XAML;
  8. namespace Content.Client.Atmos.Monitor.UI.Widgets;
  9. [GenerateTypedNameReferences]
  10. public sealed partial class SensorInfo : BoxContainer
  11. {
  12. public Action<string, AtmosMonitorThresholdType, AtmosAlarmThreshold, Gas?>? OnThresholdUpdate;
  13. public event Action<AtmosSensorData>? SensorDataCopied;
  14. private string _address;
  15. private ThresholdControl _pressureThreshold;
  16. private ThresholdControl _temperatureThreshold;
  17. private Dictionary<Gas, ThresholdControl> _gasThresholds = new();
  18. private Dictionary<Gas, RichTextLabel> _gasLabels = new();
  19. private Button _copySettings => CCopySettings;
  20. public SensorInfo(AtmosSensorData data, string address)
  21. {
  22. RobustXamlLoader.Load(this);
  23. _address = address;
  24. SensorAddress.Title = $"{address} : {data.AlarmState}";
  25. AlarmStateLabel.SetMarkup(Loc.GetString("air-alarm-ui-window-alarm-state-indicator",
  26. ("color", AirAlarmWindow.ColorForAlarm(data.AlarmState)),
  27. ("state", $"{data.AlarmState}")));
  28. PressureLabel.SetMarkup(Loc.GetString("air-alarm-ui-window-pressure-indicator",
  29. ("color", AirAlarmWindow.ColorForThreshold(data.Pressure, data.PressureThreshold)),
  30. ("pressure", $"{data.Pressure:0.##}")));
  31. TemperatureLabel.SetMarkup(Loc.GetString("air-alarm-ui-window-temperature-indicator",
  32. ("color", AirAlarmWindow.ColorForThreshold(data.Temperature, data.TemperatureThreshold)),
  33. ("tempC", $"{TemperatureHelpers.KelvinToCelsius(data.Temperature):0.#}"),
  34. ("temperature", $"{data.Temperature:0.##}")));
  35. foreach (var (gas, amount) in data.Gases)
  36. {
  37. var label = new RichTextLabel();
  38. var fractionGas = amount / data.TotalMoles;
  39. label.SetMarkup(Loc.GetString("air-alarm-ui-gases-indicator",
  40. ("gas", $"{gas}"),
  41. ("color", AirAlarmWindow.ColorForThreshold(fractionGas, data.GasThresholds[gas])),
  42. ("amount", $"{amount:0.####}"),
  43. ("percentage", $"{(100 * fractionGas):0.##}")));
  44. GasContainer.AddChild(label);
  45. _gasLabels.Add(gas, label);
  46. var threshold = data.GasThresholds[gas];
  47. var gasThresholdControl = new ThresholdControl(Loc.GetString($"air-alarm-ui-thresholds-gas-title", ("gas", $"{gas}")), threshold, AtmosMonitorThresholdType.Gas, gas, 100);
  48. gasThresholdControl.Margin = new Thickness(20, 2, 2, 2);
  49. gasThresholdControl.ThresholdDataChanged += (type, alarmThreshold, arg3) =>
  50. {
  51. OnThresholdUpdate?.Invoke(_address, type, alarmThreshold, arg3);
  52. };
  53. _gasThresholds.Add(gas, gasThresholdControl);
  54. GasContainer.AddChild(gasThresholdControl);
  55. }
  56. _pressureThreshold = new ThresholdControl(Loc.GetString("air-alarm-ui-thresholds-pressure-title"), data.PressureThreshold, AtmosMonitorThresholdType.Pressure);
  57. PressureThresholdContainer.AddChild(_pressureThreshold);
  58. _temperatureThreshold = new ThresholdControl(Loc.GetString("air-alarm-ui-thresholds-temperature-title"),
  59. data.TemperatureThreshold,
  60. AtmosMonitorThresholdType.Temperature);
  61. TemperatureThresholdContainer.AddChild(_temperatureThreshold);
  62. _pressureThreshold.ThresholdDataChanged += (type, threshold, arg3) =>
  63. {
  64. OnThresholdUpdate?.Invoke(_address, type, threshold, arg3);
  65. };
  66. _temperatureThreshold.ThresholdDataChanged += (type, threshold, arg3) =>
  67. {
  68. OnThresholdUpdate?.Invoke(_address, type, threshold, arg3);
  69. };
  70. _copySettings.OnPressed += _ =>
  71. {
  72. SensorDataCopied?.Invoke(data);
  73. };
  74. }
  75. public void ChangeData(AtmosSensorData data)
  76. {
  77. SensorAddress.Title = $"{_address} : {data.AlarmState}";
  78. AlarmStateLabel.SetMarkup(Loc.GetString("air-alarm-ui-window-alarm-state-indicator",
  79. ("color", AirAlarmWindow.ColorForAlarm(data.AlarmState)),
  80. ("state", $"{data.AlarmState}")));
  81. PressureLabel.SetMarkup(Loc.GetString("air-alarm-ui-window-pressure-indicator",
  82. ("color", AirAlarmWindow.ColorForThreshold(data.Pressure, data.PressureThreshold)),
  83. ("pressure", $"{data.Pressure:0.##}")));
  84. TemperatureLabel.SetMarkup(Loc.GetString("air-alarm-ui-window-temperature-indicator",
  85. ("color", AirAlarmWindow.ColorForThreshold(data.Temperature, data.TemperatureThreshold)),
  86. ("tempC", $"{TemperatureHelpers.KelvinToCelsius(data.Temperature):0.#}"),
  87. ("temperature", $"{data.Temperature:0.##}")));
  88. foreach (var (gas, amount) in data.Gases)
  89. {
  90. if (!_gasLabels.TryGetValue(gas, out var label))
  91. {
  92. continue;
  93. }
  94. var fractionGas = amount / data.TotalMoles;
  95. label.SetMarkup(Loc.GetString("air-alarm-ui-gases-indicator",
  96. ("gas", $"{gas}"),
  97. ("color", AirAlarmWindow.ColorForThreshold(fractionGas, data.GasThresholds[gas])),
  98. ("amount", $"{amount:0.####}"),
  99. ("percentage", $"{(100 * fractionGas):0.##}")));
  100. }
  101. _pressureThreshold.UpdateThresholdData(data.PressureThreshold, data.Pressure);
  102. _temperatureThreshold.UpdateThresholdData(data.TemperatureThreshold, data.Temperature);
  103. foreach (var (gas, control) in _gasThresholds)
  104. {
  105. if (!data.GasThresholds.TryGetValue(gas, out var threshold))
  106. {
  107. continue;
  108. }
  109. control.UpdateThresholdData(threshold, data.Gases[gas] / data.TotalMoles);
  110. }
  111. }
  112. }