ItemStatusMessages.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Robust.Client.UserInterface;
  2. namespace Content.Client.Items
  3. {
  4. /// <summary>
  5. /// Raised by the HUD logic to collect item status controls for a held entity.
  6. /// </summary>
  7. /// <remarks>
  8. /// Handlers should add any controls they want to add to <see cref="Controls"/>.
  9. /// </remarks>
  10. /// <seealso cref="ItemStatusRegisterExt"/>
  11. public sealed class ItemStatusCollectMessage : EntityEventArgs
  12. {
  13. /// <summary>
  14. /// A list of controls that will be displayed on the HUD. Handlers should add their controls here.
  15. /// </summary>
  16. public List<Control> Controls = new();
  17. }
  18. /// <summary>
  19. /// Extension methods for registering item status controls.
  20. /// </summary>
  21. /// <seealso cref="ItemStatusCollectMessage"/>
  22. public static class ItemStatusRegisterExt
  23. {
  24. /// <summary>
  25. /// Register an item status control for a component.
  26. /// </summary>
  27. /// <remarks>
  28. /// This is a convenience wrapper around <see cref="ItemStatusCollectMessage"/>.
  29. /// </remarks>
  30. /// <param name="subs">The <see cref="EntitySystem.Subs"/> handle from within entity system initialize.</param>
  31. /// <param name="createControl">
  32. /// A delegate to create the actual control.
  33. /// If the delegate returns null, no control will be added to the item status.
  34. /// </param>
  35. /// <typeparam name="TComp">The type of component for which this control should be made.</typeparam>
  36. public static void ItemStatus<TComp>(
  37. this EntitySystem.Subscriptions subs,
  38. Func<Entity<TComp>, Control?> createControl)
  39. where TComp : IComponent
  40. {
  41. subs.SubscribeLocalEvent((Entity<TComp> entity, ref ItemStatusCollectMessage args) =>
  42. {
  43. var control = createControl(entity);
  44. if (control == null)
  45. return;
  46. args.Controls.Add(control);
  47. });
  48. }
  49. }
  50. }