LateJoinGui.cs 15 KB

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