ApcMenu.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Robust.Client.AutoGenerated;
  2. using Robust.Client.UserInterface.XAML;
  3. using Robust.Client.GameObjects;
  4. using Robust.Shared.IoC;
  5. using System;
  6. using Content.Client.Stylesheets;
  7. using Content.Shared.APC;
  8. using Robust.Client.Graphics;
  9. using Robust.Client.UserInterface.Controls;
  10. using Robust.Shared.GameObjects;
  11. using Robust.Shared.Localization;
  12. using Robust.Shared.Maths;
  13. using FancyWindow = Content.Client.UserInterface.Controls.FancyWindow;
  14. namespace Content.Client.Power.APC.UI
  15. {
  16. [GenerateTypedNameReferences]
  17. public sealed partial class ApcMenu : FancyWindow
  18. {
  19. public event Action? OnBreaker;
  20. public ApcMenu()
  21. {
  22. IoCManager.InjectDependencies(this);
  23. RobustXamlLoader.Load(this);
  24. BreakerButton.OnPressed += _ => OnBreaker?.Invoke();
  25. }
  26. public void SetEntity(EntityUid entity)
  27. {
  28. EntityView.SetEntity(entity);
  29. }
  30. public void UpdateState(BoundUserInterfaceState state)
  31. {
  32. var castState = (ApcBoundInterfaceState) state;
  33. if (!BreakerButton.Disabled)
  34. {
  35. BreakerButton.Pressed = castState.MainBreaker;
  36. }
  37. if (PowerLabel != null)
  38. {
  39. PowerLabel.Text = castState.Power + " W";
  40. }
  41. if (ExternalPowerStateLabel != null)
  42. {
  43. switch (castState.ApcExternalPower)
  44. {
  45. case ApcExternalPowerState.None:
  46. ExternalPowerStateLabel.Text = Loc.GetString("apc-menu-power-state-none");
  47. ExternalPowerStateLabel.SetOnlyStyleClass(StyleNano.StyleClassPowerStateNone);
  48. break;
  49. case ApcExternalPowerState.Low:
  50. ExternalPowerStateLabel.Text = Loc.GetString("apc-menu-power-state-low");
  51. ExternalPowerStateLabel.SetOnlyStyleClass(StyleNano.StyleClassPowerStateLow);
  52. break;
  53. case ApcExternalPowerState.Good:
  54. ExternalPowerStateLabel.Text = Loc.GetString("apc-menu-power-state-good");
  55. ExternalPowerStateLabel.SetOnlyStyleClass(StyleNano.StyleClassPowerStateGood);
  56. break;
  57. default:
  58. throw new ArgumentOutOfRangeException();
  59. }
  60. }
  61. if (ChargeBar != null)
  62. {
  63. ChargeBar.Value = castState.Charge;
  64. UpdateChargeBarColor(castState.Charge);
  65. var chargePercentage = (castState.Charge / ChargeBar.MaxValue);
  66. ChargePercentage.Text = Loc.GetString("apc-menu-charge-label",("percent", chargePercentage.ToString("P0")));
  67. }
  68. }
  69. public void SetAccessEnabled(bool hasAccess)
  70. {
  71. if(hasAccess)
  72. {
  73. BreakerButton.Disabled = false;
  74. BreakerButton.ToolTip = null;
  75. }
  76. else
  77. {
  78. BreakerButton.Disabled = true;
  79. BreakerButton.ToolTip = Loc.GetString("apc-component-insufficient-access");
  80. }
  81. }
  82. private void UpdateChargeBarColor(float charge)
  83. {
  84. if (ChargeBar == null)
  85. {
  86. return;
  87. }
  88. var normalizedCharge = charge / ChargeBar.MaxValue;
  89. const float leftHue = 0.0f; // Red
  90. const float middleHue = 0.066f; // Orange
  91. const float rightHue = 0.33f; // Green
  92. const float saturation = 1.0f; // Uniform saturation
  93. const float value = 0.8f; // Uniform value / brightness
  94. const float alpha = 1.0f; // Uniform alpha
  95. // These should add up to 1.0 or your transition won't be smooth
  96. const float leftSideSize = 0.5f; // Fraction of ChargeBar lerped from leftHue to middleHue
  97. const float rightSideSize = 0.5f; // Fraction of ChargeBar lerped from middleHue to rightHue
  98. float finalHue;
  99. if (normalizedCharge <= leftSideSize)
  100. {
  101. normalizedCharge /= leftSideSize; // Adjust range to 0.0 to 1.0
  102. finalHue = MathHelper.Lerp(leftHue, middleHue, normalizedCharge);
  103. }
  104. else
  105. {
  106. normalizedCharge = (normalizedCharge - leftSideSize) / rightSideSize; // Adjust range to 0.0 to 1.0.
  107. finalHue = MathHelper.Lerp(middleHue, rightHue, normalizedCharge);
  108. }
  109. // Check if null first to avoid repeatedly creating this.
  110. ChargeBar.ForegroundStyleBoxOverride ??= new StyleBoxFlat();
  111. var foregroundStyleBoxOverride = (StyleBoxFlat) ChargeBar.ForegroundStyleBoxOverride;
  112. foregroundStyleBoxOverride.BackgroundColor =
  113. Color.FromHsv(new Vector4(finalHue, saturation, value, alpha));
  114. }
  115. }
  116. }