Files
tbd-station-14/Content.Client/Mech/Ui/MechBoundUserInterface.cs
metalgearsloth edb05e36bb Reapply "Remove some BUI boilerplate" (#30214) (#30219)
* Reapply "Remove some BUI boilerplate" (#30214)

This reverts commit cb0ba66be3.

* Fix gas tank

* Fix PA

* Fix microwave

* Comms console underwrap

* Fix rcd

* log wehs
2024-07-21 14:48:13 +10:00

71 lines
1.8 KiB
C#

using Content.Client.UserInterface.Fragments;
using Content.Shared.Mech;
using Content.Shared.Mech.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface;
namespace Content.Client.Mech.Ui;
[UsedImplicitly]
public sealed class MechBoundUserInterface : BoundUserInterface
{
[ViewVariables]
private MechMenu? _menu;
public MechBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
_menu = this.CreateWindow<MechMenu>();
_menu.SetEntity(Owner);
_menu.OpenCenteredLeft();
_menu.OnRemoveButtonPressed += uid =>
{
SendMessage(new MechEquipmentRemoveMessage(EntMan.GetNetEntity(uid)));
};
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (state is not MechBoundUiState msg)
return;
UpdateEquipmentControls(msg);
_menu?.UpdateMechStats();
_menu?.UpdateEquipmentView();
}
public void UpdateEquipmentControls(MechBoundUiState state)
{
if (!EntMan.TryGetComponent<MechComponent>(Owner, out var mechComp))
return;
foreach (var ent in mechComp.EquipmentContainer.ContainedEntities)
{
var ui = GetEquipmentUi(ent);
if (ui == null)
continue;
foreach (var (attached, estate) in state.EquipmentStates)
{
if (ent == EntMan.GetEntity(attached))
ui.UpdateState(estate);
}
}
}
public UIFragment? GetEquipmentUi(EntityUid? uid)
{
var component = EntMan.GetComponentOrNull<UIFragmentComponent>(uid);
component?.Ui?.Setup(this, uid);
return component?.Ui;
}
}