NetworkConfiguratorSystem.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Content.Client.Actions;
  2. using Content.Client.Items;
  3. using Content.Client.Message;
  4. using Content.Client.Stylesheets;
  5. using Content.Shared.DeviceNetwork.Components;
  6. using Content.Shared.DeviceNetwork.Systems;
  7. using Content.Shared.Input;
  8. using Robust.Client.Graphics;
  9. using Robust.Client.Input;
  10. using Robust.Client.Player;
  11. using Robust.Client.UserInterface;
  12. using Robust.Client.UserInterface.Controls;
  13. using Robust.Shared.Console;
  14. using Robust.Shared.Prototypes;
  15. using Robust.Shared.Timing;
  16. namespace Content.Client.NetworkConfigurator.Systems;
  17. public sealed class NetworkConfiguratorSystem : SharedNetworkConfiguratorSystem
  18. {
  19. [Dependency] private readonly IPlayerManager _playerManager = default!;
  20. [Dependency] private readonly IOverlayManager _overlay = default!;
  21. [Dependency] private readonly ActionsSystem _actions = default!;
  22. [Dependency] private readonly IInputManager _inputManager = default!;
  23. [ValidatePrototypeId<EntityPrototype>]
  24. private const string Action = "ActionClearNetworkLinkOverlays";
  25. public override void Initialize()
  26. {
  27. base.Initialize();
  28. SubscribeLocalEvent<ClearAllOverlaysEvent>(_ => ClearAllOverlays());
  29. Subs.ItemStatus<NetworkConfiguratorComponent>(OnCollectItemStatus);
  30. }
  31. private Control OnCollectItemStatus(Entity<NetworkConfiguratorComponent> entity)
  32. {
  33. _inputManager.TryGetKeyBinding((ContentKeyFunctions.AltUseItemInHand), out var binding);
  34. return new StatusControl(entity, binding?.GetKeyString() ?? "");
  35. }
  36. public bool ConfiguredListIsTracked(EntityUid uid, NetworkConfiguratorComponent? component = null)
  37. {
  38. return Resolve(uid, ref component)
  39. && component.ActiveDeviceList != null
  40. && HasComp<NetworkConfiguratorActiveLinkOverlayComponent>(component.ActiveDeviceList.Value);
  41. }
  42. /// <summary>
  43. /// Toggles a device list's (tied to this network configurator) connection visualisation on and off.
  44. /// </summary>
  45. public void ToggleVisualization(EntityUid uid, bool toggle, NetworkConfiguratorComponent? component = null)
  46. {
  47. if (_playerManager.LocalEntity == null
  48. || !Resolve(uid, ref component)
  49. || component.ActiveDeviceList == null)
  50. return;
  51. if (!toggle)
  52. {
  53. RemComp<NetworkConfiguratorActiveLinkOverlayComponent>(component.ActiveDeviceList.Value);
  54. if (!_overlay.TryGetOverlay(out NetworkConfiguratorLinkOverlay? overlay))
  55. return;
  56. overlay.Colors.Remove(component.ActiveDeviceList.Value);
  57. if (overlay.Colors.Count > 0)
  58. return;
  59. _actions.RemoveAction(overlay.Action);
  60. _overlay.RemoveOverlay<NetworkConfiguratorLinkOverlay>();
  61. return;
  62. }
  63. if (!_overlay.HasOverlay<NetworkConfiguratorLinkOverlay>())
  64. {
  65. var overlay = new NetworkConfiguratorLinkOverlay();
  66. _overlay.AddOverlay(overlay);
  67. var player = _playerManager.LocalEntity.Value;
  68. overlay.Action = Spawn(Action);
  69. _actions.AddActionDirect(player, overlay.Action.Value);
  70. }
  71. EnsureComp<NetworkConfiguratorActiveLinkOverlayComponent>(component.ActiveDeviceList.Value);
  72. }
  73. public void ClearAllOverlays()
  74. {
  75. if (!_overlay.TryGetOverlay(out NetworkConfiguratorLinkOverlay? overlay))
  76. {
  77. return;
  78. }
  79. var query = EntityQueryEnumerator<NetworkConfiguratorActiveLinkOverlayComponent>();
  80. while (query.MoveNext(out var uid, out _))
  81. {
  82. RemCompDeferred<NetworkConfiguratorActiveLinkOverlayComponent>(uid);
  83. }
  84. _actions.RemoveAction(overlay.Action);
  85. _overlay.RemoveOverlay(overlay);
  86. }
  87. private sealed class StatusControl : Control
  88. {
  89. private readonly RichTextLabel _label;
  90. private readonly NetworkConfiguratorComponent _configurator;
  91. private readonly string _keyBindingName;
  92. private bool? _linkModeActive = null;
  93. public StatusControl(NetworkConfiguratorComponent configurator, string keyBindingName)
  94. {
  95. _configurator = configurator;
  96. _keyBindingName = keyBindingName;
  97. _label = new RichTextLabel { StyleClasses = { StyleNano.StyleClassItemStatus } };
  98. AddChild(_label);
  99. }
  100. protected override void FrameUpdate(FrameEventArgs args)
  101. {
  102. base.FrameUpdate(args);
  103. if (_linkModeActive != null && _linkModeActive == _configurator.LinkModeActive)
  104. return;
  105. _linkModeActive = _configurator.LinkModeActive;
  106. var modeLocString = _linkModeActive??false
  107. ? "network-configurator-examine-mode-link"
  108. : "network-configurator-examine-mode-list";
  109. _label.SetMarkup(Robust.Shared.Localization.Loc.GetString("network-configurator-item-status-label",
  110. ("mode", Robust.Shared.Localization.Loc.GetString(modeLocString)),
  111. ("keybinding", _keyBindingName)));
  112. }
  113. }
  114. }
  115. public sealed class ClearAllNetworkLinkOverlays : IConsoleCommand
  116. {
  117. [Dependency] private readonly IEntityManager _e = default!;
  118. public string Command => "clearnetworklinkoverlays";
  119. public string Description => "Clear all network link overlays.";
  120. public string Help => Command;
  121. public void Execute(IConsoleShell shell, string argStr, string[] args)
  122. {
  123. _e.System<NetworkConfiguratorSystem>().ClearAllOverlays();
  124. }
  125. }