ChannelSelectorButton.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Numerics;
  2. using Content.Shared.Chat;
  3. namespace Content.Client.UserInterface.Systems.Chat.Controls;
  4. public sealed class ChannelSelectorButton : ChatPopupButton<ChannelSelectorPopup>
  5. {
  6. public event Action<ChatSelectChannel>? OnChannelSelect;
  7. public ChatSelectChannel SelectedChannel { get; private set; }
  8. private const int SelectorDropdownOffset = 38;
  9. public ChannelSelectorButton()
  10. {
  11. Name = "ChannelSelector";
  12. Popup.Selected += OnChannelSelected;
  13. if (Popup.FirstChannel is { } firstSelector)
  14. {
  15. Select(firstSelector);
  16. }
  17. }
  18. protected override UIBox2 GetPopupPosition()
  19. {
  20. var globalLeft = GlobalPosition.X;
  21. var globalBot = GlobalPosition.Y + Height;
  22. return UIBox2.FromDimensions(
  23. new Vector2(globalLeft, globalBot),
  24. new Vector2(SizeBox.Width, SelectorDropdownOffset));
  25. }
  26. private void OnChannelSelected(ChatSelectChannel channel)
  27. {
  28. Select(channel);
  29. }
  30. public void Select(ChatSelectChannel channel)
  31. {
  32. if (Popup.Visible)
  33. {
  34. Popup.Close();
  35. }
  36. if (SelectedChannel == channel)
  37. return;
  38. SelectedChannel = channel;
  39. OnChannelSelect?.Invoke(channel);
  40. }
  41. public static string ChannelSelectorName(ChatSelectChannel channel)
  42. {
  43. return Loc.GetString($"hud-chatbox-select-channel-{channel}");
  44. }
  45. public Color ChannelSelectColor(ChatSelectChannel channel)
  46. {
  47. return channel switch
  48. {
  49. ChatSelectChannel.Radio => Color.LimeGreen,
  50. ChatSelectChannel.LOOC => Color.MediumTurquoise,
  51. ChatSelectChannel.OOC => Color.LightSkyBlue,
  52. ChatSelectChannel.Dead => Color.MediumPurple,
  53. ChatSelectChannel.Admin => Color.HotPink,
  54. _ => Color.DarkGray
  55. };
  56. }
  57. public void UpdateChannelSelectButton(ChatSelectChannel channel, Shared.Radio.RadioChannelPrototype? radio)
  58. {
  59. Text = radio != null ? Loc.GetString(radio.Name) : ChannelSelectorName(channel);
  60. Modulate = radio?.Color ?? ChannelSelectColor(channel);
  61. }
  62. }