1
0

EscapeUIController.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Content.Client.Gameplay;
  2. using Content.Client.UserInterface.Controls;
  3. using Content.Client.UserInterface.Systems.Guidebook;
  4. using Content.Client.UserInterface.Systems.Info;
  5. using Content.Shared.CCVar;
  6. using JetBrains.Annotations;
  7. using Robust.Client.Console;
  8. using Robust.Client.UserInterface;
  9. using Robust.Client.UserInterface.Controllers;
  10. using Robust.Shared.Configuration;
  11. using Robust.Shared.Input;
  12. using Robust.Shared.Input.Binding;
  13. using Robust.Shared.Utility;
  14. using static Robust.Client.UserInterface.Controls.BaseButton;
  15. namespace Content.Client.UserInterface.Systems.EscapeMenu;
  16. [UsedImplicitly]
  17. public sealed class EscapeUIController : UIController, IOnStateEntered<GameplayState>, IOnStateExited<GameplayState>
  18. {
  19. [Dependency] private readonly IClientConsoleHost _console = default!;
  20. [Dependency] private readonly IUriOpener _uri = default!;
  21. [Dependency] private readonly IConfigurationManager _cfg = default!;
  22. [Dependency] private readonly ChangelogUIController _changelog = default!;
  23. [Dependency] private readonly InfoUIController _info = default!;
  24. [Dependency] private readonly OptionsUIController _options = default!;
  25. [Dependency] private readonly GuidebookUIController _guidebook = default!;
  26. private Options.UI.EscapeMenu? _escapeWindow;
  27. private MenuButton? EscapeButton => UIManager.GetActiveUIWidgetOrNull<MenuBar.Widgets.GameTopMenuBar>()?.EscapeButton;
  28. public void UnloadButton()
  29. {
  30. if (EscapeButton == null)
  31. {
  32. return;
  33. }
  34. EscapeButton.Pressed = false;
  35. EscapeButton.OnPressed -= EscapeButtonOnOnPressed;
  36. }
  37. public void LoadButton()
  38. {
  39. if (EscapeButton == null)
  40. {
  41. return;
  42. }
  43. EscapeButton.OnPressed += EscapeButtonOnOnPressed;
  44. }
  45. private void ActivateButton() => EscapeButton!.SetClickPressed(true);
  46. private void DeactivateButton() => EscapeButton!.SetClickPressed(false);
  47. public void OnStateEntered(GameplayState state)
  48. {
  49. DebugTools.Assert(_escapeWindow == null);
  50. _escapeWindow = UIManager.CreateWindow<Options.UI.EscapeMenu>();
  51. _escapeWindow.OnClose += DeactivateButton;
  52. _escapeWindow.OnOpen += ActivateButton;
  53. _escapeWindow.ChangelogButton.OnPressed += _ =>
  54. {
  55. CloseEscapeWindow();
  56. _changelog.ToggleWindow();
  57. };
  58. _escapeWindow.RulesButton.OnPressed += _ =>
  59. {
  60. CloseEscapeWindow();
  61. _info.OpenWindow();
  62. };
  63. _escapeWindow.DisconnectButton.OnPressed += _ =>
  64. {
  65. CloseEscapeWindow();
  66. _console.ExecuteCommand("disconnect");
  67. };
  68. _escapeWindow.OptionsButton.OnPressed += _ =>
  69. {
  70. CloseEscapeWindow();
  71. _options.OpenWindow();
  72. };
  73. _escapeWindow.QuitButton.OnPressed += _ =>
  74. {
  75. CloseEscapeWindow();
  76. _console.ExecuteCommand("quit");
  77. };
  78. _escapeWindow.WikiButton.OnPressed += _ =>
  79. {
  80. _uri.OpenUri(_cfg.GetCVar(CCVars.InfoLinksWiki));
  81. };
  82. _escapeWindow.GuidebookButton.OnPressed += _ =>
  83. {
  84. _guidebook.ToggleGuidebook();
  85. };
  86. // Hide wiki button if we don't have a link for it.
  87. _escapeWindow.WikiButton.Visible = _cfg.GetCVar(CCVars.InfoLinksWiki) != "";
  88. CommandBinds.Builder
  89. .Bind(EngineKeyFunctions.EscapeMenu,
  90. InputCmdHandler.FromDelegate(_ => ToggleWindow()))
  91. .Register<EscapeUIController>();
  92. }
  93. public void OnStateExited(GameplayState state)
  94. {
  95. if (_escapeWindow != null)
  96. {
  97. _escapeWindow.Dispose();
  98. _escapeWindow = null;
  99. }
  100. CommandBinds.Unregister<EscapeUIController>();
  101. }
  102. private void EscapeButtonOnOnPressed(ButtonEventArgs obj)
  103. {
  104. ToggleWindow();
  105. }
  106. private void CloseEscapeWindow()
  107. {
  108. _escapeWindow?.Close();
  109. }
  110. /// <summary>
  111. /// Toggles the game menu.
  112. /// </summary>
  113. public void ToggleWindow()
  114. {
  115. if (_escapeWindow == null)
  116. return;
  117. if (_escapeWindow.IsOpen)
  118. {
  119. CloseEscapeWindow();
  120. EscapeButton!.Pressed = false;
  121. }
  122. else
  123. {
  124. _escapeWindow.OpenCentered();
  125. EscapeButton!.Pressed = true;
  126. }
  127. }
  128. }