BaseShuttleControl.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System.Numerics;
  2. using Content.Client.UserInterface.Controls;
  3. using Content.Shared.Shuttles.Components;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.Graphics;
  6. using Robust.Client.ResourceManagement;
  7. using Robust.Client.UserInterface.XAML;
  8. using Robust.Shared.Map.Components;
  9. using Robust.Shared.Physics;
  10. using Robust.Shared.Threading;
  11. using Robust.Shared.Timing;
  12. using Robust.Shared.Utility;
  13. using Vector2 = System.Numerics.Vector2;
  14. namespace Content.Client.Shuttles.UI;
  15. /// <summary>
  16. /// Provides common functionality for radar-like displays on shuttle consoles.
  17. /// </summary>
  18. [GenerateTypedNameReferences]
  19. [Virtual]
  20. public partial class BaseShuttleControl : MapGridControl
  21. {
  22. [Dependency] private readonly IParallelManager _parallel = default!;
  23. protected readonly SharedMapSystem Maps;
  24. protected readonly Font Font;
  25. private GridDrawJob _drawJob;
  26. // Cache grid drawing data as it can be expensive to build
  27. public readonly Dictionary<EntityUid, GridDrawData> GridData = new();
  28. // Per-draw caching
  29. private readonly List<Vector2i> _gridTileList = new();
  30. private readonly HashSet<Vector2i> _gridNeighborSet = new();
  31. private readonly List<(Vector2 Start, Vector2 End)> _edges = new();
  32. private Vector2[] _allVertices = Array.Empty<Vector2>();
  33. private (DirectionFlag, Vector2i)[] _neighborDirections;
  34. public BaseShuttleControl() : this(32f, 32f, 32f)
  35. {
  36. }
  37. public BaseShuttleControl(float minRange, float maxRange, float range) : base(minRange, maxRange, range)
  38. {
  39. RobustXamlLoader.Load(this);
  40. Maps = EntManager.System<SharedMapSystem>();
  41. Font = new VectorFont(IoCManager.Resolve<IResourceCache>().GetResource<FontResource>("/Fonts/NotoSans/NotoSans-Regular.ttf"), 12);
  42. _drawJob = new GridDrawJob()
  43. {
  44. ScaledVertices = _allVertices,
  45. };
  46. _neighborDirections = new (DirectionFlag, Vector2i)[4];
  47. for (var i = 0; i < 4; i++)
  48. {
  49. var dir = (DirectionFlag) Math.Pow(2, i);
  50. var dirVec = dir.AsDir().ToIntVec();
  51. _neighborDirections[i] = (dir, dirVec);
  52. }
  53. }
  54. protected void DrawData(DrawingHandleScreen handle, string text)
  55. {
  56. var coordsDimensions = handle.GetDimensions(Font, text, 1f);
  57. const float coordsMargins = 5f;
  58. handle.DrawString(Font,
  59. new Vector2(coordsMargins, PixelHeight) - new Vector2(0f, coordsDimensions.Y + coordsMargins),
  60. text,
  61. Color.FromSrgb(IFFComponent.SelfColor));
  62. }
  63. protected void DrawCircles(DrawingHandleScreen handle)
  64. {
  65. // Equatorial lines
  66. var gridLines = Color.LightGray.WithAlpha(0.01f);
  67. // Each circle is this x distance of the last one.
  68. const float EquatorialMultiplier = 2f;
  69. var minDistance = MathF.Pow(EquatorialMultiplier, EquatorialMultiplier * 1.5f);
  70. var maxDistance = MathF.Pow(2f, EquatorialMultiplier * 6f);
  71. var cornerDistance = MathF.Sqrt(WorldRange * WorldRange + WorldRange * WorldRange);
  72. var origin = ScalePosition(-new Vector2(Offset.X, -Offset.Y));
  73. for (var radius = minDistance; radius <= maxDistance; radius *= EquatorialMultiplier)
  74. {
  75. if (radius > cornerDistance)
  76. continue;
  77. var color = Color.ToSrgb(gridLines).WithAlpha(0.05f);
  78. var scaledRadius = MinimapScale * radius;
  79. var text = $"{radius:0}m";
  80. var textDimensions = handle.GetDimensions(Font, text, UIScale);
  81. handle.DrawCircle(origin, scaledRadius, color, false);
  82. handle.DrawString(Font, ScalePosition(new Vector2(0f, -radius)) - new Vector2(0f, textDimensions.Y), text, UIScale, color);
  83. }
  84. const int gridLinesRadial = 8;
  85. for (var i = 0; i < gridLinesRadial; i++)
  86. {
  87. Angle angle = (Math.PI / gridLinesRadial) * i;
  88. // TODO: Handle distance properly.
  89. var aExtent = angle.ToVec() * ScaledMinimapRadius * 1.42f;
  90. var lineColor = Color.MediumSpringGreen.WithAlpha(0.02f);
  91. handle.DrawLine(origin - aExtent, origin + aExtent, lineColor);
  92. }
  93. }
  94. protected void DrawGrid(DrawingHandleScreen handle, Matrix3x2 gridToView, Entity<MapGridComponent> grid, Color color, float alpha = 0.01f)
  95. {
  96. var rator = Maps.GetAllTilesEnumerator(grid.Owner, grid.Comp);
  97. var minimapScale = MinimapScale;
  98. var midpoint = new Vector2(MidPoint, MidPoint);
  99. var tileSize = grid.Comp.TileSize;
  100. // Check if we even have data
  101. // TODO: Need to prune old grid-data if we don't draw it.
  102. var gridData = GridData.GetOrNew(grid.Owner);
  103. if (gridData.LastBuild < grid.Comp.LastTileModifiedTick)
  104. {
  105. gridData.Vertices.Clear();
  106. _gridTileList.Clear();
  107. _gridNeighborSet.Clear();
  108. // Okay so there's 2 steps to this
  109. // 1. Is that get we get a set of all tiles. This is used to decompose into triangle-strips
  110. // 2. Is that we get a list of all tiles. This is used for edge data to decompose into line-strips.
  111. while (rator.MoveNext(out var tileRef))
  112. {
  113. var index = tileRef.Value.GridIndices;
  114. _gridNeighborSet.Add(index);
  115. _gridTileList.Add(index);
  116. var bl = Maps.TileToVector(grid, index);
  117. var br = bl + new Vector2(tileSize, 0f);
  118. var tr = bl + new Vector2(tileSize, tileSize);
  119. var tl = bl + new Vector2(0f, tileSize);
  120. gridData.Vertices.Add(bl);
  121. gridData.Vertices.Add(br);
  122. gridData.Vertices.Add(tl);
  123. gridData.Vertices.Add(br);
  124. gridData.Vertices.Add(tl);
  125. gridData.Vertices.Add(tr);
  126. }
  127. gridData.EdgeIndex = gridData.Vertices.Count;
  128. _edges.Clear();
  129. foreach (var index in _gridTileList)
  130. {
  131. // We get all of the raw lines up front
  132. // then we decompose them into longer lines in a separate step.
  133. foreach (var (dir, dirVec) in _neighborDirections)
  134. {
  135. var neighbor = index + dirVec;
  136. if (_gridNeighborSet.Contains(neighbor))
  137. continue;
  138. var bl = Maps.TileToVector(grid, index);
  139. var br = bl + new Vector2(tileSize, 0f);
  140. var tr = bl + new Vector2(tileSize, tileSize);
  141. var tl = bl + new Vector2(0f, tileSize);
  142. // Could probably rotate this but this might be faster?
  143. Vector2 actualStart;
  144. Vector2 actualEnd;
  145. switch (dir)
  146. {
  147. case DirectionFlag.South:
  148. actualStart = bl;
  149. actualEnd = br;
  150. break;
  151. case DirectionFlag.East:
  152. actualStart = br;
  153. actualEnd = tr;
  154. break;
  155. case DirectionFlag.North:
  156. actualStart = tr;
  157. actualEnd = tl;
  158. break;
  159. case DirectionFlag.West:
  160. actualStart = tl;
  161. actualEnd = bl;
  162. break;
  163. default:
  164. throw new NotImplementedException();
  165. }
  166. _edges.Add((actualStart, actualEnd));
  167. }
  168. }
  169. // Decompose the edges into longer lines to save data.
  170. // Now we decompose the lines into longer lines (less data to send to the GPU)
  171. var decomposed = true;
  172. while (decomposed)
  173. {
  174. decomposed = false;
  175. for (var i = 0; i < _edges.Count; i++)
  176. {
  177. var (start, end) = _edges[i];
  178. var neighborFound = false;
  179. var neighborIndex = 0;
  180. Vector2 neighborStart;
  181. Vector2 neighborEnd = Vector2.Zero;
  182. // Does our end correspond with another start?
  183. for (var j = i + 1; j < _edges.Count; j++)
  184. {
  185. (neighborStart, neighborEnd) = _edges[j];
  186. if (!end.Equals(neighborStart))
  187. continue;
  188. neighborFound = true;
  189. neighborIndex = j;
  190. break;
  191. }
  192. if (!neighborFound)
  193. continue;
  194. // Check if our start and the neighbor's end are collinear
  195. if (!CollinearSimplifier.IsCollinear(start, end, neighborEnd, 10f * float.Epsilon))
  196. continue;
  197. decomposed = true;
  198. _edges[i] = (start, neighborEnd);
  199. _edges.RemoveAt(neighborIndex);
  200. }
  201. }
  202. gridData.Vertices.EnsureCapacity(_edges.Count * 2);
  203. foreach (var edge in _edges)
  204. {
  205. gridData.Vertices.Add(edge.Start);
  206. gridData.Vertices.Add(edge.End);
  207. }
  208. gridData.LastBuild = grid.Comp.LastTileModifiedTick;
  209. }
  210. var totalData = gridData.Vertices.Count;
  211. var triCount = gridData.EdgeIndex;
  212. var edgeCount = totalData - gridData.EdgeIndex;
  213. Extensions.EnsureLength(ref _allVertices, totalData);
  214. _drawJob.MidPoint = midpoint;
  215. _drawJob.Matrix = gridToView;
  216. _drawJob.MinimapScale = minimapScale;
  217. _drawJob.Vertices = gridData.Vertices;
  218. _drawJob.ScaledVertices = _allVertices;
  219. _parallel.ProcessNow(_drawJob, totalData);
  220. const float BatchSize = 3f * 4096;
  221. for (var i = 0; i < Math.Ceiling(triCount / BatchSize); i++)
  222. {
  223. var start = (int) (i * BatchSize);
  224. var end = (int) Math.Min(triCount, start + BatchSize);
  225. var count = end - start;
  226. handle.DrawPrimitives(DrawPrimitiveTopology.TriangleList, new Span<Vector2>(_allVertices, start, count), color.WithAlpha(alpha));
  227. }
  228. handle.DrawPrimitives(DrawPrimitiveTopology.LineList, new Span<Vector2>(_allVertices, gridData.EdgeIndex, edgeCount), color);
  229. }
  230. private record struct GridDrawJob : IParallelRobustJob
  231. {
  232. public int BatchSize => 64;
  233. public float MinimapScale;
  234. public Vector2 MidPoint;
  235. public Matrix3x2 Matrix;
  236. public List<Vector2> Vertices;
  237. public Vector2[] ScaledVertices;
  238. public void Execute(int index)
  239. {
  240. ScaledVertices[index] = Vector2.Transform(Vertices[index], Matrix);
  241. }
  242. }
  243. }
  244. public sealed class GridDrawData
  245. {
  246. /*
  247. * List of lists because we use LineStrip and TriangleStrip respectively (less data to pass to the GPU).
  248. */
  249. public List<Vector2> Vertices = new();
  250. /// <summary>
  251. /// Vertices index from when edges start.
  252. /// </summary>
  253. public int EdgeIndex;
  254. public GameTick LastBuild;
  255. }