add kcp
This commit is contained in:
45
Src/Unity/Scripts/UX/Library/CustomTextField.cs
Normal file
45
Src/Unity/Scripts/UX/Library/CustomTextField.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public class CustomTextField : TextField
|
||||
{
|
||||
public const string errorUssName = "error";
|
||||
public new class UxmlTraits : TextField.UxmlTraits
|
||||
{
|
||||
private readonly UxmlStringAttributeDescription m_RegexAttribute = new ()
|
||||
{
|
||||
name = "Regex"
|
||||
};
|
||||
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
||||
{
|
||||
base.Init(ve, bag, cc);
|
||||
var textField = (CustomTextField)ve;
|
||||
textField.Regex = m_RegexAttribute.GetValueFromBag(bag, cc);
|
||||
}
|
||||
}
|
||||
public new class UxmlFactory : UxmlFactory<CustomTextField, UxmlTraits> { }
|
||||
public CustomTextField():base()
|
||||
{
|
||||
this.RegisterValueChangedCallback(OnValueChangedInternal);
|
||||
}
|
||||
public string Regex { get; set; }
|
||||
public event Action<string> OnValueChanged;
|
||||
private void OnValueChangedInternal(ChangeEvent<string> evt)
|
||||
{
|
||||
var isMatch = string.IsNullOrEmpty(Regex) || System.Text.RegularExpressions.Regex.IsMatch(evt.newValue, Regex);
|
||||
this.Q("unity-text-input").EnableInClassList(errorUssName, !isMatch);
|
||||
if (isMatch)
|
||||
{
|
||||
OnValueChanged?.Invoke(evt.newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user