PopupUIController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Numerics;
  2. using Content.Client.Gameplay;
  3. using Content.Shared.Popups;
  4. using Robust.Client.Graphics;
  5. using Robust.Client.ResourceManagement;
  6. using Robust.Client.UserInterface;
  7. using Robust.Client.UserInterface.Controllers;
  8. namespace Content.Client.Popups;
  9. /// <summary>
  10. /// Handles screens-space popups. World popups are handled via PopupOverlay.
  11. /// </summary>
  12. public sealed class PopupUIController : UIController, IOnStateEntered<GameplayState>, IOnStateExited<GameplayState>
  13. {
  14. [UISystemDependency] private readonly PopupSystem? _popup = default!;
  15. private Font _smallFont = default!;
  16. private Font _mediumFont = default!;
  17. private Font _largeFont = default!;
  18. private PopupRootControl? _popupControl;
  19. public override void Initialize()
  20. {
  21. base.Initialize();
  22. var cache = IoCManager.Resolve<IResourceCache>();
  23. _smallFont = new VectorFont(cache.GetResource<FontResource>("/Fonts/NotoSans/NotoSans-Italic.ttf"), 10);
  24. _mediumFont = new VectorFont(cache.GetResource<FontResource>("/Fonts/NotoSans/NotoSans-Italic.ttf"), 12);
  25. _largeFont = new VectorFont(cache.GetResource<FontResource>("/Fonts/NotoSans/NotoSans-BoldItalic.ttf"), 14);
  26. }
  27. public void OnStateEntered(GameplayState state)
  28. {
  29. _popupControl = new PopupRootControl(_popup, this);
  30. UIManager.RootControl.AddChild(_popupControl);
  31. }
  32. public void OnStateExited(GameplayState state)
  33. {
  34. if (_popupControl == null)
  35. return;
  36. UIManager.RootControl.RemoveChild(_popupControl);
  37. _popupControl = null;
  38. }
  39. public void DrawPopup(PopupSystem.PopupLabel popup, DrawingHandleScreen handle, Vector2 position, float scale)
  40. {
  41. var lifetime = PopupSystem.GetPopupLifetime(popup);
  42. // Keep alpha at 1 until TotalTime passes half its lifetime, then gradually decrease to 0.
  43. var alpha = MathF.Min(1f, 1f - MathF.Max(0f, popup.TotalTime - lifetime / 2) * 2 / lifetime);
  44. var updatedPosition = position - new Vector2(0f, MathF.Min(8f, 12f * (popup.TotalTime * popup.TotalTime + popup.TotalTime)));
  45. var font = _smallFont;
  46. var color = Color.White.WithAlpha(alpha);
  47. switch (popup.Type)
  48. {
  49. case PopupType.SmallCaution:
  50. color = Color.Red;
  51. break;
  52. case PopupType.Medium:
  53. font = _mediumFont;
  54. color = Color.LightGray;
  55. break;
  56. case PopupType.MediumCaution:
  57. font = _mediumFont;
  58. color = Color.Red;
  59. break;
  60. case PopupType.Large:
  61. font = _largeFont;
  62. color = Color.LightGray;
  63. break;
  64. case PopupType.LargeCaution:
  65. font = _largeFont;
  66. color = Color.Red;
  67. break;
  68. }
  69. var dimensions = handle.GetDimensions(font, popup.Text, scale);
  70. handle.DrawString(font, updatedPosition - dimensions / 2f, popup.Text, scale, color.WithAlpha(alpha));
  71. }
  72. /// <summary>
  73. /// Handles drawing all screen popups.
  74. /// </summary>
  75. private sealed class PopupRootControl : Control
  76. {
  77. private readonly PopupSystem? _popup;
  78. private readonly PopupUIController _controller;
  79. public PopupRootControl(PopupSystem? system, PopupUIController controller)
  80. {
  81. _popup = system;
  82. _controller = controller;
  83. }
  84. protected override void Draw(DrawingHandleScreen handle)
  85. {
  86. base.Draw(handle);
  87. if (_popup == null)
  88. return;
  89. // Different window
  90. var windowId = UserInterfaceManager.RootControl.Window.Id;
  91. foreach (var popup in _popup.CursorLabels)
  92. {
  93. if (popup.InitialPos.Window != windowId)
  94. continue;
  95. _controller.DrawPopup(popup, handle, popup.InitialPos.Position, UIScale);
  96. }
  97. }
  98. }
  99. }