1
0

AdminUIController.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using Content.Client.Administration.Managers;
  2. using Content.Client.Administration.Systems;
  3. using Content.Client.Administration.UI;
  4. using Content.Client.Administration.UI.Tabs.ObjectsTab;
  5. using Content.Client.Administration.UI.Tabs.PanicBunkerTab;
  6. using Content.Client.Administration.UI.Tabs.PlayerTab;
  7. using Content.Client.Gameplay;
  8. using Content.Client.Lobby;
  9. using Content.Client.UserInterface.Controls;
  10. using Content.Client.Verbs.UI;
  11. using Content.Shared.Administration.Events;
  12. using Content.Shared.Input;
  13. using JetBrains.Annotations;
  14. using Robust.Client.Console;
  15. using Robust.Client.Input;
  16. using Robust.Client.UserInterface;
  17. using Robust.Client.UserInterface.Controllers;
  18. using Robust.Client.UserInterface.Controls;
  19. using Robust.Shared.Input;
  20. using Robust.Shared.Input.Binding;
  21. using static Robust.Client.UserInterface.Controls.BaseButton;
  22. namespace Content.Client.UserInterface.Systems.Admin;
  23. [UsedImplicitly]
  24. public sealed class AdminUIController : UIController,
  25. IOnStateEntered<GameplayState>,
  26. IOnStateEntered<LobbyState>,
  27. IOnSystemChanged<AdminSystem>
  28. {
  29. [Dependency] private readonly IClientAdminManager _admin = default!;
  30. [Dependency] private readonly IClientConGroupController _conGroups = default!;
  31. [Dependency] private readonly IClientConsoleHost _conHost = default!;
  32. [Dependency] private readonly IInputManager _input = default!;
  33. [Dependency] private readonly VerbMenuUIController _verb = default!;
  34. private AdminMenuWindow? _window;
  35. private MenuButton? AdminButton => UIManager.GetActiveUIWidgetOrNull<MenuBar.Widgets.GameTopMenuBar>()?.AdminButton;
  36. private PanicBunkerStatus? _panicBunker;
  37. public override void Initialize()
  38. {
  39. base.Initialize();
  40. SubscribeNetworkEvent<PanicBunkerChangedEvent>(OnPanicBunkerUpdated);
  41. }
  42. private void OnPanicBunkerUpdated(PanicBunkerChangedEvent msg, EntitySessionEventArgs args)
  43. {
  44. var showDialog = _panicBunker == null && msg.Status.Enabled;
  45. _panicBunker = msg.Status;
  46. _window?.PanicBunkerControl.UpdateStatus(msg.Status);
  47. if (showDialog)
  48. {
  49. UIManager.CreateWindow<PanicBunkerStatusWindow>().OpenCentered();
  50. }
  51. }
  52. public void OnStateEntered(GameplayState state)
  53. {
  54. EnsureWindow();
  55. AdminStatusUpdated();
  56. }
  57. public void OnStateEntered(LobbyState state)
  58. {
  59. EnsureWindow();
  60. AdminStatusUpdated();
  61. }
  62. public void OnSystemLoaded(AdminSystem system)
  63. {
  64. EnsureWindow();
  65. _admin.AdminStatusUpdated += AdminStatusUpdated;
  66. _input.SetInputCommand(ContentKeyFunctions.OpenAdminMenu,
  67. InputCmdHandler.FromDelegate(_ => Toggle()));
  68. }
  69. public void OnSystemUnloaded(AdminSystem system)
  70. {
  71. if (_window != null)
  72. _window.Dispose();
  73. _admin.AdminStatusUpdated -= AdminStatusUpdated;
  74. CommandBinds.Unregister<AdminUIController>();
  75. }
  76. private void EnsureWindow()
  77. {
  78. if (_window is { Disposed: false })
  79. return;
  80. if (_window?.Disposed ?? false)
  81. OnWindowDisposed();
  82. _window = UIManager.CreateWindow<AdminMenuWindow>();
  83. LayoutContainer.SetAnchorPreset(_window, LayoutContainer.LayoutPreset.Center);
  84. if (_panicBunker != null)
  85. _window.PanicBunkerControl.UpdateStatus(_panicBunker);
  86. _window.PlayerTabControl.OnEntryKeyBindDown += PlayerTabEntryKeyBindDown;
  87. _window.ObjectsTabControl.OnEntryKeyBindDown += ObjectsTabEntryKeyBindDown;
  88. _window.OnOpen += OnWindowOpen;
  89. _window.OnClose += OnWindowClosed;
  90. _window.OnDisposed += OnWindowDisposed;
  91. }
  92. public void UnloadButton()
  93. {
  94. if (AdminButton == null)
  95. {
  96. return;
  97. }
  98. AdminButton.OnPressed -= AdminButtonPressed;
  99. }
  100. public void LoadButton()
  101. {
  102. if (AdminButton == null)
  103. {
  104. return;
  105. }
  106. AdminButton.OnPressed += AdminButtonPressed;
  107. }
  108. private void OnWindowOpen()
  109. {
  110. AdminButton?.SetClickPressed(true);
  111. }
  112. private void OnWindowClosed()
  113. {
  114. AdminButton?.SetClickPressed(false);
  115. }
  116. private void OnWindowDisposed()
  117. {
  118. if (AdminButton != null)
  119. AdminButton.Pressed = false;
  120. if (_window == null)
  121. return;
  122. _window.PlayerTabControl.OnEntryKeyBindDown -= PlayerTabEntryKeyBindDown;
  123. _window.ObjectsTabControl.OnEntryKeyBindDown -= ObjectsTabEntryKeyBindDown;
  124. _window.OnOpen -= OnWindowOpen;
  125. _window.OnClose -= OnWindowClosed;
  126. _window.OnDisposed -= OnWindowDisposed;
  127. _window = null;
  128. }
  129. private void AdminStatusUpdated()
  130. {
  131. if (AdminButton != null)
  132. AdminButton.Visible = _conGroups.CanAdminMenu();
  133. }
  134. private void AdminButtonPressed(ButtonEventArgs args)
  135. {
  136. Toggle();
  137. }
  138. private void Toggle()
  139. {
  140. if (_window is {IsOpen: true})
  141. {
  142. _window.Close();
  143. }
  144. else if (_conGroups.CanAdminMenu())
  145. {
  146. _window?.Open();
  147. }
  148. }
  149. private void PlayerTabEntryKeyBindDown(GUIBoundKeyEventArgs args, ListData? data)
  150. {
  151. if (data is not PlayerListData {Info: var info})
  152. return;
  153. if (info.NetEntity == null)
  154. return;
  155. var entity = info.NetEntity.Value;
  156. var function = args.Function;
  157. if (function == EngineKeyFunctions.UIClick)
  158. _conHost.ExecuteCommand($"vv {entity}");
  159. else if (function == EngineKeyFunctions.UIRightClick)
  160. _verb.OpenVerbMenu(entity, true);
  161. else
  162. return;
  163. args.Handle();
  164. }
  165. private void ObjectsTabEntryKeyBindDown(GUIBoundKeyEventArgs args, ListData? data)
  166. {
  167. if (data is not ObjectsListData { Info: var info })
  168. return;
  169. var uid = info.Entity;
  170. var function = args.Function;
  171. if (function == EngineKeyFunctions.UIClick)
  172. _conHost.ExecuteCommand($"vv {uid}");
  173. else if (function == EngineKeyFunctions.UIRightClick)
  174. _verb.OpenVerbMenu(uid, true);
  175. else
  176. return;
  177. args.Handle();
  178. }
  179. }