2021-06-09 22:19:39 +02:00
using Content.Client.Construction.UI ;
2021-10-22 04:26:02 +02:00
using Content.Client.Hands ;
2022-09-11 20:42:12 -07:00
using Content.Client.UserInterface.Controls ;
2022-10-12 01:16:23 -07:00
using Content.Client.UserInterface.Screens ;
2022-09-04 17:21:14 -07:00
using Content.Client.Viewport ;
2021-11-30 14:31:13 +03:00
using Content.Shared.CCVar ;
2021-04-19 09:52:40 +02:00
using Robust.Client.Graphics ;
2021-02-11 01:13:03 -08:00
using Robust.Client.Input ;
using Robust.Client.UserInterface ;
2020-02-26 16:42:12 +01:00
using Robust.Client.UserInterface.Controls ;
2021-11-30 14:31:13 +03:00
using Robust.Client.UserInterface.CustomControls ;
2021-02-16 20:14:32 +01:00
using Robust.Shared.Configuration ;
2021-04-19 09:52:40 +02:00
using Robust.Shared.Timing ;
2022-10-13 05:37:32 +13:00
using Robust.Client.Player ;
using Robust.Client.GameObjects ;
2019-12-06 00:41:30 +01:00
2022-09-04 17:21:14 -07:00
namespace Content.Client.Gameplay
2019-12-06 00:41:30 +01:00
{
2022-09-04 17:21:14 -07:00
public sealed class GameplayState : GameplayStateBase , IMainViewportState
2019-12-06 00:41:30 +01:00
{
2021-04-19 09:52:40 +02:00
[Dependency] private readonly IEyeManager _eyeManager = default ! ;
2021-10-22 04:26:02 +02:00
[Dependency] private readonly IOverlayManager _overlayManager = default ! ;
2021-11-30 14:31:13 +03:00
[Dependency] private readonly IGameTiming _gameTiming = default ! ;
2022-10-13 05:37:32 +13:00
[Dependency] private readonly IPlayerManager _playerMan = default ! ;
2022-10-12 01:16:23 -07:00
[Dependency] private readonly IUserInterfaceManager _uiManager = default ! ;
2021-11-30 14:31:13 +03:00
[Dependency] private readonly IConfigurationManager _configurationManager = default ! ;
2022-10-13 05:37:32 +13:00
[Dependency] private readonly IEntityManager _entMan = default ! ;
2019-12-06 00:41:30 +01:00
2022-10-12 01:16:23 -07:00
protected override Type ? LinkedScreenType = > typeof ( DefaultGameScreen ) ;
public static readonly Vector2i ViewportSize = ( EyeManager . PixelsPerMeter * 21 , EyeManager . PixelsPerMeter * 15 ) ;
2021-11-30 14:31:13 +03:00
private FpsCounter _fpsCounter = default ! ;
2021-04-19 09:52:40 +02:00
public MainViewport Viewport { get ; private set ; } = default ! ;
2022-10-12 01:16:23 -07:00
public GameplayState ( )
{
IoCManager . InjectDependencies ( this ) ;
}
2022-09-04 16:17:05 -07:00
protected override void Startup ( )
2019-12-06 00:41:30 +01:00
{
2020-02-26 16:42:12 +01:00
base . Startup ( ) ;
2021-04-19 09:52:40 +02:00
Viewport = new MainViewport
{
Viewport =
{
ViewportSize = ViewportSize
}
} ;
2022-10-12 01:16:23 -07:00
UserInterfaceManager . StateRoot . AddChild ( Viewport ) ;
2021-04-19 09:52:40 +02:00
LayoutContainer . SetAnchorPreset ( Viewport , LayoutContainer . LayoutPreset . Wide ) ;
Viewport . SetPositionFirst ( ) ;
_eyeManager . MainViewport = Viewport . Viewport ;
2021-10-22 04:26:02 +02:00
_overlayManager . AddOverlay ( new ShowHandItemOverlay ( ) ) ;
2021-11-30 14:31:13 +03:00
_fpsCounter = new FpsCounter ( _gameTiming ) ;
2022-10-12 01:16:23 -07:00
UserInterfaceManager . PopupRoot . AddChild ( _fpsCounter ) ;
2021-11-30 14:31:13 +03:00
_fpsCounter . Visible = _configurationManager . GetCVar ( CCVars . HudFpsCounterVisible ) ;
_configurationManager . OnValueChanged ( CCVars . HudFpsCounterVisible , ( show ) = > { _fpsCounter . Visible = show ; } ) ;
2019-12-06 00:41:30 +01:00
}
2022-09-04 16:17:05 -07:00
protected override void Shutdown ( )
2019-12-06 00:41:30 +01:00
{
2021-10-22 04:26:02 +02:00
_overlayManager . RemoveOverlay < ShowHandItemOverlay > ( ) ;
2021-02-20 12:05:59 -08:00
2020-02-26 16:42:12 +01:00
base . Shutdown ( ) ;
2021-04-19 09:52:40 +02:00
Viewport . Dispose ( ) ;
// Clear viewport to some fallback, whatever.
2022-10-12 01:16:23 -07:00
_eyeManager . MainViewport = UserInterfaceManager . MainViewport ;
2021-11-30 14:31:13 +03:00
_fpsCounter . Dispose ( ) ;
2022-10-12 01:16:23 -07:00
_uiManager . ClearWindows ( ) ;
2020-07-08 05:18:16 -05:00
}
2021-04-19 09:52:40 +02:00
public override void FrameUpdate ( FrameEventArgs e )
{
base . FrameUpdate ( e ) ;
Viewport . Viewport . Eye = _eyeManager . CurrentEye ;
2022-10-13 05:37:32 +13:00
// verify that the current eye is not "null". Fuck IEyeManager.
var ent = _playerMan . LocalPlayer ? . ControlledEntity ;
if ( _eyeManager . CurrentEye . Position ! = default | | ent = = null )
return ;
_entMan . TryGetComponent ( ent , out EyeComponent ? eye ) ;
if ( eye ? . Eye = = _eyeManager . CurrentEye
& & _entMan . GetComponent < TransformComponent > ( ent . Value ) . WorldPosition = = default )
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}" ) ;
2021-04-19 09:52:40 +02:00
}
2021-04-29 15:01:13 -07:00
protected override void OnKeyBindStateChanged ( ViewportBoundKeyEventArgs args )
{
if ( args . Viewport = = null )
base . OnKeyBindStateChanged ( new ViewportBoundKeyEventArgs ( args . KeyEventArgs , Viewport . Viewport ) ) ;
else
base . OnKeyBindStateChanged ( args ) ;
}
2019-12-06 00:41:30 +01:00
}
}