#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
namespace UVC.UIToolkit
{
///
/// 라디오 그룹 속성 데이터 클래스입니다.
/// UI는 UTKRadioPropertyItemView에서 담당합니다.
///
public class UTKRadioPropertyItem : UTKPropertyItemBase
{
#region Fields
private List _choices;
#endregion
#region Properties
/// 속성 타입
public override UTKPropertyType PropertyType => UTKPropertyType.RadioGroup;
/// 선택 가능한 항목 목록
public List Choices
{
get => _choices;
set => _choices = value ?? new List();
}
/// 현재 선택된 항목의 텍스트
public string? SelectedText
{
get
{
if (Value >= 0 && Value < _choices.Count)
{
return _choices[Value];
}
return null;
}
}
#endregion
#region Constructor
///
/// 라디오 그룹 속성을 생성합니다.
///
/// 고유 ID
/// 표시 이름
/// 선택 항목 목록
/// 초기 선택 인덱스
/// 읽기 전용 여부
public UTKRadioPropertyItem(string id, string name, List choices, int selectedIndex = 0, bool isReadOnly = false)
: base(id, name, Math.Max(0, Math.Min(selectedIndex, (choices?.Count ?? 1) - 1)))
{
_choices = choices ?? new List();
IsReadOnly = isReadOnly;
}
///
/// 라디오 그룹 속성을 생성합니다.
///
/// 고유 ID
/// 표시 이름
/// 선택 항목 목록
/// 초기 선택 인덱스
/// 읽기 전용 여부
public UTKRadioPropertyItem(string id, string name, IEnumerable choices, int selectedIndex = 0, bool isReadOnly = false)
: this(id, name, choices?.ToList() ?? new List(), selectedIndex, isReadOnly)
{
}
#endregion
#region Public Methods
/// 텍스트로 선택
public void SelectByText(string text)
{
int index = _choices.IndexOf(text);
if (index >= 0)
{
Value = index;
}
}
#endregion
}
}