GuidebookUIController.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System.Linq;
  2. using Content.Client.Gameplay;
  3. using Content.Client.Guidebook;
  4. using Content.Client.Guidebook.Controls;
  5. using Content.Client.Lobby;
  6. using Content.Client.Players.PlayTimeTracking;
  7. using Content.Client.UserInterface.Controls;
  8. using Content.Shared.CCVar;
  9. using Content.Shared.Guidebook;
  10. using Content.Shared.Input;
  11. using Robust.Client.State;
  12. using Robust.Client.UserInterface;
  13. using Robust.Client.UserInterface.Controllers;
  14. using Robust.Shared.Configuration;
  15. using static Robust.Client.UserInterface.Controls.BaseButton;
  16. using Robust.Shared.Input.Binding;
  17. using Robust.Shared.Prototypes;
  18. using Robust.Shared.Utility;
  19. namespace Content.Client.UserInterface.Systems.Guidebook;
  20. public sealed class GuidebookUIController : UIController, IOnStateEntered<LobbyState>, IOnStateEntered<GameplayState>, IOnStateExited<LobbyState>, IOnStateExited<GameplayState>, IOnSystemChanged<GuidebookSystem>
  21. {
  22. [UISystemDependency] private readonly GuidebookSystem _guidebookSystem = default!;
  23. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  24. [Dependency] private readonly IConfigurationManager _configuration = default!;
  25. [Dependency] private readonly JobRequirementsManager _jobRequirements = default!;
  26. private const int PlaytimeOpenGuidebook = 60;
  27. private GuidebookWindow? _guideWindow;
  28. private MenuButton? GuidebookButton => UIManager.GetActiveUIWidgetOrNull<MenuBar.Widgets.GameTopMenuBar>()?.GuidebookButton;
  29. private ProtoId<GuideEntryPrototype>? _lastEntry;
  30. public void OnStateEntered(LobbyState state)
  31. {
  32. HandleStateEntered(state);
  33. }
  34. public void OnStateEntered(GameplayState state)
  35. {
  36. HandleStateEntered(state);
  37. }
  38. private void HandleStateEntered(State state)
  39. {
  40. DebugTools.Assert(_guideWindow == null);
  41. // setup window
  42. _guideWindow = UIManager.CreateWindow<GuidebookWindow>();
  43. _guideWindow.OnClose += OnWindowClosed;
  44. _guideWindow.OnOpen += OnWindowOpen;
  45. if (state is LobbyState &&
  46. _jobRequirements.FetchOverallPlaytime() < TimeSpan.FromMinutes(PlaytimeOpenGuidebook))
  47. {
  48. OpenGuidebook();
  49. _guideWindow.RecenterWindow(new(0.5f, 0.5f));
  50. _guideWindow.SetPositionFirst();
  51. }
  52. // setup keybinding
  53. CommandBinds.Builder
  54. .Bind(ContentKeyFunctions.OpenGuidebook,
  55. InputCmdHandler.FromDelegate(_ => ToggleGuidebook()))
  56. .Register<GuidebookUIController>();
  57. }
  58. public void OnStateExited(LobbyState state)
  59. {
  60. HandleStateExited();
  61. }
  62. public void OnStateExited(GameplayState state)
  63. {
  64. HandleStateExited();
  65. }
  66. private void HandleStateExited()
  67. {
  68. if (_guideWindow == null)
  69. return;
  70. _guideWindow.OnClose -= OnWindowClosed;
  71. _guideWindow.OnOpen -= OnWindowOpen;
  72. // shutdown
  73. _guideWindow.Dispose();
  74. _guideWindow = null;
  75. CommandBinds.Unregister<GuidebookUIController>();
  76. }
  77. public void OnSystemLoaded(GuidebookSystem system)
  78. {
  79. _guidebookSystem.OnGuidebookOpen += OpenGuidebook;
  80. }
  81. public void OnSystemUnloaded(GuidebookSystem system)
  82. {
  83. _guidebookSystem.OnGuidebookOpen -= OpenGuidebook;
  84. }
  85. internal void UnloadButton()
  86. {
  87. if (GuidebookButton == null)
  88. return;
  89. GuidebookButton.OnPressed -= GuidebookButtonOnPressed;
  90. }
  91. internal void LoadButton()
  92. {
  93. if (GuidebookButton == null)
  94. return;
  95. GuidebookButton.OnPressed += GuidebookButtonOnPressed;
  96. }
  97. private void GuidebookButtonOnPressed(ButtonEventArgs obj)
  98. {
  99. ToggleGuidebook();
  100. }
  101. public void ToggleGuidebook()
  102. {
  103. if (_guideWindow == null)
  104. return;
  105. if (_guideWindow.IsOpen)
  106. {
  107. UIManager.ClickSound();
  108. _guideWindow.Close();
  109. }
  110. else
  111. {
  112. OpenGuidebook();
  113. }
  114. }
  115. private void OnWindowClosed()
  116. {
  117. if (GuidebookButton != null)
  118. GuidebookButton.Pressed = false;
  119. if (_guideWindow != null)
  120. {
  121. _guideWindow.ReturnContainer.Visible = false;
  122. _lastEntry = _guideWindow.LastEntry;
  123. }
  124. }
  125. private void OnWindowOpen()
  126. {
  127. if (GuidebookButton != null)
  128. GuidebookButton.Pressed = true;
  129. }
  130. /// <summary>
  131. /// Opens or closes the guidebook.
  132. /// </summary>
  133. /// <param name="guides">What guides should be shown. If not specified, this will instead list all the entries</param>
  134. /// <param name="rootEntries">A list of guides that should form the base of the table of contents. If not specified,
  135. /// this will automatically simply be a list of all guides that have no parent.</param>
  136. /// <param name="forceRoot">This forces a singular guide to contain all other guides. This guide will
  137. /// contain its own children, in addition to what would normally be the root guides if this were not
  138. /// specified.</param>
  139. /// <param name="includeChildren">Whether or not to automatically include child entries. If false, this will ONLY
  140. /// show the specified entries</param>
  141. /// <param name="selected">The guide whose contents should be displayed when the guidebook is opened</param>
  142. public void OpenGuidebook(
  143. Dictionary<ProtoId<GuideEntryPrototype>, GuideEntry>? guides = null,
  144. List<ProtoId<GuideEntryPrototype>>? rootEntries = null,
  145. ProtoId<GuideEntryPrototype>? forceRoot = null,
  146. bool includeChildren = true,
  147. ProtoId<GuideEntryPrototype>? selected = null)
  148. {
  149. if (_guideWindow == null)
  150. return;
  151. if (GuidebookButton != null)
  152. GuidebookButton.SetClickPressed(!_guideWindow.IsOpen);
  153. if (guides == null)
  154. {
  155. guides = _prototypeManager.EnumeratePrototypes<GuideEntryPrototype>()
  156. .ToDictionary(x => new ProtoId<GuideEntryPrototype>(x.ID), x => (GuideEntry) x);
  157. }
  158. else if (includeChildren)
  159. {
  160. var oldGuides = guides;
  161. guides = new(oldGuides);
  162. foreach (var guide in oldGuides.Values)
  163. {
  164. RecursivelyAddChildren(guide, guides);
  165. }
  166. }
  167. if (selected == null)
  168. {
  169. if (_lastEntry is { } lastEntry && guides.ContainsKey(lastEntry))
  170. {
  171. selected = _lastEntry;
  172. }
  173. else
  174. {
  175. selected = _configuration.GetCVar(CCVars.DefaultGuide);
  176. }
  177. }
  178. _guideWindow.UpdateGuides(guides, rootEntries, forceRoot, selected);
  179. // Expand up to depth-2.
  180. _guideWindow.Tree.SetAllExpanded(false);
  181. _guideWindow.Tree.SetAllExpanded(true, 1);
  182. _guideWindow.OpenCenteredRight();
  183. }
  184. public void OpenGuidebook(
  185. List<ProtoId<GuideEntryPrototype>> guideList,
  186. List<ProtoId<GuideEntryPrototype>>? rootEntries = null,
  187. ProtoId<GuideEntryPrototype>? forceRoot = null,
  188. bool includeChildren = true,
  189. ProtoId<GuideEntryPrototype>? selected = null)
  190. {
  191. Dictionary<ProtoId<GuideEntryPrototype>, GuideEntry> guides = new();
  192. foreach (var guideId in guideList)
  193. {
  194. if (!_prototypeManager.TryIndex(guideId, out var guide))
  195. {
  196. Logger.Error($"Encountered unknown guide prototype: {guideId}");
  197. continue;
  198. }
  199. guides.Add(guideId, guide);
  200. }
  201. OpenGuidebook(guides, rootEntries, forceRoot, includeChildren, selected);
  202. }
  203. public void CloseGuidebook()
  204. {
  205. if (_guideWindow == null)
  206. return;
  207. if (_guideWindow.IsOpen)
  208. {
  209. UIManager.ClickSound();
  210. _guideWindow.Close();
  211. }
  212. }
  213. private void RecursivelyAddChildren(GuideEntry guide, Dictionary<ProtoId<GuideEntryPrototype>, GuideEntry> guides)
  214. {
  215. foreach (var childId in guide.Children)
  216. {
  217. if (guides.ContainsKey(childId))
  218. continue;
  219. if (!_prototypeManager.TryIndex(childId, out var child))
  220. {
  221. Logger.Error($"Encountered unknown guide prototype: {childId} as a child of {guide.Id}. If the child is not a prototype, it must be directly provided.");
  222. continue;
  223. }
  224. guides.Add(childId, child);
  225. RecursivelyAddChildren(child, guides);
  226. }
  227. }
  228. }