1
0

PdaMenu.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using Content.Client.GameTicking.Managers;
  2. using Content.Shared.PDA;
  3. using Robust.Shared.Utility;
  4. using Content.Shared.CartridgeLoader;
  5. using Content.Client.Message;
  6. using Robust.Client.UserInterface;
  7. using Robust.Client.AutoGenerated;
  8. using Robust.Client.Graphics;
  9. using Robust.Client.UserInterface.XAML;
  10. using Robust.Client.UserInterface.Controls;
  11. using Robust.Shared.Timing;
  12. namespace Content.Client.PDA
  13. {
  14. [GenerateTypedNameReferences]
  15. public sealed partial class PdaMenu : PdaWindow
  16. {
  17. [Dependency] private readonly IClipboardManager _clipboard = null!;
  18. [Dependency] private readonly IGameTiming _gameTiming = default!;
  19. [Dependency] private readonly IEntitySystemManager _entitySystem = default!;
  20. private readonly ClientGameTicker _gameTicker;
  21. public const int HomeView = 0;
  22. public const int ProgramListView = 1;
  23. public const int SettingsView = 2;
  24. public const int ProgramContentView = 3;
  25. private string _pdaOwner = Loc.GetString("comp-pda-ui-unknown");
  26. private string _owner = Loc.GetString("comp-pda-ui-unknown");
  27. private string _jobTitle = Loc.GetString("comp-pda-ui-unassigned");
  28. private string _stationName = Loc.GetString("comp-pda-ui-unknown");
  29. private string _alertLevel = Loc.GetString("comp-pda-ui-unknown");
  30. private string _instructions = Loc.GetString("comp-pda-ui-unknown");
  31. private int _currentView;
  32. public event Action<EntityUid>? OnProgramItemPressed;
  33. public event Action<EntityUid>? OnUninstallButtonPressed;
  34. public event Action<EntityUid>? OnInstallButtonPressed;
  35. public PdaMenu()
  36. {
  37. IoCManager.InjectDependencies(this);
  38. _gameTicker = _entitySystem.GetEntitySystem<ClientGameTicker>();
  39. RobustXamlLoader.Load(this);
  40. ViewContainer.OnChildAdded += control => control.Visible = false;
  41. HomeButton.IconTexture = new SpriteSpecifier.Texture(new("/Textures/Interface/home.png"));
  42. FlashLightToggleButton.IconTexture = new SpriteSpecifier.Texture(new("/Textures/Interface/light.png"));
  43. EjectPenButton.IconTexture = new SpriteSpecifier.Texture(new("/Textures/Interface/pencil.png"));
  44. EjectIdButton.IconTexture = new SpriteSpecifier.Texture(new("/Textures/Interface/eject.png"));
  45. EjectPaiButton.IconTexture = new SpriteSpecifier.Texture(new("/Textures/Interface/pai.png"));
  46. ProgramCloseButton.IconTexture = new SpriteSpecifier.Texture(new("/Textures/Interface/Nano/cross.svg.png"));
  47. HomeButton.OnPressed += _ => ToHomeScreen();
  48. ProgramListButton.OnPressed += _ =>
  49. {
  50. HomeButton.IsCurrent = false;
  51. ProgramListButton.IsCurrent = true;
  52. SettingsButton.IsCurrent = false;
  53. ProgramTitle.IsCurrent = false;
  54. ChangeView(ProgramListView);
  55. };
  56. SettingsButton.OnPressed += _ =>
  57. {
  58. HomeButton.IsCurrent = false;
  59. ProgramListButton.IsCurrent = false;
  60. SettingsButton.IsCurrent = true;
  61. ProgramTitle.IsCurrent = false;
  62. ChangeView(SettingsView);
  63. };
  64. ProgramTitle.OnPressed += _ =>
  65. {
  66. HomeButton.IsCurrent = false;
  67. ProgramListButton.IsCurrent = false;
  68. SettingsButton.IsCurrent = false;
  69. ProgramTitle.IsCurrent = true;
  70. ChangeView(ProgramContentView);
  71. };
  72. ProgramCloseButton.OnPressed += _ =>
  73. {
  74. HideProgramHeader();
  75. ToHomeScreen();
  76. };
  77. PdaOwnerButton.OnPressed += _ =>
  78. {
  79. _clipboard.SetText(_pdaOwner);
  80. };
  81. IdInfoButton.OnPressed += _ =>
  82. {
  83. _clipboard.SetText(_owner + ", " + _jobTitle);
  84. };
  85. StationNameButton.OnPressed += _ =>
  86. {
  87. _clipboard.SetText(_stationName);
  88. };
  89. StationAlertLevelButton.OnPressed += _ =>
  90. {
  91. _clipboard.SetText(_alertLevel);
  92. };
  93. StationTimeButton.OnPressed += _ =>
  94. {
  95. var stationTime = _gameTiming.CurTime.Subtract(_gameTicker.RoundStartTimeSpan);
  96. _clipboard.SetText((stationTime.ToString("hh\\:mm\\:ss")));
  97. };
  98. StationAlertLevelInstructionsButton.OnPressed += _ =>
  99. {
  100. _clipboard.SetText(_instructions);
  101. };
  102. HideAllViews();
  103. ToHomeScreen();
  104. }
  105. public void UpdateState(PdaUpdateState state)
  106. {
  107. FlashLightToggleButton.IsActive = state.FlashlightEnabled;
  108. if (state.PdaOwnerInfo.ActualOwnerName != null)
  109. {
  110. _pdaOwner = state.PdaOwnerInfo.ActualOwnerName;
  111. PdaOwnerLabel.SetMarkup(Loc.GetString("comp-pda-ui-owner",
  112. ("actualOwnerName", _pdaOwner)));
  113. PdaOwnerLabel.Visible = true;
  114. }
  115. else
  116. {
  117. PdaOwnerLabel.Visible = false;
  118. }
  119. if (state.PdaOwnerInfo.IdOwner != null || state.PdaOwnerInfo.JobTitle != null)
  120. {
  121. _owner = state.PdaOwnerInfo.IdOwner ?? Loc.GetString("comp-pda-ui-unknown");
  122. _jobTitle = state.PdaOwnerInfo.JobTitle ?? Loc.GetString("comp-pda-ui-unassigned");
  123. IdInfoLabel.SetMarkup(Loc.GetString("comp-pda-ui",
  124. ("owner", _owner),
  125. ("jobTitle", _jobTitle)));
  126. }
  127. else
  128. {
  129. IdInfoLabel.SetMarkup(Loc.GetString("comp-pda-ui-blank"));
  130. }
  131. _stationName = state.StationName ?? Loc.GetString("comp-pda-ui-unknown");
  132. StationNameLabel.SetMarkup(Loc.GetString("comp-pda-ui-station",
  133. ("station", _stationName)));
  134. var stationTime = _gameTiming.CurTime.Subtract(_gameTicker.RoundStartTimeSpan);
  135. StationTimeLabel.SetMarkup(Loc.GetString("comp-pda-ui-station-time",
  136. ("time", stationTime.ToString("hh\\:mm\\:ss"))));
  137. var alertLevel = state.PdaOwnerInfo.StationAlertLevel;
  138. var alertColor = state.PdaOwnerInfo.StationAlertColor;
  139. var alertLevelKey = alertLevel != null ? $"alert-level-{alertLevel}" : "alert-level-unknown";
  140. _alertLevel = Loc.GetString(alertLevelKey);
  141. StationAlertLevelLabel.SetMarkup(Loc.GetString(
  142. "comp-pda-ui-station-alert-level",
  143. ("color", alertColor),
  144. ("level", _alertLevel)
  145. ));
  146. _instructions = Loc.GetString($"{alertLevelKey}-instructions");
  147. StationAlertLevelInstructions.SetMarkup(Loc.GetString(
  148. "comp-pda-ui-station-alert-level-instructions",
  149. ("instructions", _instructions))
  150. );
  151. AddressLabel.Text = state.Address?.ToUpper() ?? " - ";
  152. EjectIdButton.IsActive = state.PdaOwnerInfo.IdOwner != null || state.PdaOwnerInfo.JobTitle != null;
  153. EjectPenButton.IsActive = state.HasPen;
  154. EjectPaiButton.IsActive = state.HasPai;
  155. ActivateMusicButton.Visible = state.CanPlayMusic;
  156. ShowUplinkButton.Visible = state.HasUplink;
  157. LockUplinkButton.Visible = state.HasUplink;
  158. }
  159. public void UpdateAvailablePrograms(List<(EntityUid, CartridgeComponent)> programs)
  160. {
  161. ProgramList.RemoveAllChildren();
  162. if (programs.Count == 0)
  163. {
  164. ProgramList.AddChild(new Label()
  165. {
  166. Text = Loc.GetString("comp-pda-io-no-programs-available"),
  167. HorizontalAlignment = HAlignment.Center,
  168. VerticalAlignment = VAlignment.Center,
  169. VerticalExpand = true
  170. });
  171. return;
  172. }
  173. var row = CreateProgramListRow();
  174. var itemCount = 1;
  175. ProgramList.AddChild(row);
  176. foreach (var (uid, component) in programs)
  177. {
  178. //Create a new row every second program item starting from the first
  179. if (itemCount % 2 != 0)
  180. {
  181. row = CreateProgramListRow();
  182. ProgramList.AddChild(row);
  183. }
  184. var item = new PdaProgramItem();
  185. if (component.Icon is not null)
  186. item.Icon.SetFromSpriteSpecifier(component.Icon);
  187. item.OnPressed += _ => OnProgramItemPressed?.Invoke(uid);
  188. switch (component.InstallationStatus)
  189. {
  190. case InstallationStatus.Cartridge:
  191. item.InstallButton.Visible = true;
  192. item.InstallButton.Text = Loc.GetString("cartridge-bound-user-interface-install-button");
  193. item.InstallButton.OnPressed += _ => OnInstallButtonPressed?.Invoke(uid);
  194. break;
  195. case InstallationStatus.Installed:
  196. item.InstallButton.Visible = true;
  197. item.InstallButton.Text = Loc.GetString("cartridge-bound-user-interface-uninstall-button");
  198. item.InstallButton.OnPressed += _ => OnUninstallButtonPressed?.Invoke(uid);
  199. break;
  200. }
  201. item.ProgramName.Text = Loc.GetString(component.ProgramName);
  202. item.SetHeight = 20;
  203. row.AddChild(item);
  204. itemCount++;
  205. }
  206. //Add a filler item to the last row when it only contains one item
  207. if (itemCount % 2 == 0)
  208. row.AddChild(new Control() { HorizontalExpand = true });
  209. }
  210. /// <summary>
  211. /// Changes the current view to the home screen (view 0) and sets the tabs `IsCurrent` flag accordingly
  212. /// </summary>
  213. public void ToHomeScreen()
  214. {
  215. HomeButton.IsCurrent = true;
  216. ProgramListButton.IsCurrent = false;
  217. SettingsButton.IsCurrent = false;
  218. ProgramTitle.IsCurrent = false;
  219. ChangeView(HomeView);
  220. }
  221. /// <summary>
  222. /// Hides the program title and close button.
  223. /// </summary>
  224. public void HideProgramHeader()
  225. {
  226. ProgramTitle.IsCurrent = false;
  227. ProgramTitle.Visible = false;
  228. ProgramCloseButton.Visible = false;
  229. ProgramListButton.Visible = true;
  230. SettingsButton.Visible = true;
  231. }
  232. /// <summary>
  233. /// Changes the current view to the program content view (view 3), sets the program title and sets the tabs `IsCurrent` flag accordingly
  234. /// </summary>
  235. public void ToProgramView(string title)
  236. {
  237. HomeButton.IsCurrent = false;
  238. ProgramListButton.IsCurrent = false;
  239. SettingsButton.IsCurrent = false;
  240. ProgramTitle.IsCurrent = false;
  241. ProgramTitle.IsCurrent = true;
  242. ProgramTitle.Visible = true;
  243. ProgramCloseButton.Visible = true;
  244. ProgramListButton.Visible = false;
  245. SettingsButton.Visible = false;
  246. ProgramTitle.LabelText = title;
  247. ChangeView(ProgramContentView);
  248. }
  249. /// <summary>
  250. /// Changes the current view to the given view number
  251. /// </summary>
  252. public void ChangeView(int view)
  253. {
  254. if (ViewContainer.ChildCount <= view)
  255. return;
  256. ViewContainer.GetChild(_currentView).Visible = false;
  257. ViewContainer.GetChild(view).Visible = true;
  258. _currentView = view;
  259. }
  260. private static BoxContainer CreateProgramListRow()
  261. {
  262. return new BoxContainer()
  263. {
  264. Orientation = BoxContainer.LayoutOrientation.Horizontal,
  265. HorizontalExpand = true
  266. };
  267. }
  268. private void HideAllViews()
  269. {
  270. var views = ViewContainer.Children;
  271. foreach (var view in views)
  272. {
  273. view.Visible = false;
  274. }
  275. }
  276. protected override void Draw(DrawingHandleScreen handle)
  277. {
  278. base.Draw(handle);
  279. var stationTime = _gameTiming.CurTime.Subtract(_gameTicker.RoundStartTimeSpan);
  280. StationTimeLabel.SetMarkup(Loc.GetString("comp-pda-ui-station-time",
  281. ("time", stationTime.ToString("hh\\:mm\\:ss"))));
  282. }
  283. }
  284. }