GhostRolesEui.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Linq;
  2. using Content.Client.Eui;
  3. using Content.Client.Players.PlayTimeTracking;
  4. using Content.Shared.Eui;
  5. using Content.Shared.Ghost.Roles;
  6. using JetBrains.Annotations;
  7. using Robust.Client.GameObjects;
  8. namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles
  9. {
  10. [UsedImplicitly]
  11. public sealed class GhostRolesEui : BaseEui
  12. {
  13. private readonly GhostRolesWindow _window;
  14. private GhostRoleRulesWindow? _windowRules = null;
  15. private uint _windowRulesId = 0;
  16. public GhostRolesEui()
  17. {
  18. _window = new GhostRolesWindow();
  19. _window.OnRoleRequestButtonClicked += info =>
  20. {
  21. _windowRules?.Close();
  22. if (info.Kind == GhostRoleKind.RaffleJoined)
  23. {
  24. SendMessage(new LeaveGhostRoleRaffleMessage(info.Identifier));
  25. return;
  26. }
  27. _windowRules = new GhostRoleRulesWindow(info.Rules, _ =>
  28. {
  29. SendMessage(new RequestGhostRoleMessage(info.Identifier));
  30. // if raffle role, close rules window on request, otherwise do
  31. // old behavior of waiting for the server to close it
  32. if (info.Kind != GhostRoleKind.FirstComeFirstServe)
  33. _windowRules?.Close();
  34. });
  35. _windowRulesId = info.Identifier;
  36. _windowRules.OnClose += () =>
  37. {
  38. _windowRules = null;
  39. };
  40. _windowRules.OpenCentered();
  41. };
  42. _window.OnRoleFollow += info =>
  43. {
  44. SendMessage(new FollowGhostRoleMessage(info.Identifier));
  45. };
  46. _window.OnClose += () =>
  47. {
  48. SendMessage(new CloseEuiMessage());
  49. };
  50. }
  51. public override void Opened()
  52. {
  53. base.Opened();
  54. _window.OpenCentered();
  55. }
  56. public override void Closed()
  57. {
  58. base.Closed();
  59. _window.Close();
  60. _windowRules?.Close();
  61. }
  62. public override void HandleState(EuiStateBase state)
  63. {
  64. base.HandleState(state);
  65. if (state is not GhostRolesEuiState ghostState)
  66. return;
  67. // We must save BodyVisible state, so all Collapsible boxes will not close
  68. // on adding new ghost role.
  69. // Save the current state of each Collapsible box being visible or not
  70. _window.SaveCollapsibleBoxesStates();
  71. // Clearing the container before adding new roles
  72. _window.ClearEntries();
  73. var entityManager = IoCManager.Resolve<IEntityManager>();
  74. var sysManager = entityManager.EntitySysManager;
  75. var spriteSystem = sysManager.GetEntitySystem<SpriteSystem>();
  76. var requirementsManager = IoCManager.Resolve<JobRequirementsManager>();
  77. // TODO: role.Requirements value doesn't work at all as an equality key, this must be fixed
  78. // Grouping roles
  79. var groupedRoles = ghostState.GhostRoles.GroupBy(
  80. role => (role.Name, role.Description, role.Requirements));
  81. // Add a new entry for each role group
  82. foreach (var group in groupedRoles)
  83. {
  84. var name = group.Key.Name;
  85. var description = group.Key.Description;
  86. var hasAccess = requirementsManager.CheckRoleRequirements(
  87. group.Key.Requirements,
  88. null,
  89. out var reason);
  90. // Adding a new role
  91. _window.AddEntry(name, description, hasAccess, reason, group, spriteSystem);
  92. }
  93. // Restore the Collapsible box state if it is saved
  94. _window.RestoreCollapsibleBoxesStates();
  95. // Close the rules window if it is no longer needed
  96. var closeRulesWindow = ghostState.GhostRoles.All(role => role.Identifier != _windowRulesId);
  97. if (closeRulesWindow)
  98. _windowRules?.Close();
  99. }
  100. }
  101. }