HandheldLightComponent.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Numerics;
  2. using Content.Shared.Light;
  3. using Content.Shared.Light.Components;
  4. using Robust.Client.Graphics;
  5. using Robust.Client.UserInterface;
  6. using Robust.Client.UserInterface.Controls;
  7. using Robust.Shared.Timing;
  8. using static Robust.Client.UserInterface.Controls.BoxContainer;
  9. namespace Content.Client.Light.Components;
  10. public sealed class HandheldLightStatus : Control
  11. {
  12. private const float TimerCycle = 1;
  13. private readonly HandheldLightComponent _parent;
  14. private readonly PanelContainer[] _sections = new PanelContainer[HandheldLightComponent.StatusLevels - 1];
  15. private float _timer;
  16. private static readonly StyleBoxFlat StyleBoxLit = new()
  17. {
  18. BackgroundColor = Color.LimeGreen
  19. };
  20. private static readonly StyleBoxFlat StyleBoxUnlit = new()
  21. {
  22. BackgroundColor = Color.Black
  23. };
  24. public HandheldLightStatus(HandheldLightComponent parent)
  25. {
  26. _parent = parent;
  27. var wrapper = new BoxContainer
  28. {
  29. Orientation = LayoutOrientation.Horizontal,
  30. SeparationOverride = 4,
  31. HorizontalAlignment = HAlignment.Center
  32. };
  33. AddChild(wrapper);
  34. for (var i = 0; i < _sections.Length; i++)
  35. {
  36. var panel = new PanelContainer {MinSize = new Vector2(20, 20)};
  37. wrapper.AddChild(panel);
  38. _sections[i] = panel;
  39. }
  40. }
  41. protected override void FrameUpdate(FrameEventArgs args)
  42. {
  43. base.FrameUpdate(args);
  44. _timer += args.DeltaSeconds;
  45. _timer %= TimerCycle;
  46. var level = _parent.Level;
  47. for (var i = 0; i < _sections.Length; i++)
  48. {
  49. if (i == 0)
  50. {
  51. if (level == 0 || level == null)
  52. {
  53. _sections[0].PanelOverride = StyleBoxUnlit;
  54. }
  55. else if (level == 1)
  56. {
  57. // Flash the last light.
  58. _sections[0].PanelOverride = _timer > TimerCycle / 2 ? StyleBoxLit : StyleBoxUnlit;
  59. }
  60. else
  61. {
  62. _sections[0].PanelOverride = StyleBoxLit;
  63. }
  64. continue;
  65. }
  66. _sections[i].PanelOverride = level >= i + 2 ? StyleBoxLit : StyleBoxUnlit;
  67. }
  68. }
  69. }