Editor/Tileset/TilesetGlobalTagsWidget.cs

Editor UI widget for Saandy Tilemapper that displays and edits a tileset's global tags. It builds a small panel with title and hint, binds a ControlSheet to the tileset's serialized object, and invokes a callback when GlobalTags changes.

Reflection
using Editor;
using Saandy.Tilemapper;
using Sandbox;
using System;

namespace Saandy.Editor.Tilemapper;

public sealed class TilesetGlobalTagsWidget : Widget
{
	private readonly TilesetResource _tileset;
	private readonly Action _onChanged;

	public TilesetGlobalTagsWidget( Widget parent, TilesetResource tileset, Action onChanged ) : base( parent )
	{
		_tileset = tileset;
		_onChanged = onChanged;

		MinimumHeight = 104;
		HorizontalSizeMode = SizeMode.Flexible;
		VerticalSizeMode = SizeMode.CanGrow;

		Layout = Layout.Column();
		Layout.Margin = 10;
		Layout.Spacing = 8;

		Build();
	}

	protected override void OnPaint()
	{
		base.OnPaint();

		Paint.ClearPen();
		Paint.SetBrush( Color.Black.WithAlpha( 0.22f ) );
		Paint.DrawRect( LocalRect, 4.0f );

		Paint.SetPen( Color.White.WithAlpha( 0.12f ), 1.0f );
		Paint.ClearBrush();
		Paint.DrawRect( LocalRect.Shrink( 0.5f ), 4.0f );
	}

	private void Build()
	{
		if ( Layout == null )
			return;

		Layout.Clear( true );

		var title = new Label( "Global Tags", this );
		title.SetStyles( "font-weight: 700; font-size: 15px;" );
		Layout.Add( title );

		var hint = new Label( "Applied to every tile in this tileset.", this );
		hint.SetStyles( "color: #aaa; font-size: 12px;" );
		Layout.Add( hint );

		if ( _tileset == null )
			return;

		_tileset.EnsureTagSets();

		var serializedObject = _tileset.GetSerialized();

		var sheet = new ControlSheet();
		sheet.AddObject( serializedObject, prop =>
		{
			if ( prop.HasAttribute<HideAttribute>() )
				return false;

			return prop.Name == nameof( TilesetResource.GlobalTags );
		} );

		serializedObject.OnPropertyChanged += prop =>
		{
			if ( prop == null || prop.Name == nameof( TilesetResource.GlobalTags ) )
				_onChanged?.Invoke();
		};

		Layout.Add( sheet );
	}
}