ActionAlertTooltip.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Content.Client.Stylesheets;
  2. using Robust.Client.UserInterface.Controls;
  3. using Robust.Shared.Timing;
  4. using Robust.Shared.Utility;
  5. using static Robust.Client.UserInterface.Controls.BoxContainer;
  6. namespace Content.Client.Actions.UI
  7. {
  8. /// <summary>
  9. /// Tooltip for actions or alerts because they are very similar.
  10. /// </summary>
  11. public sealed class ActionAlertTooltip : PanelContainer
  12. {
  13. private const float TooltipTextMaxWidth = 350;
  14. private readonly RichTextLabel _cooldownLabel;
  15. private readonly IGameTiming _gameTiming;
  16. /// <summary>
  17. /// Current cooldown displayed in this tooltip. Set to null to show no cooldown.
  18. /// </summary>
  19. public (TimeSpan Start, TimeSpan End)? Cooldown { get; set; }
  20. public ActionAlertTooltip(FormattedMessage name, FormattedMessage? desc, string? requires = null, FormattedMessage? charges = null)
  21. {
  22. _gameTiming = IoCManager.Resolve<IGameTiming>();
  23. SetOnlyStyleClass(StyleNano.StyleClassTooltipPanel);
  24. BoxContainer vbox;
  25. AddChild(vbox = new BoxContainer
  26. {
  27. Orientation = LayoutOrientation.Vertical,
  28. RectClipContent = true
  29. });
  30. var nameLabel = new RichTextLabel
  31. {
  32. MaxWidth = TooltipTextMaxWidth,
  33. StyleClasses = {StyleNano.StyleClassTooltipActionTitle}
  34. };
  35. nameLabel.SetMessage(name);
  36. vbox.AddChild(nameLabel);
  37. if (desc != null && !string.IsNullOrWhiteSpace(desc.ToString()))
  38. {
  39. var description = new RichTextLabel
  40. {
  41. MaxWidth = TooltipTextMaxWidth,
  42. StyleClasses = {StyleNano.StyleClassTooltipActionDescription}
  43. };
  44. description.SetMessage(desc);
  45. vbox.AddChild(description);
  46. }
  47. if (charges != null && !string.IsNullOrWhiteSpace(charges.ToString()))
  48. {
  49. var chargesLabel = new RichTextLabel
  50. {
  51. MaxWidth = TooltipTextMaxWidth,
  52. StyleClasses = { StyleNano.StyleClassTooltipActionCharges }
  53. };
  54. chargesLabel.SetMessage(charges);
  55. vbox.AddChild(chargesLabel);
  56. }
  57. vbox.AddChild(_cooldownLabel = new RichTextLabel
  58. {
  59. MaxWidth = TooltipTextMaxWidth,
  60. StyleClasses = {StyleNano.StyleClassTooltipActionCooldown},
  61. Visible = false
  62. });
  63. if (!string.IsNullOrWhiteSpace(requires))
  64. {
  65. var requiresLabel = new RichTextLabel
  66. {
  67. MaxWidth = TooltipTextMaxWidth,
  68. StyleClasses = {StyleNano.StyleClassTooltipActionRequirements}
  69. };
  70. if (!FormattedMessage.TryFromMarkup("[color=#635c5c]" + requires + "[/color]", out var markup))
  71. return;
  72. requiresLabel.SetMessage(markup);
  73. vbox.AddChild(requiresLabel);
  74. }
  75. }
  76. protected override void FrameUpdate(FrameEventArgs args)
  77. {
  78. base.FrameUpdate(args);
  79. if (!Cooldown.HasValue)
  80. {
  81. _cooldownLabel.Visible = false;
  82. return;
  83. }
  84. var timeLeft = Cooldown.Value.End - _gameTiming.CurTime;
  85. if (timeLeft > TimeSpan.Zero)
  86. {
  87. var duration = Cooldown.Value.End - Cooldown.Value.Start;
  88. if (!FormattedMessage.TryFromMarkup(Loc.GetString("ui-actionslot-duration", ("duration", (int)duration.TotalSeconds), ("timeLeft", (int)timeLeft.TotalSeconds + 1)), out var markup))
  89. return;
  90. _cooldownLabel.SetMessage(markup);
  91. _cooldownLabel.Visible = true;
  92. }
  93. else
  94. {
  95. _cooldownLabel.Visible = false;
  96. }
  97. }
  98. }
  99. }