ThresholdControl.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Content.Shared.Atmos;
  2. using Content.Shared.Atmos.Monitor;
  3. using Robust.Client.AutoGenerated;
  4. using Robust.Client.UserInterface.Controls;
  5. using Robust.Client.UserInterface.XAML;
  6. // holy FUCK
  7. // this technically works because some of this you can *not* do in XAML but holy FUCK
  8. namespace Content.Client.Atmos.Monitor.UI.Widgets;
  9. [GenerateTypedNameReferences]
  10. public sealed partial class ThresholdControl : BoxContainer
  11. {
  12. private AtmosAlarmThreshold _threshold;
  13. private AtmosMonitorThresholdType _type;
  14. private Gas? _gas;
  15. public event Action<AtmosMonitorThresholdType, AtmosAlarmThreshold, Gas?>? ThresholdDataChanged;
  16. private CollapsibleHeading _name => CName;
  17. private CheckBox _enabled => CEnabled;
  18. private BoxContainer _dangerBounds => CDangerBounds;
  19. private BoxContainer _warningBounds => CWarningBounds;
  20. private ThresholdBoundControl _upperBoundControl;
  21. private ThresholdBoundControl _lowerBoundControl;
  22. private ThresholdBoundControl _upperWarningBoundControl;
  23. private ThresholdBoundControl _lowerWarningBoundControl;
  24. // i have played myself by making threshold values nullable to
  25. // indicate validity/disabled status, with several layers of side effect
  26. // dependent on the other three values when you change one :HECK:
  27. public ThresholdControl(string name, AtmosAlarmThreshold threshold, AtmosMonitorThresholdType type, Gas? gas = null, float modifier = 1)
  28. {
  29. RobustXamlLoader.Load(this);
  30. _threshold = threshold;
  31. _type = type;
  32. _gas = gas;
  33. _name.Title = name;
  34. // i miss rust macros
  35. _upperBoundControl = new ThresholdBoundControl(LabelForBound("upper-bound"), _threshold.UpperBound.Value, modifier);
  36. _upperBoundControl.OnBoundChanged += (value) =>
  37. {
  38. _threshold.SetLimit(AtmosMonitorLimitType.UpperDanger, value);
  39. };
  40. _upperBoundControl.OnBoundEnabled += (isEnabled) =>
  41. {
  42. _threshold.SetEnabled(AtmosMonitorLimitType.UpperDanger, isEnabled);
  43. };
  44. _upperBoundControl.OnValidBoundChanged += () =>
  45. {
  46. ThresholdDataChanged!.Invoke(_type, _threshold, _gas);
  47. };
  48. _dangerBounds.AddChild(_upperBoundControl);
  49. _lowerBoundControl = new ThresholdBoundControl(LabelForBound("lower-bound"), _threshold.LowerBound.Value, modifier);
  50. _lowerBoundControl.OnBoundChanged += value =>
  51. {
  52. _threshold.SetLimit(AtmosMonitorLimitType.LowerDanger, value);
  53. };
  54. _lowerBoundControl.OnBoundEnabled += (isEnabled) =>
  55. {
  56. _threshold.SetEnabled(AtmosMonitorLimitType.LowerDanger, isEnabled);
  57. };
  58. _lowerBoundControl.OnValidBoundChanged += () =>
  59. {
  60. ThresholdDataChanged!.Invoke(_type, _threshold, _gas);
  61. };
  62. _dangerBounds.AddChild(_lowerBoundControl);
  63. _upperWarningBoundControl = new ThresholdBoundControl(LabelForBound("upper-warning-bound"), _threshold.UpperWarningBound.Value, modifier);
  64. _upperWarningBoundControl.OnBoundChanged += value =>
  65. {
  66. _threshold.SetLimit(AtmosMonitorLimitType.UpperWarning, value);
  67. };
  68. _upperWarningBoundControl.OnBoundEnabled += (isEnabled) =>
  69. {
  70. _threshold.SetEnabled(AtmosMonitorLimitType.UpperWarning, isEnabled);
  71. };
  72. _upperWarningBoundControl.OnValidBoundChanged += () =>
  73. {
  74. ThresholdDataChanged!.Invoke(_type, _threshold, _gas);
  75. };
  76. _warningBounds.AddChild(_upperWarningBoundControl);
  77. _lowerWarningBoundControl = new ThresholdBoundControl(LabelForBound("lower-warning-bound"), _threshold.LowerWarningBound.Value, modifier);
  78. _lowerWarningBoundControl.OnBoundChanged += value =>
  79. {
  80. _threshold.SetLimit(AtmosMonitorLimitType.LowerWarning, value);
  81. };
  82. _lowerWarningBoundControl.OnBoundEnabled += (isEnabled) =>
  83. {
  84. _threshold.SetEnabled(AtmosMonitorLimitType.LowerWarning, isEnabled);
  85. };
  86. _lowerWarningBoundControl.OnValidBoundChanged += () =>
  87. {
  88. ThresholdDataChanged!.Invoke(_type, _threshold, _gas);
  89. };
  90. _warningBounds.AddChild(_lowerWarningBoundControl);
  91. _enabled.OnToggled += args =>
  92. {
  93. _threshold.Ignore = !args.Pressed;
  94. ThresholdDataChanged!.Invoke(_type, _threshold, _gas);
  95. };
  96. _enabled.Pressed = !_threshold.Ignore;
  97. }
  98. private string LabelForBound(string boundType) //<todo.eoin Replace this with enums
  99. {
  100. return Loc.GetString($"air-alarm-ui-thresholds-{boundType}");
  101. }
  102. public void UpdateThresholdData(AtmosAlarmThreshold threshold, float currentAmount)
  103. {
  104. threshold.CheckThreshold(currentAmount, out var alarm, out var bound);
  105. var upperDangerState = AtmosAlarmType.Normal;
  106. var lowerDangerState = AtmosAlarmType.Normal;
  107. var upperWarningState = AtmosAlarmType.Normal;
  108. var lowerWarningState = AtmosAlarmType.Normal;
  109. switch (alarm)
  110. {
  111. case AtmosAlarmType.Danger:
  112. {
  113. if (bound == AtmosMonitorThresholdBound.Upper)
  114. upperDangerState = alarm;
  115. else
  116. lowerDangerState = alarm;
  117. break;
  118. }
  119. case AtmosAlarmType.Warning:
  120. {
  121. if (bound == AtmosMonitorThresholdBound.Upper)
  122. upperWarningState = alarm;
  123. else
  124. lowerWarningState = alarm;
  125. break;
  126. }
  127. }
  128. _upperBoundControl.SetValue(threshold.UpperBound.Value);
  129. _upperBoundControl.SetEnabled(threshold.UpperBound.Enabled);
  130. _upperBoundControl.SetWarningState(upperDangerState);
  131. _lowerBoundControl.SetValue(threshold.LowerBound.Value);
  132. _lowerBoundControl.SetEnabled(threshold.LowerBound.Enabled);
  133. _lowerBoundControl.SetWarningState(lowerDangerState);
  134. _upperWarningBoundControl.SetValue(threshold.UpperWarningBound.Value);
  135. _upperWarningBoundControl.SetEnabled(threshold.UpperWarningBound.Enabled);
  136. _upperWarningBoundControl.SetWarningState(upperWarningState);
  137. _lowerWarningBoundControl.SetValue(threshold.LowerWarningBound.Value);
  138. _lowerWarningBoundControl.SetEnabled(threshold.LowerWarningBound.Enabled);
  139. _lowerWarningBoundControl.SetWarningState(lowerWarningState);
  140. _enabled.Pressed = !threshold.Ignore;
  141. }
  142. }