DecalPlacerUIController.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Content.Client.Decals.UI;
  2. using Content.Client.Gameplay;
  3. using Content.Client.Sandbox;
  4. using Content.Shared.Decals;
  5. using Robust.Client.UserInterface;
  6. using Robust.Client.UserInterface.Controllers;
  7. using Robust.Client.UserInterface.Controls;
  8. using Robust.Shared.Prototypes;
  9. using Robust.Shared.Utility;
  10. namespace Content.Client.UserInterface.Systems.DecalPlacer;
  11. public sealed class DecalPlacerUIController : UIController, IOnStateExited<GameplayState>, IOnSystemChanged<SandboxSystem>
  12. {
  13. [Dependency] private readonly IPrototypeManager _prototypes = default!;
  14. [UISystemDependency] private readonly SandboxSystem _sandbox = default!;
  15. private DecalPlacerWindow? _window;
  16. public void ToggleWindow()
  17. {
  18. EnsureWindow();
  19. if (_window!.IsOpen)
  20. {
  21. _window.Close();
  22. }
  23. else if(_sandbox.SandboxAllowed)
  24. {
  25. _window.Open();
  26. }
  27. }
  28. public void OnStateExited(GameplayState state)
  29. {
  30. if (_window == null)
  31. return;
  32. _window.Dispose();
  33. _window = null;
  34. }
  35. public void OnSystemLoaded(SandboxSystem system)
  36. {
  37. _sandbox.SandboxDisabled += CloseWindow;
  38. _prototypes.PrototypesReloaded += OnPrototypesReloaded;
  39. }
  40. public void OnSystemUnloaded(SandboxSystem system)
  41. {
  42. _sandbox.SandboxDisabled -= CloseWindow;
  43. _prototypes.PrototypesReloaded -= OnPrototypesReloaded;
  44. }
  45. private void OnPrototypesReloaded(PrototypesReloadedEventArgs obj)
  46. {
  47. if (obj.WasModified<DecalPrototype>())
  48. ReloadPrototypes();
  49. }
  50. private void ReloadPrototypes()
  51. {
  52. if (_window == null || _window.Disposed)
  53. return;
  54. var prototypes = _prototypes.EnumeratePrototypes<DecalPrototype>();
  55. _window.Populate(prototypes);
  56. }
  57. private void EnsureWindow()
  58. {
  59. if (_window is { Disposed: false })
  60. return;
  61. _window = UIManager.CreateWindow<DecalPlacerWindow>();
  62. LayoutContainer.SetAnchorPreset(_window, LayoutContainer.LayoutPreset.CenterLeft);
  63. ReloadPrototypes();
  64. }
  65. private void CloseWindow()
  66. {
  67. if (_window == null || _window.Disposed)
  68. return;
  69. _window.Close();
  70. }
  71. }