257 lines
6.7 KiB
C#
257 lines
6.7 KiB
C#
|
|
#nullable enable
|
||
|
|
using System;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UIElements;
|
||
|
|
|
||
|
|
namespace UVC.UIToolkit
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 입력 필드 컴포넌트.
|
||
|
|
/// Unity TextField를 래핑하여 커스텀 스타일을 적용합니다.
|
||
|
|
/// </summary>
|
||
|
|
[UxmlElement]
|
||
|
|
public partial class UTKInputField : TextField, IDisposable
|
||
|
|
{
|
||
|
|
#region Constants
|
||
|
|
private const string USS_PATH = "UIToolkit/Input/UTKInputField";
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Fields
|
||
|
|
private bool _disposed;
|
||
|
|
private bool _isEnabled = true;
|
||
|
|
private string _errorMessage = "";
|
||
|
|
private InputFieldVariant _variant = InputFieldVariant.Default;
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Events
|
||
|
|
/// <summary>값 변경 이벤트</summary>
|
||
|
|
public event Action<string>? OnValueChanged;
|
||
|
|
/// <summary>포커스 이벤트</summary>
|
||
|
|
public event Action? OnFocused;
|
||
|
|
/// <summary>포커스 해제 이벤트</summary>
|
||
|
|
public event Action? OnBlurred;
|
||
|
|
/// <summary>엔터 키 이벤트</summary>
|
||
|
|
public event Action<string>? OnSubmit;
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Properties
|
||
|
|
/// <summary>입력 값</summary>
|
||
|
|
public string Value
|
||
|
|
{
|
||
|
|
get => value;
|
||
|
|
set => SetValue(value, true);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>플레이스홀더 텍스트</summary>
|
||
|
|
[UxmlAttribute]
|
||
|
|
public string Placeholder
|
||
|
|
{
|
||
|
|
get => textEdition.placeholder;
|
||
|
|
set => textEdition.placeholder = value;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>에러 메시지</summary>
|
||
|
|
[UxmlAttribute]
|
||
|
|
public string ErrorMessage
|
||
|
|
{
|
||
|
|
get => _errorMessage;
|
||
|
|
set
|
||
|
|
{
|
||
|
|
_errorMessage = value;
|
||
|
|
EnableInClassList("utk-input--error", !string.IsNullOrEmpty(value));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>활성화 상태</summary>
|
||
|
|
[UxmlAttribute]
|
||
|
|
public bool IsEnabled
|
||
|
|
{
|
||
|
|
get => _isEnabled;
|
||
|
|
set
|
||
|
|
{
|
||
|
|
_isEnabled = value;
|
||
|
|
SetEnabled(value);
|
||
|
|
EnableInClassList("utk-input--disabled", !value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>읽기 전용</summary>
|
||
|
|
public new bool isReadOnly
|
||
|
|
{
|
||
|
|
get => base.isReadOnly;
|
||
|
|
set
|
||
|
|
{
|
||
|
|
base.isReadOnly = value;
|
||
|
|
EnableInClassList("utk-input--readonly", value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>멀티라인 모드</summary>
|
||
|
|
public new bool multiline
|
||
|
|
{
|
||
|
|
get => base.multiline;
|
||
|
|
set
|
||
|
|
{
|
||
|
|
base.multiline = value;
|
||
|
|
EnableInClassList("utk-input--multiline", value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>스타일 변형</summary>
|
||
|
|
[UxmlAttribute]
|
||
|
|
public InputFieldVariant Variant
|
||
|
|
{
|
||
|
|
get => _variant;
|
||
|
|
set
|
||
|
|
{
|
||
|
|
_variant = value;
|
||
|
|
UpdateVariant();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Enums
|
||
|
|
public enum InputFieldVariant
|
||
|
|
{
|
||
|
|
Default,
|
||
|
|
Filled,
|
||
|
|
Outlined
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Constructor
|
||
|
|
public UTKInputField() : base()
|
||
|
|
{
|
||
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
||
|
|
|
||
|
|
var uss = Resources.Load<StyleSheet>(USS_PATH);
|
||
|
|
if (uss != null)
|
||
|
|
{
|
||
|
|
styleSheets.Add(uss);
|
||
|
|
}
|
||
|
|
|
||
|
|
SetupStyles();
|
||
|
|
SetupEvents();
|
||
|
|
SubscribeToThemeChanges();
|
||
|
|
}
|
||
|
|
|
||
|
|
public UTKInputField(string label, string placeholder = "") : this()
|
||
|
|
{
|
||
|
|
this.label = label;
|
||
|
|
Placeholder = placeholder;
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Setup
|
||
|
|
private void SetupStyles()
|
||
|
|
{
|
||
|
|
AddToClassList("utk-input");
|
||
|
|
UpdateVariant();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void SetupEvents()
|
||
|
|
{
|
||
|
|
this.RegisterValueChangedCallback(OnTextValueChanged);
|
||
|
|
|
||
|
|
RegisterCallback<FocusInEvent>(_ =>
|
||
|
|
{
|
||
|
|
EnableInClassList("utk-input--focused", true);
|
||
|
|
OnFocused?.Invoke();
|
||
|
|
});
|
||
|
|
|
||
|
|
RegisterCallback<FocusOutEvent>(_ =>
|
||
|
|
{
|
||
|
|
EnableInClassList("utk-input--focused", false);
|
||
|
|
OnBlurred?.Invoke();
|
||
|
|
});
|
||
|
|
|
||
|
|
RegisterCallback<KeyDownEvent>(evt =>
|
||
|
|
{
|
||
|
|
if (evt.keyCode == KeyCode.Return && !multiline)
|
||
|
|
{
|
||
|
|
OnSubmit?.Invoke(value);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void SubscribeToThemeChanges()
|
||
|
|
{
|
||
|
|
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
|
||
|
|
RegisterCallback<DetachFromPanelEvent>(_ =>
|
||
|
|
{
|
||
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnThemeChanged(UTKTheme theme)
|
||
|
|
{
|
||
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Event Handlers
|
||
|
|
private void OnTextValueChanged(ChangeEvent<string> evt)
|
||
|
|
{
|
||
|
|
OnValueChanged?.Invoke(evt.newValue);
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region Methods
|
||
|
|
/// <summary>
|
||
|
|
/// 값 설정
|
||
|
|
/// </summary>
|
||
|
|
public void SetValue(string newValue, bool notify)
|
||
|
|
{
|
||
|
|
if (value == newValue) return;
|
||
|
|
|
||
|
|
if (notify)
|
||
|
|
{
|
||
|
|
value = newValue ?? "";
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
SetValueWithoutNotify(newValue ?? "");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 선택 영역 설정
|
||
|
|
/// </summary>
|
||
|
|
public void SelectAll()
|
||
|
|
{
|
||
|
|
textSelection.SelectAll();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void UpdateVariant()
|
||
|
|
{
|
||
|
|
RemoveFromClassList("utk-input--default");
|
||
|
|
RemoveFromClassList("utk-input--filled");
|
||
|
|
RemoveFromClassList("utk-input--outlined");
|
||
|
|
|
||
|
|
var variantClass = _variant switch
|
||
|
|
{
|
||
|
|
InputFieldVariant.Filled => "utk-input--filled",
|
||
|
|
InputFieldVariant.Outlined => "utk-input--outlined",
|
||
|
|
_ => "utk-input--default"
|
||
|
|
};
|
||
|
|
AddToClassList(variantClass);
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region IDisposable
|
||
|
|
public void Dispose()
|
||
|
|
{
|
||
|
|
if (_disposed) return;
|
||
|
|
_disposed = true;
|
||
|
|
|
||
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
||
|
|
OnValueChanged = null;
|
||
|
|
OnFocused = null;
|
||
|
|
OnBlurred = null;
|
||
|
|
OnSubmit = null;
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
}
|
||
|
|
}
|