UTKProperyWIndow 수정 중
This commit is contained in:
@@ -8,12 +8,13 @@ namespace UVC.UIToolkit
|
||||
{
|
||||
/// <summary>
|
||||
/// 라디오 그룹 속성 아이템
|
||||
/// RadioButton 그룹으로 단일 선택
|
||||
/// UTKRadioButton 그룹으로 단일 선택
|
||||
/// </summary>
|
||||
public class UTKRadioPropertyItem : UTKPropertyItemBase<int>
|
||||
{
|
||||
#region Fields
|
||||
private RadioButtonGroup? _radioGroup;
|
||||
private VisualElement? _radioContainer;
|
||||
private List<UTKRadioButton> _radioButtons = new();
|
||||
private List<string> _choices;
|
||||
#endregion
|
||||
|
||||
@@ -27,10 +28,7 @@ namespace UVC.UIToolkit
|
||||
set
|
||||
{
|
||||
_choices = value ?? new List<string>();
|
||||
if (_radioGroup != null)
|
||||
{
|
||||
_radioGroup.choices = _choices;
|
||||
}
|
||||
RebuildRadioButtons();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +61,23 @@ namespace UVC.UIToolkit
|
||||
|
||||
#region Override Methods
|
||||
public override VisualElement CreateUI()
|
||||
{
|
||||
var container = CreateUIFromUxml("UTKRadioPropertyItem");
|
||||
if (container == null)
|
||||
{
|
||||
return CreateUIFallback();
|
||||
}
|
||||
|
||||
_radioContainer = container.Q<VisualElement>("radio-container");
|
||||
if (_radioContainer != null)
|
||||
{
|
||||
CreateRadioButtons();
|
||||
}
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
private VisualElement CreateUIFallback()
|
||||
{
|
||||
var container = CreateContainer();
|
||||
|
||||
@@ -72,11 +87,12 @@ namespace UVC.UIToolkit
|
||||
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);
|
||||
_radioContainer = new VisualElement();
|
||||
_radioContainer.name = "radio-container";
|
||||
_radioContainer.AddToClassList("utk-property-item__radio-container");
|
||||
valueContainer.Add(_radioContainer);
|
||||
|
||||
CreateRadioButtons();
|
||||
|
||||
container.Add(valueContainer);
|
||||
|
||||
@@ -87,43 +103,57 @@ namespace UVC.UIToolkit
|
||||
{
|
||||
base.BindUI(element);
|
||||
|
||||
_radioGroup = element.Q<RadioButtonGroup>("radio-group");
|
||||
_radioContainer = element.Q<VisualElement>("radio-container");
|
||||
|
||||
if (_radioGroup != null)
|
||||
if (_radioContainer != null)
|
||||
{
|
||||
_radioGroup.choices = _choices;
|
||||
_radioGroup.value = Value;
|
||||
_radioGroup.SetEnabled(!IsReadOnly);
|
||||
_radioGroup.RegisterValueChangedCallback(OnRadioChanged);
|
||||
// 기존 라디오 버튼 찾기 또는 새로 생성
|
||||
_radioButtons.Clear();
|
||||
var existingButtons = _radioContainer.Query<UTKRadioButton>().ToList();
|
||||
|
||||
if (existingButtons.Count == _choices.Count)
|
||||
{
|
||||
// 기존 버튼 재사용
|
||||
for (int i = 0; i < existingButtons.Count; i++)
|
||||
{
|
||||
var radio = existingButtons[i];
|
||||
_radioButtons.Add(radio);
|
||||
radio.IsEnabled = !IsReadOnly;
|
||||
|
||||
int index = i;
|
||||
radio.OnValueChanged += (isChecked) => OnRadioChanged(index, isChecked);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 새로 생성
|
||||
CreateRadioButtons();
|
||||
}
|
||||
|
||||
UpdateSelection();
|
||||
}
|
||||
}
|
||||
|
||||
public override void UnbindUI(VisualElement element)
|
||||
{
|
||||
if (_radioGroup != null)
|
||||
{
|
||||
_radioGroup.UnregisterValueChangedCallback(OnRadioChanged);
|
||||
_radioGroup = null;
|
||||
}
|
||||
_radioButtons.Clear();
|
||||
_radioContainer = null;
|
||||
|
||||
base.UnbindUI(element);
|
||||
}
|
||||
|
||||
public override void RefreshUI()
|
||||
{
|
||||
if (_radioGroup != null && _radioGroup.value != Value)
|
||||
{
|
||||
_radioGroup.SetValueWithoutNotify(Value);
|
||||
}
|
||||
UpdateSelection();
|
||||
}
|
||||
|
||||
protected override void UpdateReadOnlyState()
|
||||
{
|
||||
base.UpdateReadOnlyState();
|
||||
|
||||
if (_radioGroup != null)
|
||||
foreach (var radio in _radioButtons)
|
||||
{
|
||||
_radioGroup.SetEnabled(!IsReadOnly);
|
||||
radio.IsEnabled = !IsReadOnly;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
@@ -141,9 +171,60 @@ namespace UVC.UIToolkit
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
private void OnRadioChanged(ChangeEvent<int> evt)
|
||||
private void CreateRadioButtons()
|
||||
{
|
||||
Value = evt.newValue;
|
||||
if (_radioContainer == null) return;
|
||||
|
||||
_radioContainer.Clear();
|
||||
_radioButtons.Clear();
|
||||
|
||||
for (int i = 0; i < _choices.Count; i++)
|
||||
{
|
||||
var radio = new UTKRadioButton(_choices[i]);
|
||||
radio.name = $"radio-{i}";
|
||||
radio.AddToClassList("utk-property-item__radio");
|
||||
|
||||
int index = i;
|
||||
radio.OnValueChanged += (isChecked) => OnRadioChanged(index, isChecked);
|
||||
|
||||
_radioButtons.Add(radio);
|
||||
_radioContainer.Add(radio);
|
||||
}
|
||||
|
||||
UpdateSelection();
|
||||
}
|
||||
|
||||
private void RebuildRadioButtons()
|
||||
{
|
||||
if (_radioContainer != null)
|
||||
{
|
||||
CreateRadioButtons();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateSelection()
|
||||
{
|
||||
for (int i = 0; i < _radioButtons.Count; i++)
|
||||
{
|
||||
_radioButtons[i].SetChecked(i == Value, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRadioChanged(int index, bool isChecked)
|
||||
{
|
||||
if (isChecked && index != Value)
|
||||
{
|
||||
// 다른 라디오 버튼 해제
|
||||
for (int i = 0; i < _radioButtons.Count; i++)
|
||||
{
|
||||
if (i != index)
|
||||
{
|
||||
_radioButtons[i].SetChecked(false, false);
|
||||
}
|
||||
}
|
||||
|
||||
Value = index;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user