Use item status extension method (#23884)

Just removes some lines of code.
This commit is contained in:
Pieter-Jan Briers
2024-01-12 01:14:13 +01:00
committed by GitHub
parent e2dd30f640
commit e665c2487e
11 changed files with 42 additions and 76 deletions

View File

@@ -12,9 +12,9 @@ public sealed class InjectorSystem : EntitySystem
{
base.Initialize();
SubscribeLocalEvent<InjectorComponent, ComponentHandleState>(OnHandleInjectorState);
SubscribeLocalEvent<InjectorComponent, ItemStatusCollectMessage>(OnItemInjectorStatus);
Subs.ItemStatus<InjectorComponent>(ent => new InjectorStatusControl(ent));
SubscribeLocalEvent<HyposprayComponent, ComponentHandleState>(OnHandleHyposprayState);
SubscribeLocalEvent<HyposprayComponent, ItemStatusCollectMessage>(OnItemHyposprayStatus);
Subs.ItemStatus<HyposprayComponent>(ent => new HyposprayStatusControl(ent));
}
private void OnHandleInjectorState(EntityUid uid, InjectorComponent component, ref ComponentHandleState args)
@@ -30,11 +30,6 @@ public sealed class InjectorSystem : EntitySystem
component.UiUpdateNeeded = true;
}
private void OnItemInjectorStatus(EntityUid uid, InjectorComponent component, ItemStatusCollectMessage args)
{
args.Controls.Add(new InjectorStatusControl(component));
}
private void OnHandleHyposprayState(EntityUid uid, HyposprayComponent component, ref ComponentHandleState args)
{
if (args.Current is not HyposprayComponentState cState)
@@ -44,9 +39,4 @@ public sealed class InjectorSystem : EntitySystem
component.TotalVolume = cState.MaxVolume;
component.UiUpdateNeeded = true;
}
private void OnItemHyposprayStatus(EntityUid uid, HyposprayComponent component, ItemStatusCollectMessage args)
{
args.Controls.Add(new HyposprayStatusControl(component));
}
}

View File

@@ -18,7 +18,7 @@ public sealed class CrayonSystem : SharedCrayonSystem
{
base.Initialize();
SubscribeLocalEvent<CrayonComponent, ComponentHandleState>(OnCrayonHandleState);
SubscribeLocalEvent<CrayonComponent, ItemStatusCollectMessage>(OnCrayonItemStatus);
Subs.ItemStatus<CrayonComponent>(ent => new StatusControl(ent));
}
private static void OnCrayonHandleState(EntityUid uid, CrayonComponent component, ref ComponentHandleState args)
@@ -33,11 +33,6 @@ public sealed class CrayonSystem : SharedCrayonSystem
component.UIUpdateNeeded = true;
}
private static void OnCrayonItemStatus(EntityUid uid, CrayonComponent component, ItemStatusCollectMessage args)
{
args.Controls.Add(new StatusControl(component));
}
private sealed class StatusControl : Control
{
private readonly CrayonComponent _parent;

View File

@@ -1,7 +1,6 @@
using Content.Client.Fluids.UI;
using Content.Client.Items;
using Content.Shared.Fluids;
using Robust.Client.UserInterface;
namespace Content.Client.Fluids;
@@ -11,11 +10,6 @@ public sealed class AbsorbentSystem : SharedAbsorbentSystem
public override void Initialize()
{
base.Initialize();
Subs.ItemStatus<AbsorbentComponent>(GetAbsorbent);
}
private Control GetAbsorbent(EntityUid arg)
{
return new AbsorbentItemStatus(arg, EntityManager);
Subs.ItemStatus<AbsorbentComponent>(ent => new AbsorbentItemStatus(ent, EntityManager));
}
}

View File

@@ -9,11 +9,7 @@ public sealed class HandheldGpsSystem : EntitySystem
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HandheldGPSComponent, ItemStatusCollectMessage>(OnItemStatus);
}
private void OnItemStatus(Entity<HandheldGPSComponent> ent, ref ItemStatusCollectMessage args)
{
args.Controls.Add(new HandheldGpsStatusControl(ent));
Subs.ItemStatus<HandheldGPSComponent>(ent => new HandheldGpsStatusControl(ent));
}
}

View File

@@ -12,16 +12,11 @@ public sealed class ImplanterSystem : SharedImplanterSystem
base.Initialize();
SubscribeLocalEvent<ImplanterComponent, AfterAutoHandleStateEvent>(OnHandleImplanterState);
SubscribeLocalEvent<ImplanterComponent, ItemStatusCollectMessage>(OnItemImplanterStatus);
Subs.ItemStatus<ImplanterComponent>(ent => new ImplanterStatusControl(ent));
}
private void OnHandleImplanterState(EntityUid uid, ImplanterComponent component, ref AfterAutoHandleStateEvent args)
{
component.UiUpdateNeeded = true;
}
private void OnItemImplanterStatus(EntityUid uid, ImplanterComponent component, ItemStatusCollectMessage args)
{
args.Controls.Add(new ImplanterStatusControl(component));
}
}

View File

@@ -2,27 +2,51 @@
namespace Content.Client.Items
{
/// <summary>
/// Raised by the HUD logic to collect item status controls for a held entity.
/// </summary>
/// <remarks>
/// Handlers should add any controls they want to add to <see cref="Controls"/>.
/// </remarks>
/// <seealso cref="ItemStatusRegisterExt"/>
public sealed class ItemStatusCollectMessage : EntityEventArgs
{
/// <summary>
/// A list of controls that will be displayed on the HUD. Handlers should add their controls here.
/// </summary>
public List<Control> Controls = new();
}
/// <summary>
/// Extension methods for registering item status controls.
/// </summary>
/// <seealso cref="ItemStatusCollectMessage"/>
public static class ItemStatusRegisterExt
{
/// <summary>
/// Register an item status control for a component.
/// </summary>
/// <remarks>
/// This is a convenience wrapper around <see cref="ItemStatusCollectMessage"/>.
/// </remarks>
/// <param name="subs">The <see cref="EntitySystem.Subs"/> handle from within entity system initialize.</param>
/// <param name="createControl">A delegate to create the actual control.</param>
/// <param name="createControl">
/// A delegate to create the actual control.
/// If the delegate returns null, no control will be added to the item status.
/// </param>
/// <typeparam name="TComp">The type of component for which this control should be made.</typeparam>
public static void ItemStatus<TComp>(
this EntitySystem.Subscriptions subs,
Func<EntityUid, Control> createControl)
Func<Entity<TComp>, Control?> createControl)
where TComp : IComponent
{
subs.SubscribeLocalEvent<TComp, ItemStatusCollectMessage>((uid, _, args) =>
subs.SubscribeLocalEvent((Entity<TComp> entity, ref ItemStatusCollectMessage args) =>
{
args.Controls.Add(createControl(uid));
var control = createControl(entity);
if (control == null)
return;
args.Controls.Add(control);
});
}
}

View File

@@ -17,15 +17,10 @@ public sealed class HandheldLightSystem : SharedHandheldLightSystem
{
base.Initialize();
SubscribeLocalEvent<HandheldLightComponent, ItemStatusCollectMessage>(OnGetStatusControl);
Subs.ItemStatus<HandheldLightComponent>(ent => new HandheldLightStatus(ent));
SubscribeLocalEvent<HandheldLightComponent, AppearanceChangeEvent>(OnAppearanceChange);
}
private static void OnGetStatusControl(EntityUid uid, HandheldLightComponent component, ItemStatusCollectMessage args)
{
args.Controls.Add(new HandheldLightStatus(component));
}
private void OnAppearanceChange(EntityUid uid, HandheldLightComponent? component, ref AppearanceChangeEvent args)
{
if (!Resolve(uid, ref component))

View File

@@ -31,13 +31,13 @@ public sealed class NetworkConfiguratorSystem : SharedNetworkConfiguratorSystem
base.Initialize();
SubscribeLocalEvent<ClearAllOverlaysEvent>(_ => ClearAllOverlays());
SubscribeLocalEvent<NetworkConfiguratorComponent, ItemStatusCollectMessage>(OnCollectItemStatus);
Subs.ItemStatus<NetworkConfiguratorComponent>(OnCollectItemStatus);
}
private void OnCollectItemStatus(EntityUid uid, NetworkConfiguratorComponent configurator, ItemStatusCollectMessage args)
private Control OnCollectItemStatus(Entity<NetworkConfiguratorComponent> entity)
{
_inputManager.TryGetKeyBinding((ContentKeyFunctions.AltUseItemInHand), out var binding);
args.Controls.Add(new StatusControl(configurator, binding?.GetKeyString() ?? ""));
return new StatusControl(entity, binding?.GetKeyString() ?? "");
}
public bool ConfiguredListIsTracked(EntityUid uid, NetworkConfiguratorComponent? component = null)

View File

@@ -11,19 +11,11 @@ public sealed class GeigerSystem : SharedGeigerSystem
{
base.Initialize();
SubscribeLocalEvent<GeigerComponent, AfterAutoHandleStateEvent>(OnHandleState);
SubscribeLocalEvent<GeigerComponent, ItemStatusCollectMessage>(OnGetStatusMessage);
Subs.ItemStatus<GeigerComponent>(ent => ent.Comp.ShowControl ? new GeigerItemControl(ent) : null);
}
private void OnHandleState(EntityUid uid, GeigerComponent component, ref AfterAutoHandleStateEvent args)
{
component.UiUpdateNeeded = true;
}
private void OnGetStatusMessage(EntityUid uid, GeigerComponent component, ItemStatusCollectMessage args)
{
if (!component.ShowControl)
return;
args.Controls.Add(new GeigerItemControl(component));
}
}

View File

@@ -16,13 +16,8 @@ namespace Content.Client.Stack
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StackComponent, ItemStatusCollectMessage>(OnItemStatus);
SubscribeLocalEvent<StackComponent, AppearanceChangeEvent>(OnAppearanceChange);
}
private void OnItemStatus(EntityUid uid, StackComponent component, ItemStatusCollectMessage args)
{
args.Controls.Add(new StackStatusControl(component));
Subs.ItemStatus<StackComponent>(ent => new StackStatusControl(ent));
}
public override void SetCount(EntityUid uid, int amount, StackComponent? component = null)

View File

@@ -16,8 +16,8 @@ namespace Content.Client.Tools
base.Initialize();
SubscribeLocalEvent<WelderComponent, ComponentHandleState>(OnWelderHandleState);
SubscribeLocalEvent<WelderComponent, ItemStatusCollectMessage>(OnWelderGetStatusMessage);
SubscribeLocalEvent<MultipleToolComponent, ItemStatusCollectMessage>(OnGetStatusMessage);
Subs.ItemStatus<WelderComponent>(ent => new WelderStatusControl(ent));
Subs.ItemStatus<MultipleToolComponent>(ent => new MultipleToolStatusControl(ent));
}
public override void SetMultipleTool(EntityUid uid,
@@ -43,16 +43,6 @@ namespace Content.Client.Tools
}
}
private void OnGetStatusMessage(EntityUid uid, MultipleToolComponent welder, ItemStatusCollectMessage args)
{
args.Controls.Add(new MultipleToolStatusControl(welder));
}
private void OnWelderGetStatusMessage(EntityUid uid, WelderComponent component, ItemStatusCollectMessage args)
{
args.Controls.Add(new WelderStatusControl(component, uid));
}
private void OnWelderHandleState(EntityUid uid, WelderComponent welder, ref ComponentHandleState args)
{
if (args.Current is not WelderComponentState state)