Files
XRLib/Assets/Scripts/UVC/UIToolkit/Dropdown/UTKDropdown.cs
2026-01-13 20:39:45 +09:00

197 lines
5.3 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace UVC.UIToolkit
{
/// <summary>
/// 드롭다운 메뉴 컴포넌트.
/// Unity DropdownField를 래핑하여 커스텀 스타일을 적용합니다.
/// </summary>
/// <example>
/// <para><b>C# 코드에서 사용:</b></para>
/// <code>
/// // 기본 드롭다운
/// var dropdown = new UTKDropdown();
/// dropdown.Label = "국가 선택";
/// dropdown.SetChoices(new List<string> { "한국", "미국", "일본" });
/// dropdown.OnSelectionChanged += (index, value) => Debug.Log($"선택: {value}");
///
/// // 기본값 설정
/// dropdown.value = "한국";
/// dropdown.index = 0;
/// </code>
/// <para><b>UXML에서 사용:</b></para>
/// <code>
/// <ui:UXML xmlns:utk="UVC.UIToolkit">
/// <!-- 기본 드롭다운 -->
/// <utk:UTKDropdown label="정렬" choices="이름,날짜,크기" />
///
/// <!-- 기본값 지정 -->
/// <utk:UTKDropdown label="언어" choices="한국어,English,日本語" index="0" />
///
/// <!-- 비활성화 -->
/// <utk:UTKDropdown label="선택" IsEnabled="false" />
/// </ui:UXML>
/// </code>
/// </example>
[UxmlElement]
public partial class UTKDropdown : DropdownField, IDisposable
{
#region Constants
private const string USS_PATH = "UIToolkit/Dropdown/UTKDropdown";
#endregion
#region Fields
private bool _disposed;
private bool _isEnabled = true;
#endregion
#region Events
/// <summary>선택 변경 이벤트</summary>
public event Action<int, string>? OnSelectionChanged;
#endregion
#region Properties
/// <summary>선택된 인덱스</summary>
public int SelectedIndex
{
get => index;
set => index = value;
}
/// <summary>선택된 값</summary>
public string? SelectedValue => value;
/// <summary>활성화 상태</summary>
[UxmlAttribute]
public bool IsEnabled
{
get => _isEnabled;
set
{
_isEnabled = value;
SetEnabled(value);
EnableInClassList("utk-dropdown--disabled", !value);
}
}
#endregion
#region Constructor
public UTKDropdown() : base()
{
UTKThemeManager.Instance.ApplyThemeToElement(this);
var uss = Resources.Load<StyleSheet>(USS_PATH);
if (uss != null)
{
styleSheets.Add(uss);
}
SetupStyles();
SetupEvents();
SubscribeToThemeChanges();
}
public UTKDropdown(string label, List<string>? options = null) : this()
{
this.label = label;
if (options != null)
{
choices = options;
}
}
#endregion
#region Setup
private void SetupStyles()
{
AddToClassList("utk-dropdown");
}
private void SetupEvents()
{
this.RegisterValueChangedCallback(OnDropdownValueChanged);
}
private void SubscribeToThemeChanges()
{
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
RegisterCallback<DetachFromPanelEvent>(_ =>
{
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
});
}
private void OnThemeChanged(UTKTheme theme)
{
UTKThemeManager.Instance.ApplyThemeToElement(this);
}
#endregion
#region Event Handlers
private void OnDropdownValueChanged(ChangeEvent<string> evt)
{
OnSelectionChanged?.Invoke(index, evt.newValue);
}
#endregion
#region Methods
/// <summary>
/// 옵션 목록 설정
/// </summary>
public void SetOptions(List<string> options)
{
choices = options ?? new List<string>();
}
/// <summary>
/// 옵션 추가
/// </summary>
public void AddOption(string option)
{
var list = new List<string>(choices) { option };
choices = list;
}
/// <summary>
/// 값으로 선택
/// </summary>
public void SetSelectedValue(string? selectedValue, bool notify = true)
{
if (selectedValue == null)
{
index = -1;
return;
}
var idx = choices.IndexOf(selectedValue);
if (idx >= 0)
{
if (notify)
{
index = idx;
}
else
{
SetValueWithoutNotify(selectedValue);
}
}
}
#endregion
#region IDisposable
public void Dispose()
{
if (_disposed) return;
_disposed = true;
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
OnSelectionChanged = null;
}
#endregion
}
}