mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-05-19 12:27:52 -04:00
New UI rendering that groups multiple panels to be grouped into a single draw call when possible. New custom rendering API for custom panels using `Panel.Draw.XX` to emit drawing commands.
98 lines
1.8 KiB
C#
98 lines
1.8 KiB
C#
using Sandbox.Rendering;
|
|
|
|
namespace Sandbox.UI;
|
|
|
|
/// <summary>
|
|
/// A generic panel that draws an SVG scaled to size
|
|
/// </summary>
|
|
[Library( "svg" ), Expose]
|
|
public partial class SvgPanel : Panel
|
|
{
|
|
|
|
/// <summary>
|
|
/// Content path to the SVG file
|
|
/// </summary>
|
|
public string Src
|
|
{
|
|
get => _src;
|
|
|
|
set
|
|
{
|
|
if ( _src == value )
|
|
return;
|
|
|
|
_src = value;
|
|
ReloadTexture();
|
|
}
|
|
}
|
|
internal string _src;
|
|
|
|
/// <summary>
|
|
/// Optional color to draw the SVG with
|
|
/// </summary>
|
|
public string Color
|
|
{
|
|
get => _color;
|
|
|
|
set
|
|
{
|
|
if ( _color == value )
|
|
return;
|
|
|
|
_color = value;
|
|
ReloadTexture();
|
|
}
|
|
}
|
|
internal string _color;
|
|
|
|
Texture texture;
|
|
int sizeHash;
|
|
|
|
public override void FinalLayout( Vector2 offset )
|
|
{
|
|
base.FinalLayout( offset );
|
|
|
|
if ( !IsVisible ) return;
|
|
|
|
var hash = HashCode.Combine( Box.Rect.Width, Box.Rect.Height, ComputedStyle.BackgroundSizeX, ComputedStyle.BackgroundSizeY );
|
|
if ( hash == sizeHash ) return;
|
|
|
|
sizeHash = hash;
|
|
ReloadTexture();
|
|
}
|
|
|
|
private async void ReloadTexture()
|
|
{
|
|
if ( ComputedStyle is null )
|
|
return;
|
|
|
|
var rect = Box.Rect;
|
|
var width = rect.Width;
|
|
var height = rect.Height;
|
|
|
|
if ( ComputedStyle.BackgroundSizeX.HasValue && ComputedStyle.BackgroundSizeX != Length.Undefined )
|
|
width = ComputedStyle.BackgroundSizeX.Value.GetPixels( width );
|
|
|
|
if ( ComputedStyle.BackgroundSizeY.HasValue && ComputedStyle.BackgroundSizeY != Length.Undefined )
|
|
height = ComputedStyle.BackgroundSizeY.Value.GetPixels( height );
|
|
|
|
var url = $"{Src}?w={(int)width}&h={(int)height}";
|
|
|
|
if ( !string.IsNullOrEmpty( Color ) )
|
|
{
|
|
url += $"&color={Color}";
|
|
}
|
|
|
|
texture = await Texture.LoadAsync( url );
|
|
IsRenderDirty = true;
|
|
}
|
|
|
|
public override void OnDraw()
|
|
{
|
|
if ( texture == null )
|
|
return;
|
|
|
|
DrawBackgroundTexture( texture, Length.Cover );
|
|
}
|
|
}
|