ThresholdBoundControl.xaml.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Content.Shared.Atmos.Monitor;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.UserInterface.Controls;
  4. using Robust.Client.UserInterface.XAML;
  5. namespace Content.Client.Atmos.Monitor.UI.Widgets;
  6. [GenerateTypedNameReferences]
  7. public sealed partial class ThresholdBoundControl : BoxContainer
  8. {
  9. // raw values to use in thresholds, prefer these
  10. // over directly setting Modified(Value/LastValue)
  11. // when working with the FloatSpinBox
  12. private float _value;
  13. // convenience thing for getting multiplied values
  14. // and also setting value to a usable value
  15. private float ScaledValue
  16. {
  17. get => _value * _uiValueScale;
  18. set => _value = value / _uiValueScale;
  19. }
  20. private float _uiValueScale;
  21. public event Action? OnValidBoundChanged;
  22. public Action<float>? OnBoundChanged;
  23. public Action<bool>? OnBoundEnabled;
  24. public void SetValue(float value)
  25. {
  26. _value = value;
  27. CSpinner.Value = ScaledValue;
  28. }
  29. public void SetEnabled(bool enabled)
  30. {
  31. CBoundEnabled.Pressed = enabled;
  32. if (enabled)
  33. {
  34. CBoundLabel.RemoveStyleClass("Disabled");
  35. }
  36. else
  37. {
  38. CBoundLabel.SetOnlyStyleClass("Disabled");
  39. }
  40. }
  41. public void SetWarningState(AtmosAlarmType alarm)
  42. {
  43. if(alarm == AtmosAlarmType.Normal)
  44. {
  45. CBoundLabel.FontColorOverride = null;
  46. }
  47. else
  48. {
  49. CBoundLabel.FontColorOverride = AirAlarmWindow.ColorForAlarm(alarm);
  50. }
  51. }
  52. public ThresholdBoundControl(string controlLabel, float value, float uiValueScale = 1)
  53. {
  54. RobustXamlLoader.Load(this);
  55. _uiValueScale = uiValueScale > 0 ? uiValueScale : 1;
  56. _value = value;
  57. CBoundLabel.Text = controlLabel;
  58. CSpinner.Value = ScaledValue;
  59. CSpinner.OnValueChanged += SpinnerValueChanged;
  60. CBoundEnabled.OnToggled += CheckboxToggled;
  61. }
  62. private void SpinnerValueChanged(FloatSpinBox.FloatSpinBoxEventArgs args)
  63. {
  64. // ensure that the value in the spinbox is transformed
  65. ScaledValue = args.Value;
  66. // set the value in the scope above
  67. OnBoundChanged!(_value);
  68. OnValidBoundChanged!.Invoke();
  69. }
  70. private void CheckboxToggled(BaseButton.ButtonToggledEventArgs args)
  71. {
  72. OnBoundEnabled!(args.Pressed);
  73. OnValidBoundChanged!.Invoke();
  74. }
  75. }