swb_base/ui/RootWeaponDisplay.cs

UI component that builds the root HUD for a weapon. On start it loads a stylesheet, creates the weapon crosshair if enabled, adds a sniper scope overlay for scoped weapons without sight attachments, and always adds a centre ADS dot.

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
			// ⚠️ WAS AN EARLY `return`, which also skipped everything added after this block. Turned
			// into a nested branch when the ADS dot went in below -- a scoped weapon wearing a sight
			// attachment would otherwise be the one weapon with no centre dot.
			var attachment = Weapon.GetActiveAttachmentForCategory( Attachments.AttachmentCategory.Sight );
			if ( !attachment.IsValid() )
			{
				var sniperScope = new SniperScope( Weapon, Weapon.ScopeInfo.LensTexture, Weapon.ScopeInfo.ScopeTexture );
				Panel.AddChild( sniperScope );
			}
		}

		// ⛔ ADDED LAST, so it draws OVER the scope overlay. Nothing in this hud sets a z-index, so
		// DOM order is the whole of the stacking rule -- added first, the dot sits under the scope
		// texture and is invisible on exactly the weapons that most want a centre mark.
		Panel.AddChild( new NZombies.AdsCenterDot( Weapon ) );
	}
}