ECS characterinfo (#5669)
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Client.CharacterInfo.Components;
|
||||
using Content.Client.HUD;
|
||||
using Content.Shared.Input;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.CustomControls;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using static Robust.Client.UserInterface.Controls.BoxContainer;
|
||||
|
||||
namespace Content.Client.CharacterInterface
|
||||
@@ -20,8 +14,6 @@ namespace Content.Client.CharacterInterface
|
||||
[RegisterComponent]
|
||||
public class CharacterInterfaceComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IGameHud _gameHud = default!;
|
||||
|
||||
public override string Name => "Character Interface Component";
|
||||
|
||||
/// <summary>
|
||||
@@ -30,104 +22,31 @@ namespace Content.Client.CharacterInterface
|
||||
/// <remarks>
|
||||
/// Null if it would otherwise be empty.
|
||||
/// </remarks>
|
||||
public CharacterWindow? Window { get; private set; }
|
||||
public CharacterWindow? Window { get; set; }
|
||||
|
||||
private List<ICharacterUI>? _uiComponents;
|
||||
|
||||
/// <summary>
|
||||
/// Create the window with all character UIs and bind it to a keypress
|
||||
/// </summary>
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
//Use all the character ui interfaced components to create the character window
|
||||
_uiComponents = IoCManager.Resolve<IEntityManager>().GetComponents<ICharacterUI>(Owner).ToList();
|
||||
if (_uiComponents.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Window = new CharacterWindow(_uiComponents);
|
||||
Window.OnClose += () => _gameHud.CharacterButtonDown = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose of window and the keypress binding
|
||||
/// </summary>
|
||||
protected override void OnRemove()
|
||||
{
|
||||
base.OnRemove();
|
||||
|
||||
if (_uiComponents != null)
|
||||
{
|
||||
foreach (var component in _uiComponents)
|
||||
{
|
||||
// Make sure these don't get deleted when the window is disposed.
|
||||
component.Scene.Orphan();
|
||||
}
|
||||
}
|
||||
|
||||
_uiComponents = null;
|
||||
|
||||
Window?.Close();
|
||||
Window = null;
|
||||
|
||||
var inputMgr = IoCManager.Resolve<IInputManager>();
|
||||
inputMgr.SetInputCommand(ContentKeyFunctions.OpenCharacterMenu, null);
|
||||
}
|
||||
|
||||
public void PlayerDetached()
|
||||
{
|
||||
if (Window != null)
|
||||
{
|
||||
_gameHud.CharacterButtonVisible = false;
|
||||
Window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void PlayerAttached()
|
||||
{
|
||||
if (Window != null)
|
||||
{
|
||||
_gameHud.CharacterButtonVisible = true;
|
||||
|
||||
_gameHud.CharacterButtonToggled = b =>
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
Window.OpenCentered();
|
||||
}
|
||||
else
|
||||
{
|
||||
Window.Close();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
public List<ICharacterUI>? UIComponents;
|
||||
|
||||
/// <summary>
|
||||
/// A window that collects and shows all the individual character user interfaces
|
||||
/// </summary>
|
||||
public class CharacterWindow : SS14Window
|
||||
{
|
||||
private readonly BoxContainer _contentsVBox;
|
||||
private readonly List<ICharacterUI> _windowComponents;
|
||||
|
||||
public CharacterWindow(List<ICharacterUI> windowComponents)
|
||||
{
|
||||
Title = "Character";
|
||||
|
||||
_contentsVBox = new BoxContainer
|
||||
var contentsVBox = new BoxContainer
|
||||
{
|
||||
Orientation = LayoutOrientation.Vertical
|
||||
};
|
||||
Contents.AddChild(_contentsVBox);
|
||||
Contents.AddChild(contentsVBox);
|
||||
|
||||
windowComponents.Sort((a, b) => ((int) a.Priority).CompareTo((int) b.Priority));
|
||||
foreach (var element in windowComponents)
|
||||
{
|
||||
_contentsVBox.AddChild(element.Scene);
|
||||
contentsVBox.AddChild(element.Scene);
|
||||
}
|
||||
|
||||
_windowComponents = windowComponents;
|
||||
|
||||
@@ -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