UI/DemoHudController.cs

A UI controller component that finds the local RootUI object and disables all HUD panels except a DirectorDebugHud, creating and enabling that debug panel when needed.

using Sandbox;
using Sandbox.UI;

namespace AdaptiveDirectorDemo.UI;

/// <summary>
/// Keeps only the adaptive Director telemetry on the weapon base's existing
/// local ScreenPanel. All stock weapon and health presentation is disabled.
/// </summary>
[Title( "Demo HUD Controller" )]
[Category( "Adaptive Director Demo" )]
public sealed class DemoHudController : Component
{
	private GameObject _rootUi;
	private DirectorDebugHud _directorDebugHud;

	protected override void OnUpdate()
	{
		var rootUi = FindLocalRootUi();
		if ( rootUi is null )
		{
			_rootUi = null;
			_directorDebugHud = null;
			return;
		}

		// The demo is currently a focused Director visualizer. Keep every stock
		// HUD panel off and leave only DirectorDebugHud enabled.
		foreach ( var panel in rootUi.Components.GetAll<PanelComponent>( FindMode.EverythingInSelf ) )
		{
			if ( panel is DirectorDebugHud )
				continue;

			panel.Enabled = false;
		}

		if ( _rootUi == rootUi &&
			_directorDebugHud is not null && _directorDebugHud.IsValid )
			return;

		_rootUi = rootUi;
		_directorDebugHud = rootUi.Components.GetOrCreate<DirectorDebugHud>();
		_directorDebugHud.Enabled = true;
	}

	private GameObject FindLocalRootUi()
	{
		foreach ( var candidate in Scene.GetAllObjects( false ) )
		{
			if ( candidate.Name == "RootUI" && candidate.Parent?.Name == "Player" )
				return candidate;
		}

		return null;
	}
}