Files
XRLib/Assets/Scripts/UVC/UIToolkit/Property/Items/UTKStringPropertyItem.cs
2026-02-02 19:33:27 +09:00

156 lines
4.1 KiB
C#

#nullable enable
using UnityEngine.UIElements;
namespace UVC.UIToolkit
{
/// <summary>
/// 문자열 속성 아이템
/// TextField를 사용한 텍스트 입력
/// </summary>
public class UTKStringPropertyItem : UTKPropertyItemBase<string>
{
#region Fields
private UTKInputField? _inputField;
private bool _isMultiline;
private int _maxLength;
#endregion
#region Properties
public override UTKPropertyType PropertyType => UTKPropertyType.String;
/// <summary>멀티라인 모드 여부</summary>
public bool IsMultiline
{
get => _isMultiline;
set
{
_isMultiline = value;
if (_inputField != null)
{
_inputField.multiline = value;
}
}
}
/// <summary>최대 문자 길이 (0 = 무제한)</summary>
public int MaxLength
{
get => _maxLength;
set
{
_maxLength = value;
if (_inputField != null)
{
_inputField.maxLength = value;
}
}
}
#endregion
#region Constructor
public UTKStringPropertyItem(string id, string name, string initialValue = "")
: base(id, name, initialValue ?? string.Empty)
{
}
#endregion
#region Override Methods
public override VisualElement CreateUI()
{
var container = CreateUIFromUxml("UTKStringPropertyItem");
if (container == null)
{
return CreateUIFallback();
}
_inputField = container.Q<UTKInputField>("value-field");
if (_inputField != null)
{
_inputField.Value = Value;
_inputField.multiline = _isMultiline;
if (_maxLength > 0)
{
_inputField.maxLength = _maxLength;
}
}
return container;
}
private VisualElement CreateUIFallback()
{
var container = CreateContainer();
var label = CreateNameLabel();
container.Add(label);
var valueContainer = new VisualElement();
valueContainer.AddToClassList("utk-property-item__value");
_inputField = new UTKInputField();
_inputField.name = "value-field";
_inputField.Value = Value;
_inputField.multiline = _isMultiline;
if (_maxLength > 0)
{
_inputField.maxLength = _maxLength;
}
valueContainer.Add(_inputField);
container.Add(valueContainer);
return container;
}
public override void BindUI(VisualElement element)
{
base.BindUI(element);
_inputField = element.Q<UTKInputField>("value-field");
if (_inputField != null)
{
_inputField.Value = Value;
_inputField.SetEnabled(!IsReadOnly);
_inputField.OnValueChanged += OnTextChanged;
}
}
public override void UnbindUI(VisualElement element)
{
if (_inputField != null)
{
_inputField.OnValueChanged -= OnTextChanged;
_inputField = null;
}
base.UnbindUI(element);
}
public override void RefreshUI()
{
if (_inputField != null && _inputField.Value != Value)
{
_inputField.SetValue(Value, false);
}
}
protected override void UpdateReadOnlyState()
{
base.UpdateReadOnlyState();
if (_inputField != null)
{
_inputField.SetEnabled(!IsReadOnly);
}
}
#endregion
#region Private Methods
private void OnTextChanged(string newValue)
{
DebounceValueChange(newValue).Forget();
}
#endregion
}
}