254 lines
6.9 KiB
C#
254 lines
6.9 KiB
C#
#nullable enable
|
|
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using UVC.UIToolkit.Common;
|
|
|
|
namespace UVC.UIToolkit
|
|
{
|
|
/// <summary>
|
|
/// 체크박스 컴포넌트.
|
|
/// 선택/해제 상태를 토글할 수 있습니다.
|
|
/// </summary>
|
|
/// <example>
|
|
/// <para><b>C# 코드에서 사용:</b></para>
|
|
/// <code>
|
|
/// // 기본 체크박스
|
|
/// var checkbox = new UTKCheckBox();
|
|
/// checkbox.Text = "약관에 동의합니다";
|
|
/// checkbox.OnValueChanged += (isChecked) => Debug.Log($"체크: {isChecked}");
|
|
///
|
|
/// // 상태 설정
|
|
/// checkbox.IsChecked = true;
|
|
/// checkbox.IsIndeterminate = true; // 부분 선택 상태
|
|
/// </code>
|
|
/// <para><b>UXML에서 사용:</b></para>
|
|
/// <code>
|
|
/// <ui:UXML xmlns:utk="UVC.UIToolkit">
|
|
/// <!-- 기본 체크박스 -->
|
|
/// <utk:UTKCheckBox Text="이메일 수신 동의" />
|
|
///
|
|
/// <!-- 기본값 체크됨 -->
|
|
/// <utk:UTKCheckBox Text="자동 로그인" IsChecked="true" />
|
|
///
|
|
/// <!-- 비활성화 -->
|
|
/// <utk:UTKCheckBox Text="필수 동의" IsEnabled="false" IsChecked="true" />
|
|
/// </ui:UXML>
|
|
/// </code>
|
|
/// </example>
|
|
[UxmlElement]
|
|
public partial class UTKCheckBox : VisualElement, IDisposable
|
|
{
|
|
#region Constants
|
|
private const string USS_PATH = "UIToolkit/Button/UTKCheckBox";
|
|
#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();
|
|
|
|
// UXML에서 로드될 때 속성이 설정된 후 UI 갱신
|
|
RegisterCallback<AttachToPanelEvent>(_ =>
|
|
{
|
|
if (_label != null) _label.text = _text;
|
|
UpdateState();
|
|
});
|
|
}
|
|
|
|
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 = UTKMaterialIcons.Check };
|
|
_checkIcon.AddToClassList("utk-checkbox__icon");
|
|
UTKMaterialIcons.ApplyIconStyle(_checkIcon, 14);
|
|
_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 ? UTKMaterialIcons.Remove : UTKMaterialIcons.Check;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region IDisposable
|
|
public void Dispose()
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
OnValueChanged = null;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|