A UI component that globally toggles in-game UI panels and the engine-drawn weapon crosshair. It tracks a static IsEnabled ConVar, enables/disables child PanelComponent instances, and adds or removes the camera's "ui" render-exclude tag.
public sealed class UI : Component
{
[ConVar( "sbdm.ui", ConVarFlags.Cheat ), Change( nameof( OnIsEnabledChanged ) )]
public static bool IsEnabled { get; set; } = true;
public static Color CrosshairActive = "#E6AD12";
public static Color CrosshairInactive = CrosshairActive.WithAlpha( 0.2f );
private static UI Current { get; set; }
static void OnIsEnabledChanged( bool before, bool after )
{
if ( Current.IsValid() )
{
Current.Update();
}
}
protected override void OnStart()
{
Current = this;
Update();
}
private void Update()
{
foreach ( var x in GetComponentsInChildren<PanelComponent>( true ) )
{
x.Enabled = IsEnabled;
}
// Weapon HUDs (crosshairs) are drawn by the engine straight onto the camera, which gates them
// on its "ui" exclude tag rather than on our panels - so flip that too, or they survive this.
if ( Scene.IsValid() && Scene.Camera.IsValid() )
{
if ( IsEnabled )
Scene.Camera.RenderExcludeTags.Remove( "ui" );
else
Scene.Camera.RenderExcludeTags.Add( "ui" );
}
}
}