Files
XRLib/Assets/Scripts/UVC/UIToolkit/Property/Items/UTKEnumPropertyItem.cs

129 lines
3.5 KiB
C#
Raw Normal View History

2026-01-08 20:15:57 +09:00
#nullable enable
using System;
using UnityEngine.UIElements;
2026-01-20 20:18:47 +09:00
namespace UVC.UIToolkit
2026-01-08 20:15:57 +09:00
{
/// <summary>
/// 열거형 속성 아이템
2026-02-02 19:33:27 +09:00
/// UTKEnumDropDown을 사용한 열거형 선택
2026-01-08 20:15:57 +09:00
/// </summary>
public class UTKEnumPropertyItem : UTKPropertyItemBase<Enum>
{
#region Fields
2026-02-02 19:33:27 +09:00
private UTKEnumDropDown? _enumDropdown;
2026-01-08 20:15:57 +09:00
private Type _enumType;
#endregion
#region Properties
public override UTKPropertyType PropertyType => UTKPropertyType.Enum;
/// <summary>열거형 타입</summary>
public Type EnumType => _enumType;
#endregion
#region Constructor
2026-02-03 20:43:36 +09:00
public UTKEnumPropertyItem(string id, string name, Enum initialValue, bool isReadOnly = false)
2026-01-08 20:15:57 +09:00
: base(id, name, initialValue ?? throw new ArgumentNullException(nameof(initialValue)))
{
_enumType = initialValue.GetType();
2026-02-03 20:43:36 +09:00
base._isReadOnly = isReadOnly;
2026-01-08 20:15:57 +09:00
}
#endregion
#region Override Methods
public override VisualElement CreateUI()
2026-02-02 19:33:27 +09:00
{
var container = CreateUIFromUxml("UTKEnumPropertyItem");
if (container == null)
{
return CreateUIFallback();
}
_enumDropdown = container.Q<UTKEnumDropDown>("enum-dropdown");
if (_enumDropdown != null)
{
_enumDropdown.Init(Value);
2026-02-03 20:43:36 +09:00
_enumDropdown.IsEnabled = !IsReadOnly;
2026-02-02 19:33:27 +09:00
}
return container;
}
private VisualElement CreateUIFallback()
2026-01-08 20:15:57 +09:00
{
var container = CreateContainer();
var label = CreateNameLabel();
container.Add(label);
var valueContainer = new VisualElement();
valueContainer.AddToClassList("utk-property-item__value");
2026-02-02 19:33:27 +09:00
_enumDropdown = new UTKEnumDropDown();
_enumDropdown.name = "enum-dropdown";
_enumDropdown.Init(Value);
2026-02-03 20:43:36 +09:00
_enumDropdown.IsEnabled = !IsReadOnly;
2026-02-02 19:33:27 +09:00
valueContainer.Add(_enumDropdown);
2026-01-08 20:15:57 +09:00
container.Add(valueContainer);
return container;
}
public override void BindUI(VisualElement element)
{
base.BindUI(element);
2026-02-02 19:33:27 +09:00
_enumDropdown = element.Q<UTKEnumDropDown>("enum-dropdown");
2026-01-08 20:15:57 +09:00
2026-02-02 19:33:27 +09:00
if (_enumDropdown != null)
2026-01-08 20:15:57 +09:00
{
2026-02-02 19:33:27 +09:00
_enumDropdown.Value = Value;
_enumDropdown.IsEnabled = !IsReadOnly;
_enumDropdown.OnValueChanged += OnEnumDropdownChanged;
2026-01-08 20:15:57 +09:00
}
}
public override void UnbindUI(VisualElement element)
{
2026-02-02 19:33:27 +09:00
if (_enumDropdown != null)
2026-01-08 20:15:57 +09:00
{
2026-02-02 19:33:27 +09:00
_enumDropdown.OnValueChanged -= OnEnumDropdownChanged;
_enumDropdown = null;
2026-01-08 20:15:57 +09:00
}
base.UnbindUI(element);
}
public override void RefreshUI()
{
2026-02-02 19:33:27 +09:00
if (_enumDropdown != null && _enumDropdown.Value != Value)
2026-01-08 20:15:57 +09:00
{
2026-02-02 19:33:27 +09:00
_enumDropdown.Value = Value;
2026-01-08 20:15:57 +09:00
}
}
protected override void UpdateReadOnlyState()
{
base.UpdateReadOnlyState();
2026-02-02 19:33:27 +09:00
if (_enumDropdown != null)
2026-01-08 20:15:57 +09:00
{
2026-02-02 19:33:27 +09:00
_enumDropdown.IsEnabled = !IsReadOnly;
2026-01-08 20:15:57 +09:00
}
}
#endregion
#region Private Methods
2026-02-02 19:33:27 +09:00
private void OnEnumDropdownChanged(Enum? newValue)
2026-01-08 20:15:57 +09:00
{
2026-02-02 19:33:27 +09:00
if (newValue != null)
2026-01-08 20:15:57 +09:00
{
2026-02-02 19:33:27 +09:00
Value = newValue;
2026-01-08 20:15:57 +09:00
}
}
#endregion
}
}