using System.Threading; namespace Sandbox.UI; public partial class Panel { internal float TimeNow => PanelRealTime.TimeNow; internal float TimeDelta => PanelRealTime.TimeDelta; /// /// Can be used to store random data without sub-classing the panel. /// [Hide] public object UserData { get; set; } CancellationTokenSource _deleteTokenSource; /// /// Get a token that is cancelled when the panel is deleted /// [Hide] public CancellationToken DeletionToken { get { if ( IsDeleting || !IsValid ) return CancellationToken.None; _deleteTokenSource ??= new CancellationTokenSource(); return _deleteTokenSource.Token; } } } static class PanelRealTime { public static float TimeNow; public static float TimeDelta; public static void Update() { var delta = RealTime.Delta; // If we're running lower than 30fps, clamp it to avoid weirdness delta = delta.Clamp( 0.000f, 1.0f / 30.0f ); TimeDelta = delta; TimeNow += TimeDelta; } }