Turn SandboxManager into a system (#6867)

This commit is contained in:
metalgearsloth
2022-02-24 11:00:30 +11:00
committed by GitHub
parent ec30db79f8
commit bee627ac6a
13 changed files with 193 additions and 267 deletions

View File

@@ -1,11 +0,0 @@
using System;
namespace Content.Client.Sandbox
{
public interface ISandboxManager
{
void Initialize();
bool SandboxAllowed { get; }
event Action<bool> AllowedChanged;
}
}

View File

@@ -1,11 +1,12 @@
using System;
using Content.Client.Administration.Managers;
using Content.Client.Decals.UI;
using Content.Client.HUD;
using Content.Client.Markers;
using Content.Client.SubFloor;
using Content.Shared.Administration;
using Content.Shared.GameTicking;
using Content.Shared.Input;
using Content.Shared.Sandbox;
using Content.Shared.SubFloor;
using Robust.Client.Console;
using Robust.Client.Debugging;
using Robust.Client.Graphics;
@@ -14,13 +15,9 @@ using Robust.Client.Placement;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.GameObjects;
using Robust.Shared.Input.Binding;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using static Robust.Client.UserInterface.Controls.BoxContainer;
namespace Content.Client.Sandbox
@@ -116,41 +113,33 @@ namespace Content.Client.Sandbox
}
internal sealed class SandboxManager : SharedSandboxManager, ISandboxManager
public sealed class SandboxSystem : SharedSandboxSystem
{
[Dependency] private readonly IClientConsoleHost _consoleHost = default!;
[Dependency] private readonly IGameHud _gameHud = default!;
[Dependency] private readonly IClientNetManager _netManager = default!;
[Dependency] private readonly IPlacementManager _placementManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly IClientAdminManager _adminManager = default!;
public bool SandboxAllowed { get; private set; }
public event Action<bool>? AllowedChanged;
private SandboxWindow? _window;
private SandboxWindow? _sandboxWindow;
private EntitySpawnWindow? _spawnWindow;
private TileSpawnWindow? _tilesSpawnWindow;
private DecalPlacerWindow? _decalSpawnWindow;
private bool _sandboxWindowToggled;
public void Initialize()
public override void Initialize()
{
_netManager.RegisterNetMessage<MsgSandboxStatus>(message => SetAllowed(message.SandboxAllowed));
_netManager.RegisterNetMessage<MsgSandboxGiveAccess>();
_netManager.RegisterNetMessage<MsgSandboxRespawn>();
_netManager.RegisterNetMessage<MsgSandboxGiveAghost>();
_netManager.RegisterNetMessage<MsgSandboxSuicide>();
base.Initialize();
SubscribeNetworkEvent<MsgSandboxStatus>(OnSandboxStatus);
SubscribeNetworkEvent<RoundRestartCleanupEvent>(OnRoundRestart);
_adminManager.AdminStatusUpdated += OnAdminStatus;
_gameHud.SandboxButtonToggled += SandboxButtonPressed;
// Do these need cleanup?
_inputManager.SetInputCommand(ContentKeyFunctions.OpenEntitySpawnWindow,
InputCmdHandler.FromDelegate(session => ToggleEntitySpawnWindow()));
_inputManager.SetInputCommand(ContentKeyFunctions.OpenSandboxWindow,
@@ -161,83 +150,129 @@ namespace Content.Client.Sandbox
InputCmdHandler.FromDelegate(session => ToggleDecalsWindow()));
}
private void OnAdminStatus()
{
if (CanSandbox())
Enable();
else
Disable();
}
private bool CanSandbox()
{
return SandboxAllowed || _adminManager.IsActive();
}
/// <summary>
/// Run when sandbox is disabled
/// </summary>
private void Disable()
{
_gameHud.SandboxButtonVisible = false;
_sandboxWindow?.Close();
_sandboxWindow = null;
_spawnWindow?.Close();
_tilesSpawnWindow?.Close();
_decalSpawnWindow?.Close();
}
private void Enable()
{
_gameHud.SandboxButtonVisible = true;
}
private void OnRoundRestart(RoundRestartCleanupEvent ev)
{
// Go through and cleanup windows (even if they remain adminned better to just shut them).
Disable();
if (CanSandbox())
Enable();
}
public override void Shutdown()
{
base.Shutdown();
// TODO: Gamehud moment
_gameHud.SandboxButtonToggled -= SandboxButtonPressed;
_adminManager.AdminStatusUpdated -= OnAdminStatus;
}
private void OnSandboxStatus(MsgSandboxStatus ev)
{
SetAllowed(ev.SandboxAllowed);
}
private void SandboxButtonPressed(bool newValue)
{
_sandboxWindowToggled = newValue;
UpdateSandboxWindowVisibility();
}
private void ToggleSandboxWindow()
{
_sandboxWindowToggled = !_sandboxWindowToggled;
UpdateSandboxWindowVisibility();
}
private void UpdateSandboxWindowVisibility()
{
if (_sandboxWindowToggled && SandboxAllowed)
OpenWindow();
if (CanSandbox() && _sandboxWindow?.IsOpen != true)
OpenSandboxWindow();
else
_window?.Close();
_sandboxWindow?.Close();
}
private void SetAllowed(bool newAllowed)
{
if (newAllowed == SandboxAllowed)
{
return;
}
SandboxAllowed = newAllowed;
_gameHud.SandboxButtonVisible = newAllowed;
_gameHud.SandboxButtonVisible = CanSandbox();
if (!newAllowed)
{
// Sandbox permission revoked, close window.
_window?.Close();
}
AllowedChanged?.Invoke(newAllowed);
if (!CanSandbox())
Disable();
}
private void OpenWindow()
private void OpenSandboxWindow()
{
if (_window != null)
if (_sandboxWindow != null)
{
if (!_sandboxWindow.IsOpen)
_sandboxWindow.Open();
return;
}
_window = new SandboxWindow();
_sandboxWindow = new SandboxWindow();
_window.OnClose += WindowOnOnClose;
_sandboxWindow.OnClose += SandboxWindowOnClose;
_window.RespawnButton.OnPressed += OnRespawnButtonOnOnPressed;
_window.SpawnTilesButton.OnPressed += OnSpawnTilesButtonClicked;
_window.SpawnEntitiesButton.OnPressed += OnSpawnEntitiesButtonClicked;
_window.SpawnDecalsButton.OnPressed += OnSpawnDecalsButtonClicked;
_window.GiveFullAccessButton.OnPressed += OnGiveAdminAccessButtonClicked;
_window.GiveAghostButton.OnPressed += OnGiveAghostButtonClicked;
_window.ToggleLightButton.OnToggled += OnToggleLightButtonClicked;
_window.ToggleFovButton.OnToggled += OnToggleFovButtonClicked;
_window.ToggleShadowsButton.OnToggled += OnToggleShadowsButtonClicked;
_window.SuicideButton.OnPressed += OnSuicideButtonClicked;
_window.ToggleSubfloorButton.OnPressed += OnToggleSubfloorButtonClicked;
_window.ShowMarkersButton.OnPressed += OnShowMarkersButtonClicked;
_window.ShowBbButton.OnPressed += OnShowBbButtonClicked;
_window.MachineLinkingButton.OnPressed += OnMachineLinkingButtonClicked;
_sandboxWindow.RespawnButton.OnPressed += OnRespawnButtonOnOnPressed;
_sandboxWindow.SpawnTilesButton.OnPressed += OnSpawnTilesButtonClicked;
_sandboxWindow.SpawnEntitiesButton.OnPressed += OnSpawnEntitiesButtonClicked;
_sandboxWindow.SpawnDecalsButton.OnPressed += OnSpawnDecalsButtonClicked;
_sandboxWindow.GiveFullAccessButton.OnPressed += OnGiveAdminAccessButtonClicked;
_sandboxWindow.GiveAghostButton.OnPressed += OnGiveAghostButtonClicked;
_sandboxWindow.ToggleLightButton.OnToggled += OnToggleLightButtonClicked;
_sandboxWindow.ToggleFovButton.OnToggled += OnToggleFovButtonClicked;
_sandboxWindow.ToggleShadowsButton.OnToggled += OnToggleShadowsButtonClicked;
_sandboxWindow.SuicideButton.OnPressed += OnSuicideButtonClicked;
_sandboxWindow.ToggleSubfloorButton.OnPressed += OnToggleSubfloorButtonClicked;
_sandboxWindow.ShowMarkersButton.OnPressed += OnShowMarkersButtonClicked;
_sandboxWindow.ShowBbButton.OnPressed += OnShowBbButtonClicked;
_sandboxWindow.MachineLinkingButton.OnPressed += OnMachineLinkingButtonClicked;
_window.OpenCentered();
_sandboxWindow.OpenCentered();
}
private void WindowOnOnClose()
private void SandboxWindowOnClose()
{
_window = null;
_sandboxWindowToggled = false;
_sandboxWindow = null;
}
private void OnRespawnButtonOnOnPressed(BaseButton.ButtonEventArgs args)
{
_netManager.ClientSendMessage(_netManager.CreateNetMessage<MsgSandboxRespawn>());
RaiseNetworkEvent(new MsgSandboxRespawn());
}
private void OnSpawnEntitiesButtonClicked(BaseButton.ButtonEventArgs args)
@@ -291,24 +326,27 @@ namespace Content.Client.Sandbox
private void OnGiveAdminAccessButtonClicked(BaseButton.ButtonEventArgs args)
{
_netManager.ClientSendMessage(_netManager.CreateNetMessage<MsgSandboxGiveAccess>());
RaiseNetworkEvent(new MsgSandboxGiveAccess());
}
private void OnGiveAghostButtonClicked(BaseButton.ButtonEventArgs args)
{
_netManager.ClientSendMessage(_netManager.CreateNetMessage<MsgSandboxGiveAghost>());
RaiseNetworkEvent(new MsgSandboxGiveAghost());
}
private void OnSuicideButtonClicked(BaseButton.ButtonEventArgs args)
{
_netManager.ClientSendMessage(_netManager.CreateNetMessage<MsgSandboxSuicide>());
RaiseNetworkEvent(new MsgSandboxSuicide());
}
private void ToggleEntitySpawnWindow()
// TODO: These should check for command perms + be reset if the round is over.
public void ToggleEntitySpawnWindow()
{
if (_spawnWindow == null)
{
_spawnWindow = new EntitySpawnWindow(_placementManager, _prototypeManager, _resourceCache);
if (!CanSandbox()) return;
_spawnWindow = new EntitySpawnWindow(_placementManager, PrototypeManager, _resourceCache);
_spawnWindow.OpenToLeft();
return;
}
@@ -323,10 +361,12 @@ namespace Content.Client.Sandbox
}
}
private void ToggleTilesWindow()
public void ToggleTilesWindow()
{
if (_tilesSpawnWindow == null)
{
if (!CanSandbox()) return;
_tilesSpawnWindow = new TileSpawnWindow(_tileDefinitionManager, _placementManager, _resourceCache);
_tilesSpawnWindow.OpenToLeft();
return;
@@ -342,11 +382,13 @@ namespace Content.Client.Sandbox
}
}
private void ToggleDecalsWindow()
public void ToggleDecalsWindow()
{
if (_decalSpawnWindow == null)
{
_decalSpawnWindow = new DecalPlacerWindow(_prototypeManager);
if (!CanSandbox()) return;
_decalSpawnWindow = new DecalPlacerWindow(PrototypeManager);
_decalSpawnWindow.OpenToLeft();
return;
}
@@ -361,6 +403,7 @@ namespace Content.Client.Sandbox
}
}
// TODO: need to cleanup these
private void ToggleLight()
{
_consoleHost.ExecuteCommand("togglelight");