134 lines
3.5 KiB
C#
134 lines
3.5 KiB
C#
|
|
#nullable enable
|
||
|
|
using UnityEngine.UIElements;
|
||
|
|
|
||
|
|
namespace UVC.UIToolkit.Property
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 문자열 속성 아이템
|
||
|
|
/// TextField를 사용한 텍스트 입력
|
||
|
|
/// </summary>
|
||
|
|
public class UTKStringPropertyItem : UTKPropertyItemBase<string>
|
||
|
|
{
|
||
|
|
#region Fields
|
||
|
|
private TextField? _textField;
|
||
|
|
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 (_textField != null)
|
||
|
|
{
|
||
|
|
_textField.multiline = value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>최대 문자 길이 (0 = 무제한)</summary>
|
||
|
|
public int MaxLength
|
||
|
|
{
|
||
|
|
get => _maxLength;
|
||
|
|
set
|
||
|
|
{
|
||
|
|
_maxLength = value;
|
||
|
|
if (_textField != null)
|
||
|
|
{
|
||
|
|
_textField.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 = CreateContainer();
|
||
|
|
|
||
|
|
var label = CreateNameLabel();
|
||
|
|
container.Add(label);
|
||
|
|
|
||
|
|
var valueContainer = new VisualElement();
|
||
|
|
valueContainer.AddToClassList("utk-property-item__value");
|
||
|
|
|
||
|
|
_textField = new TextField();
|
||
|
|
_textField.name = "value-field";
|
||
|
|
_textField.value = Value;
|
||
|
|
_textField.multiline = _isMultiline;
|
||
|
|
if (_maxLength > 0)
|
||
|
|
{
|
||
|
|
_textField.maxLength = _maxLength;
|
||
|
|
}
|
||
|
|
|
||
|
|
valueContainer.Add(_textField);
|
||
|
|
container.Add(valueContainer);
|
||
|
|
|
||
|
|
return container;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void BindUI(VisualElement element)
|
||
|
|
{
|
||
|
|
base.BindUI(element);
|
||
|
|
|
||
|
|
_textField = element.Q<TextField>("value-field");
|
||
|
|
if (_textField != null)
|
||
|
|
{
|
||
|
|
_textField.value = Value;
|
||
|
|
_textField.SetEnabled(!IsReadOnly);
|
||
|
|
_textField.RegisterValueChangedCallback(OnTextChanged);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void UnbindUI(VisualElement element)
|
||
|
|
{
|
||
|
|
if (_textField != null)
|
||
|
|
{
|
||
|
|
_textField.UnregisterValueChangedCallback(OnTextChanged);
|
||
|
|
_textField = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
base.UnbindUI(element);
|
||
|
|
}
|
||
|
|
|
||
|
|
public override void RefreshUI()
|
||
|
|
{
|
||
|
|
if (_textField != null && _textField.value != Value)
|
||
|
|
{
|
||
|
|
_textField.SetValueWithoutNotify(Value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override void UpdateReadOnlyState()
|
||
|
|
{
|
||
|
|
base.UpdateReadOnlyState();
|
||
|
|
|
||
|
|
if (_textField != null)
|
||
|
|
{
|
||
|
|
_textField.SetEnabled(!IsReadOnly);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Private Methods
|
||
|
|
private void OnTextChanged(ChangeEvent<string> evt)
|
||
|
|
{
|
||
|
|
DebounceValueChange(evt.newValue).Forget();
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
}
|
||
|
|
}
|