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
164 lines
3.5 KiB
C#
164 lines
3.5 KiB
C#
using System.Globalization;
|
|
|
|
namespace Sandbox.UI
|
|
{
|
|
public abstract partial class BaseStyles
|
|
{
|
|
static float? ParseFloat( string value )
|
|
{
|
|
if ( float.TryParse( value, CultureInfo.InvariantCulture, out var result ) )
|
|
return result;
|
|
|
|
return null;
|
|
}
|
|
|
|
static int? ParseInt( string value )
|
|
{
|
|
if ( int.TryParse( value, CultureInfo.InvariantCulture, out var result ) )
|
|
return result;
|
|
|
|
return null;
|
|
}
|
|
|
|
static float? ParseSeconds( string value )
|
|
{
|
|
value = value.Trim();
|
|
|
|
// Milliseconds - must be checked before the bare 's' suffix since "200ms" also ends in 's'.
|
|
if ( value.EndsWith( "ms" ) )
|
|
{
|
|
var ms = ParseFloat( value.Substring( 0, value.Length - 2 ) );
|
|
return ms.HasValue ? ms.Value / 1000.0f : default;
|
|
}
|
|
|
|
if ( value.EndsWith( "s" ) )
|
|
{
|
|
return ParseFloat( value.Substring( 0, value.Length - 1 ) );
|
|
}
|
|
|
|
return default;
|
|
}
|
|
|
|
static float? ParseAspectRatio( string value )
|
|
{
|
|
value = value.Trim();
|
|
|
|
// 'auto' on its own means no forced ratio; 'auto 16/9' means fall back to the given ratio.
|
|
if ( value.StartsWith( "auto", System.StringComparison.OrdinalIgnoreCase ) )
|
|
{
|
|
value = value.Substring( 4 ).Trim();
|
|
if ( value.Length == 0 ) return null;
|
|
}
|
|
|
|
var vals = value.Split( new[] { ' ', ':', '/' }, StringSplitOptions.RemoveEmptyEntries );
|
|
if ( vals.Length == 0 ) return null;
|
|
if ( vals.Length == 1 )
|
|
{
|
|
return ParseFloat( vals[0] );
|
|
}
|
|
return ParseFloat( vals[0] ) / ParseFloat( vals[1] );
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether there is an active CSS animation.
|
|
/// </summary>
|
|
public bool HasAnimation
|
|
{
|
|
get
|
|
{
|
|
if ( _animationname is null ) return false;
|
|
if ( _animationname.Length == 0 ) return false;
|
|
if ( _animationname == "none" ) return false;
|
|
if ( string.IsNullOrWhiteSpace( _animationname ) ) return false;
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
protected void Lerp( ref float? o, in float? a, in float? b, in float? defaultValue, float delta )
|
|
{
|
|
if ( !a.HasValue && !b.HasValue )
|
|
return;
|
|
|
|
float from = a ?? defaultValue.Value;
|
|
float to = b ?? defaultValue.Value;
|
|
|
|
if ( from == to )
|
|
{
|
|
o = from;
|
|
return;
|
|
}
|
|
|
|
o = from.LerpTo( to, delta );
|
|
}
|
|
|
|
protected void Lerp( ref PanelTransform? o, in PanelTransform? a, in PanelTransform? b, in PanelTransform? defaultValue, float delta )
|
|
{
|
|
if ( !a.HasValue && !b.HasValue )
|
|
return;
|
|
|
|
var from = a ?? defaultValue.Value;
|
|
var to = b ?? defaultValue.Value;
|
|
|
|
if ( from == to )
|
|
{
|
|
o = from;
|
|
return;
|
|
}
|
|
|
|
o = PanelTransform.Lerp( from, to, delta );
|
|
}
|
|
|
|
protected void Lerp( ref Color? o, in Color? a, in Color? b, in Color? defaultValue, float delta )
|
|
{
|
|
if ( !a.HasValue && !b.HasValue )
|
|
return;
|
|
|
|
var from = a ?? defaultValue ?? default;
|
|
var to = b ?? defaultValue ?? default;
|
|
|
|
if ( from == to )
|
|
{
|
|
o = from;
|
|
return;
|
|
}
|
|
|
|
o = Color.Lerp( from, to, delta );
|
|
}
|
|
|
|
protected void Lerp( ref Length? o, in Length? a, in Length? b, in Length? defaultValue, float delta )
|
|
{
|
|
if ( !a.HasValue && !b.HasValue )
|
|
return;
|
|
|
|
var from = a ?? defaultValue.Value;
|
|
var to = b ?? defaultValue.Value;
|
|
|
|
if ( from == to )
|
|
{
|
|
o = from;
|
|
return;
|
|
}
|
|
|
|
o = Length.Lerp( from, to, delta );
|
|
}
|
|
|
|
protected void Lerp( ref int? o, in int? a, in int? b, in int? defaultValue, float delta )
|
|
{
|
|
if ( !a.HasValue && !b.HasValue )
|
|
return;
|
|
|
|
var from = a ?? defaultValue.Value;
|
|
var to = b ?? defaultValue.Value;
|
|
|
|
if ( from == to )
|
|
{
|
|
o = from;
|
|
return;
|
|
}
|
|
|
|
o = (int)MathX.Lerp( from, to, delta );
|
|
}
|
|
}
|
|
}
|