GameplayStateBase.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Client.Clickable;
  4. using Content.Client.UserInterface;
  5. using Content.Client.Viewport;
  6. using Content.Shared.Input;
  7. using Robust.Client.ComponentTrees;
  8. using Robust.Client.GameObjects;
  9. using Robust.Client.Graphics;
  10. using Robust.Client.Input;
  11. using Robust.Client.Player;
  12. using Robust.Client.State;
  13. using Robust.Client.UserInterface;
  14. using Robust.Client.UserInterface.Controls;
  15. using Robust.Client.UserInterface.CustomControls;
  16. using Robust.Shared.Console;
  17. using Robust.Shared.Graphics;
  18. using Robust.Shared.Input;
  19. using Robust.Shared.Input.Binding;
  20. using Robust.Shared.Map;
  21. using Robust.Shared.Player;
  22. using Robust.Shared.Timing;
  23. using YamlDotNet.Serialization.TypeInspectors;
  24. namespace Content.Client.Gameplay
  25. {
  26. // OH GOD.
  27. // Ok actually it's fine.
  28. // Instantiated dynamically through the StateManager, Dependencies will be resolved.
  29. [Virtual]
  30. public class GameplayStateBase : State, IEntityEventSubscriber
  31. {
  32. [Dependency] private readonly IEyeManager _eyeManager = default!;
  33. [Dependency] private readonly IInputManager _inputManager = default!;
  34. [Dependency] private readonly IPlayerManager _playerManager = default!;
  35. [Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
  36. [Dependency] private readonly IGameTiming _timing = default!;
  37. [Dependency] private readonly IMapManager _mapManager = default!;
  38. [Dependency] protected readonly IUserInterfaceManager UserInterfaceManager = default!;
  39. [Dependency] private readonly IEntityManager _entityManager = default!;
  40. [Dependency] private readonly IViewVariablesManager _vvm = default!;
  41. [Dependency] private readonly IConsoleHost _conHost = default!;
  42. private ClickableEntityComparer _comparer = default!;
  43. private (ViewVariablesPath? path, string[] segments) ResolveVvHoverObject(string path)
  44. {
  45. var segments = path.Split('/');
  46. var uid = RecursivelyFindUiEntity(UserInterfaceManager.CurrentlyHovered);
  47. var netUid = _entityManager.GetNetEntity(uid);
  48. return (netUid != null ? new ViewVariablesInstancePath(netUid) : null, segments);
  49. }
  50. private EntityUid? RecursivelyFindUiEntity(Control? control)
  51. {
  52. if (control == null)
  53. return null;
  54. switch (control)
  55. {
  56. case IViewportControl vp:
  57. if (_inputManager.MouseScreenPosition.IsValid)
  58. return GetClickedEntity(vp.PixelToMap(_inputManager.MouseScreenPosition.Position));
  59. return null;
  60. case SpriteView sprite:
  61. return sprite.Entity;
  62. case IEntityControl ui:
  63. return ui.UiEntity;
  64. }
  65. return RecursivelyFindUiEntity(control.Parent);
  66. }
  67. private IEnumerable<string>? ListVVHoverPaths(string[] segments)
  68. {
  69. return null;
  70. }
  71. protected override void Startup()
  72. {
  73. _vvm.RegisterDomain("enthover", ResolveVvHoverObject, ListVVHoverPaths);
  74. _inputManager.KeyBindStateChanged += OnKeyBindStateChanged;
  75. _comparer = new ClickableEntityComparer();
  76. CommandBinds.Builder
  77. .Bind(ContentKeyFunctions.InspectEntity, new PointerInputCmdHandler(HandleInspect, outsidePrediction: true))
  78. .Register<GameplayStateBase>();
  79. }
  80. protected override void Shutdown()
  81. {
  82. _vvm.UnregisterDomain("enthover");
  83. _inputManager.KeyBindStateChanged -= OnKeyBindStateChanged;
  84. CommandBinds.Unregister<GameplayStateBase>();
  85. }
  86. private bool HandleInspect(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
  87. {
  88. _conHost.ExecuteCommand($"vv /c/enthover");
  89. return true;
  90. }
  91. public EntityUid? GetClickedEntity(MapCoordinates coordinates)
  92. {
  93. return GetClickedEntity(coordinates, _eyeManager.CurrentEye);
  94. }
  95. public EntityUid? GetClickedEntity(MapCoordinates coordinates, IEye? eye)
  96. {
  97. if (eye == null)
  98. return null;
  99. var first = GetClickableEntities(coordinates, eye).FirstOrDefault();
  100. return first.IsValid() ? first : null;
  101. }
  102. public IEnumerable<EntityUid> GetClickableEntities(EntityCoordinates coordinates)
  103. {
  104. var transformSystem = _entitySystemManager.GetEntitySystem<SharedTransformSystem>();
  105. return GetClickableEntities(transformSystem.ToMapCoordinates(coordinates));
  106. }
  107. public IEnumerable<EntityUid> GetClickableEntities(MapCoordinates coordinates)
  108. {
  109. return GetClickableEntities(coordinates, _eyeManager.CurrentEye);
  110. }
  111. public IEnumerable<EntityUid> GetClickableEntities(MapCoordinates coordinates, IEye? eye)
  112. {
  113. /*
  114. * TODO:
  115. * 1. Stuff like MeleeWeaponSystem need an easy way to hook into viewport specific entities / entities under mouse
  116. * 2. Cleanup the mess around InteractionOutlineSystem + below the keybind click detection.
  117. */
  118. if (eye == null)
  119. return Array.Empty<EntityUid>();
  120. // Find all the entities intersecting our click
  121. var spriteTree = _entityManager.EntitySysManager.GetEntitySystem<SpriteTreeSystem>();
  122. var entities = spriteTree.QueryAabb(coordinates.MapId, Box2.CenteredAround(coordinates.Position, new Vector2(1, 1)));
  123. // Check the entities against whether or not we can click them
  124. var foundEntities = new List<(EntityUid, int, uint, float)>(entities.Count);
  125. var clickQuery = _entityManager.GetEntityQuery<ClickableComponent>();
  126. var clickables = _entityManager.System<ClickableSystem>();
  127. foreach (var entity in entities)
  128. {
  129. if (clickQuery.TryGetComponent(entity.Uid, out var component) &&
  130. clickables.CheckClick((entity.Uid, component, entity.Component, entity.Transform), coordinates.Position, eye, out var drawDepthClicked, out var renderOrder, out var bottom))
  131. {
  132. foundEntities.Add((entity.Uid, drawDepthClicked, renderOrder, bottom));
  133. }
  134. }
  135. if (foundEntities.Count == 0)
  136. return Array.Empty<EntityUid>();
  137. // Do drawdepth & y-sorting. First index is the top-most sprite (opposite of normal render order).
  138. foundEntities.Sort(_comparer);
  139. return foundEntities.Select(a => a.Item1);
  140. }
  141. private sealed class ClickableEntityComparer : IComparer<(EntityUid clicked, int depth, uint renderOrder, float bottom)>
  142. {
  143. public int Compare((EntityUid clicked, int depth, uint renderOrder, float bottom) x,
  144. (EntityUid clicked, int depth, uint renderOrder, float bottom) y)
  145. {
  146. var cmp = y.depth.CompareTo(x.depth);
  147. if (cmp != 0)
  148. {
  149. return cmp;
  150. }
  151. cmp = y.renderOrder.CompareTo(x.renderOrder);
  152. if (cmp != 0)
  153. {
  154. return cmp;
  155. }
  156. cmp = -y.bottom.CompareTo(x.bottom);
  157. if (cmp != 0)
  158. {
  159. return cmp;
  160. }
  161. return y.clicked.CompareTo(x.clicked);
  162. }
  163. }
  164. /// <summary>
  165. /// Converts a state change event from outside the simulation to inside the simulation.
  166. /// </summary>
  167. /// <param name="args">Event data values for a bound key state change.</param>
  168. protected virtual void OnKeyBindStateChanged(ViewportBoundKeyEventArgs args)
  169. {
  170. // If there is no InputSystem, then there is nothing to forward to, and nothing to do here.
  171. if(!_entitySystemManager.TryGetEntitySystem(out InputSystem? inputSys))
  172. return;
  173. var kArgs = args.KeyEventArgs;
  174. var func = kArgs.Function;
  175. var funcId = _inputManager.NetworkBindMap.KeyFunctionID(func);
  176. EntityCoordinates coordinates = default;
  177. EntityUid? entityToClick = null;
  178. if (args.Viewport is IViewportControl vp && kArgs.PointerLocation.IsValid)
  179. {
  180. var mousePosWorld = vp.PixelToMap(kArgs.PointerLocation.Position);
  181. if (vp is ScalingViewport svp)
  182. {
  183. entityToClick = GetClickedEntity(mousePosWorld, svp.Eye);
  184. }
  185. else
  186. {
  187. entityToClick = GetClickedEntity(mousePosWorld);
  188. }
  189. var transformSystem = _entitySystemManager.GetEntitySystem<SharedTransformSystem>();
  190. var mapSystem = _entitySystemManager.GetEntitySystem<MapSystem>();
  191. coordinates = _mapManager.TryFindGridAt(mousePosWorld, out var uid, out _) ?
  192. mapSystem.MapToGrid(uid, mousePosWorld) :
  193. transformSystem.ToCoordinates(mousePosWorld);
  194. }
  195. else
  196. {
  197. coordinates = EntityCoordinates.Invalid;
  198. }
  199. var message = new ClientFullInputCmdMessage(_timing.CurTick, _timing.TickFraction, funcId)
  200. {
  201. State = kArgs.State,
  202. Coordinates = coordinates,
  203. ScreenCoordinates = kArgs.PointerLocation,
  204. Uid = entityToClick ?? default,
  205. }; // TODO make entityUid nullable
  206. // client side command handlers will always be sent the local player session.
  207. var session = _playerManager.LocalSession;
  208. if (inputSys.HandleInputCommand(session, func, message))
  209. {
  210. kArgs.Handle();
  211. }
  212. }
  213. }
  214. }