UI/Voices.razor

The voice chat indicator panel in the HUD. Lists Voice components whose LastPlayed is recent, draws each speaker's avatar and display name, and smooths their amplitude into a row scale and background color. Exposes a static ListHeight so other HUD panels can stack above it.

@using Sandbox
@using Sandbox.UI
@namespace Facepunch.UI
@inherits PanelComponent

@if ( !Hud.IsVisible( HudElement.Voices ) )
    return;

<root>

    @foreach ( var voice in DisplayedVoices() )
    {
        var activity = GetActivity( voice );

        <div class="item-row" style="transform: scale( @GetRowScale( activity ) ); background-color: @GetRowColor( activity )">
            <div class="avatar-wrap">
                <img class="avatar" src="avatar:@voice.Network.Owner.SteamId" />
            </div>
            <label class="icon">volume_up</label>
            <label class="name">@voice.Network.Owner.DisplayName</label>
        </div>
    }

</root>

@code
{
    public IEnumerable<Voice> VoiceList => Scene.GetAllComponents<Voice>();

    private Dictionary<ulong, float> _smoothed = new();

    /// <summary>
    /// How tall the list is on screen in screen pixels, zero when nobody is shown. The notices stack above it.
    /// </summary>
    public static float ListHeight { get; private set; }

    protected override void OnUpdate()
    {
        // starts above the vitals
        Panel.Style.Bottom = Length.Pixels( Hud.StackBottom * Panel.ScaleFromScreen );
        Panel.Style.Right = Hud.MarginLength;
        ListHeight = Hud.IsVisible( HudElement.Voices ) && AnyDisplayed() ? Panel.Box.Rect.Height : 0;
    }

    private readonly List<Voice> _displayed = new();

    private List<Voice> DisplayedVoices()
    {
        _displayed.Clear();
        Scene.GetAll( _displayed );

        for ( int i = _displayed.Count - 1; i >= 0; i-- )
        {
            if ( !CanDisplay( _displayed[i] ) ) _displayed.RemoveAt( i );
        }

        return _displayed;
    }

    private bool AnyDisplayed() => DisplayedVoices().Count > 0;

    protected override void OnDisabled()
    {
        ListHeight = 0;
    }

    private float GetSmoothed( Voice voice )
    {
        var id = voice.Network.Owner.SteamId;
        _smoothed[id] = _smoothed.GetValueOrDefault( id, 0f ).LerpTo( voice.Amplitude, Time.Delta * 10f );
        return _smoothed[id];
    }

    private bool CanDisplay( Voice voice )
    {
        if ( voice.Network.Owner is null ) return false;
        return voice.LastPlayed < 0.25f;
    }

    /// <summary>
    /// How much someone is talking, 0 to 1. Never quite zero while they are in the list, so a quiet moment still looks lit.
    /// </summary>
    private float GetActivity( Voice voice ) => Math.Clamp( GetSmoothed( voice ) * 4f, 0.25f, 1f );

    // The row grows and lights up with it
    private const float MaxScale = 1.1f;

    private string GetRowScale( float activity ) => ( 1f + ( MaxScale - 1f ) * activity ).ToString( "0.###", System.Globalization.CultureInfo.InvariantCulture );

    private string GetRowColor( float activity )
    {
        // from the resting colour of the row, black at 33%, towards a lighter grey
        var grey = (int)MathX.Lerp( 0, 90, activity );
        var alpha = MathX.Lerp( 0.33f, 0.5f, activity );

        return $"rgba( {grey}, {grey}, {grey}, {alpha.ToString( "0.###", System.Globalization.CultureInfo.InvariantCulture )} )";
    }

    protected override int BuildHash()
    {
        return HashCode.Combine( Time.Now );
    }
}