CommunicationsConsoleMenu.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Globalization;
  2. using Content.Client.UserInterface.Controls;
  3. using Content.Shared.CCVar;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.UserInterface.XAML;
  6. using Robust.Shared.Configuration;
  7. using Robust.Shared.Timing;
  8. using Robust.Shared.Utility;
  9. namespace Content.Client.Communications.UI
  10. {
  11. [GenerateTypedNameReferences]
  12. public sealed partial class CommunicationsConsoleMenu : FancyWindow
  13. {
  14. [Dependency] private readonly IConfigurationManager _cfg = default!;
  15. [Dependency] private readonly IGameTiming _timing = default!;
  16. [Dependency] private readonly ILocalizationManager _loc = default!;
  17. public bool CanAnnounce;
  18. public bool CanBroadcast;
  19. public bool CanCall;
  20. public bool AlertLevelSelectable;
  21. public bool CountdownStarted;
  22. public string CurrentLevel = string.Empty;
  23. public TimeSpan? CountdownEnd;
  24. public event Action? OnEmergencyLevel;
  25. public event Action<string>? OnAlertLevel;
  26. public event Action<string>? OnAnnounce;
  27. public event Action<string>? OnBroadcast;
  28. public CommunicationsConsoleMenu()
  29. {
  30. IoCManager.InjectDependencies(this);
  31. RobustXamlLoader.Load(this);
  32. MessageInput.Placeholder = new Rope.Leaf(_loc.GetString("comms-console-menu-announcement-placeholder"));
  33. var maxAnnounceLength = _cfg.GetCVar(CCVars.ChatMaxAnnouncementLength);
  34. MessageInput.OnTextChanged += (args) =>
  35. {
  36. if (args.Control.TextLength > maxAnnounceLength)
  37. {
  38. AnnounceButton.Disabled = true;
  39. AnnounceButton.ToolTip = Loc.GetString("comms-console-message-too-long");
  40. }
  41. else
  42. {
  43. AnnounceButton.Disabled = !CanAnnounce;
  44. AnnounceButton.ToolTip = null;
  45. }
  46. };
  47. AnnounceButton.OnPressed += _ => OnAnnounce?.Invoke(Rope.Collapse(MessageInput.TextRope));
  48. AnnounceButton.Disabled = !CanAnnounce;
  49. BroadcastButton.OnPressed += _ => OnBroadcast?.Invoke(Rope.Collapse(MessageInput.TextRope));
  50. BroadcastButton.Disabled = !CanBroadcast;
  51. AlertLevelButton.OnItemSelected += args =>
  52. {
  53. var metadata = AlertLevelButton.GetItemMetadata(args.Id);
  54. if (metadata != null && metadata is string cast)
  55. {
  56. OnAlertLevel?.Invoke(cast);
  57. }
  58. };
  59. AlertLevelButton.Disabled = !AlertLevelSelectable;
  60. EmergencyShuttleButton.OnPressed += _ => OnEmergencyLevel?.Invoke();
  61. EmergencyShuttleButton.Disabled = !CanCall;
  62. }
  63. protected override void FrameUpdate(FrameEventArgs args)
  64. {
  65. base.FrameUpdate(args);
  66. UpdateCountdown();
  67. }
  68. // The current alert could make levels unselectable, so we need to ensure that the UI reacts properly.
  69. // If the current alert is unselectable, the only item in the alerts list will be
  70. // the current alert. Otherwise, it will be the list of alerts, with the current alert
  71. // selected.
  72. public void UpdateAlertLevels(List<string>? alerts, string currentAlert)
  73. {
  74. AlertLevelButton.Clear();
  75. if (alerts == null)
  76. {
  77. var name = currentAlert;
  78. if (Loc.TryGetString($"alert-level-{currentAlert}", out var locName))
  79. {
  80. name = locName;
  81. }
  82. AlertLevelButton.AddItem(name);
  83. AlertLevelButton.SetItemMetadata(AlertLevelButton.ItemCount - 1, currentAlert);
  84. }
  85. else
  86. {
  87. foreach (var alert in alerts)
  88. {
  89. var name = alert;
  90. if (Loc.TryGetString($"alert-level-{alert}", out var locName))
  91. {
  92. name = locName;
  93. }
  94. AlertLevelButton.AddItem(name);
  95. AlertLevelButton.SetItemMetadata(AlertLevelButton.ItemCount - 1, alert);
  96. if (alert == currentAlert)
  97. {
  98. AlertLevelButton.Select(AlertLevelButton.ItemCount - 1);
  99. }
  100. }
  101. }
  102. }
  103. public void UpdateCountdown()
  104. {
  105. if (!CountdownStarted)
  106. {
  107. CountdownLabel.SetMessage(string.Empty);
  108. EmergencyShuttleButton.Text = Loc.GetString("comms-console-menu-call-shuttle");
  109. return;
  110. }
  111. var diff = MathHelper.Max((CountdownEnd - _timing.CurTime) ?? TimeSpan.Zero, TimeSpan.Zero);
  112. EmergencyShuttleButton.Text = Loc.GetString("comms-console-menu-recall-shuttle");
  113. var infoText = Loc.GetString($"comms-console-menu-time-remaining",
  114. ("time", diff.ToString(@"hh\:mm\:ss", CultureInfo.CurrentCulture)));
  115. CountdownLabel.SetMessage(infoText);
  116. }
  117. }
  118. }