1
0

SandboxUIController.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System.Numerics;
  2. using Content.Client.Administration.Managers;
  3. using Content.Client.Gameplay;
  4. using Content.Client.Markers;
  5. using Content.Client.Sandbox;
  6. using Content.Client.SubFloor;
  7. using Content.Client.UserInterface.Controls;
  8. using Content.Client.UserInterface.Systems.DecalPlacer;
  9. using Content.Client.UserInterface.Systems.Sandbox.Windows;
  10. using Content.Shared.Input;
  11. using JetBrains.Annotations;
  12. using Robust.Client.Debugging;
  13. using Robust.Client.Graphics;
  14. using Robust.Client.Input;
  15. using Robust.Client.Player;
  16. using Robust.Client.UserInterface;
  17. using Robust.Client.UserInterface.Controllers;
  18. using Robust.Client.UserInterface.Controllers.Implementations;
  19. using Robust.Shared.Console;
  20. using Robust.Shared.Input.Binding;
  21. using Robust.Shared.Map;
  22. using Robust.Shared.Player;
  23. using Robust.Shared.Utility;
  24. using static Robust.Client.UserInterface.Controls.BaseButton;
  25. namespace Content.Client.UserInterface.Systems.Sandbox;
  26. // TODO hud refactor should part of this be in engine?
  27. [UsedImplicitly]
  28. public sealed class SandboxUIController : UIController, IOnStateChanged<GameplayState>, IOnSystemChanged<SandboxSystem>
  29. {
  30. [Dependency] private readonly IConsoleHost _console = default!;
  31. [Dependency] private readonly IEyeManager _eye = default!;
  32. [Dependency] private readonly IInputManager _input = default!;
  33. [Dependency] private readonly ILightManager _light = default!;
  34. [Dependency] private readonly IClientAdminManager _admin = default!;
  35. [Dependency] private readonly IPlayerManager _player = default!;
  36. [UISystemDependency] private readonly DebugPhysicsSystem _debugPhysics = default!;
  37. [UISystemDependency] private readonly MarkerSystem _marker = default!;
  38. [UISystemDependency] private readonly SandboxSystem _sandbox = default!;
  39. private SandboxWindow? _window;
  40. // TODO hud refactor cache
  41. private EntitySpawningUIController EntitySpawningController => UIManager.GetUIController<EntitySpawningUIController>();
  42. private TileSpawningUIController TileSpawningController => UIManager.GetUIController<TileSpawningUIController>();
  43. private DecalPlacerUIController DecalPlacerController => UIManager.GetUIController<DecalPlacerUIController>();
  44. private MenuButton? SandboxButton => UIManager.GetActiveUIWidgetOrNull<MenuBar.Widgets.GameTopMenuBar>()?.SandboxButton;
  45. public void OnStateEntered(GameplayState state)
  46. {
  47. DebugTools.Assert(_window == null);
  48. EnsureWindow();
  49. CheckSandboxVisibility();
  50. _input.SetInputCommand(ContentKeyFunctions.OpenEntitySpawnWindow,
  51. InputCmdHandler.FromDelegate(_ =>
  52. {
  53. if (!_admin.CanAdminPlace())
  54. return;
  55. EntitySpawningController.ToggleWindow();
  56. }));
  57. _input.SetInputCommand(ContentKeyFunctions.OpenSandboxWindow,
  58. InputCmdHandler.FromDelegate(_ => ToggleWindow()));
  59. _input.SetInputCommand(ContentKeyFunctions.OpenTileSpawnWindow,
  60. InputCmdHandler.FromDelegate(_ =>
  61. {
  62. if (!_admin.CanAdminPlace())
  63. return;
  64. TileSpawningController.ToggleWindow();
  65. }));
  66. _input.SetInputCommand(ContentKeyFunctions.OpenDecalSpawnWindow,
  67. InputCmdHandler.FromDelegate(_ =>
  68. {
  69. if (!_admin.CanAdminPlace())
  70. return;
  71. DecalPlacerController.ToggleWindow();
  72. }));
  73. CommandBinds.Builder
  74. .Bind(ContentKeyFunctions.EditorCopyObject, new PointerInputCmdHandler(Copy))
  75. .Register<SandboxSystem>();
  76. }
  77. public void UnloadButton()
  78. {
  79. if (SandboxButton == null)
  80. {
  81. return;
  82. }
  83. SandboxButton.OnPressed -= SandboxButtonPressed;
  84. }
  85. public void LoadButton()
  86. {
  87. if (SandboxButton == null)
  88. {
  89. return;
  90. }
  91. SandboxButton.OnPressed += SandboxButtonPressed;
  92. }
  93. private void EnsureWindow()
  94. {
  95. if (_window is { Disposed: false })
  96. return;
  97. _window = UIManager.CreateWindow<SandboxWindow>();
  98. // Pre-center the window without forcing it to the center every time.
  99. _window.OpenCentered();
  100. _window.Close();
  101. _window.OnOpen += () => { SandboxButton!.Pressed = true; };
  102. _window.OnClose += () => { SandboxButton!.Pressed = false; };
  103. // TODO: These need moving to opened so at least if they're not synced properly on open they work.
  104. _window.ToggleLightButton.Pressed = !_light.Enabled;
  105. _window.ToggleFovButton.Pressed = !_eye.CurrentEye.DrawFov;
  106. _window.ToggleShadowsButton.Pressed = !_light.DrawShadows;
  107. _window.ShowMarkersButton.Pressed = _marker.MarkersVisible;
  108. _window.ShowBbButton.Pressed = (_debugPhysics.Flags & PhysicsDebugFlags.Shapes) != 0x0;
  109. _window.AiOverlayButton.OnPressed += args =>
  110. {
  111. var player = _player.LocalEntity;
  112. if (player == null)
  113. return;
  114. var pnent = EntityManager.GetNetEntity(player.Value);
  115. // Need NetworkedAddComponent but engine PR.
  116. if (args.Button.Pressed)
  117. _console.ExecuteCommand($"addcomp {pnent.Id} StationAiOverlay");
  118. else
  119. _console.ExecuteCommand($"rmcomp {pnent.Id} StationAiOverlay");
  120. };
  121. _window.RespawnButton.OnPressed += _ => _sandbox.Respawn();
  122. _window.SpawnTilesButton.OnPressed += _ => TileSpawningController.ToggleWindow();
  123. _window.SpawnEntitiesButton.OnPressed += _ => EntitySpawningController.ToggleWindow();
  124. _window.SpawnDecalsButton.OnPressed += _ => DecalPlacerController.ToggleWindow();
  125. _window.GiveFullAccessButton.OnPressed += _ => _sandbox.GiveAdminAccess();
  126. _window.GiveAghostButton.OnPressed += _ => _sandbox.GiveAGhost();
  127. _window.ToggleLightButton.OnToggled += _ => _sandbox.ToggleLight();
  128. _window.ToggleFovButton.OnToggled += _ => _sandbox.ToggleFov();
  129. _window.ToggleShadowsButton.OnToggled += _ => _sandbox.ToggleShadows();
  130. _window.SuicideButton.OnPressed += _ => _sandbox.Suicide();
  131. _window.ToggleSubfloorButton.OnPressed += _ => _sandbox.ToggleSubFloor();
  132. _window.ShowMarkersButton.OnPressed += _ => _sandbox.ShowMarkers();
  133. _window.ShowBbButton.OnPressed += _ => _sandbox.ShowBb();
  134. }
  135. private void CheckSandboxVisibility()
  136. {
  137. if (SandboxButton == null)
  138. return;
  139. SandboxButton.Visible = _sandbox.SandboxAllowed;
  140. }
  141. public void OnStateExited(GameplayState state)
  142. {
  143. if (_window != null)
  144. {
  145. _window.Close();
  146. _window = null;
  147. }
  148. CommandBinds.Unregister<SandboxSystem>();
  149. }
  150. public void OnSystemLoaded(SandboxSystem system)
  151. {
  152. system.SandboxDisabled += CloseAll;
  153. system.SandboxEnabled += CheckSandboxVisibility;
  154. system.SandboxDisabled += CheckSandboxVisibility;
  155. }
  156. public void OnSystemUnloaded(SandboxSystem system)
  157. {
  158. system.SandboxDisabled -= CloseAll;
  159. system.SandboxEnabled -= CheckSandboxVisibility;
  160. system.SandboxDisabled -= CheckSandboxVisibility;
  161. }
  162. private void SandboxButtonPressed(ButtonEventArgs args)
  163. {
  164. ToggleWindow();
  165. }
  166. private void CloseAll()
  167. {
  168. _window?.Close();
  169. EntitySpawningController.CloseWindow();
  170. TileSpawningController.CloseWindow();
  171. }
  172. private bool Copy(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
  173. {
  174. return _sandbox.Copy(session, coords, uid);
  175. }
  176. private void ToggleWindow()
  177. {
  178. if (_window == null)
  179. return;
  180. if (_sandbox.SandboxAllowed && _window.IsOpen != true)
  181. {
  182. UIManager.ClickSound();
  183. _window.Open();
  184. }
  185. else
  186. {
  187. UIManager.ClickSound();
  188. _window.Close();
  189. }
  190. }
  191. #region Buttons
  192. public void SetToggleSubfloors(bool value)
  193. {
  194. if (_window == null)
  195. return;
  196. _window.ToggleSubfloorButton.Pressed = value;
  197. }
  198. #endregion
  199. }