1
0

IntrinsicUISystem.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Content.Shared.Actions;
  2. namespace Content.Shared.UserInterface;
  3. public sealed class IntrinsicUISystem : EntitySystem
  4. {
  5. [Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
  6. [Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;
  7. public override void Initialize()
  8. {
  9. SubscribeLocalEvent<IntrinsicUIComponent, MapInitEvent>(InitActions);
  10. SubscribeLocalEvent<IntrinsicUIComponent, ComponentShutdown>(OnShutdown);
  11. SubscribeLocalEvent<IntrinsicUIComponent, ToggleIntrinsicUIEvent>(OnActionToggle);
  12. }
  13. private void OnActionToggle(EntityUid uid, IntrinsicUIComponent component, ToggleIntrinsicUIEvent args)
  14. {
  15. if (args.Key == null)
  16. return;
  17. args.Handled = InteractUI(uid, args.Key, component);
  18. }
  19. private void OnShutdown(EntityUid uid, IntrinsicUIComponent component, ref ComponentShutdown args)
  20. {
  21. foreach (var actionEntry in component.UIs.Values)
  22. {
  23. var actionId = actionEntry.ToggleActionEntity;
  24. _actionsSystem.RemoveAction(uid, actionId);
  25. }
  26. }
  27. private void InitActions(EntityUid uid, IntrinsicUIComponent component, MapInitEvent args)
  28. {
  29. foreach (var entry in component.UIs.Values)
  30. {
  31. _actionsSystem.AddAction(uid, ref entry.ToggleActionEntity, entry.ToggleAction);
  32. }
  33. }
  34. public bool InteractUI(EntityUid uid, Enum key, IntrinsicUIComponent? iui = null)
  35. {
  36. if (!Resolve(uid, ref iui))
  37. return false;
  38. var attempt = new IntrinsicUIOpenAttemptEvent(uid, key);
  39. RaiseLocalEvent(uid, attempt);
  40. if (attempt.Cancelled)
  41. return false;
  42. return _uiSystem.TryToggleUi(uid, key, uid);
  43. }
  44. }
  45. // Competing with ActivatableUI for horrible event names.
  46. public sealed class IntrinsicUIOpenAttemptEvent : CancellableEntityEventArgs
  47. {
  48. public EntityUid User { get; }
  49. public Enum? Key { get; }
  50. public IntrinsicUIOpenAttemptEvent(EntityUid who, Enum? key)
  51. {
  52. User = who;
  53. Key = key;
  54. }
  55. }