ResearchClientServerSelectionMenu.xaml.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Robust.Client.AutoGenerated;
  2. using Robust.Client.UserInterface.Controls;
  3. using Robust.Client.UserInterface.CustomControls;
  4. using Robust.Client.UserInterface.XAML;
  5. namespace Content.Client.Research.UI
  6. {
  7. [GenerateTypedNameReferences]
  8. public sealed partial class ResearchClientServerSelectionMenu : DefaultWindow
  9. {
  10. private int _serverCount;
  11. private string[] _serverNames = Array.Empty<string>();
  12. private int[] _serverIds = Array.Empty<int>();
  13. private int _selectedServerId = -1;
  14. public event Action<int>? OnServerSelected;
  15. public event Action? OnServerDeselected;
  16. public ResearchClientServerSelectionMenu()
  17. {
  18. RobustXamlLoader.Load(this);
  19. IoCManager.InjectDependencies(this);
  20. Servers.OnItemSelected += OnItemSelected;
  21. Servers.OnItemDeselected += OnItemDeselected;
  22. }
  23. public void OnItemSelected(ItemList.ItemListSelectedEventArgs itemListSelectedEventArgs)
  24. {
  25. OnServerSelected?.Invoke(_serverIds[itemListSelectedEventArgs.ItemIndex]);
  26. }
  27. public void OnItemDeselected(ItemList.ItemListDeselectedEventArgs itemListDeselectedEventArgs)
  28. {
  29. OnServerDeselected?.Invoke();
  30. }
  31. public void Populate(int serverCount, string[] serverNames, int[] serverIds, int selectedServerId)
  32. {
  33. _serverCount = serverCount;
  34. _serverNames = serverNames;
  35. _serverIds = serverIds;
  36. _selectedServerId = selectedServerId;
  37. // Disable so we can select the new selected server without triggering a new sync request.
  38. Servers.OnItemSelected -= OnItemSelected;
  39. Servers.OnItemDeselected -= OnItemDeselected;
  40. Servers.Clear();
  41. for (var i = 0; i < _serverCount; i++)
  42. {
  43. var id = _serverIds[i];
  44. Servers.AddItem(Loc.GetString("research-client-server-selection-menu-server-entry-text", ("id", id), ("serverName", _serverNames[i])));
  45. if (id == _selectedServerId)
  46. {
  47. Servers[i].Selected = true;
  48. }
  49. }
  50. Servers.OnItemSelected += OnItemSelected;
  51. Servers.OnItemDeselected += OnItemDeselected;
  52. }
  53. }
  54. }