1
0

CrewMonitoringWindow.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Linq;
  3. using System.Numerics;
  4. using Content.Client.Pinpointer.UI;
  5. using Content.Client.Stylesheets;
  6. using Content.Client.UserInterface.Controls;
  7. using Content.Shared.Medical.SuitSensor;
  8. using Content.Shared.StatusIcon;
  9. using Robust.Client.AutoGenerated;
  10. using Robust.Client.GameObjects;
  11. using Robust.Client.Graphics;
  12. using Robust.Client.UserInterface;
  13. using Robust.Client.UserInterface.Controls;
  14. using Robust.Client.UserInterface.XAML;
  15. using Robust.Shared.Map;
  16. using Robust.Shared.Prototypes;
  17. using Robust.Shared.Timing;
  18. using Robust.Shared.Utility;
  19. using static Robust.Client.UserInterface.Controls.BoxContainer;
  20. namespace Content.Client.Medical.CrewMonitoring;
  21. [GenerateTypedNameReferences]
  22. public sealed partial class CrewMonitoringWindow : FancyWindow
  23. {
  24. [Dependency] private readonly IEntityManager _entManager = default!;
  25. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  26. private readonly SharedTransformSystem _transformSystem;
  27. private readonly SpriteSystem _spriteSystem;
  28. private NetEntity? _trackedEntity;
  29. private bool _tryToScrollToListFocus;
  30. private Texture? _blipTexture;
  31. public CrewMonitoringWindow()
  32. {
  33. RobustXamlLoader.Load(this);
  34. IoCManager.InjectDependencies(this);
  35. _transformSystem = _entManager.System<SharedTransformSystem>();
  36. _spriteSystem = _entManager.System<SpriteSystem>();
  37. NavMap.TrackedEntitySelectedAction += SetTrackedEntityFromNavMap;
  38. }
  39. public void Set(string stationName, EntityUid? mapUid)
  40. {
  41. _blipTexture = _spriteSystem.Frame0(new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/NavMap/beveled_circle.png")));
  42. if (_entManager.TryGetComponent<TransformComponent>(mapUid, out var xform))
  43. NavMap.MapUid = xform.GridUid;
  44. else
  45. NavMap.Visible = false;
  46. StationName.AddStyleClass("LabelBig");
  47. StationName.Text = stationName;
  48. NavMap.ForceNavMapUpdate();
  49. }
  50. protected override void FrameUpdate(FrameEventArgs args)
  51. {
  52. base.FrameUpdate(args);
  53. if (_tryToScrollToListFocus)
  54. TryToScrollToFocus();
  55. }
  56. public void ShowSensors(List<SuitSensorStatus> sensors, EntityUid monitor, EntityCoordinates? monitorCoords)
  57. {
  58. ClearOutDatedData();
  59. // No server label
  60. if (sensors.Count == 0)
  61. {
  62. NoServerLabel.Visible = true;
  63. return;
  64. }
  65. NoServerLabel.Visible = false;
  66. // Order sensor data
  67. var orderedSensors = sensors.OrderBy(n => n.Name).OrderBy(j => j.Job);
  68. var assignedSensors = new HashSet<SuitSensorStatus>();
  69. var departments = sensors.SelectMany(d => d.JobDepartments).Distinct().OrderBy(n => n);
  70. // Create department labels and populate lists
  71. foreach (var department in departments)
  72. {
  73. var departmentSensors = orderedSensors.Where(d => d.JobDepartments.Contains(department));
  74. if (departmentSensors == null || !departmentSensors.Any())
  75. continue;
  76. foreach (var sensor in departmentSensors)
  77. assignedSensors.Add(sensor);
  78. if (SensorsTable.ChildCount > 0)
  79. {
  80. var spacer = new Control()
  81. {
  82. SetHeight = 20,
  83. };
  84. SensorsTable.AddChild(spacer);
  85. }
  86. var deparmentLabel = new RichTextLabel()
  87. {
  88. Margin = new Thickness(10, 0),
  89. HorizontalExpand = true,
  90. };
  91. deparmentLabel.SetMessage(department);
  92. deparmentLabel.StyleClasses.Add(StyleNano.StyleClassTooltipActionDescription);
  93. SensorsTable.AddChild(deparmentLabel);
  94. PopulateDepartmentList(departmentSensors);
  95. }
  96. // Account for any non-station users
  97. var remainingSensors = orderedSensors.Except(assignedSensors);
  98. if (remainingSensors.Any())
  99. {
  100. var spacer = new Control()
  101. {
  102. SetHeight = 20,
  103. };
  104. SensorsTable.AddChild(spacer);
  105. var deparmentLabel = new RichTextLabel()
  106. {
  107. Margin = new Thickness(10, 0),
  108. HorizontalExpand = true,
  109. };
  110. deparmentLabel.SetMessage(Loc.GetString("crew-monitoring-user-interface-no-department"));
  111. deparmentLabel.StyleClasses.Add(StyleNano.StyleClassTooltipActionDescription);
  112. SensorsTable.AddChild(deparmentLabel);
  113. PopulateDepartmentList(remainingSensors);
  114. }
  115. // Show monitor on nav map
  116. if (monitorCoords != null && _blipTexture != null)
  117. {
  118. NavMap.TrackedEntities[_entManager.GetNetEntity(monitor)] = new NavMapBlip(monitorCoords.Value, _blipTexture, Color.Cyan, true, false);
  119. }
  120. }
  121. private void PopulateDepartmentList(IEnumerable<SuitSensorStatus> departmentSensors)
  122. {
  123. // Populate departments
  124. foreach (var sensor in departmentSensors)
  125. {
  126. if (!string.IsNullOrEmpty(SearchLineEdit.Text)
  127. && !sensor.Name.Contains(SearchLineEdit.Text, StringComparison.CurrentCultureIgnoreCase)
  128. && !sensor.Job.Contains(SearchLineEdit.Text, StringComparison.CurrentCultureIgnoreCase))
  129. continue;
  130. var coordinates = _entManager.GetCoordinates(sensor.Coordinates);
  131. // Add a button that will hold a username and other details
  132. NavMap.LocalizedNames.TryAdd(sensor.SuitSensorUid, sensor.Name + ", " + sensor.Job);
  133. var sensorButton = new CrewMonitoringButton()
  134. {
  135. SuitSensorUid = sensor.SuitSensorUid,
  136. Coordinates = coordinates,
  137. Disabled = (coordinates == null),
  138. HorizontalExpand = true,
  139. };
  140. if (sensor.SuitSensorUid == _trackedEntity)
  141. sensorButton.AddStyleClass(StyleNano.StyleClassButtonColorGreen);
  142. SensorsTable.AddChild(sensorButton);
  143. // Primary container to hold the button UI elements
  144. var mainContainer = new BoxContainer()
  145. {
  146. Orientation = LayoutOrientation.Horizontal,
  147. HorizontalExpand = true,
  148. };
  149. sensorButton.AddChild(mainContainer);
  150. // User status container
  151. var statusContainer = new BoxContainer()
  152. {
  153. SizeFlagsStretchRatio = 1.25f,
  154. Orientation = LayoutOrientation.Horizontal,
  155. HorizontalExpand = true,
  156. };
  157. mainContainer.AddChild(statusContainer);
  158. // Suit coords indicator
  159. var suitCoordsIndicator = new TextureRect()
  160. {
  161. Texture = _blipTexture,
  162. TextureScale = new Vector2(0.25f, 0.25f),
  163. Modulate = coordinates != null ? Color.LimeGreen : Color.DarkRed,
  164. HorizontalAlignment = HAlignment.Center,
  165. VerticalAlignment = VAlignment.Center,
  166. };
  167. statusContainer.AddChild(suitCoordsIndicator);
  168. // Specify texture for the user status icon
  169. var specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Alerts/human_crew_monitoring.rsi"), "alive");
  170. if (!sensor.IsAlive)
  171. {
  172. specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Alerts/human_crew_monitoring.rsi"), "dead");
  173. }
  174. else if (sensor.DamagePercentage != null)
  175. {
  176. var index = MathF.Round(4f * sensor.DamagePercentage.Value);
  177. if (index >= 5)
  178. specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Alerts/human_crew_monitoring.rsi"), "critical");
  179. else
  180. specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Alerts/human_crew_monitoring.rsi"), "health" + index);
  181. }
  182. // Status icon
  183. var statusIcon = new AnimatedTextureRect
  184. {
  185. HorizontalAlignment = HAlignment.Center,
  186. VerticalAlignment = VAlignment.Center,
  187. Margin = new Thickness(0, 1, 3, 0),
  188. };
  189. statusIcon.SetFromSpriteSpecifier(specifier);
  190. statusIcon.DisplayRect.TextureScale = new Vector2(2f, 2f);
  191. statusContainer.AddChild(statusIcon);
  192. // User name
  193. var nameLabel = new Label()
  194. {
  195. Text = sensor.Name,
  196. HorizontalExpand = true,
  197. ClipText = true,
  198. };
  199. statusContainer.AddChild(nameLabel);
  200. // User job container
  201. var jobContainer = new BoxContainer()
  202. {
  203. Orientation = LayoutOrientation.Horizontal,
  204. HorizontalExpand = true,
  205. };
  206. mainContainer.AddChild(jobContainer);
  207. // Job icon
  208. if (_prototypeManager.TryIndex<JobIconPrototype>(sensor.JobIcon, out var proto))
  209. {
  210. var jobIcon = new TextureRect()
  211. {
  212. TextureScale = new Vector2(2f, 2f),
  213. VerticalAlignment = VAlignment.Center,
  214. Texture = _spriteSystem.Frame0(proto.Icon),
  215. Margin = new Thickness(5, 0, 5, 0),
  216. };
  217. jobContainer.AddChild(jobIcon);
  218. }
  219. // Job name
  220. var jobLabel = new Label()
  221. {
  222. Text = sensor.Job,
  223. HorizontalExpand = true,
  224. ClipText = true,
  225. };
  226. jobContainer.AddChild(jobLabel);
  227. // Add user coordinates to the navmap
  228. if (coordinates != null && NavMap.Visible && _blipTexture != null)
  229. {
  230. NavMap.TrackedEntities.TryAdd(sensor.SuitSensorUid,
  231. new NavMapBlip
  232. (CoordinatesToLocal(coordinates.Value),
  233. _blipTexture,
  234. (_trackedEntity == null || sensor.SuitSensorUid == _trackedEntity) ? Color.LimeGreen : Color.LimeGreen * Color.DimGray,
  235. sensor.SuitSensorUid == _trackedEntity));
  236. NavMap.Focus = _trackedEntity;
  237. // On button up
  238. sensorButton.OnButtonUp += args =>
  239. {
  240. var prevTrackedEntity = _trackedEntity;
  241. if (_trackedEntity == sensor.SuitSensorUid)
  242. {
  243. _trackedEntity = null;
  244. }
  245. else
  246. {
  247. _trackedEntity = sensor.SuitSensorUid;
  248. NavMap.CenterToCoordinates(coordinates.Value);
  249. }
  250. NavMap.Focus = _trackedEntity;
  251. UpdateSensorsTable(_trackedEntity, prevTrackedEntity);
  252. };
  253. }
  254. }
  255. }
  256. private void SetTrackedEntityFromNavMap(NetEntity? netEntity)
  257. {
  258. var prevTrackedEntity = _trackedEntity;
  259. _trackedEntity = netEntity;
  260. if (_trackedEntity == prevTrackedEntity)
  261. prevTrackedEntity = null;
  262. NavMap.Focus = _trackedEntity;
  263. _tryToScrollToListFocus = true;
  264. UpdateSensorsTable(_trackedEntity, prevTrackedEntity);
  265. }
  266. private void UpdateSensorsTable(NetEntity? currTrackedEntity, NetEntity? prevTrackedEntity)
  267. {
  268. foreach (var sensor in SensorsTable.Children)
  269. {
  270. if (sensor is not CrewMonitoringButton)
  271. continue;
  272. var castSensor = (CrewMonitoringButton) sensor;
  273. if (castSensor.SuitSensorUid == prevTrackedEntity)
  274. castSensor.RemoveStyleClass(StyleNano.StyleClassButtonColorGreen);
  275. else if (castSensor.SuitSensorUid == currTrackedEntity)
  276. castSensor.AddStyleClass(StyleNano.StyleClassButtonColorGreen);
  277. if (castSensor?.Coordinates == null)
  278. continue;
  279. if (NavMap.TrackedEntities.TryGetValue(castSensor.SuitSensorUid, out var data))
  280. {
  281. data = new NavMapBlip
  282. (CoordinatesToLocal(data.Coordinates),
  283. data.Texture,
  284. (currTrackedEntity == null || castSensor.SuitSensorUid == currTrackedEntity) ? Color.LimeGreen : Color.LimeGreen * Color.DimGray,
  285. castSensor.SuitSensorUid == currTrackedEntity);
  286. NavMap.TrackedEntities[castSensor.SuitSensorUid] = data;
  287. }
  288. }
  289. }
  290. private void TryToScrollToFocus()
  291. {
  292. if (!_tryToScrollToListFocus)
  293. return;
  294. if (TryGetNextScrollPosition(out float? nextScrollPosition))
  295. {
  296. SensorScroller.VScrollTarget = nextScrollPosition.Value;
  297. if (MathHelper.CloseToPercent(SensorScroller.VScroll, SensorScroller.VScrollTarget))
  298. {
  299. _tryToScrollToListFocus = false;
  300. return;
  301. }
  302. }
  303. }
  304. private bool TryGetNextScrollPosition([NotNullWhen(true)] out float? nextScrollPosition)
  305. {
  306. nextScrollPosition = 0;
  307. foreach (var sensor in SensorsTable.Children)
  308. {
  309. if (sensor is CrewMonitoringButton &&
  310. ((CrewMonitoringButton) sensor).SuitSensorUid == _trackedEntity)
  311. return true;
  312. nextScrollPosition += sensor.Height;
  313. }
  314. // Failed to find control
  315. nextScrollPosition = null;
  316. return false;
  317. }
  318. /// <summary>
  319. /// Converts the input coordinates to an EntityCoordinates which are in
  320. /// reference to the grid that the map is displaying. This is a stylistic
  321. /// choice; this window deliberately limits the rate that blips update,
  322. /// but if the blip is attached to another grid which is moving, that
  323. /// blip will move smoothly, unlike the others. By converting the
  324. /// coordinates, we are back in control of the blip movement.
  325. /// </summary>
  326. private EntityCoordinates CoordinatesToLocal(EntityCoordinates refCoords)
  327. {
  328. if (NavMap.MapUid != null)
  329. {
  330. return _transformSystem.WithEntityId(refCoords, (EntityUid)NavMap.MapUid);
  331. }
  332. else
  333. {
  334. return refCoords;
  335. }
  336. }
  337. private void ClearOutDatedData()
  338. {
  339. SensorsTable.RemoveAllChildren();
  340. NavMap.TrackedCoordinates.Clear();
  341. NavMap.TrackedEntities.Clear();
  342. NavMap.LocalizedNames.Clear();
  343. }
  344. }
  345. public sealed class CrewMonitoringButton : Button
  346. {
  347. public int IndexInTable;
  348. public NetEntity SuitSensorUid;
  349. public EntityCoordinates? Coordinates;
  350. }