1
0

CooldownGraphic.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Robust.Client.Graphics;
  2. using Robust.Client.UserInterface;
  3. using Robust.Shared.Prototypes;
  4. using Robust.Shared.Timing;
  5. namespace Content.Client.Cooldown
  6. {
  7. public sealed class CooldownGraphic : Control
  8. {
  9. [Dependency] private readonly IGameTiming _gameTiming = default!;
  10. [Dependency] private readonly IPrototypeManager _protoMan = default!;
  11. private readonly ShaderInstance _shader;
  12. public CooldownGraphic()
  13. {
  14. IoCManager.InjectDependencies(this);
  15. _shader = _protoMan.Index<ShaderPrototype>("CooldownAnimation").InstanceUnique();
  16. }
  17. /// <summary>
  18. /// Progress of the cooldown animation.
  19. /// Possible values range from 1 to -1, where 1 to 0 is a depleting circle animation and 0 to -1 is a blink animation.
  20. /// </summary>
  21. public float Progress { get; set; }
  22. protected override void Draw(DrawingHandleScreen handle)
  23. {
  24. Span<float> x = new float[10];
  25. Color color;
  26. var lerp = 1f - MathF.Abs(Progress); // for future bikeshedding purposes
  27. if (Progress >= 0f)
  28. {
  29. var hue = (5f / 18f) * lerp;
  30. color = Color.FromHsv((hue, 0.75f, 0.75f, 0.50f));
  31. }
  32. else
  33. {
  34. var alpha = MathHelper.Clamp(0.5f * lerp, 0f, 0.5f);
  35. color = new Color(1f, 1f, 1f, alpha);
  36. }
  37. _shader.SetParameter("progress", Progress);
  38. handle.UseShader(_shader);
  39. handle.DrawRect(PixelSizeBox, color);
  40. handle.UseShader(null);
  41. }
  42. public void FromTime(TimeSpan start, TimeSpan end)
  43. {
  44. var duration = end - start;
  45. var curTime = _gameTiming.CurTime;
  46. var length = duration.TotalSeconds;
  47. var progress = (curTime - start).TotalSeconds / length;
  48. var ratio = (progress <= 1 ? (1 - progress) : (curTime - end).TotalSeconds * -5);
  49. Progress = MathHelper.Clamp((float) ratio, -1, 1);
  50. Visible = ratio > -1f;
  51. }
  52. }
  53. }