1
0

LateJoinGui.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System.Linq;
  2. using System.Numerics;
  3. using Content.Client.CrewManifest;
  4. using Content.Client.GameTicking.Managers;
  5. using Content.Client.Lobby;
  6. using Content.Client.UserInterface.Controls;
  7. using Content.Client.Players.PlayTimeTracking;
  8. using Content.Shared.CCVar;
  9. using Content.Shared.Preferences;
  10. using Content.Shared.Roles;
  11. using Content.Shared.StatusIcon;
  12. using Robust.Client.Console;
  13. using Robust.Shared.Log;
  14. using Robust.Client.GameObjects;
  15. using Robust.Client.UserInterface;
  16. using Robust.Client.UserInterface.Controls;
  17. using Robust.Client.UserInterface.CustomControls;
  18. using Robust.Shared.Configuration;
  19. using Robust.Shared.Prototypes;
  20. using Robust.Shared.Utility;
  21. using static Robust.Client.UserInterface.Controls.BoxContainer;
  22. namespace Content.Client.LateJoin
  23. {
  24. public sealed class LateJoinGui : DefaultWindow
  25. {
  26. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  27. [Dependency] private readonly IClientConsoleHost _consoleHost = default!;
  28. [Dependency] private readonly IConfigurationManager _configManager = default!;
  29. [Dependency] private readonly IEntitySystemManager _entitySystem = default!;
  30. [Dependency] private readonly JobRequirementsManager _jobRequirements = default!;
  31. [Dependency] private readonly IClientPreferencesManager _preferencesManager = default!;
  32. public event Action<(NetEntity, string)> SelectedId;
  33. private readonly ISawmill _sawmill;
  34. private readonly ClientGameTicker _gameTicker;
  35. private readonly SpriteSystem _sprites;
  36. private readonly CrewManifestSystem _crewManifest;
  37. private readonly Dictionary<NetEntity, Dictionary<string, List<JobButton>>> _jobButtons = new();
  38. private readonly Dictionary<NetEntity, Dictionary<string, BoxContainer>> _jobCategories = new();
  39. private readonly List<ScrollContainer> _jobLists = new();
  40. private readonly Control _base;
  41. public LateJoinGui()
  42. {
  43. MinSize = SetSize = new Vector2(360, 560);
  44. IoCManager.InjectDependencies(this);
  45. _sprites = _entitySystem.GetEntitySystem<SpriteSystem>();
  46. _sawmill = Logger.GetSawmill("latejoin");
  47. _crewManifest = _entitySystem.GetEntitySystem<CrewManifestSystem>();
  48. _gameTicker = _entitySystem.GetEntitySystem<ClientGameTicker>();
  49. Title = Loc.GetString("late-join-gui-title");
  50. _base = new BoxContainer()
  51. {
  52. Orientation = LayoutOrientation.Vertical,
  53. VerticalExpand = true,
  54. };
  55. Contents.AddChild(_base);
  56. _jobRequirements.Updated += RebuildUI;
  57. RebuildUI();
  58. SelectedId += x =>
  59. {
  60. var (station, jobId) = x;
  61. _sawmill.Info("latejoin", $"Late joining as ID: {jobId}");
  62. _consoleHost.ExecuteCommand($"joingame {CommandParsing.Escape(jobId)} {station}");
  63. Close();
  64. };
  65. _gameTicker.LobbyJobsAvailableUpdated += JobsAvailableUpdated;
  66. _gameTicker.PlayerFactionCountsUpdated += OnPlayerFactionCountsUpdated;
  67. }
  68. private void RebuildUI()
  69. {
  70. _base.RemoveAllChildren();
  71. _jobLists.Clear();
  72. _jobButtons.Clear();
  73. _jobCategories.Clear();
  74. if (!_gameTicker.DisallowedLateJoin && _gameTicker.StationNames.Count == 0)
  75. _sawmill.Warning("No stations exist, nothing to display in late-join GUI");
  76. foreach (var (id, name) in _gameTicker.StationNames)
  77. {
  78. var jobList = new BoxContainer
  79. {
  80. Orientation = LayoutOrientation.Vertical,
  81. Margin = new Thickness(0, 0, 5f, 0),
  82. };
  83. var collapseButton = new ContainerButton()
  84. {
  85. HorizontalAlignment = HAlignment.Right,
  86. ToggleMode = true,
  87. Children =
  88. {
  89. new TextureRect
  90. {
  91. StyleClasses = { OptionButton.StyleClassOptionTriangle },
  92. Margin = new Thickness(8, 0),
  93. HorizontalAlignment = HAlignment.Center,
  94. VerticalAlignment = VAlignment.Center,
  95. }
  96. }
  97. };
  98. _base.AddChild(new StripeBack()
  99. {
  100. Children =
  101. {
  102. new PanelContainer()
  103. {
  104. Children =
  105. {
  106. new Label()
  107. {
  108. StyleClasses = { "LabelBig" },
  109. Text = name,
  110. Align = Label.AlignMode.Center,
  111. },
  112. collapseButton
  113. }
  114. }
  115. }
  116. });
  117. if (_configManager.GetCVar(CCVars.CrewManifestWithoutEntity))
  118. {
  119. var crewManifestButton = new Button()
  120. {
  121. Text = Loc.GetString("crew-manifest-button-label")
  122. };
  123. crewManifestButton.OnPressed += _ => _crewManifest.RequestCrewManifest(id);
  124. _base.AddChild(crewManifestButton);
  125. }
  126. var jobListScroll = new ScrollContainer()
  127. {
  128. VerticalExpand = true,
  129. Children = { jobList },
  130. Visible = false,
  131. };
  132. if (_jobLists.Count == 0)
  133. jobListScroll.Visible = true;
  134. _jobLists.Add(jobListScroll);
  135. _base.AddChild(jobListScroll);
  136. collapseButton.OnToggled += _ =>
  137. {
  138. foreach (var section in _jobLists)
  139. {
  140. section.Visible = false;
  141. }
  142. jobListScroll.Visible = true;
  143. };
  144. var firstCategory = true;
  145. var departments = _prototypeManager.EnumeratePrototypes<DepartmentPrototype>().ToArray();
  146. Array.Sort(departments, DepartmentUIComparer.Instance);
  147. _jobButtons[id] = new Dictionary<string, List<JobButton>>();
  148. foreach (var department in departments)
  149. {
  150. var departmentName = Loc.GetString(department.Name);
  151. _jobCategories[id] = new Dictionary<string, BoxContainer>();
  152. var stationAvailable = _gameTicker.JobsAvailable[id];
  153. var jobsAvailable = new List<JobPrototype>();
  154. foreach (var jobId in department.Roles)
  155. {
  156. if (!stationAvailable.ContainsKey(jobId))
  157. continue;
  158. jobsAvailable.Add(_prototypeManager.Index<JobPrototype>(jobId));
  159. }
  160. jobsAvailable.Sort(JobUIComparer.Instance);
  161. // Do not display departments with no jobs available.
  162. if (jobsAvailable.Count == 0)
  163. continue;
  164. var category = new BoxContainer
  165. {
  166. Orientation = LayoutOrientation.Vertical,
  167. Name = department.ID,
  168. ToolTip = Loc.GetString("late-join-gui-jobs-amount-in-department-tooltip",
  169. ("departmentName", departmentName))
  170. };
  171. if (firstCategory)
  172. {
  173. firstCategory = false;
  174. }
  175. else
  176. {
  177. category.AddChild(new Control
  178. {
  179. MinSize = new Vector2(0, 23),
  180. });
  181. }
  182. var currentFactionPlayers = 0;
  183. var currentText = departmentName;
  184. var currentcount = _gameTicker.PlayerFactionCounts;
  185. foreach (var faction in currentcount)
  186. {
  187. if (faction.Key == departmentName)
  188. {
  189. currentFactionPlayers = faction.Value;
  190. }
  191. }
  192. if (currentFactionPlayers > 0)
  193. {
  194. currentText = $"{departmentName} ({currentFactionPlayers} players)";
  195. }
  196. category.AddChild(new PanelContainer
  197. {
  198. Children =
  199. {
  200. new Label
  201. {
  202. StyleClasses = { "LabelBig" },
  203. Text = currentText
  204. }
  205. }
  206. });
  207. _jobCategories[id][department.ID] = category;
  208. jobList.AddChild(category);
  209. foreach (var prototype in jobsAvailable)
  210. {
  211. var value = stationAvailable[prototype.ID];
  212. var jobLabel = new Label
  213. {
  214. Margin = new Thickness(5f, 0, 0, 0)
  215. };
  216. var jobButton = new JobButton(jobLabel, prototype.ID, prototype.LocalizedName, prototype.OriginalName, value);
  217. var jobSelector = new BoxContainer
  218. {
  219. Orientation = LayoutOrientation.Horizontal,
  220. HorizontalExpand = true
  221. };
  222. var icon = new TextureRect
  223. {
  224. TextureScale = new Vector2(2, 2),
  225. VerticalAlignment = VAlignment.Center
  226. };
  227. var jobIcon = _prototypeManager.Index(prototype.Icon);
  228. icon.Texture = _sprites.Frame0(jobIcon.Icon);
  229. jobSelector.AddChild(icon);
  230. jobSelector.AddChild(jobLabel);
  231. jobButton.AddChild(jobSelector);
  232. category.AddChild(jobButton);
  233. jobButton.OnPressed += _ => SelectedId.Invoke((id, jobButton.JobId));
  234. if (!_jobRequirements.IsAllowed(prototype, (HumanoidCharacterProfile?)_preferencesManager.Preferences?.SelectedCharacter, out var reason))
  235. {
  236. jobButton.Disabled = true;
  237. if (!reason.IsEmpty)
  238. {
  239. var tooltip = new Tooltip();
  240. tooltip.SetMessage(reason);
  241. jobButton.TooltipSupplier = _ => tooltip;
  242. }
  243. jobSelector.AddChild(new TextureRect
  244. {
  245. TextureScale = new Vector2(0.4f, 0.4f),
  246. Stretch = TextureRect.StretchMode.KeepCentered,
  247. Texture = _sprites.Frame0(new SpriteSpecifier.Texture(new("/Textures/Interface/Nano/lock.svg.192dpi.png"))),
  248. HorizontalExpand = true,
  249. HorizontalAlignment = HAlignment.Right,
  250. });
  251. }
  252. else if (value == 0)
  253. {
  254. jobButton.Disabled = true;
  255. }
  256. if (!_jobButtons[id].ContainsKey(prototype.ID))
  257. {
  258. _jobButtons[id][prototype.ID] = new List<JobButton>();
  259. }
  260. _jobButtons[id][prototype.ID].Add(jobButton);
  261. }
  262. }
  263. }
  264. }
  265. private void JobsAvailableUpdated(IReadOnlyDictionary<NetEntity, Dictionary<ProtoId<JobPrototype>, int?>> updatedJobs)
  266. {
  267. foreach (var stationEntries in updatedJobs)
  268. {
  269. if (_jobButtons.ContainsKey(stationEntries.Key))
  270. {
  271. var jobsAvailable = stationEntries.Value;
  272. var existingJobEntries = _jobButtons[stationEntries.Key];
  273. foreach (var existingJobEntry in existingJobEntries)
  274. {
  275. if (jobsAvailable.ContainsKey(existingJobEntry.Key))
  276. {
  277. var updatedJobValue = jobsAvailable[existingJobEntry.Key];
  278. foreach (var matchingJobButton in existingJobEntry.Value)
  279. {
  280. if (matchingJobButton.Amount != updatedJobValue)
  281. {
  282. matchingJobButton.RefreshLabel(updatedJobValue);
  283. matchingJobButton.Disabled |= matchingJobButton.Amount == 0;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. private void OnPlayerFactionCountsUpdated()
  292. {
  293. RebuildUI(); // Rebuild the UI to reflect new faction counts
  294. }
  295. [Obsolete("Overrides obsolete member 'Control.Dispose(bool)'.")]
  296. protected override void Dispose(bool disposing)
  297. {
  298. base.Dispose(disposing);
  299. if (disposing)
  300. {
  301. _jobRequirements.Updated -= RebuildUI;
  302. _gameTicker.LobbyJobsAvailableUpdated -= JobsAvailableUpdated;
  303. _gameTicker.PlayerFactionCountsUpdated -= OnPlayerFactionCountsUpdated;
  304. _jobButtons.Clear();
  305. _jobCategories.Clear();
  306. }
  307. }
  308. }
  309. sealed class JobButton : ContainerButton
  310. {
  311. public Label JobLabel { get; }
  312. public string JobId { get; }
  313. public string JobLocalisedName { get; }
  314. public string OriginalName { get; }
  315. public int? Amount { get; private set; }
  316. private bool _initialised = false;
  317. public JobButton(Label jobLabel, ProtoId<JobPrototype> jobId, string jobLocalisedName, string originalName, int? amount)
  318. {
  319. JobLabel = jobLabel;
  320. JobId = jobId;
  321. JobLocalisedName = jobLocalisedName;
  322. OriginalName = originalName;
  323. RefreshLabel(amount);
  324. AddStyleClass(StyleClassButton);
  325. _initialised = true;
  326. }
  327. public void RefreshLabel(int? amount)
  328. {
  329. if (Amount == amount && _initialised)
  330. {
  331. return;
  332. }
  333. Amount = amount;
  334. if (OriginalName != string.Empty)
  335. {
  336. JobLabel.Text = Amount != null ?
  337. Loc.GetString("late-join-gui-job-slot-capped", ("originalName", OriginalName), ("jobName", JobLocalisedName), ("amount", Amount)) :
  338. Loc.GetString("late-join-gui-job-slot-uncapped", ("originalName", OriginalName), ("jobName", JobLocalisedName));
  339. }
  340. else
  341. {
  342. JobLabel.Text = Amount != null ?
  343. Loc.GetString("late-join-gui-job-slot-capped-no-original", ("jobName", JobLocalisedName), ("amount", Amount)) :
  344. Loc.GetString("late-join-gui-job-slot-uncapped-no-original", ("jobName", JobLocalisedName));
  345. }
  346. }
  347. }
  348. }