Editor/Effects/SFXREffectsListControlWidget.cs
using Editor;
using Sandbox;
using Sandbox.Helpers;
using System.Collections.Generic;
using System.Linq;

namespace SFXR.Editor;

[CustomEditor( typeof( List<SFXREffect> ) )]
public class SFXREffectsListControlWidget : ControlWidget
{
    private SerializedCollection Collection;

    private Layout Content;

    private Button addButton;

    public override bool SupportsMultiEdit => false;

    public SFXREffectsListControlWidget( SerializedProperty property )
        : base( property )
    {
        SetSizeMode( SizeMode.Ignore, SizeMode.Ignore );

        base.Layout = Layout.Column();
        base.Layout.Spacing = 2f;
        if ( property.TryGetAsObject( out var obj ) && obj is SerializedCollection collection )
        {
            Collection = collection;
            Collection.OnEntryAdded = Rebuild;
            Collection.OnEntryRemoved = Rebuild;
            Content = Layout.Column();
            base.Layout.Add( Content );
            Layout layout = base.Layout.AddRow();
            layout.Margin = 8;
            layout.AddStretchCell();
            addButton = layout.Add( new Button( "Add Effect" )
            {
                ToolTip = "Add new effect",
            } );
            addButton.MinimumWidth = 200;
            addButton.Clicked = () => AddEffectDialog( addButton );
            layout.AddStretchCell();
            Rebuild();
        }
    }

    public void Rebuild()
    {
        Content.Clear( deleteWidgets: true );
        Content.Margin = 0f;
        Layout layout = Layout.Column();
        layout.Spacing = 2f;
        int num = 0;
        foreach ( SerializedProperty item in Collection )
        {
            int index = num;
            var itemLayout = Layout.Row();
            var thing = ControlWidget.Create( item );
            thing.MinimumHeight = 120;
            itemLayout.Add( thing );
            itemLayout.Add( new IconButton( "remove", delegate
            {
                RemoveEntry( index );
            } )
            {
                Background = Color.Transparent,
                FixedWidth = ControlWidget.ControlRowHeight,
                FixedHeight = ControlWidget.ControlRowHeight
            } );
            layout.Add( itemLayout );
            num++;
        }

        Content.Add( layout );
        Content.Margin = ((num > 0) ? 3 : 0);
    }

    private void AddEntry()
    {
        Collection.Add( null );
    }

    private void RemoveEntry( int index )
    {
        Collection.RemoveAt( index );
    }

    protected override void OnPaint()
    {
        // Paint.Antialiasing = true;
        // Paint.ClearPen();
        // Paint.SetBrush( Theme.ControlText.Darken( 0.6f ) );
        // if ( Collection.Count() > 0 )
        // {
        //     Rect rect = Content.OuterRect;
        //     Paint.DrawRect( in rect, 2f );
        //     Vector2 point = addButton.Position;
        //     Vector2 size = addButton.Size;
        //     rect = new Rect( in point, in size );
        //     float left = 0f;
        //     float top = 8f;
        //     float right = 0f;
        //     float bottom = 0f;
        //     rect = rect.Grow( in left, in top, in right, in bottom );
        //     Paint.DrawRect( in rect, 2f );
        // }
        // else
        // {
        //     Vector2 point = addButton.Position;
        //     Vector2 size = addButton.Size;
        //     Rect rect = new Rect( in point, in size );
        //     float left = 0f;
        //     float top = 0f;
        //     float right = 0f;
        //     float bottom = 0f;
        //     rect = rect.Grow( in left, in top, in right, in bottom );
        //     Paint.DrawRect( in rect, 2f );
        // }
    }

    public void AddEffectDialog( Button source )
    {
        var s = new SFXREffectTypeSelector( this );
        s.OnSelect += ( t ) => AddEffect( t );
        s.OpenAt( source.ScreenRect.BottomLeft, animateOffset: new Vector2( 0, -4 ) );
        s.FixedWidth = source.Width;
    }

    void AddEffect( TypeDescription type )
    {
        if ( !type.TargetType.IsAssignableTo( typeof( SFXREffect ) ) )
        {
            Log.Error( $"Type {type.TargetType} is not assignable to {typeof( SFXREffect )}" );
            return;
        }

        SFXREffect effect = type.Create<SFXREffect>();
        Collection.Add( effect );
    }


}