UIToolkit 기본 UI 개발 중
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace UVC.UIToolkit.Property
|
||||
{
|
||||
/// <summary>
|
||||
/// 라디오 그룹 속성 아이템
|
||||
/// RadioButton 그룹으로 단일 선택
|
||||
/// </summary>
|
||||
public class UTKRadioPropertyItem : UTKPropertyItemBase<int>
|
||||
{
|
||||
#region Fields
|
||||
private RadioButtonGroup? _radioGroup;
|
||||
private List<string> _choices;
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
public override UTKPropertyType PropertyType => UTKPropertyType.RadioGroup;
|
||||
|
||||
/// <summary>선택 가능한 항목 목록</summary>
|
||||
public List<string> Choices
|
||||
{
|
||||
get => _choices;
|
||||
set
|
||||
{
|
||||
_choices = value ?? new List<string>();
|
||||
if (_radioGroup != null)
|
||||
{
|
||||
_radioGroup.choices = _choices;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>현재 선택된 항목의 텍스트</summary>
|
||||
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<string> choices, int selectedIndex = 0)
|
||||
: base(id, name, Math.Max(0, Math.Min(selectedIndex, (choices?.Count ?? 1) - 1)))
|
||||
{
|
||||
_choices = choices ?? new List<string>();
|
||||
}
|
||||
|
||||
public UTKRadioPropertyItem(string id, string name, IEnumerable<string> choices, int selectedIndex = 0)
|
||||
: this(id, name, choices?.ToList() ?? new List<string>(), 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<RadioButtonGroup>("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
|
||||
/// <summary>텍스트로 선택</summary>
|
||||
public void SelectByText(string text)
|
||||
{
|
||||
int index = _choices.IndexOf(text);
|
||||
if (index >= 0)
|
||||
{
|
||||
Value = index;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
private void OnRadioChanged(ChangeEvent<int> evt)
|
||||
{
|
||||
Value = evt.newValue;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user