Created
January 25, 2021 08:59
-
-
Save garrynewman/cc26ea04b20c81f9aae358aeb4ee75ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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