LoadingScreen.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Robust.Client.ResourceManagement;
  2. using Robust.Client.State;
  3. using Robust.Client.UserInterface;
  4. using Robust.Shared.CPUJob.JobQueues;
  5. using Robust.Shared.Timing;
  6. namespace Content.Client.Replay.UI.Loading;
  7. [Virtual]
  8. public class LoadingScreen<TResult> : State
  9. {
  10. [Dependency] private readonly IResourceCache _resourceCache = default!;
  11. [Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
  12. public event Action<TResult?, Exception?>? OnJobFinished;
  13. private LoadingScreenControl _screen = default!;
  14. public Job<TResult>? Job;
  15. public override void FrameUpdate(FrameEventArgs e)
  16. {
  17. base.FrameUpdate(e);
  18. if (Job == null)
  19. return;
  20. Job.Run();
  21. if (Job.Status != JobStatus.Finished)
  22. return;
  23. OnJobFinished?.Invoke(Job.Result, Job.Exception);
  24. Job = null;
  25. }
  26. protected override void Startup()
  27. {
  28. _screen = new(_resourceCache);
  29. _userInterfaceManager.StateRoot.AddChild(_screen);
  30. }
  31. protected override void Shutdown()
  32. {
  33. _screen.Dispose();
  34. }
  35. public void UpdateProgress(float value, float maxValue, string header, string subtext = "")
  36. {
  37. _screen.Bar.Value = value;
  38. _screen.Bar.MaxValue = maxValue;
  39. _screen.Header.Text = header;
  40. _screen.Subtext.Text = subtext;
  41. }
  42. }