GhostRolesWindow.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Linq;
  2. using Content.Shared.Ghost.Roles;
  3. using Robust.Client.AutoGenerated;
  4. using Robust.Client.GameObjects;
  5. using Robust.Client.UserInterface.Controls;
  6. using Robust.Client.UserInterface.CustomControls;
  7. using Robust.Client.UserInterface.XAML;
  8. using Robust.Shared.Utility;
  9. namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
  10. {
  11. [GenerateTypedNameReferences]
  12. public sealed partial class GhostRolesWindow : DefaultWindow
  13. {
  14. public event Action<GhostRoleInfo>? OnRoleRequestButtonClicked;
  15. public event Action<GhostRoleInfo>? OnRoleFollow;
  16. private Dictionary<(string name, string description), Collapsible> _collapsibleBoxes = new();
  17. private HashSet<(string name, string description)> _uncollapsedStates = new();
  18. public GhostRolesWindow()
  19. {
  20. RobustXamlLoader.Load(this);
  21. }
  22. public void ClearEntries()
  23. {
  24. NoRolesMessage.Visible = true;
  25. EntryContainer.DisposeAllChildren();
  26. _collapsibleBoxes.Clear();
  27. }
  28. public void SaveCollapsibleBoxesStates()
  29. {
  30. _uncollapsedStates.Clear();
  31. foreach (var (key, collapsible) in _collapsibleBoxes)
  32. {
  33. if (collapsible.BodyVisible)
  34. {
  35. _uncollapsedStates.Add(key);
  36. }
  37. }
  38. }
  39. public void RestoreCollapsibleBoxesStates()
  40. {
  41. foreach (var (key, collapsible) in _collapsibleBoxes)
  42. {
  43. collapsible.BodyVisible = _uncollapsedStates.Contains(key);
  44. }
  45. }
  46. public void AddEntry(string name, string description, bool hasAccess, FormattedMessage? reason, IEnumerable<GhostRoleInfo> roles, SpriteSystem spriteSystem)
  47. {
  48. NoRolesMessage.Visible = false;
  49. var ghostRoleInfos = roles.ToList();
  50. var rolesCount = ghostRoleInfos.Count;
  51. var info = new GhostRoleInfoBox(name, description);
  52. var buttons = new GhostRoleButtonsBox(hasAccess, reason, ghostRoleInfos, spriteSystem);
  53. buttons.OnRoleSelected += OnRoleRequestButtonClicked;
  54. buttons.OnRoleFollow += OnRoleFollow;
  55. EntryContainer.AddChild(info);
  56. if (rolesCount > 1)
  57. {
  58. var buttonHeading = new CollapsibleHeading(Loc.GetString("ghost-roles-window-available-button", ("rolesCount", rolesCount)));
  59. buttonHeading.AddStyleClass(ContainerButton.StyleClassButton);
  60. buttonHeading.Label.HorizontalAlignment = HAlignment.Center;
  61. buttonHeading.Label.HorizontalExpand = true;
  62. buttonHeading.Margin = new Thickness(8, 0, 8, 2);
  63. var body = new CollapsibleBody
  64. {
  65. Margin = new Thickness(0, 5, 0, 0),
  66. };
  67. // TODO: Add Requirements to this key when it'll be fixed and work as an equality key in GhostRolesEui
  68. var key = (name, description);
  69. var collapsible = new Collapsible(buttonHeading, body)
  70. {
  71. Orientation = BoxContainer.LayoutOrientation.Vertical,
  72. Margin = new Thickness(0, 0, 0, 8),
  73. };
  74. body.AddChild(buttons);
  75. EntryContainer.AddChild(collapsible);
  76. _collapsibleBoxes.Add(key, collapsible);
  77. }
  78. else
  79. {
  80. EntryContainer.AddChild(buttons);
  81. }
  82. }
  83. }
  84. }