swb_base/ui/RootWeaponDisplay.cs

UI PanelComponent that displays weapon HUD elements for a given Weapon. On start it disables itself for proxy contexts, loads a stylesheet, creates a crosshair if enabled, and adds a sniper scope HUD when the weapon is scoping and no sight attachment provides its own scope.

File Access
using Sandbox.UI;

namespace SWB.Base.UI;

public class RootWeaponDisplay : PanelComponent
{
	public Weapon Weapon { get; set; }

	protected override void OnStart()
	{
		if ( IsProxy )
		{
			Enabled = false;
			return;
		}

		Panel.StyleSheet.Load( "/swb_base/ui/RootWeaponDisplay.cs.scss" );

		if ( Weapon.CrosshairSettings.Enabled )
			Weapon.CreateCrosshair( Panel );

		if ( Weapon.Scoping )
		{
			// Sight attachments should take care of their own scope hud elements
			var attachment = Weapon.GetActiveAttachmentForCategory( Attachments.AttachmentCategory.Sight );
			if ( attachment.IsValid() ) return;

			var sniperScope = new SniperScope( Weapon, Weapon.ScopeInfo.LensTexture, Weapon.ScopeInfo.ScopeTexture );
			Panel.AddChild( sniperScope );
		}
	}
}