mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-08-04 09:43:37 -04:00
* Enable CA2000 in editorconfig * Remove unused CaptureStdOut class * Add missing dispose calls in Sandbox.Tools * Add missing dispose calls in tests * Add missing dispose calls in launchers * Add missing dispose calls in Topten.RichtTextKit * Add missing dispose calls in Engine * Add missing dispose calls in SboxBuild * Add nullchecks to a few dispose calls * Fix more missing disposal calls and leaks * Disable CA2000 for Textures * Fix disposing too early in ImageFileTextureGenerator * Fix disposing codec, ownership is transferred to animation * Add missing using in ByteStream benchmark
44 lines
1.1 KiB
C#
44 lines
1.1 KiB
C#
using Sandbox.UI;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading;
|
|
|
|
namespace Sandbox.Resources;
|
|
|
|
[Title( "Text" )]
|
|
[Icon( "format_size" )]
|
|
[ClassName( "text" )]
|
|
public class TextTextureGenerator : TextureGenerator
|
|
{
|
|
public Vector2Int Size { get; set; } = 128;
|
|
|
|
public Margin Margin { get; set; } = 0;
|
|
|
|
public TextFlag TextFlags { get; set; } = TextFlag.Center | TextFlag.DontClip;
|
|
|
|
[InlineEditor( Label = false )]
|
|
public TextRendering.Scope TextScope { get; set; } = new( "🤕", Color.White, 64 );
|
|
|
|
[Hide, JsonIgnore]
|
|
public override bool CacheToDisk => true;
|
|
|
|
protected override ValueTask<Texture> CreateTexture( Options options, CancellationToken ct )
|
|
{
|
|
var w = Size.x.Clamp( 1, 1024 * 4 );
|
|
var h = Size.y.Clamp( 1, 1024 * 4 );
|
|
|
|
var rect = new Rect( 0, 0, w, h );
|
|
rect = rect.Shrink( Margin );
|
|
|
|
using var bitmap = new Bitmap( w, h, false );
|
|
bitmap.Clear( Color.Transparent );
|
|
bitmap.DrawText( TextScope, rect, TextFlags );
|
|
|
|
return ValueTask.FromResult( bitmap.ToTexture() );
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{TextScope.Text} ({TextScope.FontName}, {TextScope.FontSize})";
|
|
}
|
|
}
|