PressureBar.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Content.Shared.Disposal;
  2. using Robust.Client.Graphics;
  3. using Robust.Client.UserInterface.Controls;
  4. using Robust.Shared.Timing;
  5. namespace Content.Client.Disposal.UI;
  6. public sealed class PressureBar : ProgressBar
  7. {
  8. public bool UpdatePressure(TimeSpan fullTime)
  9. {
  10. var currentTime = IoCManager.Resolve<IGameTiming>().CurTime;
  11. var pressure = (float) Math.Min(1.0f, 1.0f - (fullTime.TotalSeconds - currentTime.TotalSeconds) * SharedDisposalUnitSystem.PressurePerSecond);
  12. UpdatePressureBar(pressure);
  13. return pressure >= 1.0f;
  14. }
  15. private void UpdatePressureBar(float pressure)
  16. {
  17. Value = pressure;
  18. var normalized = pressure / MaxValue;
  19. const float leftHue = 0.0f; // Red
  20. const float middleHue = 0.066f; // Orange
  21. const float rightHue = 0.33f; // Green
  22. const float saturation = 1.0f; // Uniform saturation
  23. const float value = 0.8f; // Uniform value / brightness
  24. const float alpha = 1.0f; // Uniform alpha
  25. // These should add up to 1.0 or your transition won't be smooth
  26. const float leftSideSize = 0.5f; // Fraction of _chargeBar lerped from leftHue to middleHue
  27. const float rightSideSize = 0.5f; // Fraction of _chargeBar lerped from middleHue to rightHue
  28. float finalHue;
  29. if (normalized <= leftSideSize)
  30. {
  31. normalized /= leftSideSize; // Adjust range to 0.0 to 1.0
  32. finalHue = MathHelper.Lerp(leftHue, middleHue, normalized);
  33. }
  34. else
  35. {
  36. normalized = (normalized - leftSideSize) / rightSideSize; // Adjust range to 0.0 to 1.0.
  37. finalHue = MathHelper.Lerp(middleHue, rightHue, normalized);
  38. }
  39. // Check if null first to avoid repeatedly creating this.
  40. ForegroundStyleBoxOverride ??= new StyleBoxFlat();
  41. var foregroundStyleBoxOverride = (StyleBoxFlat) ForegroundStyleBoxOverride;
  42. foregroundStyleBoxOverride.BackgroundColor =
  43. Color.FromHsv(new Vector4(finalHue, saturation, value, alpha));
  44. }
  45. }