1
0

InteractionOutlineSystem.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Content.Client.ContextMenu.UI;
  2. using Content.Client.Gameplay;
  3. using Content.Client.Interactable.Components;
  4. using Content.Client.Viewport;
  5. using Content.Shared.CCVar;
  6. using Content.Shared.Interaction;
  7. using Robust.Client.Graphics;
  8. using Robust.Client.Input;
  9. using Robust.Client.Player;
  10. using Robust.Client.State;
  11. using Robust.Client.UserInterface;
  12. using Robust.Client.UserInterface.CustomControls;
  13. using Robust.Shared.Configuration;
  14. namespace Content.Client.Outline;
  15. public sealed class InteractionOutlineSystem : EntitySystem
  16. {
  17. [Dependency] private readonly IConfigurationManager _configManager = default!;
  18. [Dependency] private readonly IEyeManager _eyeManager = default!;
  19. [Dependency] private readonly IInputManager _inputManager = default!;
  20. [Dependency] private readonly IPlayerManager _playerManager = default!;
  21. [Dependency] private readonly IStateManager _stateManager = default!;
  22. [Dependency] private readonly IUserInterfaceManager _uiManager = default!;
  23. [Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
  24. /// <summary>
  25. /// Whether to currently draw the outline. The outline may be temporarily disabled by other systems
  26. /// </summary>
  27. private bool _enabled = true;
  28. /// <summary>
  29. /// Whether to draw the outline at all. Overrides <see cref="_enabled"/>.
  30. /// </summary>
  31. private bool _cvarEnabled = true;
  32. private EntityUid? _lastHoveredEntity;
  33. public override void Initialize()
  34. {
  35. base.Initialize();
  36. Subs.CVar(_configManager, CCVars.OutlineEnabled, SetCvarEnabled);
  37. UpdatesAfter.Add(typeof(SharedEyeSystem));
  38. }
  39. public void SetCvarEnabled(bool cvarEnabled)
  40. {
  41. _cvarEnabled = cvarEnabled;
  42. // clear last hover if required:
  43. if (_cvarEnabled)
  44. return;
  45. if (_lastHoveredEntity == null || Deleted(_lastHoveredEntity))
  46. return;
  47. if (TryComp(_lastHoveredEntity, out InteractionOutlineComponent? outline))
  48. outline.OnMouseLeave(_lastHoveredEntity.Value);
  49. }
  50. public void SetEnabled(bool enabled)
  51. {
  52. if (enabled == _enabled)
  53. return;
  54. _enabled = enabled;
  55. // clear last hover if required:
  56. if (enabled)
  57. return;
  58. if (_lastHoveredEntity == null || Deleted(_lastHoveredEntity))
  59. return;
  60. if (TryComp(_lastHoveredEntity, out InteractionOutlineComponent? outline))
  61. outline.OnMouseLeave(_lastHoveredEntity.Value);
  62. }
  63. public override void FrameUpdate(float frameTime)
  64. {
  65. base.FrameUpdate(frameTime);
  66. if (!_enabled || !_cvarEnabled)
  67. return;
  68. // If there is no local player, there is no session, and therefore nothing to do here.
  69. var localSession = _playerManager.LocalSession;
  70. if (localSession == null)
  71. return;
  72. // TODO InteractionOutlineComponent
  73. // BUG: The logic that gets the renderScale here assumes that the entity is only visible in a single
  74. // viewport. The entity will be highlighted in ALL viewport where it is visible, regardless of which
  75. // viewport is being used to hover over it. If these Viewports have very different render scales, this may
  76. // lead to extremely thick outlines in the other viewports. Fixing this probably requires changing how the
  77. // hover outline works, so that it only highlights the entity in a single viewport.
  78. // GameScreen is still in charge of what entities are visible under a specific cursor position.
  79. // Potentially change someday? who knows.
  80. var currentState = _stateManager.CurrentState;
  81. if (currentState is not GameplayStateBase screen) return;
  82. EntityUid? entityToClick = null;
  83. var renderScale = 1;
  84. if (_uiManager.CurrentlyHovered is IViewportControl vp
  85. && _inputManager.MouseScreenPosition.IsValid)
  86. {
  87. var mousePosWorld = vp.PixelToMap(_inputManager.MouseScreenPosition.Position);
  88. if (vp is ScalingViewport svp)
  89. {
  90. renderScale = svp.CurrentRenderScale;
  91. entityToClick = screen.GetClickedEntity(mousePosWorld, svp.Eye);
  92. }
  93. else
  94. {
  95. entityToClick = screen.GetClickedEntity(mousePosWorld);
  96. }
  97. }
  98. else if (_uiManager.CurrentlyHovered is EntityMenuElement element)
  99. {
  100. entityToClick = element.Entity;
  101. // TODO InteractionOutlineComponent
  102. // Currently we just take the renderscale from the main viewport. In the future, when the bug mentioned
  103. // above is fixed, the viewport should probably be the one that was clicked on to open the entity menu
  104. // in the first place.
  105. renderScale = _eyeManager.MainViewport.GetRenderScale();
  106. }
  107. var inRange = false;
  108. if (localSession.AttachedEntity != null && !Deleted(entityToClick))
  109. {
  110. inRange = _interactionSystem.InRangeUnobstructed(localSession.AttachedEntity.Value, entityToClick.Value);
  111. }
  112. InteractionOutlineComponent? outline;
  113. if (entityToClick == _lastHoveredEntity)
  114. {
  115. if (entityToClick != null && TryComp(entityToClick, out outline))
  116. {
  117. outline.UpdateInRange(entityToClick.Value, inRange, renderScale);
  118. }
  119. return;
  120. }
  121. if (_lastHoveredEntity != null && !Deleted(_lastHoveredEntity) &&
  122. TryComp(_lastHoveredEntity, out outline))
  123. {
  124. outline.OnMouseLeave(_lastHoveredEntity.Value);
  125. }
  126. _lastHoveredEntity = entityToClick;
  127. if (_lastHoveredEntity != null && TryComp(_lastHoveredEntity, out outline))
  128. {
  129. outline.OnMouseEnter(_lastHoveredEntity.Value, inRange, renderScale);
  130. }
  131. }
  132. }