#nullable enable using System; using System.Collections.Generic; using System.Linq; using UnityEngine.UIElements; namespace UVC.UIToolkit { /// /// 라디오 그룹 속성 아이템 /// RadioButton 그룹으로 단일 선택 /// public class UTKRadioPropertyItem : UTKPropertyItemBase { #region Fields private RadioButtonGroup? _radioGroup; private List _choices; #endregion #region Properties public override UTKPropertyType PropertyType => UTKPropertyType.RadioGroup; /// 선택 가능한 항목 목록 public List Choices { get => _choices; set { _choices = value ?? new List(); if (_radioGroup != null) { _radioGroup.choices = _choices; } } } /// 현재 선택된 항목의 텍스트 public string? SelectedText { get { if (Value >= 0 && Value < _choices.Count) { return _choices[Value]; } return null; } } #endregion #region Constructor public UTKRadioPropertyItem(string id, string name, List choices, int selectedIndex = 0) : base(id, name, Math.Max(0, Math.Min(selectedIndex, (choices?.Count ?? 1) - 1))) { _choices = choices ?? new List(); } public UTKRadioPropertyItem(string id, string name, IEnumerable choices, int selectedIndex = 0) : this(id, name, choices?.ToList() ?? new List(), selectedIndex) { } #endregion #region Override Methods public override VisualElement CreateUI() { var container = CreateContainer(); var label = CreateNameLabel(); container.Add(label); var valueContainer = new VisualElement(); valueContainer.AddToClassList("utk-property-item__value"); _radioGroup = new RadioButtonGroup(); _radioGroup.name = "radio-group"; _radioGroup.choices = _choices; _radioGroup.value = Value; valueContainer.Add(_radioGroup); container.Add(valueContainer); return container; } public override void BindUI(VisualElement element) { base.BindUI(element); _radioGroup = element.Q("radio-group"); if (_radioGroup != null) { _radioGroup.choices = _choices; _radioGroup.value = Value; _radioGroup.SetEnabled(!IsReadOnly); _radioGroup.RegisterValueChangedCallback(OnRadioChanged); } } public override void UnbindUI(VisualElement element) { if (_radioGroup != null) { _radioGroup.UnregisterValueChangedCallback(OnRadioChanged); _radioGroup = null; } base.UnbindUI(element); } public override void RefreshUI() { if (_radioGroup != null && _radioGroup.value != Value) { _radioGroup.SetValueWithoutNotify(Value); } } protected override void UpdateReadOnlyState() { base.UpdateReadOnlyState(); if (_radioGroup != null) { _radioGroup.SetEnabled(!IsReadOnly); } } #endregion #region Public Methods /// 텍스트로 선택 public void SelectByText(string text) { int index = _choices.IndexOf(text); if (index >= 0) { Value = index; } } #endregion #region Private Methods private void OnRadioChanged(ChangeEvent evt) { Value = evt.newValue; } #endregion } }