Terrain/TerrainPalette.cs
namespace HC3.Terrain;

#nullable enable

/// <summary>
/// Describes which materials tiles can use when painting.
/// </summary>
[AssetType( Name = "Terrain Palette", Extension = "trnplt" )]
public sealed class TerrainPalette : GameResource
{
	public List<TileMaterial> Materials { get; set; } = new();

	private GpuBuffer<Vector3>? _colorBuffer;

	protected override Bitmap CreateAssetTypeIcon( int width, int height )
	{
		return CreateSimpleAssetTypeIcon( "palette", width, height );
	}

	public void Apply( RenderAttributes attribs )
	{
		if ( !_colorBuffer.IsValid() )
		{
			_colorBuffer = new GpuBuffer<Vector3>( Materials.Count );
			UpdateBuffer( _colorBuffer );
		}

		attribs.Set( "Palette", _colorBuffer );
	}

	protected override void PostReload()
	{
		if ( _colorBuffer is { } buffer )
		{
			UpdateBuffer( buffer );
		}
	}

	private void UpdateBuffer( GpuBuffer<Vector3> buffer )
	{
		buffer.SetData( Materials.Select( x => (Vector3)x.Color ).ToArray() );
	}
}

public sealed class TileMaterial
{
	public string Name { get; set; } = "Unnamed";
	public Color Color { get; set; } = Color.Parse( "#5a724c" )!.Value;

	public override string ToString() => $"{Name} ({Color.Hex})";
}