using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UIElements;
namespace Alchemy.Editor.Elements
{
///
/// Visual Element that draws an IList
///
public sealed class ListField : VisualElement
{
sealed class Item : VisualElement
{
public int index;
}
const string ItemClassName = "unity-list-view__item";
public ListField(IList target, string label)
{
Assert.IsNotNull(target);
if (target is Array array)
{
this.array = array;
arrayElementType = array.GetType().GetElementType();
var listType = typeof(List<>).MakeGenericType(arrayElementType);
list = (IList)Activator.CreateInstance(listType);
foreach (var item in array) list.Add(item);
}
else
{
list = target;
}
listView = GUIHelper.CreateDefaultListView(label);
listView.makeItem = () => new Item();
listView.bindItem = (element, index) =>
{
((Item)element).index = index;
var value = list[index];
var listType = list.GetType();
var valueType = value != null ? value.GetType() : listType.IsGenericType ? listType.GenericTypeArguments[0] : typeof(object);
var fieldElement = new GenericField(value, valueType, label);
element.Add(fieldElement);
var labelElement = fieldElement.Q