117 lines
3.3 KiB
C#
117 lines
3.3 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace UVC.UIToolkit.Property
|
|
{
|
|
/// <summary>
|
|
/// 열거형 속성 아이템
|
|
/// DropdownField를 사용한 열거형 선택
|
|
/// </summary>
|
|
public class UTKEnumPropertyItem : UTKPropertyItemBase<Enum>
|
|
{
|
|
#region Fields
|
|
private DropdownField? _dropdown;
|
|
private Type _enumType;
|
|
private List<string> _choices = new();
|
|
#endregion
|
|
|
|
#region Properties
|
|
public override UTKPropertyType PropertyType => UTKPropertyType.Enum;
|
|
|
|
/// <summary>열거형 타입</summary>
|
|
public Type EnumType => _enumType;
|
|
#endregion
|
|
|
|
#region Constructor
|
|
public UTKEnumPropertyItem(string id, string name, Enum initialValue)
|
|
: base(id, name, initialValue ?? throw new ArgumentNullException(nameof(initialValue)))
|
|
{
|
|
_enumType = initialValue.GetType();
|
|
_choices = Enum.GetNames(_enumType).ToList();
|
|
}
|
|
#endregion
|
|
|
|
#region Override Methods
|
|
public override VisualElement CreateUI()
|
|
{
|
|
var container = CreateContainer();
|
|
|
|
var label = CreateNameLabel();
|
|
container.Add(label);
|
|
|
|
var valueContainer = new VisualElement();
|
|
valueContainer.AddToClassList("utk-property-item__value");
|
|
|
|
_dropdown = new DropdownField(_choices, _choices.IndexOf(Value.ToString()));
|
|
_dropdown.name = "dropdown-field";
|
|
valueContainer.Add(_dropdown);
|
|
|
|
container.Add(valueContainer);
|
|
|
|
return container;
|
|
}
|
|
|
|
public override void BindUI(VisualElement element)
|
|
{
|
|
base.BindUI(element);
|
|
|
|
_dropdown = element.Q<DropdownField>("dropdown-field");
|
|
|
|
if (_dropdown != null)
|
|
{
|
|
_dropdown.choices = _choices;
|
|
_dropdown.index = _choices.IndexOf(Value.ToString());
|
|
_dropdown.SetEnabled(!IsReadOnly);
|
|
_dropdown.RegisterValueChangedCallback(OnDropdownChanged);
|
|
}
|
|
}
|
|
|
|
public override void UnbindUI(VisualElement element)
|
|
{
|
|
if (_dropdown != null)
|
|
{
|
|
_dropdown.UnregisterValueChangedCallback(OnDropdownChanged);
|
|
_dropdown = null;
|
|
}
|
|
|
|
base.UnbindUI(element);
|
|
}
|
|
|
|
public override void RefreshUI()
|
|
{
|
|
if (_dropdown != null)
|
|
{
|
|
var currentValue = Value.ToString();
|
|
if (_dropdown.value != currentValue)
|
|
{
|
|
_dropdown.SetValueWithoutNotify(currentValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void UpdateReadOnlyState()
|
|
{
|
|
base.UpdateReadOnlyState();
|
|
|
|
if (_dropdown != null)
|
|
{
|
|
_dropdown.SetEnabled(!IsReadOnly);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Private Methods
|
|
private void OnDropdownChanged(ChangeEvent<string> evt)
|
|
{
|
|
if (Enum.TryParse(_enumType, evt.newValue, out object? result) && result is Enum enumValue)
|
|
{
|
|
Value = enumValue;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|