2022-10-17 15:13:41 -07:00
using Content.Client.UserInterface.Controls ;
2023-03-10 19:25:56 -08:00
using Content.Client.UserInterface.Systems.Gameplay ;
2022-10-17 15:13:41 -07:00
using Content.Shared.CCVar ;
using Robust.Client.Graphics ;
using Robust.Client.Player ;
using Robust.Client.UserInterface.Controllers ;
using Robust.Shared.Configuration ;
using Robust.Shared.Timing ;
namespace Content.Client.UserInterface.Systems.Viewport ;
public sealed class ViewportUIController : UIController
{
[Dependency] private readonly IEyeManager _eyeManager = default ! ;
[Dependency] private readonly IPlayerManager _playerMan = default ! ;
[Dependency] private readonly IEntityManager _entMan = default ! ;
[Dependency] private readonly IConfigurationManager _configurationManager = default ! ;
public static readonly Vector2i ViewportSize = ( EyeManager . PixelsPerMeter * 21 , EyeManager . PixelsPerMeter * 15 ) ;
public const int ViewportHeight = 15 ;
private MainViewport ? Viewport = > UIManager . ActiveScreen ? . GetWidget < MainViewport > ( ) ;
public override void Initialize ( )
{
_configurationManager . OnValueChanged ( CCVars . ViewportMinimumWidth , _ = > UpdateViewportRatio ( ) ) ;
_configurationManager . OnValueChanged ( CCVars . ViewportMaximumWidth , _ = > UpdateViewportRatio ( ) ) ;
_configurationManager . OnValueChanged ( CCVars . ViewportWidth , _ = > UpdateViewportRatio ( ) ) ;
2024-04-20 02:46:02 -04:00
_configurationManager . OnValueChanged ( CCVars . ViewportVerticalFit , _ = > UpdateViewportRatio ( ) ) ;
2023-03-10 19:25:56 -08:00
var gameplayStateLoad = UIManager . GetUIController < GameplayStateLoadController > ( ) ;
gameplayStateLoad . OnScreenLoad + = OnScreenLoad ;
}
private void OnScreenLoad ( )
{
ReloadViewport ( ) ;
2022-10-17 15:13:41 -07:00
}
private void UpdateViewportRatio ( )
{
if ( Viewport = = null )
{
return ;
}
var min = _configurationManager . GetCVar ( CCVars . ViewportMinimumWidth ) ;
var max = _configurationManager . GetCVar ( CCVars . ViewportMaximumWidth ) ;
var width = _configurationManager . GetCVar ( CCVars . ViewportWidth ) ;
2024-04-20 02:46:02 -04:00
var verticalfit = _configurationManager . GetCVar ( CCVars . ViewportVerticalFit ) & & _configurationManager . GetCVar ( CCVars . ViewportStretch ) ;
2022-10-17 15:13:41 -07:00
2024-04-20 02:46:02 -04:00
if ( verticalfit )
{
width = max ;
}
else if ( width < min | | width > max )
2022-10-17 15:13:41 -07:00
{
width = CCVars . ViewportWidth . DefaultValue ;
}
Viewport . Viewport . ViewportSize = ( EyeManager . PixelsPerMeter * width , EyeManager . PixelsPerMeter * ViewportHeight ) ;
2024-04-20 02:46:02 -04:00
Viewport . UpdateCfg ( ) ;
2022-10-17 15:13:41 -07:00
}
public void ReloadViewport ( )
{
if ( Viewport = = null )
{
return ;
}
UpdateViewportRatio ( ) ;
Viewport . Viewport . HorizontalExpand = true ;
Viewport . Viewport . VerticalExpand = true ;
_eyeManager . MainViewport = Viewport . Viewport ;
}
public override void FrameUpdate ( FrameEventArgs e )
{
if ( Viewport = = null )
{
return ;
}
base . FrameUpdate ( e ) ;
Viewport . Viewport . Eye = _eyeManager . CurrentEye ;
// verify that the current eye is not "null". Fuck IEyeManager.
2024-02-13 22:48:39 +01:00
var ent = _playerMan . LocalEntity ;
2022-10-17 15:13:41 -07:00
if ( _eyeManager . CurrentEye . Position ! = default | | ent = = null )
return ;
_entMan . TryGetComponent ( ent , out EyeComponent ? eye ) ;
2023-09-01 12:30:29 +10:00
if ( eye ? . Eye = = _eyeManager . CurrentEye
2024-02-28 00:51:20 +11:00
& & _entMan . GetComponent < TransformComponent > ( ent . Value ) . WorldPosition = = default )
2022-10-17 15:13:41 -07:00
return ; // nothing to worry about, the player is just in null space... actually that is probably a problem?
// Currently, this shouldn't happen. This likely happened because the main eye was set to null. When this
// does happen it can create hard to troubleshoot bugs, so lets print some helpful warnings:
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}" ) ;
}
}