using System;
using Ichni.RhythmGame;
using Lean.Pool;
using UnityEngine;
namespace Ichni.Editor
{
///
/// 处理所有 Enum 类型的 Drawer。
/// 通过反射获取字段的枚举类型,创建 DynamicUIEnumDropdown。
///
public class EnumDropdownDrawer : IPropertyDrawer
{
public int DefaultSpan => 1;
public float DefaultHeight => LayoutPacker.DefaultRowHeight;
public DynamicUIElement Draw(DrawContext ctx)
{
var go = LeanPool.Spawn(ctx.Registry.GetPrefab("enumDropdown"), ctx.Parent);
var element = go.GetComponent();
// 获取枚举类型:优先使用 DrawContext 中的 EnumType,fallback 到反射
Type enumType = ctx.EnumType;
if (enumType == null && ctx.Target != null && !string.IsNullOrEmpty(ctx.FieldName))
{
var field = ctx.Target.GetType().GetField(ctx.FieldName);
if (field != null) enumType = field.FieldType;
else
{
var prop = ctx.Target.GetType().GetProperty(ctx.FieldName);
if (prop != null) enumType = prop.PropertyType;
}
}
if (enumType != null)
{
element.SetUpEnum(enumType);
}
element.Initialize(ctx.Target, ctx.Label, ctx.FieldName);
int span = ctx.OverrideSpan ?? DefaultSpan;
float height = ctx.OverrideHeight ?? DefaultHeight;
element.SetLayoutSize(span * LayoutPacker.TotalRowWidth / LayoutPacker.MaxSpanPerRow, height);
return element;
}
}
}