Files
OldThink/Content.Client/EscapeMenu/EscapeMenuOwner.cs

89 lines
2.5 KiB
C#
Raw Normal View History

2021-06-09 22:19:39 +02:00
using Content.Client.HUD;
using Content.Client.Viewport;
2019-05-14 15:19:41 +02:00
using Robust.Client.Console;
using Robust.Client.Input;
using Robust.Client.State;
2019-05-14 15:19:41 +02:00
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
2019-05-14 15:19:41 +02:00
using Robust.Shared.IoC;
2021-06-09 22:19:39 +02:00
namespace Content.Client.EscapeMenu
2019-05-14 15:19:41 +02:00
{
internal sealed class EscapeMenuOwner : IEscapeMenuOwner
{
[Dependency] private readonly IClientConsoleHost _consoleHost = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly IStateManager _stateManager = default!;
[Dependency] private readonly IGameHud _gameHud = default!;
2019-05-14 15:19:41 +02:00
2021-06-09 22:19:39 +02:00
private UI.EscapeMenu? _escapeMenu;
2019-05-14 15:19:41 +02:00
public void Initialize()
{
_stateManager.OnStateChanged += StateManagerOnOnStateChanged;
_gameHud.EscapeButtonToggled += _setOpenValue;
2019-05-14 15:19:41 +02:00
}
private void StateManagerOnOnStateChanged(StateChangedEventArgs obj)
{
if (obj.NewState is GameScreenBase)
2019-05-14 15:19:41 +02:00
{
// Switched TO GameScreen.
2021-06-09 22:19:39 +02:00
_escapeMenu = new UI.EscapeMenu(_consoleHost);
2019-05-14 15:19:41 +02:00
_escapeMenu.OnClose += () => _gameHud.EscapeButtonDown = false;
_inputManager.SetInputCommand(EngineKeyFunctions.EscapeMenu,
InputCmdHandler.FromDelegate(_ => Enabled()));
2019-05-14 15:19:41 +02:00
}
else if (obj.OldState is GameScreenBase)
2019-05-14 15:19:41 +02:00
{
// Switched FROM GameScreen.
_escapeMenu?.Dispose();
2019-05-14 15:19:41 +02:00
_escapeMenu = null;
_inputManager.SetInputCommand(EngineKeyFunctions.EscapeMenu, null);
}
}
private void Enabled()
{
if (_escapeMenu != null && _escapeMenu.IsOpen)
{
if (_escapeMenu.IsAtFront())
{
_setOpenValue(false);
}
else
{
_escapeMenu.MoveToFront();
}
}
else
{
_setOpenValue(true);
}
}
private void _setOpenValue(bool value)
{
if (value)
{
_gameHud.EscapeButtonDown = true;
_escapeMenu?.OpenCentered();
}
else
{
_gameHud.EscapeButtonDown = false;
_escapeMenu?.Close();
}
}
2019-05-14 15:19:41 +02:00
}
public interface IEscapeMenuOwner
{
void Initialize();
}
}