UIToolkit 기본 UI 개발 중
This commit is contained in:
145
Assets/Scripts/UVC/UIToolkit/Button/UTKRadioButton.cs
Normal file
145
Assets/Scripts/UVC/UIToolkit/Button/UTKRadioButton.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UVC.UIToolkit
|
||||
{
|
||||
/// <summary>
|
||||
/// 라디오 버튼 컴포넌트.
|
||||
/// Unity RadioButton을 래핑하여 커스텀 스타일을 적용합니다.
|
||||
/// </summary>
|
||||
[UxmlElement]
|
||||
public partial class UTKRadioButton : RadioButton, IDisposable
|
||||
{
|
||||
#region Constants
|
||||
private const string USS_PATH = "UIToolkit/Button/UTKRadioButton";
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
private bool _disposed;
|
||||
private bool _isEnabled = true;
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
/// <summary>체크 상태 변경 이벤트</summary>
|
||||
public event Action<bool>? OnValueChanged;
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
/// <summary>라벨 텍스트</summary>
|
||||
public string Text
|
||||
{
|
||||
get => text;
|
||||
set => text = value;
|
||||
}
|
||||
|
||||
/// <summary>체크 상태</summary>
|
||||
public bool IsChecked
|
||||
{
|
||||
get => value;
|
||||
set => SetChecked(value, true);
|
||||
}
|
||||
|
||||
/// <summary>활성화 상태</summary>
|
||||
[UxmlAttribute]
|
||||
public bool IsEnabled
|
||||
{
|
||||
get => _isEnabled;
|
||||
set
|
||||
{
|
||||
_isEnabled = value;
|
||||
SetEnabled(value);
|
||||
EnableInClassList("utk-radio--disabled", !value);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
public UTKRadioButton() : base()
|
||||
{
|
||||
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
||||
|
||||
var uss = Resources.Load<StyleSheet>(USS_PATH);
|
||||
if (uss != null)
|
||||
{
|
||||
styleSheets.Add(uss);
|
||||
}
|
||||
|
||||
SetupStyles();
|
||||
SetupEvents();
|
||||
SubscribeToThemeChanges();
|
||||
}
|
||||
|
||||
public UTKRadioButton(string text) : this()
|
||||
{
|
||||
this.text = text;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Setup
|
||||
private void SetupStyles()
|
||||
{
|
||||
AddToClassList("utk-radio");
|
||||
}
|
||||
|
||||
private void SetupEvents()
|
||||
{
|
||||
this.RegisterValueChangedCallback(OnRadioValueChanged);
|
||||
}
|
||||
|
||||
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 OnRadioValueChanged(ChangeEvent<bool> evt)
|
||||
{
|
||||
EnableInClassList("utk-radio--checked", evt.newValue);
|
||||
OnValueChanged?.Invoke(evt.newValue);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
/// <summary>
|
||||
/// 체크 상태 설정
|
||||
/// </summary>
|
||||
public void SetChecked(bool checked_, bool notify)
|
||||
{
|
||||
if (value == checked_) return;
|
||||
|
||||
if (notify)
|
||||
{
|
||||
value = checked_;
|
||||
}
|
||||
else
|
||||
{
|
||||
SetValueWithoutNotify(checked_);
|
||||
EnableInClassList("utk-radio--checked", checked_);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region IDisposable
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
|
||||
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
||||
OnValueChanged = null;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user