How To Create a custom Pseudo Class
using System.Collections.Generic;
using SoyWar.UIToolkit;
using SoyWar.UIToolkit.PseudoClasses;
using UnityEngine;
using UnityEngine.UIElements;
public class CustomPseudoClass : PseudoClass
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
private static void Init()
{
// Adds to the list of usable pseudo-classes, which can be called by name in the Selector Parser and Selector Builder.
PseudoClass.Add<CustomPseudoClass>("custom");
}
// Checks if the VisualElement is valid according to the rules of this pseudo-class
public override bool Check(ICollection<VisualElement> roots, VisualElement visualElement, UIToolkitComponent.TypeSearch searchFrom)
{
// Example: The VisualElement is only valid if it is a TextField and if it contains the same content as the "Value" variable.
return visualElement is TextField textField && textField.value == Value;
}
// Add Event to check validity though the pseudo class manipulator.
protected override void AddManipulatorCallback(PseudoClassManipulator manipulator)
{
// Example: Checks the validity of the VisualElement each time the KeyUpEvent is called
manipulator.AddEventCallback<KeyUpEvent>(() =>
{
// Updates the validity of the VisualElement through the rules of this pseudo class
manipulator.CheckUpdate();
// Equivalent to
manipulator.Check = Check(manipulator);
// Or
manipulator.Check = Check(manipulator.Roots, manipulator.target, manipulator.SearchFrom);
});
}
// Checks the validity of the pseudo-class when creating the selector
protected override bool Validate()
{
//Example: The pseudo-class is only valid if it has a value
return Value != null;
}
}