174 lines
5.0 KiB
C#
174 lines
5.0 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace UVC.UIToolkit.Property
|
|
{
|
|
/// <summary>
|
|
/// 드롭다운 목록 속성 아이템
|
|
/// 문자열 리스트에서 선택
|
|
/// </summary>
|
|
public class UTKDropdownPropertyItem : UTKPropertyItemBase<string>
|
|
{
|
|
#region Fields
|
|
private DropdownField? _dropdown;
|
|
private List<string> _choices;
|
|
#endregion
|
|
|
|
#region Properties
|
|
public override UTKPropertyType PropertyType => UTKPropertyType.DropdownList;
|
|
|
|
/// <summary>선택 가능한 항목 목록</summary>
|
|
public List<string> Choices
|
|
{
|
|
get => _choices;
|
|
set
|
|
{
|
|
_choices = value ?? new List<string>();
|
|
if (_dropdown != null)
|
|
{
|
|
_dropdown.choices = _choices;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Constructor
|
|
public UTKDropdownPropertyItem(string id, string name, List<string> choices, string initialValue = "")
|
|
: base(id, name, initialValue)
|
|
{
|
|
_choices = choices ?? new List<string>();
|
|
|
|
// initialValue가 choices에 없으면 첫 번째 항목 선택
|
|
if (!string.IsNullOrEmpty(initialValue) && !_choices.Contains(initialValue) && _choices.Count > 0)
|
|
{
|
|
Value = _choices[0];
|
|
}
|
|
}
|
|
|
|
public UTKDropdownPropertyItem(string id, string name, IEnumerable<string> choices, int selectedIndex = 0)
|
|
: base(id, name, string.Empty)
|
|
{
|
|
_choices = choices?.ToList() ?? new List<string>();
|
|
|
|
if (_choices.Count > 0 && selectedIndex >= 0 && selectedIndex < _choices.Count)
|
|
{
|
|
Value = _choices[selectedIndex];
|
|
}
|
|
else if (_choices.Count > 0)
|
|
{
|
|
Value = _choices[0];
|
|
}
|
|
}
|
|
#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");
|
|
|
|
int selectedIndex = _choices.IndexOf(Value);
|
|
_dropdown = new DropdownField(_choices, Math.Max(0, selectedIndex));
|
|
_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;
|
|
int selectedIndex = _choices.IndexOf(Value);
|
|
_dropdown.index = Math.Max(0, selectedIndex);
|
|
_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 && _dropdown.value != Value)
|
|
{
|
|
_dropdown.SetValueWithoutNotify(Value);
|
|
}
|
|
}
|
|
|
|
protected override void UpdateReadOnlyState()
|
|
{
|
|
base.UpdateReadOnlyState();
|
|
|
|
if (_dropdown != null)
|
|
{
|
|
_dropdown.SetEnabled(!IsReadOnly);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Public Methods
|
|
/// <summary>선택 항목 추가</summary>
|
|
public void AddChoice(string choice)
|
|
{
|
|
if (!_choices.Contains(choice))
|
|
{
|
|
_choices.Add(choice);
|
|
if (_dropdown != null)
|
|
{
|
|
_dropdown.choices = _choices;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>선택 항목 제거</summary>
|
|
public bool RemoveChoice(string choice)
|
|
{
|
|
bool removed = _choices.Remove(choice);
|
|
if (removed && _dropdown != null)
|
|
{
|
|
_dropdown.choices = _choices;
|
|
|
|
// 현재 선택 값이 제거되면 첫 번째 항목 선택
|
|
if (Value == choice && _choices.Count > 0)
|
|
{
|
|
Value = _choices[0];
|
|
}
|
|
}
|
|
return removed;
|
|
}
|
|
#endregion
|
|
|
|
#region Private Methods
|
|
private void OnDropdownChanged(ChangeEvent<string> evt)
|
|
{
|
|
Value = evt.newValue;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|