#nullable enable using UnityEngine.UIElements; namespace UVC.UIToolkit.Property { /// /// 문자열 속성 아이템 /// TextField를 사용한 텍스트 입력 /// public class UTKStringPropertyItem : UTKPropertyItemBase { #region Fields private TextField? _textField; private bool _isMultiline; private int _maxLength; #endregion #region Properties public override UTKPropertyType PropertyType => UTKPropertyType.String; /// 멀티라인 모드 여부 public bool IsMultiline { get => _isMultiline; set { _isMultiline = value; if (_textField != null) { _textField.multiline = value; } } } /// 최대 문자 길이 (0 = 무제한) 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("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 evt) { DebounceValueChange(evt.newValue).Forget(); } #endregion } }