UTKPropertyItem 개선
This commit is contained in:
@@ -1,42 +1,41 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UVC.UIToolkit
|
||||
{
|
||||
/// <summary>
|
||||
/// 드롭다운 목록 속성 아이템
|
||||
/// 문자열 리스트에서 선택
|
||||
/// 드롭다운 목록 속성 데이터 클래스입니다.
|
||||
/// UI는 UTKDropdownPropertyItemView에서 담당합니다.
|
||||
/// </summary>
|
||||
public class UTKDropdownPropertyItem : UTKPropertyItemBase<string>
|
||||
{
|
||||
#region Fields
|
||||
private UTKDropdown? _dropdown;
|
||||
private List<string> _choices;
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
/// <summary>속성 타입</summary>
|
||||
public override UTKPropertyType PropertyType => UTKPropertyType.DropdownList;
|
||||
|
||||
/// <summary>선택 가능한 항목 목록</summary>
|
||||
public List<string> Choices
|
||||
{
|
||||
get => _choices;
|
||||
set
|
||||
{
|
||||
_choices = value ?? new List<string>();
|
||||
if (_dropdown != null)
|
||||
{
|
||||
_dropdown.SetOptions(_choices);
|
||||
}
|
||||
}
|
||||
set => _choices = value ?? new List<string>();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
public UTKDropdownPropertyItem(string id, string name, List<string> choices, string initialValue = "", bool isReadOnly = false)
|
||||
/// <summary>
|
||||
/// 드롭다운 속성을 생성합니다.
|
||||
/// </summary>
|
||||
/// <param name="id">고유 ID</param>
|
||||
/// <param name="name">표시 이름</param>
|
||||
/// <param name="choices">선택 항목 목록</param>
|
||||
/// <param name="initialValue">초기 선택 값</param>
|
||||
/// <param name="isReadOnly">읽기 전용 여부</param>
|
||||
public UTKDropdownPropertyItem(string id, string name, List<string> choices, string initialValue = "", bool isReadOnly = false)
|
||||
: base(id, name, initialValue)
|
||||
{
|
||||
_choices = choices ?? new List<string>();
|
||||
@@ -46,10 +45,18 @@ namespace UVC.UIToolkit
|
||||
{
|
||||
Value = _choices[0];
|
||||
}
|
||||
base._isReadOnly = isReadOnly;
|
||||
IsReadOnly = isReadOnly;
|
||||
}
|
||||
|
||||
public UTKDropdownPropertyItem(string id, string name, IEnumerable<string> choices, int selectedIndex = 0, bool isReadOnly = false)
|
||||
/// <summary>
|
||||
/// 드롭다운 속성을 생성합니다 (인덱스 기반).
|
||||
/// </summary>
|
||||
/// <param name="id">고유 ID</param>
|
||||
/// <param name="name">표시 이름</param>
|
||||
/// <param name="choices">선택 항목 목록</param>
|
||||
/// <param name="selectedIndex">초기 선택 인덱스</param>
|
||||
/// <param name="isReadOnly">읽기 전용 여부</param>
|
||||
public UTKDropdownPropertyItem(string id, string name, IEnumerable<string> choices, int selectedIndex = 0, bool isReadOnly = false)
|
||||
: base(id, name, string.Empty)
|
||||
{
|
||||
_choices = choices?.ToList() ?? new List<string>();
|
||||
@@ -62,97 +69,7 @@ namespace UVC.UIToolkit
|
||||
{
|
||||
Value = _choices[0];
|
||||
}
|
||||
base._isReadOnly = isReadOnly;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Override Methods
|
||||
public override VisualElement CreateUI()
|
||||
{
|
||||
var container = CreateUIFromUxml("UTKDropdownPropertyItem");
|
||||
if (container == null)
|
||||
{
|
||||
return CreateUIFallback();
|
||||
}
|
||||
|
||||
_dropdown = container.Q<UTKDropdown>("dropdown-field");
|
||||
if (_dropdown != null)
|
||||
{
|
||||
_dropdown.SetOptions(_choices);
|
||||
int selectedIndex = _choices.IndexOf(Value);
|
||||
_dropdown.SelectedIndex = Math.Max(0, selectedIndex);
|
||||
_dropdown.IsEnabled = !IsReadOnly;
|
||||
}
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
private VisualElement CreateUIFallback()
|
||||
{
|
||||
var container = CreateContainer();
|
||||
|
||||
var label = CreateNameLabel();
|
||||
container.Add(label);
|
||||
|
||||
var valueContainer = new VisualElement();
|
||||
valueContainer.AddToClassList("utk-property-item__value");
|
||||
|
||||
_dropdown = new UTKDropdown();
|
||||
_dropdown.name = "dropdown-field";
|
||||
_dropdown.SetOptions(_choices);
|
||||
int selectedIndex = _choices.IndexOf(Value);
|
||||
_dropdown.SelectedIndex = Math.Max(0, selectedIndex);
|
||||
_dropdown.IsEnabled = !IsReadOnly;
|
||||
valueContainer.Add(_dropdown);
|
||||
|
||||
container.Add(valueContainer);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
public override void BindUI(VisualElement element)
|
||||
{
|
||||
base.BindUI(element);
|
||||
|
||||
_dropdown = element.Q<UTKDropdown>("dropdown-field");
|
||||
|
||||
if (_dropdown != null)
|
||||
{
|
||||
_dropdown.SetOptions(_choices);
|
||||
int selectedIndex = _choices.IndexOf(Value);
|
||||
_dropdown.SelectedIndex = Math.Max(0, selectedIndex);
|
||||
_dropdown.IsEnabled = !IsReadOnly;
|
||||
_dropdown.OnSelectionChanged += OnDropdownChanged;
|
||||
}
|
||||
}
|
||||
|
||||
public override void UnbindUI(VisualElement element)
|
||||
{
|
||||
if (_dropdown != null)
|
||||
{
|
||||
_dropdown.OnSelectionChanged -= OnDropdownChanged;
|
||||
_dropdown = null;
|
||||
}
|
||||
|
||||
base.UnbindUI(element);
|
||||
}
|
||||
|
||||
public override void RefreshUI()
|
||||
{
|
||||
if (_dropdown != null && _dropdown.SelectedValue != Value)
|
||||
{
|
||||
_dropdown.SetSelectedValue(Value, notify: false);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void UpdateReadOnlyState()
|
||||
{
|
||||
base.UpdateReadOnlyState();
|
||||
|
||||
if (_dropdown != null)
|
||||
{
|
||||
_dropdown.IsEnabled = !IsReadOnly;
|
||||
}
|
||||
IsReadOnly = isReadOnly;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -163,10 +80,6 @@ namespace UVC.UIToolkit
|
||||
if (!_choices.Contains(choice))
|
||||
{
|
||||
_choices.Add(choice);
|
||||
if (_dropdown != null)
|
||||
{
|
||||
_dropdown.AddOption(choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,25 +87,12 @@ namespace UVC.UIToolkit
|
||||
public bool RemoveChoice(string choice)
|
||||
{
|
||||
bool removed = _choices.Remove(choice);
|
||||
if (removed && _dropdown != null)
|
||||
if (removed && Value == choice && _choices.Count > 0)
|
||||
{
|
||||
_dropdown.SetOptions(_choices);
|
||||
|
||||
// 현재 선택 값이 제거되면 첫 번째 항목 선택
|
||||
if (Value == choice && _choices.Count > 0)
|
||||
{
|
||||
Value = _choices[0];
|
||||
}
|
||||
Value = _choices[0];
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
private void OnDropdownChanged(int index, string newValue)
|
||||
{
|
||||
Value = newValue;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user