IntercomMenu.xaml.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Threading.Channels;
  2. using Content.Client.UserInterface.Controls;
  3. using Content.Shared.Radio.Components;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.UserInterface.XAML;
  6. using Robust.Shared.Prototypes;
  7. using Robust.Shared.Utility;
  8. namespace Content.Client.Radio.Ui;
  9. [GenerateTypedNameReferences]
  10. public sealed partial class IntercomMenu : FancyWindow
  11. {
  12. [Dependency] private readonly IPrototypeManager _prototype = default!;
  13. public event Action<bool>? OnMicPressed;
  14. public event Action<bool>? OnSpeakerPressed;
  15. public event Action<string>? OnChannelSelected;
  16. private readonly List<string> _channels = new();
  17. public IntercomMenu()
  18. {
  19. RobustXamlLoader.Load(this);
  20. IoCManager.InjectDependencies(this);
  21. MicButton.OnPressed += args => OnMicPressed?.Invoke(args.Button.Pressed);
  22. SpeakerButton.OnPressed += args => OnSpeakerPressed?.Invoke(args.Button.Pressed);
  23. }
  24. public void Update(Entity<IntercomComponent> entity)
  25. {
  26. MicButton.Pressed = entity.Comp.MicrophoneEnabled;
  27. SpeakerButton.Pressed = entity.Comp.SpeakerEnabled;
  28. MicButton.Disabled = entity.Comp.SupportedChannels.Count == 0;
  29. SpeakerButton.Disabled = entity.Comp.SupportedChannels.Count == 0;
  30. ChannelOptions.Disabled = entity.Comp.SupportedChannels.Count == 0;
  31. ChannelOptions.Clear();
  32. _channels.Clear();
  33. for (var i = 0; i < entity.Comp.SupportedChannels.Count; i++)
  34. {
  35. var channel = entity.Comp.SupportedChannels[i];
  36. if (!_prototype.TryIndex(channel, out var prototype))
  37. continue;
  38. _channels.Add(channel);
  39. ChannelOptions.AddItem(Loc.GetString(prototype.Name), i);
  40. if (channel == entity.Comp.CurrentChannel)
  41. ChannelOptions.Select(i);
  42. }
  43. if (entity.Comp.SupportedChannels.Count == 0)
  44. {
  45. ChannelOptions.AddItem(Loc.GetString("intercom-options-none"), 0);
  46. ChannelOptions.Select(0);
  47. }
  48. ChannelOptions.OnItemSelected += args =>
  49. {
  50. if (!_channels.TryGetValue(args.Id, out var proto))
  51. return;
  52. ChannelOptions.SelectId(args.Id);
  53. OnChannelSelected?.Invoke(proto);
  54. };
  55. }
  56. }