ViewportUIController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Content.Client.UserInterface.Controls;
  2. using Content.Client.UserInterface.Systems.Gameplay;
  3. using Content.Shared.CCVar;
  4. using Robust.Client.Graphics;
  5. using Robust.Client.Player;
  6. using Robust.Client.UserInterface;
  7. using Robust.Client.UserInterface.Controllers;
  8. using Robust.Shared.Configuration;
  9. using Robust.Shared.Map;
  10. using Robust.Shared.Timing;
  11. namespace Content.Client.UserInterface.Systems.Viewport;
  12. public sealed class ViewportUIController : UIController
  13. {
  14. [Dependency] private readonly IEyeManager _eyeManager = default!;
  15. [Dependency] private readonly IPlayerManager _playerMan = default!;
  16. [Dependency] private readonly IEntityManager _entMan = default!;
  17. [Dependency] private readonly IConfigurationManager _configurationManager = default!;
  18. [UISystemDependency] private readonly SharedTransformSystem? _transformSystem = default!;
  19. public static readonly Vector2i ViewportSize = (EyeManager.PixelsPerMeter * 21, EyeManager.PixelsPerMeter * 15);
  20. public const int ViewportHeight = 15;
  21. private MainViewport? Viewport => UIManager.ActiveScreen?.GetWidget<MainViewport>();
  22. public override void Initialize()
  23. {
  24. _configurationManager.OnValueChanged(CCVars.ViewportMinimumWidth, _ => UpdateViewportRatio());
  25. _configurationManager.OnValueChanged(CCVars.ViewportMaximumWidth, _ => UpdateViewportRatio());
  26. _configurationManager.OnValueChanged(CCVars.ViewportWidth, _ => UpdateViewportRatio());
  27. _configurationManager.OnValueChanged(CCVars.ViewportVerticalFit, _ => UpdateViewportRatio());
  28. var gameplayStateLoad = UIManager.GetUIController<GameplayStateLoadController>();
  29. gameplayStateLoad.OnScreenLoad += OnScreenLoad;
  30. }
  31. private void OnScreenLoad()
  32. {
  33. ReloadViewport();
  34. }
  35. private void UpdateViewportRatio()
  36. {
  37. if (Viewport == null)
  38. {
  39. return;
  40. }
  41. var min = _configurationManager.GetCVar(CCVars.ViewportMinimumWidth);
  42. var max = _configurationManager.GetCVar(CCVars.ViewportMaximumWidth);
  43. var width = _configurationManager.GetCVar(CCVars.ViewportWidth);
  44. var verticalfit = _configurationManager.GetCVar(CCVars.ViewportVerticalFit) && _configurationManager.GetCVar(CCVars.ViewportStretch);
  45. if (verticalfit)
  46. {
  47. width = max;
  48. }
  49. else if (width < min || width > max)
  50. {
  51. width = CCVars.ViewportWidth.DefaultValue;
  52. }
  53. Viewport.Viewport.ViewportSize = (EyeManager.PixelsPerMeter * width, EyeManager.PixelsPerMeter * ViewportHeight);
  54. Viewport.UpdateCfg();
  55. }
  56. public void ReloadViewport()
  57. {
  58. if (Viewport == null)
  59. {
  60. return;
  61. }
  62. UpdateViewportRatio();
  63. Viewport.Viewport.HorizontalExpand = true;
  64. Viewport.Viewport.VerticalExpand = true;
  65. _eyeManager.MainViewport = Viewport.Viewport;
  66. }
  67. public override void FrameUpdate(FrameEventArgs e)
  68. {
  69. if (Viewport == null)
  70. {
  71. return;
  72. }
  73. base.FrameUpdate(e);
  74. Viewport.Viewport.Eye = _eyeManager.CurrentEye;
  75. // verify that the current eye is not "null". Fuck IEyeManager.
  76. var ent = _playerMan.LocalEntity;
  77. if (_eyeManager.CurrentEye.Position != default || ent == null)
  78. return;
  79. _entMan.TryGetComponent(ent, out EyeComponent? eye);
  80. if (eye?.Eye == _eyeManager.CurrentEye
  81. && _entMan.GetComponent<TransformComponent>(ent.Value).MapID == MapId.Nullspace)
  82. {
  83. // nothing to worry about, the player is just in null space... actually that is probably a problem?
  84. return;
  85. }
  86. // Currently, this shouldn't happen. This likely happened because the main eye was set to null. When this
  87. // does happen it can create hard to troubleshoot bugs, so lets print some helpful warnings:
  88. Logger.Warning($"Main viewport's eye is in nullspace (main eye is null?). Attached entity: {_entMan.ToPrettyString(ent.Value)}. Entity has eye comp: {eye != null}");
  89. }
  90. }