LawDisplay.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Content.Client.Chat.Managers;
  2. using Content.Client.Message;
  3. using Content.Shared.Chat;
  4. using Content.Shared.Radio;
  5. using Content.Shared.Silicons.Laws;
  6. using Content.Shared.Speech;
  7. using Robust.Client.AutoGenerated;
  8. using Robust.Client.UserInterface;
  9. using Robust.Client.UserInterface.Controls;
  10. using Robust.Client.UserInterface.XAML;
  11. using Robust.Shared.Prototypes;
  12. using Robust.Shared.Timing;
  13. using Robust.Shared.Utility;
  14. namespace Content.Client.Silicons.Laws.Ui;
  15. [GenerateTypedNameReferences]
  16. public sealed partial class LawDisplay : Control
  17. {
  18. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  19. [Dependency] private readonly IChatManager _chatManager = default!;
  20. [Dependency] private readonly IGameTiming _timing = default!;
  21. [Dependency] private readonly EntityManager _entityManager = default!;
  22. private static readonly TimeSpan PressCooldown = TimeSpan.FromSeconds(3);
  23. private readonly Dictionary<Button, TimeSpan> _nextAllowedPress = new();
  24. public LawDisplay(EntityUid uid, SiliconLaw law, HashSet<string>? radioChannels)
  25. {
  26. RobustXamlLoader.Load(this);
  27. IoCManager.InjectDependencies(this);
  28. var identifier = law.LawIdentifierOverride ?? $"{law.Order}";
  29. var lawIdentifier = Loc.GetString("laws-ui-law-header", ("id", identifier));
  30. var lawDescription = Loc.GetString(law.LawString);
  31. var lawIdentifierPlaintext = FormattedMessage.RemoveMarkupPermissive(lawIdentifier);
  32. var lawDescriptionPlaintext = FormattedMessage.RemoveMarkupPermissive(lawDescription);
  33. LawNumberLabel.SetMarkup(lawIdentifier);
  34. LawLabel.SetMessage(lawDescription);
  35. // If you can't talk, you can't state your laws...
  36. if (!_entityManager.TryGetComponent<SpeechComponent>(uid, out var speech) || speech.SpeechSounds is null)
  37. return;
  38. var localButton = new Button
  39. {
  40. Text = Loc.GetString("hud-chatbox-select-channel-Local"),
  41. Modulate = Color.DarkGray,
  42. StyleClasses = { "chatSelectorOptionButton" },
  43. MinHeight = 35,
  44. MinWidth = 75,
  45. };
  46. _nextAllowedPress[localButton] = TimeSpan.Zero;
  47. localButton.OnPressed += _ =>
  48. {
  49. _chatManager.SendMessage($"{lawIdentifierPlaintext}: {lawDescriptionPlaintext}", ChatSelectChannel.Local);
  50. _nextAllowedPress[localButton] = _timing.CurTime + PressCooldown;
  51. };
  52. LawAnnouncementButtons.AddChild(localButton);
  53. if (radioChannels == null)
  54. return;
  55. foreach (var radioChannel in radioChannels)
  56. {
  57. if (!_prototypeManager.TryIndex<RadioChannelPrototype>(radioChannel, out var radioChannelProto))
  58. continue;
  59. var radioChannelButton = new Button
  60. {
  61. Text = Loc.GetString(radioChannelProto.Name),
  62. Modulate = radioChannelProto.Color,
  63. StyleClasses = { "chatSelectorOptionButton" },
  64. MinHeight = 35,
  65. MinWidth = 75,
  66. };
  67. _nextAllowedPress[radioChannelButton] = TimeSpan.Zero;
  68. radioChannelButton.OnPressed += _ =>
  69. {
  70. switch (radioChannel)
  71. {
  72. case SharedChatSystem.CommonChannel:
  73. _chatManager.SendMessage($"{SharedChatSystem.RadioCommonPrefix} {lawIdentifierPlaintext}: {lawDescriptionPlaintext}", ChatSelectChannel.Radio); break;
  74. default:
  75. _chatManager.SendMessage($"{SharedChatSystem.RadioChannelPrefix}{radioChannelProto.KeyCode} {lawIdentifierPlaintext}: {lawDescriptionPlaintext}", ChatSelectChannel.Radio); break;
  76. }
  77. _nextAllowedPress[radioChannelButton] = _timing.CurTime + PressCooldown;
  78. };
  79. LawAnnouncementButtons.AddChild(radioChannelButton);
  80. }
  81. }
  82. protected override void FrameUpdate(FrameEventArgs args)
  83. {
  84. base.FrameUpdate(args);
  85. var curTime = _timing.CurTime;
  86. foreach (var (button, nextPress) in _nextAllowedPress)
  87. {
  88. button.Disabled = curTime < nextPress;
  89. }
  90. }
  91. }