UIToolkit 기본 UI 개발 중
This commit is contained in:
169
Assets/Scripts/UVC/UIToolkit/Dropdown/UTKDropdown.cs
Normal file
169
Assets/Scripts/UVC/UIToolkit/Dropdown/UTKDropdown.cs
Normal file
@@ -0,0 +1,169 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UVC.UIToolkit
|
||||
{
|
||||
/// <summary>
|
||||
/// 드롭다운 메뉴 컴포넌트.
|
||||
/// Unity DropdownField를 래핑하여 커스텀 스타일을 적용합니다.
|
||||
/// </summary>
|
||||
[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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user