ECS characterinfo (#5669)
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using System.Linq;
|
||||
using Content.Client.CharacterInfo.Components;
|
||||
using Content.Client.HUD;
|
||||
using Content.Shared.Input;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Client.UserInterface.CustomControls;
|
||||
using Robust.Shared.GameObjects;
|
||||
@@ -15,6 +18,7 @@ namespace Content.Client.CharacterInterface
|
||||
{
|
||||
[Dependency] private readonly IGameHud _gameHud = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IInputManager _inputManager = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -22,11 +26,13 @@ namespace Content.Client.CharacterInterface
|
||||
|
||||
CommandBinds.Builder
|
||||
.Bind(ContentKeyFunctions.OpenCharacterMenu,
|
||||
InputCmdHandler.FromDelegate(s => HandleOpenCharacterMenu()))
|
||||
InputCmdHandler.FromDelegate(_ => HandleOpenCharacterMenu()))
|
||||
.Register<CharacterInterfaceSystem>();
|
||||
|
||||
SubscribeLocalEvent<CharacterInterfaceComponent, PlayerAttachedEvent>((_, component, _) => component.PlayerAttached());
|
||||
SubscribeLocalEvent<CharacterInterfaceComponent, PlayerDetachedEvent>((_, component, _) => component.PlayerDetached());
|
||||
SubscribeLocalEvent<CharacterInterfaceComponent, ComponentInit>(OnComponentInit);
|
||||
SubscribeLocalEvent<CharacterInterfaceComponent, ComponentRemove>(OnComponentRemove);
|
||||
SubscribeLocalEvent<CharacterInterfaceComponent, PlayerAttachedEvent>(OnPlayerAttached);
|
||||
SubscribeLocalEvent<CharacterInterfaceComponent, PlayerDetachedEvent>(OnPlayerDetached);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
@@ -35,30 +41,76 @@ namespace Content.Client.CharacterInterface
|
||||
base.Shutdown();
|
||||
}
|
||||
|
||||
private void OnComponentInit(EntityUid uid, CharacterInterfaceComponent comp, ComponentInit args)
|
||||
{
|
||||
//Use all the character ui interfaced components to create the character window
|
||||
comp.UIComponents = EntityManager.GetComponents<ICharacterUI>(uid).ToList();
|
||||
if (comp.UIComponents.Count == 0)
|
||||
return;
|
||||
|
||||
comp.Window = new CharacterInterfaceComponent.CharacterWindow(comp.UIComponents);
|
||||
comp.Window.OnClose += () => _gameHud.CharacterButtonDown = false;
|
||||
}
|
||||
|
||||
private void OnComponentRemove(EntityUid uid, CharacterInterfaceComponent comp, ComponentRemove args)
|
||||
{
|
||||
if (comp.UIComponents != null)
|
||||
{
|
||||
foreach (var component in comp.UIComponents)
|
||||
{
|
||||
// Make sure these don't get deleted when the window is disposed.
|
||||
component.Scene.Orphan();
|
||||
}
|
||||
}
|
||||
|
||||
comp.UIComponents = null;
|
||||
|
||||
comp.Window?.Close();
|
||||
comp.Window = null;
|
||||
|
||||
_inputManager.SetInputCommand(ContentKeyFunctions.OpenCharacterMenu, null);
|
||||
}
|
||||
|
||||
private void OnPlayerAttached(EntityUid uid, CharacterInterfaceComponent comp, PlayerAttachedEvent args)
|
||||
{
|
||||
if (comp.Window == null)
|
||||
return;
|
||||
|
||||
_gameHud.CharacterButtonVisible = true;
|
||||
_gameHud.CharacterButtonToggled = b =>
|
||||
{
|
||||
if (b)
|
||||
comp.Window.OpenCentered();
|
||||
else
|
||||
comp.Window.Close();
|
||||
};
|
||||
}
|
||||
|
||||
private void OnPlayerDetached(EntityUid uid, CharacterInterfaceComponent comp, PlayerDetachedEvent args)
|
||||
{
|
||||
if (comp.Window == null)
|
||||
return;
|
||||
|
||||
_gameHud.CharacterButtonVisible = false;
|
||||
comp.Window.Close();
|
||||
}
|
||||
|
||||
private void HandleOpenCharacterMenu()
|
||||
{
|
||||
if (!EntityManager.TryGetComponent(_playerManager.LocalPlayer?.ControlledEntity, out CharacterInterfaceComponent? characterInterface))
|
||||
{
|
||||
if (_playerManager.LocalPlayer?.ControlledEntity == null
|
||||
|| !EntityManager.TryGetComponent(_playerManager.LocalPlayer.ControlledEntity, out CharacterInterfaceComponent? characterInterface))
|
||||
return;
|
||||
}
|
||||
|
||||
var menu = characterInterface.Window;
|
||||
|
||||
if (menu == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (menu.IsOpen)
|
||||
{
|
||||
if (menu.IsAtFront())
|
||||
{
|
||||
_setOpenValue(menu, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
menu.MoveToFront();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -68,16 +120,11 @@ namespace Content.Client.CharacterInterface
|
||||
|
||||
private void _setOpenValue(SS14Window menu, bool value)
|
||||
{
|
||||
_gameHud.CharacterButtonDown = value;
|
||||
if (value)
|
||||
{
|
||||
_gameHud.CharacterButtonDown = true;
|
||||
menu.OpenCentered();
|
||||
}
|
||||
else
|
||||
{
|
||||
_gameHud.CharacterButtonDown = false;
|
||||
menu.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user