ActivatableUISystem.Power.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Content.Shared.Item.ItemToggle;
  2. using Content.Shared.Item.ItemToggle.Components;
  3. using Content.Shared.PowerCell;
  4. using Robust.Shared.Containers;
  5. namespace Content.Shared.UserInterface;
  6. public sealed partial class ActivatableUISystem
  7. {
  8. [Dependency] private readonly ItemToggleSystem _toggle = default!;
  9. [Dependency] private readonly SharedPowerCellSystem _cell = default!;
  10. private void InitializePower()
  11. {
  12. SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, ActivatableUIOpenAttemptEvent>(OnBatteryOpenAttempt);
  13. SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, BoundUIOpenedEvent>(OnBatteryOpened);
  14. SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, BoundUIClosedEvent>(OnBatteryClosed);
  15. SubscribeLocalEvent<ActivatableUIRequiresPowerCellComponent, ItemToggledEvent>(OnToggled);
  16. }
  17. private void OnToggled(Entity<ActivatableUIRequiresPowerCellComponent> ent, ref ItemToggledEvent args)
  18. {
  19. // only close ui when losing power
  20. if (!TryComp<ActivatableUIComponent>(ent, out var activatable) || args.Activated)
  21. return;
  22. if (activatable.Key == null)
  23. {
  24. Log.Error($"Encountered null key in activatable ui on entity {ToPrettyString(ent)}");
  25. return;
  26. }
  27. _uiSystem.CloseUi(ent.Owner, activatable.Key);
  28. }
  29. private void OnBatteryOpened(EntityUid uid, ActivatableUIRequiresPowerCellComponent component, BoundUIOpenedEvent args)
  30. {
  31. var activatable = Comp<ActivatableUIComponent>(uid);
  32. if (!args.UiKey.Equals(activatable.Key))
  33. return;
  34. _toggle.TryActivate(uid);
  35. }
  36. private void OnBatteryClosed(EntityUid uid, ActivatableUIRequiresPowerCellComponent component, BoundUIClosedEvent args)
  37. {
  38. var activatable = Comp<ActivatableUIComponent>(uid);
  39. if (!args.UiKey.Equals(activatable.Key))
  40. return;
  41. // Stop drawing power if this was the last person with the UI open.
  42. if (!_uiSystem.IsUiOpen(uid, activatable.Key))
  43. _toggle.TryDeactivate(uid);
  44. }
  45. /// <summary>
  46. /// Call if you want to check if the UI should close due to a recent battery usage.
  47. /// </summary>
  48. public void CheckUsage(EntityUid uid, ActivatableUIComponent? active = null, ActivatableUIRequiresPowerCellComponent? component = null, PowerCellDrawComponent? draw = null)
  49. {
  50. if (!Resolve(uid, ref component, ref draw, ref active, false))
  51. return;
  52. if (active.Key == null)
  53. {
  54. Log.Error($"Encountered null key in activatable ui on entity {ToPrettyString(uid)}");
  55. return;
  56. }
  57. if (_cell.HasActivatableCharge(uid))
  58. return;
  59. _uiSystem.CloseUi(uid, active.Key);
  60. }
  61. private void OnBatteryOpenAttempt(EntityUid uid, ActivatableUIRequiresPowerCellComponent component, ActivatableUIOpenAttemptEvent args)
  62. {
  63. if (!TryComp<PowerCellDrawComponent>(uid, out var draw))
  64. return;
  65. // Check if we have the appropriate drawrate / userate to even open it.
  66. if (args.Cancelled ||
  67. !_cell.HasActivatableCharge(uid, draw, user: args.User) ||
  68. !_cell.HasDrawCharge(uid, draw, user: args.User))
  69. {
  70. args.Cancel();
  71. }
  72. }
  73. }