220 lines
5.6 KiB
C#
220 lines
5.6 KiB
C#
|
|
#nullable enable
|
|||
|
|
using System;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UIElements;
|
|||
|
|
|
|||
|
|
namespace UVC.UIToolkit
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 체크박스 컴포넌트.
|
|||
|
|
/// 선택/해제 상태를 토글할 수 있습니다.
|
|||
|
|
/// </summary>
|
|||
|
|
[UxmlElement]
|
|||
|
|
public partial class UTKCheckBox : VisualElement, IDisposable
|
|||
|
|
{
|
|||
|
|
#region Constants
|
|||
|
|
private const string USS_PATH = "UIToolkit/Button/UTKCheckBox";
|
|||
|
|
private const string CHECK_ICON = "✓";
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Fields
|
|||
|
|
private bool _disposed;
|
|||
|
|
private VisualElement? _checkBox;
|
|||
|
|
private Label? _checkIcon;
|
|||
|
|
private Label? _label;
|
|||
|
|
|
|||
|
|
private string _text = "";
|
|||
|
|
private bool _isChecked;
|
|||
|
|
private bool _isIndeterminate;
|
|||
|
|
private bool _isEnabled = true;
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Events
|
|||
|
|
/// <summary>체크 상태 변경 이벤트</summary>
|
|||
|
|
public event Action<bool>? OnValueChanged;
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Properties
|
|||
|
|
/// <summary>라벨 텍스트</summary>
|
|||
|
|
[UxmlAttribute]
|
|||
|
|
public string Text
|
|||
|
|
{
|
|||
|
|
get => _text;
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_text = value;
|
|||
|
|
if (_label != null) _label.text = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>체크 상태</summary>
|
|||
|
|
[UxmlAttribute]
|
|||
|
|
public bool IsChecked
|
|||
|
|
{
|
|||
|
|
get => _isChecked;
|
|||
|
|
set => SetChecked(value, true);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>불확정 상태 (일부 선택됨)</summary>
|
|||
|
|
[UxmlAttribute]
|
|||
|
|
public bool IsIndeterminate
|
|||
|
|
{
|
|||
|
|
get => _isIndeterminate;
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_isIndeterminate = value;
|
|||
|
|
UpdateState();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>활성화 상태</summary>
|
|||
|
|
[UxmlAttribute]
|
|||
|
|
public bool IsEnabled
|
|||
|
|
{
|
|||
|
|
get => _isEnabled;
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_isEnabled = value;
|
|||
|
|
SetEnabled(value);
|
|||
|
|
EnableInClassList("utk-checkbox--disabled", !value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Constructor
|
|||
|
|
public UTKCheckBox()
|
|||
|
|
{
|
|||
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|||
|
|
|
|||
|
|
var uss = Resources.Load<StyleSheet>(USS_PATH);
|
|||
|
|
if (uss != null)
|
|||
|
|
{
|
|||
|
|
styleSheets.Add(uss);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CreateUI();
|
|||
|
|
SetupEvents();
|
|||
|
|
SubscribeToThemeChanges();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public UTKCheckBox(string text, bool isChecked = false) : this()
|
|||
|
|
{
|
|||
|
|
Text = text;
|
|||
|
|
SetChecked(isChecked, false);
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region UI Creation
|
|||
|
|
private void CreateUI()
|
|||
|
|
{
|
|||
|
|
AddToClassList("utk-checkbox");
|
|||
|
|
focusable = true;
|
|||
|
|
|
|||
|
|
_checkBox = new VisualElement { name = "checkbox" };
|
|||
|
|
_checkBox.AddToClassList("utk-checkbox__box");
|
|||
|
|
|
|||
|
|
_checkIcon = new Label { name = "check-icon", text = CHECK_ICON };
|
|||
|
|
_checkIcon.AddToClassList("utk-checkbox__icon");
|
|||
|
|
_checkBox.Add(_checkIcon);
|
|||
|
|
|
|||
|
|
Add(_checkBox);
|
|||
|
|
|
|||
|
|
_label = new Label { name = "label" };
|
|||
|
|
_label.AddToClassList("utk-checkbox__label");
|
|||
|
|
Add(_label);
|
|||
|
|
|
|||
|
|
UpdateState();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void SetupEvents()
|
|||
|
|
{
|
|||
|
|
RegisterCallback<ClickEvent>(OnClick);
|
|||
|
|
RegisterCallback<KeyDownEvent>(OnKeyDown);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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 OnClick(ClickEvent evt)
|
|||
|
|
{
|
|||
|
|
if (!_isEnabled) return;
|
|||
|
|
Toggle();
|
|||
|
|
evt.StopPropagation();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnKeyDown(KeyDownEvent evt)
|
|||
|
|
{
|
|||
|
|
if (!_isEnabled) return;
|
|||
|
|
if (evt.keyCode == KeyCode.Return || evt.keyCode == KeyCode.Space)
|
|||
|
|
{
|
|||
|
|
Toggle();
|
|||
|
|
evt.StopPropagation();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Methods
|
|||
|
|
/// <summary>
|
|||
|
|
/// 체크 상태 토글
|
|||
|
|
/// </summary>
|
|||
|
|
public void Toggle()
|
|||
|
|
{
|
|||
|
|
_isIndeterminate = false;
|
|||
|
|
SetChecked(!_isChecked, true);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 체크 상태 설정
|
|||
|
|
/// </summary>
|
|||
|
|
public void SetChecked(bool value, bool notify)
|
|||
|
|
{
|
|||
|
|
if (_isChecked == value && !_isIndeterminate) return;
|
|||
|
|
|
|||
|
|
_isChecked = value;
|
|||
|
|
_isIndeterminate = false;
|
|||
|
|
UpdateState();
|
|||
|
|
|
|||
|
|
if (notify)
|
|||
|
|
{
|
|||
|
|
OnValueChanged?.Invoke(value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void UpdateState()
|
|||
|
|
{
|
|||
|
|
EnableInClassList("utk-checkbox--checked", _isChecked && !_isIndeterminate);
|
|||
|
|
EnableInClassList("utk-checkbox--indeterminate", _isIndeterminate);
|
|||
|
|
|
|||
|
|
if (_checkIcon != null)
|
|||
|
|
{
|
|||
|
|
_checkIcon.text = _isIndeterminate ? "−" : CHECK_ICON;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region IDisposable
|
|||
|
|
public void Dispose()
|
|||
|
|
{
|
|||
|
|
if (_disposed) return;
|
|||
|
|
_disposed = true;
|
|||
|
|
|
|||
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|||
|
|
OnValueChanged = null;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|