// Taken from https://github.com/adammyhre/Unity-Utils/blob/master/UnityUtils/Scripts/Extensions/VisualElementExtensions.cs under MIT License.
using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace Xprees.EditorTools.Editor.Extensions
{
public static class VisualElementExtensions
{
///
/// Creates a new child VisualElement and adds it to the parent.
///
/// The parent VisualElement to add the child to.
/// The CSS classes to add to the child.
/// The created child VisualElement.
public static VisualElement CreateChild(this VisualElement parent, params string[] classes)
{
var child = new VisualElement();
child.AddClass(classes).AddTo(parent);
return child;
}
///
/// Creates a new child of type T and adds it to the parent.
///
/// The type of the child VisualElement.
/// The parent VisualElement to add the child to.
/// The CSS classes to add to the child.
/// The created child VisualElement of type T.
public static T CreateChild(this VisualElement parent, params string[] classes)
where T : VisualElement, new()
{
var child = new T();
child.AddClass(classes).AddTo(parent);
return child;
}
///
/// Adds the child VisualElement to the parent and returns the child.
///
/// The type of the child VisualElement.
/// The child VisualElement to add.
/// The parent VisualElement to add the child to.
/// The added child VisualElement.
public static T AddTo(this T child, VisualElement parent) where T : VisualElement
{
parent.Add(child);
return child;
}
///
/// See for adding a child to a parent.
///
public static void RemoveFrom(this T child, VisualElement parent)
where T : VisualElement => parent.Remove(child);
///
/// Adds the specified CSS classes to the VisualElement.
///
/// The type of the VisualElement.
/// The VisualElement to add the classes to.
/// The CSS classes to add.
/// The VisualElement with the added classes.
public static T AddClass(this T visualElement, params string[] classes) where T : VisualElement
{
foreach (string cls in classes)
{
if (!string.IsNullOrEmpty(cls))
{
visualElement.AddToClassList(cls);
}
}
return visualElement;
}
///
/// See for adding classes.
///
public static void RemoveClass(this T visualElement, params string[] classes) where T : VisualElement
{
foreach (string cls in classes)
{
if (!string.IsNullOrEmpty(cls))
{
visualElement.RemoveFromClassList(cls);
}
}
}
///
/// Adds a manipulator to the VisualElement.
///
/// The type of the VisualElement.
/// The VisualElement to add the manipulator to.
/// The manipulator to add.
/// The VisualElement with the added manipulator.
public static T WithManipulator(this T visualElement, IManipulator manipulator) where T : VisualElement
{
visualElement.AddManipulator(manipulator);
return visualElement;
}
///
/// Sets the background image of a VisualElement using a given Sprite.
///
/// The VisualElement whose background image will be set.
/// The Sprite to use as the background image.
public static void SetImageFromSprite(this VisualElement imageContainer, Sprite sprite)
{
var texture = sprite.texture;
if (texture)
{
imageContainer.style.backgroundImage = new StyleBackground(texture);
}
}
///
/// Finds an element recursively using a predicate function.
/// Unity's Query().Where() only searches descendants, so this method checks the element itself first.
///
/// The VisualElement to search in (including itself).
/// The predicate function to match elements.
/// The found VisualElement, or null if not found.
static VisualElement FindElement(this VisualElement element, Func predicate)
{
if (predicate(element))
{
return element;
}
return element.Query().Where(predicate).First();
}
///
/// Finds an element by name recursively.
///
/// The VisualElement to search in (including itself).
/// The name of the element to find.
/// The found VisualElement, or null if not found.
public static VisualElement FindElementByName(this VisualElement element, string name)
{
return element.FindElement(e => e.name == name);
}
///
/// Finds an element by tooltip recursively.
/// Unity's Query API doesn't provide tooltip search, so this uses Query().Where() with a predicate.
///
/// The VisualElement to search in (including itself).
/// The tooltip text of the element to find.
/// The found VisualElement, or null if not found.
public static VisualElement FindElementByTooltip(this VisualElement element, string tooltip)
{
return element.FindElement(e => e.tooltip == tooltip);
}
///
/// Finds an element by CSS class recursively.
///
/// The VisualElement to search in (including itself).
/// The CSS class name to find.
/// Optional filter function to further refine the search.
/// The found VisualElement, or null if not found.
public static VisualElement FindElementByClass(
this VisualElement element,
string className,
Func filter = null
)
{
return element.FindElement(e => e.ClassListContains(className) && (filter == null || filter(e)));
}
}
}