mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-08-01 00:08:05 -04:00
ADDED css min(), max() and clamp() for lengths css-wide keywords inherit, initial, unset, revert (incl. shorthands) added currentColor keyword to css colors can parse oklch(), lab() and hwb() added css text-align: justify css white-space: pre-wrap and break-spaces added css word-break: break-word parses css image-rendering: crisp-edges (point sampling) added css inset shorthand now parses 1–4 values in the css border-width shorthand added css transition-duration, -delay, -property and -timing-function longhands css :has() now supports descendant selectors added css opacity percentages added css overflow: auto (maps to scroll) added css gap: normal added css filter: none css transform: scale() now takes comma-separated args css animations now accept ms units added more css justify-content, align-* and text-align keywords (start, end, …) parses css dvh/svh/lvh/dvw viewport units (treated as vh/vw) parses css object-fit: scale-down (treated as contain) added css margin-block / margin-inline (+ the logical margin sides) added css padding-block / padding-inline (+ the logical padding sides) added css inset-block / inset-inline (+ the logical inset sides) added the css flex-flow shorthand added the css font shorthand added css font-size keywords (xx-small … xxx-large) css letter-spacing and word-spacing now accept normal added css font-smooth: none css aspect-ratio now accepts the auto form css font-family now maps generic families (serif, sans-serif, monospace) added css flex-flow added css font FIXED fixed css !important dropping the whole declaration css line-height without units is now a multiplier fixed css calc() division and full-expression parsing fixed the css flex shorthand for single-number and three-value forms fixed the css transition shorthand when the property is omitted css font-family now uses the first family in a comma stack colors now parse fully in the css background shorthand an unknown css @-rule no longer drops the whole stylesheet css variables no longer match on partial tokens injected css variables are kept across stylesheet hotloads fixed css selector specificity overflowing when comparing rules css background: none now resets the background (image and colour) css filter: none now overrides a filter set by a base class css transform: none now overrides a transform set by a base class IMPROVED css rule matching is ~3–5× faster on large stylesheets (rules indexed by class) ~2× fewer allocations in css transitions (snapshot styles once, not per property) finished css animations no longer re-layout every frame less GC in css sibling/child selector queries (no list copies or wrappers) less GC diffing active css rules (no LINQ or hashsets) faster, lower-GC Yoga layout wrapper css stylesheet hotload now watches newly imported files
227 lines
5.1 KiB
C#
227 lines
5.1 KiB
C#
using Sandbox.Utility;
|
|
|
|
namespace Sandbox.UI;
|
|
|
|
/// <summary>
|
|
/// Handles the storage, progression and application of CSS transitions for a single <see cref="Panel"/>.
|
|
/// </summary>
|
|
public sealed class Transitions
|
|
{
|
|
public delegate void TransitionFunction( Styles style, float delta );
|
|
|
|
public struct Entry
|
|
{
|
|
public string Property { get; init; }
|
|
public double StartTime { get; init; }
|
|
public double Length { get; init; }
|
|
public int Target { get; init; }
|
|
public Utility.Easing.Function EasingFunction { get; init; }
|
|
public bool IsKilled { get; private set; }
|
|
|
|
public TransitionFunction Action { get; init; }
|
|
|
|
public Entry( string property, double startTime, double length, int target, TransitionFunction action, Easing.Function easingFunction ) : this()
|
|
{
|
|
Property = property;
|
|
StartTime = startTime;
|
|
Length = length;
|
|
Target = target;
|
|
Action = action;
|
|
EasingFunction = easingFunction;
|
|
|
|
IsKilled = false;
|
|
}
|
|
|
|
internal void Kill()
|
|
{
|
|
IsKilled = true;
|
|
}
|
|
|
|
internal void Restore()
|
|
{
|
|
IsKilled = false;
|
|
}
|
|
|
|
public float Ease( float delta ) => EasingFunction.Invoke( delta );
|
|
public void Invoke( Styles style, float delta ) => Action.Invoke( style, delta );
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"Entry( '{Property}', {StartTime}s, {Length}s, '{Target}', {Action}, {EasingFunction} )";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Active CSS transitions.
|
|
/// </summary>
|
|
public List<Entry> Entries = null;
|
|
|
|
/// <summary>
|
|
/// Whether there are any active CSS transitions.
|
|
/// </summary>
|
|
public bool HasAny => Entries?.Count > 0;
|
|
|
|
private Panel panel;
|
|
|
|
internal Transitions( Panel panel )
|
|
{
|
|
this.panel = panel;
|
|
}
|
|
|
|
internal void Kill( Styles from )
|
|
{
|
|
if ( !from.HasTransitions ) return;
|
|
if ( Entries == null ) return;
|
|
|
|
for ( int i = 0; i < Entries.Count; i++ )
|
|
{
|
|
if ( !from.Transitions.List.Any( x => x.Property == Entries[i].Property ) )
|
|
continue;
|
|
|
|
var transition = Entries[i];
|
|
transition.Kill();
|
|
Entries[i] = transition;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear all transitions. This will immediately remove transitions, leaving styles wherever they are.
|
|
/// </summary>
|
|
internal void Clear()
|
|
{
|
|
Entries?.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Immediately snaps all transitions to the end point, at which point they're removed.
|
|
/// </summary>
|
|
internal void Kill()
|
|
{
|
|
if ( Entries == null ) return;
|
|
|
|
for ( int i = 0; i < Entries.Count; i++ )
|
|
{
|
|
var transition = Entries[i];
|
|
transition.Kill();
|
|
Entries[i] = transition;
|
|
}
|
|
}
|
|
|
|
internal void Add( Styles from, Styles to, double startTime )
|
|
{
|
|
if ( !to.HasTransitions ) return;
|
|
|
|
// Snapshot once and share across every descriptor - the lerp only ever reads these
|
|
var fromCopy = (Styles)from.Clone();
|
|
var toCopy = (Styles)to.Clone();
|
|
|
|
foreach ( var desc in to.Transitions.List )
|
|
{
|
|
TransitionFunction action = (desc.Property == "all")
|
|
? ( style, delta ) => style.FromLerp( fromCopy, toCopy, delta )
|
|
: ( style, delta ) => style.LerpProperty( desc.Property, fromCopy, toCopy, delta );
|
|
|
|
Transition( desc, from, to, action, startTime );
|
|
}
|
|
}
|
|
|
|
void Transition( in TransitionDesc desc, BaseStyles from, BaseStyles to, TransitionFunction action, double startTime )
|
|
{
|
|
if ( from == to ) return;
|
|
|
|
var target = HashCode.Combine( to?.GetHashCode(), desc.Property );
|
|
if ( TryRestoreTransition( target ) ) return;
|
|
|
|
Add( desc, target, action, startTime );
|
|
}
|
|
|
|
void Add( in TransitionDesc desc, int target, TransitionFunction action, double startTime )
|
|
{
|
|
var length = 1.0f;
|
|
var property = desc.Property;
|
|
|
|
if ( desc.Duration.HasValue )
|
|
length = desc.Duration.Value / 1000.0f;
|
|
|
|
if ( length <= 0 && !desc.Delay.HasValue )
|
|
return;
|
|
|
|
if ( desc.Delay.HasValue )
|
|
{
|
|
startTime += desc.Delay.Value / 1000.0f;
|
|
}
|
|
|
|
Entries ??= new List<Entry>();
|
|
var easingFunction = Easing.GetFunction( desc.TimingFunction );
|
|
|
|
var entry = new Entry( property, startTime, length, target, action, easingFunction );
|
|
Entries.Add( entry );
|
|
}
|
|
|
|
internal bool Run( Styles style, double now )
|
|
{
|
|
if ( !HasAny )
|
|
return false;
|
|
|
|
var removedExpired = false;
|
|
for ( int i = Entries.Count - 1; i >= 0; i-- )
|
|
{
|
|
if ( Entries[i].StartTime + Entries[i].Length < now )
|
|
{
|
|
Entries.RemoveAt( i );
|
|
removedExpired = true;
|
|
}
|
|
}
|
|
|
|
if ( removedExpired )
|
|
{
|
|
panel.SetNeedsPreLayout();
|
|
}
|
|
|
|
foreach ( var entry in Entries )
|
|
{
|
|
//
|
|
// Pre-start (delay)
|
|
//
|
|
if ( now < entry.StartTime )
|
|
{
|
|
entry.Invoke( style, entry.Ease( 0 ) );
|
|
continue;
|
|
}
|
|
|
|
var endTime = entry.StartTime + entry.Length;
|
|
if ( now > endTime ) continue;
|
|
|
|
var t = entry.IsKilled ? 1f : (now - entry.StartTime) / entry.Length;
|
|
|
|
entry.Invoke( style, entry.Ease( (float)t ) );
|
|
}
|
|
|
|
if ( Entries.RemoveAll( x => x.IsKilled ) > 0 )
|
|
{
|
|
panel.SetNeedsPreLayout();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
internal bool TryRestoreTransition( int targetValue )
|
|
{
|
|
if ( Entries == null ) return false;
|
|
|
|
for ( int i = Entries.Count - 1; i >= 0; i-- )
|
|
{
|
|
if ( Entries[i].Target != targetValue )
|
|
continue;
|
|
|
|
var entry = Entries[i];
|
|
entry.Restore();
|
|
Entries[i] = entry;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|