Files
tbd-station-14/Content.Client/UserInterface/Systems/EscapeMenu/EscapeUIController.cs
Jezithyr 571dd4e6d5 Hud refactor (#7202)
Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
Co-authored-by: Jezithyr <jmaster9999@gmail.com>
Co-authored-by: Jezithyr <Jezithyr@gmail.com>
Co-authored-by: Visne <39844191+Visne@users.noreply.github.com>
Co-authored-by: wrexbe <wrexbe@protonmail.com>
Co-authored-by: wrexbe <81056464+wrexbe@users.noreply.github.com>
2022-10-12 10:16:23 +02:00

124 lines
3.5 KiB
C#

using Content.Client.Gameplay;
using Content.Client.Info;
using Content.Client.Links;
using Content.Client.UserInterface.Controls;
using Content.Client.UserInterface.Systems.Info;
using JetBrains.Annotations;
using Robust.Client.Console;
using Robust.Client.Input;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controllers;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.Utility;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client.UserInterface.Systems.EscapeMenu;
[UsedImplicitly]
public sealed class EscapeUIController : UIController, IOnStateEntered<GameplayState>, IOnStateExited<GameplayState>
{
[Dependency] private readonly IClientConsoleHost _console = default!;
[Dependency] private readonly IUriOpener _uri = default!;
private Options.UI.EscapeMenu? _escapeWindow;
private MenuButton? _escapeButton;
public void OnStateEntered(GameplayState state)
{
DebugTools.Assert(_escapeWindow == null);
_escapeButton = UIManager.GetActiveUIWidget<MenuBar.Widgets.GameTopMenuBar>().EscapeButton;
_escapeButton.OnPressed += EscapeButtonOnOnPressed;
_escapeWindow = UIManager.CreateWindow<Options.UI.EscapeMenu>();
_escapeWindow.OnClose += () => { _escapeButton.Pressed = false; };
_escapeWindow.OnOpen += () => { _escapeButton.Pressed = true; };
_escapeWindow.ChangelogButton.OnPressed += _ =>
{
CloseEscapeWindow();
// Put this back when changelog button no longer controls the window
// UIManager.GetUIController<ChangelogUIController>().ToggleWindow();
};
_escapeWindow.RulesButton.OnPressed += _ =>
{
CloseEscapeWindow();
UIManager.GetUIController<InfoUIController>().OpenWindow();
};
_escapeWindow.DisconnectButton.OnPressed += _ =>
{
CloseEscapeWindow();
_console.ExecuteCommand("disconnect");
};
_escapeWindow.OptionsButton.OnPressed += _ =>
{
CloseEscapeWindow();
UIManager.GetUIController<OptionsUIController>().OpenWindow();
};
_escapeWindow.QuitButton.OnPressed += _ =>
{
CloseEscapeWindow();
_console.ExecuteCommand("quit");
};
_escapeWindow.WikiButton.OnPressed += _ =>
{
_uri.OpenUri(UILinks.Wiki);
};
CommandBinds.Builder
.Bind(EngineKeyFunctions.EscapeMenu,
InputCmdHandler.FromDelegate(_ => ToggleWindow()))
.Register<EscapeUIController>();
}
public void OnStateExited(GameplayState state)
{
if (_escapeWindow != null)
{
_escapeWindow.Dispose();
_escapeWindow = null;
}
if (_escapeButton != null)
{
_escapeButton.OnPressed -= EscapeButtonOnOnPressed;
_escapeButton.Pressed = false;
_escapeButton = null;
}
CommandBinds.Unregister<EscapeUIController>();
}
private void EscapeButtonOnOnPressed(ButtonEventArgs obj)
{
ToggleWindow();
}
private void CloseEscapeWindow()
{
_escapeWindow?.Close();
}
private void ToggleWindow()
{
if (_escapeWindow == null)
return;
if (_escapeWindow.IsOpen)
{
CloseEscapeWindow();
}
else
{
_escapeWindow.OpenCentered();
_escapeButton!.Pressed = true;
}
}
}