Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Sandbox;
using Sandbox.Hooks;
using Sandbox.UI;
using Sandbox.UI.Construct;
using System;
using System.Collections.Generic;
namespace Sandbox.UI
{
public partial class Scoreboard<T> : Panel where T : ScoreboardEntry, new()
{
public Panel Canvas { get; protected set; }
public TextEntry Input { get; protected set; }
Dictionary<int, T> Entries = new ();
public Scoreboard()
{
StyleSheet = StyleSheet.FromFile( "/ui/scoreboard/Scoreboard.scss" );
AddClass( "scoreboard" );
AddHeader();
Canvas = Add.Panel( "canvas" );
PlayerInfo.OnPlayerAdded += AddPlayer;
PlayerInfo.OnPlayerUpdated += UpdatePlayer;
PlayerInfo.OnPlayerRemoved += RemovePlayer;
foreach ( var player in PlayerInfo.All )
{
AddPlayer( player );
}
}
public override void Tick()
{
base.Tick();
SetClass( "open", Player.Local?.Input.Down( InputButton.Score ) ?? false );
}
protected virtual void AddHeader()
{
var p = Add.Panel( "header" );
p.Add.Label( "Name", "name" );
p.Add.Label( "Kills", "kills" );
p.Add.Label( "Deaths", "deaths" );
p.Add.Label( "Ping", "ping" );
}
protected virtual void AddPlayer( PlayerInfo.Entry entry )
{
var p = Canvas.AddChild<T>();
p.UpdateFrom( entry );
Entries[entry.Id] = p;
}
protected virtual void UpdatePlayer( PlayerInfo.Entry entry )
{
if ( Entries.TryGetValue( entry.Id, out var panel ) )
{
panel.UpdateFrom( entry );
}
}
protected virtual void RemovePlayer( PlayerInfo.Entry entry )
{
if ( Entries.TryGetValue( entry.Id, out var panel ) )
{
panel.Delete();
Entries.Remove( entry.Id );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment