// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved.
// This code can only be used under the standard Unity Asset Store End User License Agreement
// A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Cursor = UnityEngine.UIElements.Cursor;
using Position = UnityEngine.UIElements.Position;
// ReSharper disable MemberCanBePrivate.Global
namespace Doozy.Runtime.UIElements.Extensions
{
public static class VisualElementExtensions
{
#region AddChild, RemoveAllChildren, AddTemplateContainer
/// Add a child element to the target element and get back the reference to the target
/// Target VisualElement
/// Child VisualElement
public static T AddChild(this T target, VisualElement child) where T : VisualElement
{
target.Add(child);
return target;
}
/// Remove all children of the target element
/// Target VisualElement
public static T RemoveAllChildren(this T target) where T : VisualElement
{
foreach (VisualElement child in new List(target.Children()))
child.RemoveFromHierarchy();
return target;
}
/// [Editor] Add a template container to the target element and get back the reference to the target
/// Target VisualElement
/// Path to the uxml file
/// Set how much the item will grow relative to the rest of the flexible items inside the same container - style.flexGrow
public static T AddTemplateContainer(this T target, string layoutPath, float flexGrow = 1) where T : VisualElement
{
#if UNITY_EDITOR
TemplateContainer templateContainer = UnityEditor.AssetDatabase.LoadAssetAtPath(layoutPath).CloneTree().SetStyleFlexGrow(flexGrow);
templateContainer.style.alignSelf = new StyleEnum(Align.Stretch);
target.AddChild(templateContainer);
#else
Doozy.Runtime.Common.Debugger.LogWarning("This method works only in the Editor");
#endif
return target;
}
#endregion
#region AddSpace, AddHorizontalSpace, AddVerticalSpace
public const float k_Spacing = 4;
public static T AddEndOfLineSpace(this T target, int multiplier = 1) where T : VisualElement =>
target.AddChild(new VisualElement().SetStyleFlexShrink(0).SetName("EndSpace").SetStyleHeight(k_Spacing * 6 * multiplier));
public static T AddSpaceBlock(this T target, int multiplier = 1) where T : VisualElement =>
target.AddChild(new VisualElement().SetStyleFlexShrink(0).SetName("SpaceBlock").SetStyleHeight(k_Spacing * multiplier).SetStyleWidth(k_Spacing * multiplier));
public static T AddSpace(this T target, float width, float height) where T : VisualElement =>
target.AddChild(new VisualElement().SetStyleFlexShrink(0).SetName("Space").SetStyleSize(width, height));
public static T AddSpace(this T target, float space) where T : VisualElement =>
target.AddSpace(space, space);
public static T AddHorizontalSpace(this T target, float height) where T : VisualElement =>
target.AddChild(new VisualElement().SetStyleFlexShrink(0).SetName("HSpace").SetStyleHeight(height));
public static T AddVerticalSpace(this T target, float width) where T : VisualElement =>
target.AddChild(new VisualElement().SetStyleFlexShrink(0).SetName("VSpace").SetStyleWidth(width));
public static T AddFlexibleSpace(this T target) where T : VisualElement =>
target.AddChild(new VisualElement().SetStyleFlexGrow(1).SetName("FlexibleSpace"));
#endregion
#region AddClass, RemoveClass
/// [Editor] Add the 'Dark' or 'Light' class name to the target element, depending on the current active Editor Theme, and get back the reference to the target
/// Target VisualElement
public static T AddCurrentThemeClass(this T target) where T : VisualElement
{
#if UNITY_EDITOR
return target.AddClass(UnityEditor.EditorGUIUtility.isProSkin ? "Dark" : "Light");
#else
Doozy.Runtime.Common.Debugger.LogWarning("This method works only in the Editor");
return target;
#endif
}
/// Add a class name to the target element and get back the reference to the target
/// Target VisualElement
/// Name of the class
public static T AddClass(this T target, string className) where T : VisualElement
{
if (!target.ClassListContains(className))
target.AddToClassList(className);
return target;
}
/// Remove a class name from the target element and get back the reference to the target
/// Target VisualElement
/// Name of the class
public static T RemoveClass(this T target, string className) where T : VisualElement
{
if (target.ClassListContains(className))
target.RemoveFromClassList(className);
return target;
}
#endregion
#region AddStyle, RemoveStyle
/// [Editor] Add a style sheet to the target element and get back the reference to the target
/// Target VisualElement
/// StyleSheet reference
public static T AddStyle(this T target, StyleSheet styleSheet) where T : VisualElement
{
if (styleSheet == null) return target;
target.styleSheets.Add(styleSheet);
return target;
}
/// [Editor] Add a style sheet to the target element and get back the reference to the target
/// Target VisualElement
/// Path to the StyleSheet uss file
public static T AddStyle(this T target, string styleSheetPath) where T : VisualElement
{
#if UNITY_EDITOR
target.AddStyle(UnityEditor.AssetDatabase.LoadAssetAtPath(styleSheetPath));
#else
Doozy.Runtime.Common.Debugger.LogWarning("This method works only in the Editor");
#endif
return target;
}
/// Remove a style sheet from the target element and get back the reference to the target
/// Target VisualElement
/// StyleSheet reference
public static T RemoveStyle(this T target, StyleSheet styleSheet) where T : VisualElement
{
if (target.styleSheets.Contains(styleSheet))
target.styleSheets.Remove(styleSheet);
return target;
}
/// Remove a style sheet from the target element and get back the reference to the target
/// Target VisualElement
/// StyleSheet name
public static T RemoveStyle(this T target, string styleSheetName) where T : VisualElement
{
StyleSheet styleSheet = null;
for (var i = 0; i < target.styleSheets.count; i++)
{
if (!target.styleSheets[i].name.Equals(styleSheetName)) continue;
styleSheet = target.styleSheets[i];
break;
}
if (styleSheet != null)
target.styleSheets.Remove(styleSheet);
return target;
}
#endregion
#region Name
/// Set the name of the target element and get back the reference to the target
/// name
///
/// Target VisualElement
/// Name value
public static T SetName(this T target, string value) where T : VisualElement
{
target.name = value;
return target;
}
/// Get the name of the target element
/// name
///
/// Target VisualElement
public static string GetName(this T target) where T : VisualElement =>
target.name;
#endregion
#region Tooltip
/// Set the text to display inside an information box after the user hovers the element for a small amount of time
/// tooltip
///
/// Target VisualElement
/// Tooltip value
public static T SetTooltip(this T target, string value) where T : VisualElement
{
target.tooltip = value;
return target;
}
/// Get the text displayed inside an information box after the user hovers the element for a small amount of time
/// tooltip
///
/// Target VisualElement
public static string GetTooltip(this T target) where T : VisualElement =>
target.tooltip;
#endregion
#region PickingMode
///
/// Set if this element can be pick during mouseEvents or IPanel.Pick queries
/// pickingMode
///
/// Target VisualElement
/// PickingMode value
public static T SetPickingMode(this T target, PickingMode value) where T : VisualElement
{
target.pickingMode = value;
return target;
}
/// Get if this element can be pick during mouseEvents or IPanel.Pick queries
/// pickingMode
///
/// Target VisualElement
public static PickingMode GetPickingMode(this T target) where T : VisualElement =>
target.pickingMode;
#endregion
#region Style - Position
///
/// Set the element's positioning in its parent container
/// style.position
///
/// Target VisualElement
/// Position value
public static T SetStylePosition(this T target, Position value) where T : VisualElement
{
target.style.position = new StyleEnum(value);
return target;
}
///
/// Get the element's positioning in its parent container
/// style.position
///
/// Target VisualElement
public static Position GetStylePosition(this T target) where T : VisualElement =>
target.style.position.value;
#endregion
#region Style - Overflow
///
/// Set how a container behaves if its content overflows its own box
/// style.overflow
///
/// Target VisualElement
/// Overflow value
public static T SetStyleOverflow(this T target, Overflow value) where T : VisualElement
{
target.style.overflow = new StyleEnum(value);
return target;
}
///
/// Get how a container behaves if its content overflows its own box
/// style.overflow
///
/// Target VisualElement
public static Overflow GetStyleOverflow(this T target) where T : VisualElement =>
target.style.overflow.value;
#endregion
#region Style - AlignSelf
///
/// Similar to align-items, but only for this specific element
/// style.alignSelf
///
/// Target VisualElement
/// Align value
public static T SetStyleAlignSelf(this T target, Align value) where T : VisualElement
{
target.style.alignSelf = new StyleEnum(value);
return target;
}
///
/// Similar to align-items, but only for this specific element
/// style.alignSelf
///
/// Target VisualElement
public static Align GetStyleAlignSelf(this T target) where T : VisualElement =>
target.style.alignSelf.value;
#endregion
#region Style - AlignItems
///
/// Set the alignment of children on the cross axis of this container
/// style.alignItems
///
/// Target VisualElement
/// Align value
public static T SetStyleAlignItems(this T target, Align value) where T : VisualElement
{
target.style.alignItems = new StyleEnum(value);
return target;
}
///
/// Get the alignment of children on the cross axis of this container
/// style.alignItems
///
/// Target VisualElement
public static Align GetStyleAlignItems(this T target) where T : VisualElement =>
target.style.alignItems.value;
#endregion
#region Style - AlignContent
///
/// Set the alignment of the whole area of children on the cross axis if they span over multiple lines in this container
/// style.alignContent
///
/// Target VisualElement
/// Align value
public static T SetStyleAlignContent(this T target, Align value) where T : VisualElement
{
target.style.alignContent = new StyleEnum(value);
return target;
}
///
/// Get the alignment of the whole area of children on the cross axis if they span over multiple lines in this container
/// style.alignContent
///
/// Target VisualElement
public static Align GetStyleAlignContent(this T target) where T : VisualElement =>
target.style.alignContent.value;
#endregion
#region Style - JustifyContent
///
/// Set the justification of children on the main axis of this container
/// style.justifyContent
///
/// Target VisualElement
/// Justify value
public static T SetStyleJustifyContent(this T target, Justify value) where T : VisualElement
{
target.style.justifyContent = new StyleEnum(value);
return target;
}
///
/// Get the justification of children on the main axis of this container
/// style.justifyContent
///
/// Target VisualElement
public static Justify GetStyleJustifyContent(this T target) where T : VisualElement =>
target.style.justifyContent.value;
#endregion
#region Style - FlexDirection
/// Set the direction of the main axis to layout children in a container
/// style.flexDirection
///
/// Target VisualElement
/// FlexDirection value
public static T SetStyleFlexDirection(this T target, FlexDirection value) where T : VisualElement
{
target.style.flexDirection = new StyleEnum(value);
return target;
}
/// Get the direction of the main axis to layout children in a container
/// style.flexDirection
///
/// Target VisualElement
public static FlexDirection GetStyleFlexDirection(this T target) where T : VisualElement =>
target.style.flexDirection.value;
/// Set the direction of the main axis to layout children in a container
/// style.flexDirection
///
/// Target VisualElement
/// StyleKeyword value
public static T SetStyleFlexDirectionKeyword(this T target, StyleKeyword value) where T : VisualElement
{
target.style.flexDirection = new StyleEnum(value);
return target;
}
/// Get the direction of the main axis to layout children in a container
/// style.flexDirection
///
/// Target VisualElement
public static StyleKeyword GetStyleFlexDirectionKeyword(this T target) where T : VisualElement =>
target.style.flexDirection.keyword;
#endregion
#region Style - FlexGrow
/// Set how the target will grow relative to the rest of the flexible items inside the same container
/// style.flexGrow
///
/// Target VisualElement
/// 0 = fixed size / 1 = flexible
public static T SetStyleFlexGrow(this T target, float value) where T : VisualElement
{
target.style.flexGrow = value;
return target;
}
/// Set how the target will grow relative to the rest of the flexible items inside the same container
/// style.flexGrow
///
/// Target VisualElement
public static float GetStyleFlexGrow(this T target) where T : VisualElement =>
target.style.flexGrow.value;
/// Set how the target will grow relative to the rest of the flexible items inside the same container
/// style.flexGrow
///
/// Target VisualElement
/// StyleKeyword value
public static T SetStyleFlexGrowKeyword(this T target, StyleKeyword value) where T : VisualElement
{
target.style.flexGrow = new StyleFloat(value);
return target;
}
/// Get how the target will grow relative to the rest of the flexible items inside the same container
/// style.flexGrow
///
/// Target VisualElement
public static StyleKeyword GetStyleFlexGrowKeyword(this T target) where T : VisualElement =>
target.style.flexGrow.keyword;
#endregion
#region Style - FlexShrink
/// Set how the target will shrink relative to the rest of the flexible items inside the same container
/// style.flexShrink
///
/// Target VisualElement
/// 0 = fixed size / 1 = flexible
public static T SetStyleFlexShrink(this T target, float value) where T : VisualElement
{
target.style.flexShrink = value;
return target;
}
/// Get how the target will shrink relative to the rest of the flexible items inside the same container
/// style.flexShrink
///
/// Target VisualElement
public static float GetStyleFlexShrink(this T target) where T : VisualElement =>
target.style.flexShrink.value;
/// Set how the item will shrink relative to the rest of the flexible items inside the same container
/// style.flexShrink
///
/// Target VisualElement
/// StyleKeyword value
public static T SetStyleFlexShrinkKeyword(this T target, StyleKeyword value) where T : VisualElement
{
target.style.flexShrink = new StyleFloat(value);
return target;
}
/// Get how the item will shrink relative to the rest of the flexible items inside the same container
/// style.flexShrink
///
/// Target VisualElement
public static StyleKeyword GetStyleFlexShrinkKeyword(this T target) where T : VisualElement =>
target.style.flexShrink.keyword;
#endregion
#region Style - FlexWrap
///
/// Set the placement of children over multiple lines if not enough space is available in this container
/// style.flexWrap
///
/// Target VisualElement
/// Wrap value
public static T SetStyleFlexWrap(this T target, Wrap value) where T : VisualElement
{
target.style.flexWrap = value;
return target;
}
///
/// Get the placement of children over multiple lines if not enough space is available in this container
///
/// Target VisualElement
public static Wrap GetStyleFlexWrap(this T target) where T : VisualElement =>
target.style.flexWrap.value;
#endregion
#region Style - FlexBasis
///
/// Set the initial main size of a flex item, on the main flex axis.
/// The final layout must be smaller or larger,
/// according to the flex shrinking and growing determined by the flex property.
/// style.flexBasis
///
/// Target VisualElement
/// 0 = fixed size / 1 = flexible
public static T SetStyleFlexBasis(this T target, float value) where T : VisualElement
{
target.style.flexBasis = value;
return target;
}
///
/// Get the initial main size of a flex item, on the main flex axis.
/// The final layout must be smaller or larger,
/// according to the flex shrinking and growing determined by the flex property.
/// style.flexBasis
///
/// Target VisualElement
public static float GetStyleFlexBasis(this T target) where T : VisualElement =>
target.style.flexBasis.value.value;
///
/// Set the initial main size of a flex item, on the main flex axis.
/// The final layout must be smaller or larger,
/// according to the flex shrinking and growing determined by the flex property.
/// style.flexBasis
///
/// Target VisualElement
/// StyleLength value
public static T SetStyleFlexBasisStyleLength(this T target, StyleLength value) where T : VisualElement
{
target.style.flexBasis = value;
return target;
}
///
/// Get the initial main size of a flex item, on the main flex axis.
/// The final layout must be smaller or larger,
/// according to the flex shrinking and growing determined by the flex property.
/// style.flexBasis
///
/// Target VisualElement
public static StyleKeyword GetStyleFlexBasisKeyword(this T target) where T : VisualElement =>
target.style.flexBasis.keyword;
#endregion
#region Style - BorderColor
/// Set the Colors for all the element's borders (Top, Left, Right, Bottom)
/// style.borderLeftColor
/// style.borderTopColor
/// style.borderRightColor
/// style.borderBottomColor
///
/// Target VisualElement
/// Left border color
/// Top border color
/// Right border color
/// Bottom border color
public static T SetStyleBorderColor(this T target, Color left, Color top, Color right, Color bottom) where T : VisualElement
{
target.style.borderLeftColor = left;
target.style.borderTopColor = top;
target.style.borderRightColor = right;
target.style.borderBottomColor = bottom;
return target;
}
/// Set the same Color for all the element's borders (Top, Left, Right, Bottom)
/// style.borderLeftColor
/// style.borderTopColor
/// style.borderRightColor
/// style.borderBottomColor
///
/// Target VisualElement
/// Color value
public static T SetStyleBorderColor(this T target, Color value) where T : VisualElement =>
target.SetStyleBorderColor(value, value, value, value);
#region SetStyleBorderLeftColor, GetStyleBorderLeftColor
/// Set the Color of the element's Left border
/// style.borderLeftColor
///
/// Target VisualElement
/// Color value
public static T SetStyleBorderLeftColor(this T target, Color value) where T : VisualElement
{
target.style.borderLeftColor = value;
return target;
}
/// Get the Color of the element's left border
/// style.borderLeftColor
///
/// Target VisualElement
public static Color GetStyleBorderLeftColor(this T target) where T : VisualElement =>
target.resolvedStyle.borderLeftColor;
#endregion
#region SetStyleBorderTopColor, GetStyleBorderTopColor
/// Set the Color of the element's Top border
/// style.borderTopColor
///
/// Target VisualElement
/// Color value
public static T SetStyleBorderTopColor(this T target, Color value) where T : VisualElement
{
target.style.borderTopColor = value;
return target;
}
/// Get the Color of the element's left border
/// style.borderTopColor
///
/// Target VisualElement
public static Color GetStyleBorderTopColor(this T target) where T : VisualElement =>
target.resolvedStyle.borderTopColor;
#endregion
#region SetStyleBorderRightColor, GetStyleBorderRightColor
/// Set the Color of the element's Right border
/// style.borderRightColor
///
/// Target VisualElement
/// Color value
public static T SetStyleBorderRightColor(this T target, Color value) where T : VisualElement
{
target.style.borderRightColor = value;
return target;
}
/// Get the Color of the element's left border
/// style.borderRightColor
///
/// Target VisualElement
public static Color GetStyleBorderRightColor(this T target) where T : VisualElement =>
target.resolvedStyle.borderRightColor;
#endregion
#region SetStyleBorderBottomColor, GetStyleBorderBottomColor
/// Set the Color of the element's Bottom border
/// style.borderBottomColor
///
/// Target VisualElement
/// Color value
public static T SetStyleBorderBottomColor(this T target, Color value) where T : VisualElement
{
target.style.borderBottomColor = value;
return target;
}
/// Get the Color of the element's left border
/// style.borderBottomColor
///
/// Target VisualElement
public static Color GetStyleBorderBottomColor(this T target) where T : VisualElement =>
target.resolvedStyle.borderBottomColor;
#endregion
#endregion
#region Style - BorderWidth
/// Set the border Width for all the element's borders (Top, Left, Right, Bottom)
/// style.borderLeftWidth
/// style.borderTopWidth
/// style.borderRightWidth
/// style.borderBottomWidth
///
/// Target VisualElement
/// Left border width
/// Top border width
/// Right border with
/// Bottom border width
public static T SetStyleBorderWidth(this T target, float left, float top, float right, float bottom) where T : VisualElement
{
target.style.borderLeftWidth = left;
target.style.borderTopWidth = top;
target.style.borderRightWidth = right;
target.style.borderBottomWidth = bottom;
return target;
}
/// Set the same border Width for all the element's borders (Top, Left, Right, Bottom)
/// style.borderLeftWidth
/// style.borderTopWidth
/// style.borderRightWidth
/// style.borderBottomWidth
///
/// Target VisualElement
/// Width value
public static T SetStyleBorderWidth(this T target, float value) where T : VisualElement =>
target.SetStyleBorderWidth(value, value, value, value);
/// Set the border Width for all the element's borders (Top, Left, Right, Bottom)
/// style.borderLeftWidth
/// style.borderTopWidth
/// style.borderRightWidth
/// style.borderBottomWidth
///
/// Target VisualElement
/// Edge values for all the border's widths
public static T SetStyleBorderWidth(this T target, EdgeValues edge) where T : VisualElement =>
target.SetStyleBorderWidth(edge.Left, edge.Top, edge.Right, edge.Bottom);
#region SetStyleBorderLeftWidth, GetStyleBorderLeftWidth
/// Set the Width value of the element's Left border
/// style.borderLeftWidth
///
/// Target VisualElement
/// Width value
public static T SetStyleBorderLeftWidth(this T target, float value) where T : VisualElement
{
target.style.borderLeftWidth = value;
return target;
}
/// Get the Width value of the element's left border
/// style.borderLeftWidth
///
/// Target VisualElement
public static float GetStyleBorderLeftWidth(this T target) where T : VisualElement =>
target.style.borderLeftWidth.value;
#endregion
#region SetStyleBorderTopWidth, GetStyleBorderTopWidth
/// Set the Width value of the element's Top border
/// style.borderTopWidth
///
/// Target VisualElement
/// Width value
public static T SetStyleBorderTopWidth(this T target, float value) where T : VisualElement
{
target.style.borderTopWidth = value;
return target;
}
/// Get the Width value of the element's left border
/// style.borderTopWidth
///
/// Target VisualElement
public static float GetStyleBorderTopWidth(this T target) where T : VisualElement =>
target.style.borderTopWidth.value;
#endregion
#region SetStyleBorderRightWidth, GetStyleBorderRightWidth
/// Set the Width value of the element's Right border
/// style.borderRightWidth
///
/// Target VisualElement
/// Width value
public static T SetStyleBorderRightWidth(this T target, float value) where T : VisualElement
{
target.style.borderRightWidth = value;
return target;
}
/// Get the Width value of the element's left border
/// style.borderRightWidth
///
/// Target VisualElement
public static float GetStyleBorderRightWidth(this T target) where T : VisualElement =>
target.style.borderRightWidth.value;
#endregion
#region SetStyleBorderBottomWidth, GetStyleBorderBottomWidth
/// Set the Width value of the element's Bottom border
/// style.borderBottomWidth
///
/// Target VisualElement
/// Width value
public static T SetStyleBorderBottomWidth(this T target, float value) where T : VisualElement
{
target.style.borderBottomWidth = value;
return target;
}
/// Get the Width value of the element's left border
/// style.borderBottomWidth
///
/// Target VisualElement
public static float GetStyleBorderBottomWidth(this T target) where T : VisualElement =>
target.style.borderBottomWidth.value;
#endregion
#endregion
#region Style - BorderRadius
/// Set the border (corner) Radius for all the element's corners (TopLeft, TopRight, BottomRight, BottomLeft)
/// style.borderTopLeftRadius
/// style.borderTopRightRadius
/// style.borderBottomRightRadius
/// style.borderBottomLeftRadius
///
/// Target VisualElement
/// TopLeft border (corner) radius
/// TopRight border (corner) radius
/// BottomRight border (corner) radius
/// BottomLeft border (corner) radius
public static T SetStyleBorderRadius(this T target, float topLeft, float topRight, float bottomRight, float bottomLeft) where T : VisualElement
{
target.style.borderTopLeftRadius = topLeft;
target.style.borderTopRightRadius = topRight;
target.style.borderBottomRightRadius = bottomRight;
target.style.borderBottomLeftRadius = bottomLeft;
return target;
}
/// Set the same border (corner) Radius for all the element's corners (TopLeft, TopRight, BottomRight, BottomLeft)
/// style.borderTopLeftRadius
/// style.borderTopRightRadius
/// style.borderBottomRightRadius
/// style.borderBottomLeftRadius
///
/// Target VisualElement
/// Radius value
public static T SetStyleBorderRadius(this T target, float value) where T : VisualElement =>
target.SetStyleBorderRadius(value, value, value, value);
/// Set the border (corner) Radius for all the element's corners (TopLeft, TopRight, BottomRight, BottomLeft)
/// style.borderTopLeftRadius
/// style.borderTopRightRadius
/// style.borderBottomRightRadius
/// style.borderBottomLeftRadius
///
/// Target VisualElement
/// Edge values for all all the element's corners
/// edge.left = TopLeft border (corner) radius
/// edge.top = TopRight border (corner) radius
/// edge.top = BottomRight border (corner) radius
/// edge.bottom = BottomLeft border (corner) radius
///
public static T SetStyleBorderRadius(this T target, EdgeValues edge) where T : VisualElement =>
target.SetStyleBorderRadius(edge.Left, edge.Top, edge.Right, edge.Bottom);
#region SetStyleBorderTopLeftRadius, GetStyleBorderTopLeftRadius
/// Set the border (corner) Radius value of the element's TopLeft corner
/// style.borderTopLeftRadius
///
/// Target VisualElement
/// TopLeft border (corner) radius value
public static T SetStyleBorderTopLeftRadius(this T target, float value) where T : VisualElement
{
target.style.borderTopLeftRadius = value;
return target;
}
/// Get the border (corner) Radius value of the element's TopLeft corner
/// style.borderTopLeftRadius
///
/// Target VisualElement
public static float GetStyleBorderTopLeftRadius(this T target) where T : VisualElement =>
target.style.borderTopLeftRadius.value.value;
#endregion
#region SetStyleBorderTopRightRadius, GetStyleBorderTopRightRadius
/// Set the border (corner) Radius value of the element's TopRight corner
/// style.borderTopRightRadius
///
/// Target VisualElement
/// TopRight border (corner) radius value
public static T SetStyleBorderTopRightRadius(this T target, float value) where T : VisualElement
{
target.style.borderTopRightRadius = value;
return target;
}
/// Get the border (corner) Radius value of the element's TopRight corner
/// style.borderTopRightRadius
///
/// Target VisualElement
public static float GetStyleBorderTopRightRadius(this T target) where T : VisualElement =>
target.style.borderTopRightRadius.value.value;
#endregion
#region SetStyleBorderBottomRightRadius, GetStyleBorderBottomRightRadius
/// Set the border (corner) Radius value of the element's BottomRight corner
/// style.borderBottomRightRadius
///
/// Target VisualElement
/// BottomRight border (corner) radius value
public static T SetStyleBorderBottomRightRadius(this T target, float value) where T : VisualElement
{
target.style.borderBottomRightRadius = value;
return target;
}
/// Get the border (corner) Radius value of the element's BottomRight corner
/// style.borderBottomRightRadius
///
/// Target VisualElement
public static float GetStyleBorderBottomRightRadius(this T target) where T : VisualElement =>
target.style.borderBottomRightRadius.value.value;
#endregion
#region SetStyleBorderBottomLeftRadius, GetStyleBorderBottomLeftRadius
/// Set the border (corner) Radius value of the element's BottomLeft corner
/// style.borderBottomLeftRadius
///
/// Target VisualElement
/// BottomLeft border (corner) radius value
public static T SetStyleBorderBottomLeftRadius(this T target, float value) where T : VisualElement
{
target.style.borderBottomLeftRadius = value;
return target;
}
/// Get the border (corner) Radius value of the element's BottomLeft corner
/// style.borderBottomLeftRadius
///
/// Target VisualElement
public static float GetStyleBorderBottomLeftRadius(this T target) where T : VisualElement =>
target.style.borderBottomLeftRadius.value.value;
#endregion
#endregion
#region Style - BackgroundColor
/// Set the background color to paint in the element's box
/// style.backgroundColor
///
/// Target VisualElement
/// Color value
public static T SetStyleBackgroundColor(this T target, Color color) where T : VisualElement
{
target.style.backgroundColor = color;
return target;
}
/// Get the background color of element's box
/// style.backgroundColor
///
/// Target VisualElement
public static Color GetStyleBackgroundColor(this T target) where T : VisualElement =>
target.resolvedStyle.backgroundColor;
#endregion
#region Style - Opacity
/// Set the transparency of an element (and its children)
/// style.opacity
///
/// Target VisualElement
/// Opacity value (0-1)
public static T SetStyleOpacity(this T target, float value) where T : VisualElement
{
target.style.opacity = value;
return target;
}
/// Get the transparency of an element (and its children)
/// style.opacity
///
/// Target VisualElement
public static float GetStyleOpacity(this T target) where T : VisualElement =>
target.style.opacity.value;
#endregion
#region Style - Height
/// Set the fixed height of an element for the layout
/// style.height
///
/// Target VisualElement
/// Height float value
public static T SetStyleHeight(this T target, float height) where T : VisualElement
{
target.style.height = height;
return target;
}
/// Set the fixed height of an element for the layout
/// style.height
///
/// Target VisualElement
/// StyleKeyword
public static T SetStyleHeight(this T target, StyleKeyword styleKeyword) where T : VisualElement
{
target.style.height = new StyleLength(styleKeyword);
return target;
}
/// Set the fixed height of an element to auto
/// style.height
///
public static T ResetStyleHeight(this T target) where T : VisualElement =>
target.SetStyleHeight(StyleKeyword.Auto);
/// Get the fixed height of an element for the layout
/// style.height
///
/// Target VisualElement
public static float GetStyleHeight(this T target) where T : VisualElement =>
target.style.height.value.value;
/// Set the fixed MinHeight, Height and MaxHeight of an element for the layout
/// style.minHeight
/// style.height
/// style.maxHeight
///
/// Target VisualElement
/// MinHeight float value
/// Height float value
/// MaxHeight float value
public static T SetStyleHeight(this T target, float minHeight, float height, float maxHeight) where T : VisualElement
{
return target.SetStyleMinHeight(minHeight)
.SetStyleHeight(height)
.SetStyleMaxHeight(maxHeight);
}
#endregion
#region Style - MinHeight
/// Set the minimum height for an element, when it is flexible or measures its own size
/// style.minHeight
///
/// Target VisualElement
/// MinHeight float value
public static T SetStyleMinHeight(this T target, float height) where T : VisualElement
{
target.style.minHeight = height;
return target;
}
/// Set the minimum height of an element, when it is flexible or measures its own size
/// style.minHeight
///
/// Target VisualElement
/// StyleKeyword
public static T SetStyleMinHeight(this T target, StyleKeyword styleKeyword) where T : VisualElement
{
target.style.minHeight = new StyleLength(styleKeyword);
return target;
}
/// Set the minimum height of an element to auto
/// style.minHeight
///
public static T ResetStyleMinHeight(this T target) where T : VisualElement =>
target.SetStyleMinHeight(StyleKeyword.Auto);
/// Get the minimum height for an element, when it is flexible or measures its own size
/// style.minHeight
///
/// Target VisualElement
public static float GetStyleMinHeight(this T target) where T : VisualElement =>
target.style.minHeight.value.value;
#endregion
#region Style - MaxHeight
/// Set the maximum height for an element, when it is flexible or measures its own size
/// style.maxHeight
///
/// Target VisualElement
/// MaxHeight float value
public static T SetStyleMaxHeight(this T target, float height) where T : VisualElement
{
target.style.maxHeight = height;
return target;
}
/// Set the maximum height of an element, when it is flexible or measures its own size
/// style.maxHeight
///
/// Target VisualElement
/// StyleKeyword
public static T SetStyleMaxHeight(this T target, StyleKeyword styleKeyword) where T : VisualElement
{
target.style.maxHeight = new StyleLength(styleKeyword);
return target;
}
/// Set the maximum height of an element to auto
/// style.maxHeight
///
public static T ResetStyleMaxHeight(this T target) where T : VisualElement =>
target.SetStyleMaxHeight(StyleKeyword.Auto);
/// Get the maximum height for an element, when it is flexible or measures its own size
/// style.maxHeight
///
/// Target VisualElement
public static float GetStyleMaxHeight(this T target) where T : VisualElement =>
target.style.maxHeight.value.value;
#endregion
#region Style - Width
/// Set the fixed width of an element for the layout
/// style.width
///
/// Target VisualElement
/// Width float value
public static T SetStyleWidth(this T target, float width) where T : VisualElement
{
target.style.width = width;
return target;
}
/// Set the fixed width of an element for the layout
/// style.width
///
/// Target VisualElement
/// StyleKeyword
public static T SetStyleWidth(this T target, StyleKeyword styleKeyword) where T : VisualElement
{
target.style.width = new StyleLength(styleKeyword);
return target;
}
/// Set the fixed width of an element to auto
/// style.width
///
public static T ResetStyleWidth(this T target) where T : VisualElement =>
target.SetStyleWidth(StyleKeyword.Auto);
/// Get the fixed width of an element for the layout
/// style.width
///
/// Target VisualElement
public static float GetStyleWidth(this T target) where T : VisualElement =>
target.style.width.value.value;
/// Set the fixed MinWidth, Width and MaxWidth of an element for the layout
/// style.minWidth
/// style.width
/// style.maxWidth
///
/// Target VisualElement
///
///
///
///
///
public static T SetStyleWidth(this T target, float minWidth, float width, float maxWidth) where T : VisualElement
{
return target.SetStyleMinWidth(minWidth)
.SetStyleWidth(width)
.SetStyleMaxWidth(maxWidth);
}
#endregion
#region Style - MinWidth
/// Set the minimum width for an element, when it is flexible or measures its own size
/// style.minWidth
///
/// Target VisualElement
/// MinWidth float value
public static T SetStyleMinWidth(this T target, float width) where T : VisualElement
{
target.style.minWidth = width;
return target;
}
/// Set the minimum width of an element, when it is flexible or measures its own size
/// style.minWidth
///
/// Target VisualElement
/// StyleKeyword
public static T SetStyleMinWidth(this T target, StyleKeyword styleKeyword) where T : VisualElement
{
target.style.minWidth = new StyleLength(styleKeyword);
return target;
}
/// Set the minimum width of an element to auto
/// style.minWidth
///
public static T ResetStyleMinWidth(this T target) where T : VisualElement =>
target.SetStyleMinWidth(StyleKeyword.Auto);
/// Get the minimum width for an element, when it is flexible or measures its own size
/// style.minWidth
///
/// Target VisualElement
public static float GetStyleMinWidth(this T target) where T : VisualElement =>
target.style.minWidth.value.value;
#endregion
#region Style - MaxWidth
/// Set the maximum width for an element, when it is flexible or measures its own size
/// style.maxWidth
///
/// Target VisualElement
/// MaxWidth float value
public static T SetStyleMaxWidth(this T target, float width) where T : VisualElement
{
target.style.maxWidth = width;
return target;
}
/// Set the maximum width of an element, when it is flexible or measures its own size
/// style.maxWidth
///
/// Target VisualElement
/// StyleKeyword
public static T SetStyleMaxWidth(this T target, StyleKeyword styleKeyword) where T : VisualElement
{
target.style.maxWidth = new StyleLength(styleKeyword);
return target;
}
/// Set the maximum width of an element to auto
/// style.maxWidth
///
public static T ResetStyleMaxWidth(this T target) where T : VisualElement =>
target.SetStyleMaxWidth(StyleKeyword.Auto);
/// Get the maximum width for an element, when it is flexible or measures its own size
/// style.maxWidth
///
/// Target VisualElement
public static float GetStyleMaxWidth(this T target) where T : VisualElement =>
target.style.maxWidth.value.value;
#endregion
#region Style - Size (Width and Height) - does not exist natively in Unity
///
/// Set the fixed values for the width and height of an element for the layout
/// style.width
/// style.height
///
/// Target VisualElement
/// Width float value
/// Height Float value
public static T SetStyleSize(this T target, float width, float height) where T : VisualElement =>
target.SetStyleWidth(width).SetStyleHeight(height);
///
/// Set the same fixed value for the width and height of an element for the layout
/// style.width
/// style.height
///
/// Target VisualElement
/// Width and Height float value
public static T SetStyleSize(this T target, float value) where T : VisualElement =>
target.SetStyleSize(value, value);
///
/// Set the same fixed value for the width and height of an element for the layout
/// style.width
/// style.height
///
/// Target VisualElement
/// StyleKeyword
public static T SetStyleSize(this T target, StyleKeyword styleKeyword) where T : VisualElement
{
target.SetStyleWidth(styleKeyword);
target.SetStyleHeight(styleKeyword);
return target;
}
///
/// Set the width and height of an element to auto
/// style.width
/// style.height
///
public static T ResetStyleSize(this T target) where T : VisualElement =>
target.SetStyleSize(StyleKeyword.Auto);
#endregion
#region Style - MinSize (MinWidth and MinHeight) - does not exist natively in Unity
///
/// Set the values for the minimum width and minimum height for an element, when it is flexible or measures its own size
/// style.minWidth
/// style.minHeight
///
/// Target VisualElement
/// MinWidth float value
/// MinHeight Float value
public static T SetStyleMinSize(this T target, float width, float height) where T : VisualElement =>
target.SetStyleMinWidth(width).SetStyleMinHeight(height);
///
/// Set the same values for minimum width and minimum height for an element, when it is flexible or measures its own size
/// style.minWidth
/// style.minHeight
///
/// Target VisualElement
/// MinWidth and MinHeight float value
public static T SetStyleMinSize(this T target, float value) where T : VisualElement =>
target.SetStyleMinWidth(value).SetStyleMinHeight(value);
///
/// Set the same values for minimum width and minimum height for an element, when it is flexible or measures its own size
/// style.minWidth
/// style.minHeight
///
/// Target VisualElement
/// StyleKeyword
public static T SetStyleMinSize(this T target, StyleKeyword styleKeyword) where T : VisualElement
{
target.SetStyleMinWidth(styleKeyword);
target.SetStyleMinHeight(styleKeyword);
return target;
}
///
/// Set the minimum width and minimum height of an element to auto
/// style.minWidth
/// style.minHeight
///
public static T ResetStyleMinSize(this T target) where T : VisualElement =>
target.SetStyleMinSize(StyleKeyword.Auto);
#endregion
#region Style - MaxSize (MaxWidth and MaxHeight) - does not exist natively in Unity
///
/// Set the values for the maximum width and maximum height for an element, when it is flexible or measures its own size
/// style.maxWidth
/// style.maxHeight
///
/// Target VisualElement
/// MaxWidth float value
/// MaxHeight Float value
public static T StyleMaxSize(this T target, float width, float height) where T : VisualElement =>
target.SetStyleMaxWidth(width).SetStyleMaxHeight(height);
///
/// Set the same values for the maximum width and maximum height for an element, when it is flexible or measures its own size
/// style.maxWidth
/// style.maxHeight
///
/// Target VisualElement
/// MaxWidth and MaxHeight float value
public static T StyleMaxSize(this T target, float value) where T : VisualElement =>
target.SetStyleMaxWidth(value).SetStyleMaxHeight(value);
///
/// Set the same values for maximum width and maximum height for an element, when it is flexible or measures its own size
/// style.maxWidth
/// style.maxHeight
///
/// Target VisualElement
/// StyleKeyword
public static T SetStyleMaxSize(this T target, StyleKeyword styleKeyword) where T : VisualElement
{
target.SetStyleMaxWidth(styleKeyword);
target.SetStyleMaxHeight(styleKeyword);
return target;
}
///
/// Set the maximum width and maximum height of an element to auto
/// style.maxWidth
/// style.maxHeight
///
public static T ResetStyleMaxSize(this T target) where T : VisualElement =>
target.SetStyleMaxSize(StyleKeyword.Auto);
#endregion
#region Style - BackgroundImage
#region Texture2D
/// Set the background image to paint in the element's box
/// Target VisualElement
/// Texture2D reference
public static T SetStyleBackgroundImage(this T target, Texture2D value) where T : VisualElement
{
target.style.backgroundImage = new StyleBackground(value);
return target;
}
/// Get the background image Texture2D used to paint in the element's box
/// Target VisualElement
public static Texture2D GetStyleBackgroundImageTexture2D(this T target) where T : VisualElement =>
target.style.backgroundImage.value.texture;
#endregion
#region VectorImage
/// Set the background image to paint in the element's box
/// Target VisualElement
/// An asset that represents a vector image
public static T SetStyleBackgroundImage(this T target, VectorImage value) where T : VisualElement
{
target.style.backgroundImage = new StyleBackground(value);
return target;
}
/// Get the background image VectorImage used to paint in the element's box
/// Target VisualElement
public static VectorImage GetStyleBackgroundImageVectorImage(this T target) where T : VisualElement =>
target.style.backgroundImage.value.vectorImage;
#endregion
#region VisualElement Background
/// Set the background image to paint in the element's box
/// Target VisualElement
/// Described a VisualElement background
public static T SetStyleBackgroundImage(this T target, Background value) where T : VisualElement
{
target.style.backgroundImage = new StyleBackground(value);
return target;
}
/// Get the background image Background value used to paint in the element's box
/// Target VisualElement
public static Background GetStyleBackgroundImageBackground(this T target) where T : VisualElement =>
target.style.backgroundImage.value;
#endregion
#region VisualElement StyleKeyword
/// Set the background image to paint in the element's box
/// Target VisualElement
/// Keyword used on style value types
public static T SetStyleBackgroundImage(this T target, StyleKeyword value) where T : VisualElement
{
target.style.backgroundImage = new StyleBackground(value);
return target;
}
/// Get the background image StyleKeyword value used to paint in the element's box
/// Target VisualElement
public static StyleKeyword GetStyleBackgroundImageStyleKeyword(this T target) where T : VisualElement =>
target.style.backgroundImage.keyword;
#endregion
#region [Editor] Path methods
/// [Editor] Set the background image to paint in the element's box
/// Target VisualElement
/// Path to the texture
public static T SetStyleBackgroundImage(this T target, string texturePath) where T : VisualElement
{
#if UNITY_EDITOR
target.SetStyleBackgroundImage(UnityEditor.AssetDatabase.LoadAssetAtPath(texturePath));
#else
Doozy.Runtime.Common.Debugger.LogWarning("This method works only in the Editor");
#endif
return target;
}
/// [Editor] Get the path to the background image Texture2D used to paint in the element's box
/// Target VisualElement
public static string GetStyleBackgroundImagePath(this T target) where T : VisualElement
{
#if UNITY_EDITOR
return UnityEditor.AssetDatabase.GetAssetPath(target.style.backgroundImage.value.texture);
#else
return "This method works only in the Editor";
#endif
}
#endregion
#endregion
#region Style - UnityBackgroundImageTintColor
/// Set the tinting color for the element's backgroundImage
/// Target VisualElement
/// Color value
public static T SetStyleBackgroundImageTintColor(this T target, Color value) where T : VisualElement
{
target.style.unityBackgroundImageTintColor = new StyleColor(value);
return target;
}
/// Get the tinting color for the element's backgroundImage
/// Target VisualElement
public static Color GetStyleBackgroundImageTintColor(this T target) where T : VisualElement =>
target.resolvedStyle.unityBackgroundImageTintColor;
/// Set the tinting color for the element's backgroundImage
/// Target VisualElement
/// Keyword used on style value types
public static T SetStyleBackgroundImageTintColor(this T target, StyleKeyword value) where T : VisualElement
{
target.style.unityBackgroundImageTintColor = new StyleColor(value);
return target;
}
/// Get the StyleKeyword value used for the element's backgroundImage
/// Target VisualElement
public static StyleKeyword GetStyleBackgroundImageTintColorStyleKeyword(this T target) where T : VisualElement =>
target.style.unityBackgroundImageTintColor.keyword;
#endregion
#region Style - UnityBackgroundScaleMode
/// Background image scaling in element's box
/// Target VisualElement
/// Keyword used on style value types
// ReSharper disable once RedundantNameQualifier
public static T SetStyleBackgroundScaleMode(this T target, UnityEngine.ScaleMode value) where T : VisualElement
{
#if UNITY_2022_2_OR_NEWER
switch (value)
{
case UnityEngine.ScaleMode.StretchToFill:
target.style.backgroundPositionX = new BackgroundPosition(BackgroundPositionKeyword.Center);
target.style.backgroundPositionY = new BackgroundPosition(BackgroundPositionKeyword.Center);
target.style.backgroundRepeat = new BackgroundRepeat(Repeat.NoRepeat, Repeat.NoRepeat);
target.style.backgroundSize = new BackgroundSize(BackgroundSizeType.Cover);
break;
case UnityEngine.ScaleMode.ScaleAndCrop:
target.style.backgroundPositionX = new BackgroundPosition(BackgroundPositionKeyword.Center);
target.style.backgroundPositionY = new BackgroundPosition(BackgroundPositionKeyword.Center);
target.style.backgroundRepeat = new BackgroundRepeat(Repeat.NoRepeat, Repeat.NoRepeat);
target.style.backgroundSize = new BackgroundSize(BackgroundSizeType.Length);
break;
case UnityEngine.ScaleMode.ScaleToFit:
target.style.backgroundPositionX = new BackgroundPosition(BackgroundPositionKeyword.Center);
target.style.backgroundPositionY = new BackgroundPosition(BackgroundPositionKeyword.Center);
target.style.backgroundRepeat = new BackgroundRepeat(Repeat.NoRepeat, Repeat.NoRepeat);
target.style.backgroundSize = new BackgroundSize(BackgroundSizeType.Contain);
break;
default:
throw new System.ArgumentOutOfRangeException(nameof(value), value, null);
}
#else
// ReSharper disable once RedundantNameQualifier
target.style.unityBackgroundScaleMode = new StyleEnum(value);
#endif
return target;
}
#endregion
#region Style - UnitySlice (Left, Top, Right, Bottom) - 9-Slice
/// Set the size of all the 9-slice's edges when painting an element's background image (Left, Top, Right, Bottom)
/// style.unitySliceLeft
/// style.unitySliceTop
/// style.unitySliceRight
/// style.unitySliceBottom
///
/// Target VisualElement
/// Left edge 9-slice
/// Top edge 9-slice
/// Right edge 9-slice
/// Bottom edge 9-slice
public static T SetStyleUnitySlice(this T target, int left, int top, int right, int bottom) where T : VisualElement
{
target.style.unitySliceLeft = left;
target.style.unitySliceTop = top;
target.style.unitySliceRight = right;
target.style.unitySliceBottom = bottom;
return target;
}
/// Set the same size of all the 9-slice's edges when painting an element's background image (Left, Top, Right, Bottom)
/// style.unitySliceLeft
/// style.unitySliceTop
/// style.unitySliceRight
/// style.unitySliceBottom
///
/// Target VisualElement
/// Slice value
public static T SetStyleUnitySlice(this T target, int value) where T : VisualElement =>
target.SetStyleUnitySlice(value, value, value, value);
/// Set the same size of all the 9-slice's edges when painting an element's background image (Left, Top, Right, Bottom)
/// style.unitySliceLeft
/// style.unitySliceTop
/// style.unitySliceRight
/// style.unitySliceBottom
///
/// Target VisualElement
/// Edge 9-slice values
public static T SetStyleUnitySlice(this T target, EdgeValues edge) where T : VisualElement =>
target.SetStyleUnitySlice((int)edge.Left, (int)edge.Top, (int)edge.Right, (int)edge.Bottom);
#region SetStyleUnitySliceLeft, GetStyleUnitySliceLeft
/// Set the size of the Left 9-slice's edge when painting an element's background image
/// style.unitySliceLeft
///
/// Target VisualElement
/// 9-Slice value
public static T SetStyleUnitySliceLeft(this T target, int value) where T : VisualElement
{
target.style.unitySliceLeft = value;
return target;
}
/// Get the size of the Left 9-slice's edge when painting an element's background image
/// style.unitySliceLeft
///
/// Target VisualElement
public static int GetStyleUnitySliceLeft(this T target) where T : VisualElement =>
target.style.unitySliceLeft.value;
#endregion
#region SetStyleUnitySliceTop, GetStyleUnitySliceTop
/// Set the size of the Top 9-slice's edge when painting an element's background image
/// style.unitySliceTop
///
/// Target VisualElement
/// 9-Slice value
public static T SetStyleUnitySliceTop(this T target, int value) where T : VisualElement
{
target.style.unitySliceTop = value;
return target;
}
/// Get the size of the Top 9-slice's edge when painting an element's background image
/// style.unitySliceTop
///
/// Target VisualElement
public static int GetStyleUnitySliceTop(this T target) where T : VisualElement =>
target.style.unitySliceTop.value;
#endregion
#region SetStyleUnitySliceRight, GetStyleUnitySliceRight
/// Set the size of the Right 9-slice's edge when painting an element's background image
/// style.unitySliceRight
///
/// Target VisualElement
/// 9-Slice value
public static T SetStyleUnitySliceRight(this T target, int value) where T : VisualElement
{
target.style.unitySliceRight = value;
return target;
}
/// Get the size of the Right 9-slice's edge when painting an element's background image
/// style.unitySliceRight
///
/// Target VisualElement
public static int GetStyleUnitySliceRight(this T target) where T : VisualElement =>
target.style.unitySliceRight.value;
#endregion
#region SetStyleUnitySliceBottom, GetStyleUnitySliceBottom
/// Set the size of the Bottom 9-slice's edge when painting an element's background image
/// style.unitySliceBottom
///
/// Target VisualElement
/// 9-Slice value
public static T SetStyleUnitySliceBottom(this T target, int value) where T : VisualElement
{
target.style.unitySliceBottom = value;
return target;
}
/// Get the size of the Bottom 9-slice's edge when painting an element's background image
/// style.unitySliceBottom
///
/// Target VisualElement
public static int GetStyleUnitySliceBottom(this T target) where T : VisualElement =>
target.style.unitySliceBottom.value;
#endregion
#endregion
#region Style - Color
///
/// Set the color to use when drawing the text of an element
/// style.color
///
/// Target VisualElement
/// Color value
public static T SetStyleColor(this T target, Color value) where T : VisualElement
{
target.style.color = value;
return target;
}
///
/// Get the color used when drawing the text of an element
/// style.color
///
/// Target VisualElement
public static Color GetStyleColor(this T target) where T : VisualElement =>
target.resolvedStyle.color;
#endregion
#region Style - Whitespace
///
/// Word wrapping over multiple lines if not enough space is available to draw the text of an element.
///
/// Target VisualElement
/// WhiteSpace value
/// Target VisualElement
/// Target VisualElement
public static T SetWhiteSpace(this T target, WhiteSpace value) where T : VisualElement
{
target.style.whiteSpace = value;
return target;
}
///
/// Get the word wrapping over multiple lines if not enough space is available to draw the text of an element.
///
/// Target VisualElement
/// Target VisualElement
/// WhiteSpace value
public static WhiteSpace GetWhiteSpace(this T target) where T : VisualElement =>
target.style.whiteSpace.value;
#endregion
#region Style - UnityFont
///
/// Set the font to draw the element's text.
///
/// Target VisualElement
/// Font value
/// Target VisualElement
/// Target VisualElement
public static T SetStyleUnityFont(this T target, Font value) where T : VisualElement
{
target.style.unityFont = value;
return target;
}
///
/// Get the font to draw the element's text.
///
/// Target VisualElement
/// Target VisualElement
/// Font value
public static Font GetStyleUnityFont(this T target) where T : VisualElement =>
target.style.unityFont.value;
#endregion
#region Style - UnityFontStyleAndWeight
///
/// Set the font style and weight (normal, bold, italic) to draw the element's text.
///
/// Target VisualElement
/// FontStyle value
/// Target VisualElement
/// Target VisualElement
public static T SetStyleUnityFontStyleAndWeight(this T target, FontStyle value) where T : VisualElement
{
target.style.unityFontStyleAndWeight = new StyleEnum(value);
return target;
}
///
/// Get the font style and weight (normal, bold, italic) used to draw the element's text.
///
/// Target VisualElement
/// Target VisualElement
/// FontStyle value
public static FontStyle GetStyleUnityFontStyleAndWeight(this T target) where T : VisualElement =>
target.style.unityFontStyleAndWeight.value;
#endregion
#region Style - FontSize
///
/// Set the font size to draw the element's text
/// style.fontSize
///
/// Target VisualElement
/// Font size value
public static T SetStyleFontSize(this T target, int value) where T : VisualElement
{
target.style.fontSize = value;
return target;
}
///
/// Get the font size used to draw the element's text
/// style.fontSize
///
/// Target VisualElement
public static float GetStyleFontSize(this T target) where T : VisualElement =>
target.style.fontSize.value.value;
#endregion
#region Style - UnityTextAlign
///
/// Set the horizontal and vertical text alignment in the element's box
/// style.unityTextAlign
///
/// Target VisualElement
/// TextAnchor value
public static T SetStyleTextAlign(this T target, TextAnchor value) where T : VisualElement
{
target.style.unityTextAlign = value;
return target;
}
///
/// Get the horizontal and vertical text alignment in the element's box
/// style.unityTextAlign
///
/// Target VisualElement
public static TextAnchor GetStyleTextAlign(this T target) where T : VisualElement =>
target.style.unityTextAlign.value;
#endregion
#region Style - Cursor
/// Mouse cursor to display when the mouse pointer is over an element
/// Target VisualElement
/// The texture to use for the cursor style. To use a texture as a cursor, import the texture with "Read/Write enabled" in the texture importer (or using the "Cursor" defaults)
/// The offset from the top left of the texture to use as the target point (must be within the bounds of the cursor)
/// VisualElement
public static T SetStyleCursor(this T target, Texture2D texture, Vector2 hotspot) where T : VisualElement
{
target.style.cursor = new Cursor()
{
texture = texture,
hotspot = hotspot
};
return target;
}
/// Mouse cursor to display when the mouse pointer is over an element (hotspot offset is calculated automatically)
/// Target VisualElement
/// The texture to use for the cursor style. To use a texture as a cursor, import the texture with "Read/Write enabled" in the texture importer (or using the "Cursor" defaults)
/// VisualElement
public static T SetStyleCursor(this T target, Texture2D texture) where T : VisualElement
{
target.style.cursor = new Cursor()
{
texture = texture,
hotspot = new Vector2(texture.width / 2f, texture.height / 2f)
};
return target;
}
/// Mouse cursor to display when the mouse pointer is over an element
/// Target VisualElement
/// Cursor
/// VisualElement
public static T SetStyleCursor(this T target, Cursor cursor) where T : VisualElement
{
target.style.cursor = new StyleCursor(cursor);
return target;
}
/// Mouse cursor to display when the mouse pointer is over an element
/// Target VisualElement
/// Style Keyword
/// VisualElement
public static T SetStyleCursor(this T target, StyleKeyword keyword) where T : VisualElement
{
target.style.cursor = new StyleCursor(keyword);
return target;
}
#endregion
#region Style - Margins
/// Set all margins to 0 (zero)
/// Target VisualElement
public static T ClearMargins(this T target) where T : VisualElement =>
target.SetStyleMargins(0);
///
/// Set the space reserved for the all the edges margins during the layout phase (Left, Top, Right, Bottom)
/// style.marginLeft
/// style.marginTop
/// style.marginRight
/// style.marginBottom
///
/// Target VisualElement
/// Left edge margin
/// Top edge margin
/// Right edge margin
/// Bottom edge margin
public static T SetStyleMargins(this T target, float left, float top, float right, float bottom) where T : VisualElement
{
target.style.marginLeft = left;
target.style.marginTop = top;
target.style.marginRight = right;
target.style.marginBottom = bottom;
return target;
}
///
/// Set the same space reserved for the all the edges margins during the layout phase (Left, Top, Right, Bottom)
/// style.marginLeft
/// style.marginTop
/// style.marginRight
/// style.marginBottom
///
/// Target VisualElement
/// Edge margin value
public static T SetStyleMargins(this T target, float value) where T : VisualElement =>
target.SetStyleMargins(value, value, value, value);
///
/// Set the same space reserved for the all the edges margins during the layout phase (Left, Top, Right, Bottom)
/// style.marginLeft
/// style.marginTop
/// style.marginRight
/// style.marginBottom
///
/// Target VisualElement
/// Edge values for margins
public static T SetStyleMargins(this T target, EdgeValues edge) where T : VisualElement =>
target.SetStyleMargins(edge.Left, edge.Top, edge.Right, edge.Bottom);
#region SetStyleMarginLeft, GetStyleMarginLeft
/// Set the space reserved for the Left edge margin during the layout phase
/// style.marginLeft
///
/// Target VisualElement
/// Left edge margin value
public static T SetStyleMarginLeft(this T target, float value) where T : VisualElement
{
target.style.marginLeft = value;
return target;
}
/// Get the space reserved for the Left edge margin during the layout phase
/// style.marginLeft
///
/// Target VisualElement
public static float GetStyleMarginLeft(this T target) where T : VisualElement =>
target.style.marginLeft.value.value;
#endregion
#region SetStyleMarginTop, GetStyleMarginTop
/// Set the space reserved for the Top edge margin during the layout phase
/// style.marginTop
///
/// Target VisualElement
/// Top edge margin value
public static T SetStyleMarginTop(this T target, float value) where T : VisualElement
{
target.style.marginTop = value;
return target;
}
/// Get the space reserved for the Top edge margin during the layout phase
/// style.marginTop
///
/// Target VisualElement
public static float GetStyleMarginTop(this T target) where T : VisualElement =>
target.style.marginTop.value.value;
#endregion
#region SetStyleMarginRight, GetStyleMarginRight
/// Set the space reserved for the Right edge margin during the layout phase
/// style.marginRight
///
/// Target VisualElement
/// Right edge margin value
public static T SetStyleMarginRight(this T target, float value) where T : VisualElement
{
target.style.marginRight = value;
return target;
}
/// Get the space reserved for the Right edge margin during the layout phase
/// style.marginRight
///
/// Target VisualElement
public static float GetStyleMarginRight(this T target) where T : VisualElement =>
target.style.marginRight.value.value;
#endregion
#region SetStyleMarginBottom, GetStyleMarginBottom
/// Set the space reserved for the Bottom edge margin during the layout phase
/// style.marginBottom
///
/// Target VisualElement
/// Bottom edge margin value
public static T SetStyleMarginBottom(this T target, float value) where T : VisualElement
{
target.style.marginBottom = value;
return target;
}
/// Get the space reserved for the Bottom edge margin during the layout phase
/// style.marginBottom
///
/// Target VisualElement
public static float GetStyleMarginBottom(this T target) where T : VisualElement =>
target.style.marginBottom.value.value;
#endregion
#endregion
#region Style - Padding
/// Set all paddings to 0 (zero)
/// Target VisualElement
public static T ClearPaddings(this T target) where T : VisualElement =>
target.SetStylePadding(0f);
///
/// Set the space reserved for all the edges paddings during the layout phase
/// style.paddingLeft
/// style.paddingTop
/// style.paddingRight
/// style.paddingBottom
///
/// Target VisualElement
/// Left edge padding
/// Top edge padding
/// Right edge padding
/// Bottom edge padding
public static T SetStylePadding(this T target, float left, float top, float right, float bottom) where T : VisualElement
{
target.style.paddingLeft = left;
target.style.paddingTop = top;
target.style.paddingRight = right;
target.style.paddingBottom = bottom;
return target;
}
///
/// Set the same space reserved for all the edges paddings during the layout phase
/// style.paddingLeft
/// style.paddingTop
/// style.paddingRight
/// style.paddingBottom
///
/// Target VisualElement
/// Edge padding value
public static T SetStylePadding(this T target, float value) where T : VisualElement =>
target.SetStylePadding(value, value, value, value);
///
/// Set the space reserved for all the edges paddings during the layout phase
/// style.paddingLeft
/// style.paddingTop
/// style.paddingRight
/// style.paddingBottom
///
/// Target VisualElement
/// Edge values for padding
public static T SetStylePadding(this T target, EdgeValues edge) where T : VisualElement =>
target.SetStylePadding(edge.Left, edge.Top, edge.Right, edge.Bottom);
#region SetStylePaddingLeft, GetStylePaddingLeft
/// Set the space reserved for the Left edge padding during the layout phase
/// style.paddingLeft
///
/// Target VisualElement
/// Left edge padding value
public static T SetStylePaddingLeft(this T target, float value) where T : VisualElement
{
target.style.paddingLeft = value;
return target;
}
/// Get the space reserved for the Left edge padding during the layout phase
/// style.paddingLeft
///
/// Target VisualElement
public static float GetStylePaddingLeft(this T target) where T : VisualElement =>
target.style.paddingLeft.value.value;
#endregion
#region SetStylePaddingTop, GetStylePaddingTop
/// Set the space reserved for the Top edge padding during the layout phase
/// style.paddingTop
///
/// Target VisualElement
/// Top edge padding value
public static T SetStylePaddingTop(this T target, float value) where T : VisualElement
{
target.style.paddingTop = value;
return target;
}
/// Get the space reserved for the Top edge padding during the layout phase
/// style.paddingTop
///
/// Target VisualElement
public static float GetStylePaddingTop(this T target) where T : VisualElement =>
target.style.paddingTop.value.value;
#endregion
#region SetStylePaddingRight, GetStylePaddingRight
/// Set the space reserved for the Right edge padding during the layout phase
/// style.paddingRight
///
/// Target VisualElement
/// Right edge padding value
public static T SetStylePaddingRight(this T target, float value) where T : VisualElement
{
target.style.paddingRight = value;
return target;
}
/// Get the space reserved for the Right edge padding during the layout phase
/// style.paddingRight
///
/// Target VisualElement
public static float GetStylePaddingRight(this T target) where T : VisualElement =>
target.style.paddingRight.value.value;
#endregion
#region SetStylePaddingBottom, GetStylePaddingBottom
/// Set the space reserved for the Bottom edge padding during the layout phase
/// style.paddingBottom
///
/// Target VisualElement
/// Bottom edge padding value
public static T SetStylePaddingBottom(this T target, float value) where T : VisualElement
{
target.style.paddingBottom = value;
return target;
}
/// Get the space reserved for the Bottom edge padding during the layout phase
/// style.paddingBottom
///
/// Target VisualElement
public static float GetStylePaddingBottom(this T target) where T : VisualElement =>
target.style.paddingBottom.value.value;
#endregion
#endregion
#region Resize
///
/// Resize the element proportionately to the given width
/// Adjusts the height automatically
///
/// Target VisualElement
/// Target width
public static T ResizeToWidth(this T target, float referenceWidth) where T : VisualElement
{
float width = target.resolvedStyle.width;
float height = target.resolvedStyle.height;
float ratio = referenceWidth / width;
width = referenceWidth;
height *= ratio;
return target.SetStyleSize(width, height);
}
///
/// Resize the element proportionately to the given height
/// Adjusts the width automatically
///
/// Target VisualElement
/// Target height
public static T ResizeToHeight(this T target, float referenceHeight) where T : VisualElement
{
float width = target.resolvedStyle.width;
float height = target.resolvedStyle.height;
float ratio = referenceHeight / height;
height = referenceHeight;
width *= ratio;
return target.SetStyleSize(width, height);
}
///
/// Resize the element, to the given texture size, proportionately to the given width
/// Adjusts the height automatically
///
/// Target VisualElement
/// Target Texture
/// Target width
public static T ResizeToTextureWidth(this T target, Texture texture, float referenceWidth) where T : VisualElement
{
if (texture == null)
return target;
float width = texture.width;
float height = texture.height;
float ratio = referenceWidth / width;
width = referenceWidth;
height *= ratio;
return target.SetStyleSize(width, height);
}
///
/// Resize the element, to the given texture size, proportionately to the given height
/// Adjusts the width automatically
///
/// Target VisualElement
/// Target Texture
/// /// Target height
public static T ResizeToTextureHeight(this T target, Texture texture, float referenceHeight) where T : VisualElement
{
if (texture == null)
return target;
float width = texture.width;
float height = texture.height;
float ratio = referenceHeight / height;
height = referenceHeight;
width *= ratio;
return target.SetStyleSize(width, height);
}
///
/// Resize the element, to its own background image texture size, proportionately to the given width
/// Adjusts the height automatically
///
/// Target VisualElement
/// Target width
public static T ResizeToTextureWidth(this T target, float referenceWidth) where T : VisualElement =>
target.ResizeToTextureWidth(target.GetStyleBackgroundImageTexture2D(), referenceWidth);
///
/// Resize the element, to its own background image texture size, proportionately to the given height
/// Adjusts the width automatically
///
/// Target VisualElement
/// /// Target height
public static T ResizeToTextureHeight(this T target, float referenceHeight) where T : VisualElement =>
target.ResizeToTextureHeight(target.GetStyleBackgroundImageTexture2D(), referenceHeight);
/// Resize the element to the given texture size and apply a given ratio (to make it bigger or smaller proportionately)
/// Target VisualElement
/// Target Texture
/// Size change ratio
public static T ResizeToTextureSize(this T target, Texture texture, float ratio = 1) where T : VisualElement
{
bool hasTexture = texture != null;
float width = hasTexture ? texture.width : 0f;
float height = hasTexture ? texture.height : 0f;
ratio = Mathf.Max(0, ratio);
width *= ratio;
height *= ratio;
return target.SetStyleSize(width, height);
}
/// Resize the element to its own background image texture size and apply a given ratio (to make it bigger or smaller proportionately)
/// Target VisualElement
/// Size change ratio
public static T ResizeToTextureSize(this T target, float ratio = 1) where T : VisualElement =>
target.ResizeToTextureSize(target.GetStyleBackgroundImageTexture2D(), ratio);
#endregion
/// Reset all relevant layout settings - Margins, Paddings, Distances - by setting them all to 0 (zero)
/// Target VisualElement
public static T ResetLayout(this T target) where T : VisualElement =>
target
.SetStyleDisplay(DisplayStyle.Flex)
.ResetStyleMinSize()
.ResetStyleSize()
.ResetStyleMaxSize()
.ClearMargins()
.ClearPaddings()
.ClearDistances();
#region Style - Display
///
/// Set how an element is displayed in the layout
/// style.display
///
/// Target VisualElement
/// DisplayStyle value
public static T SetStyleDisplay(this T target, DisplayStyle value) where T : VisualElement
{
target.style.display = value;
return target;
}
///
/// Get how an element is displayed in the layout
/// style.display
///
/// Target VisualElement
public static DisplayStyle GetStyleDisplay(this T target) where T : VisualElement =>
target.style.display.value;
///
/// Set the element to display normally
/// style.display
/// DisplayStyle.Flex
///
/// Target VisualElement
public static T Show(this T target) where T : VisualElement =>
target.SetStyleDisplay(DisplayStyle.Flex);
///
/// Set the element as not visible and absent from the layout
/// style.display
/// DisplayStyle.None
///
/// Target VisualElement
public static T Hide(this T target) where T : VisualElement =>
target.SetStyleDisplay(DisplayStyle.None);
#endregion
#region Style - Left, Top, Right, Bottom - Distance from the element's box
/// Set all distances Left, Top, Right, Bottom to 0 (zero)
/// Target VisualElement
public static T ClearDistances(this T target) where T : VisualElement =>
target.SetStyleDistance(0, 0, 0, 0);
#region Distance - set all Left, Top, Right and Bottom
///
/// Set the Left distance from the element's box during layout
/// style.left
/// style.top
/// style.right
/// style.bottom
///
/// Target VisualElement
/// Left distance
/// Top distance
/// Right distance
/// Bottom distance
public static T SetStyleDistance(this T target, float left, float top, float right, float bottom) where T : VisualElement
{
target.style.left = left;
target.style.top = top;
target.style.right = right;
target.style.bottom = bottom;
return target;
}
#endregion
#region Left
///
/// Set the Left distance from the element's box during layout
/// style.left
///
/// Target VisualElement
/// Left distance
public static T SetStyleLeft(this T target, float value) where T : VisualElement
{
target.style.left = value;
return target;
}
///
/// Get the Left distance from the element's box during layout
/// style.left
///
/// Target VisualElement
public static float GetStyleLeft(this T target) where T : VisualElement =>
target.style.left.value.value;
#endregion
#region Top
///
/// Set the Top distance from the element's box during layout
/// style.left
///
/// Target VisualElement
/// Top distance
public static T SetStyleTop(this T target, float value) where T : VisualElement
{
target.style.top = value;
return target;
}
///
/// Get the Top distance from the element's box during layout
/// style.left
///
/// Target VisualElement
public static float GetStyleTop(this T target) where T : VisualElement =>
target.style.top.value.value;
#endregion
#region Right
///
/// Set the Right distance from the element's box during layout
/// style.left
///
/// Target VisualElement
/// Right distance
public static T SetStyleRight(this T target, float value) where T : VisualElement
{
target.style.right = value;
return target;
}
///
/// Get the Right distance from the element's box during layout
/// style.left
///
/// Target VisualElement
public static float GetStyleRight(this T target) where T : VisualElement =>
target.style.right.value.value;
#endregion
#region Bottom
///
/// Set the Bottom distance from the element's box during layout
/// style.left
///
/// Target VisualElement
/// Bottom distance
public static T SetStyleBottom(this T target, float value) where T : VisualElement
{
target.style.bottom = value;
return target;
}
///
/// Get the Bottom distance from the element's box during layout
/// style.left
///
/// Target VisualElement
public static float GetStyleBottom(this T target) where T : VisualElement =>
target.style.bottom.value.value;
#endregion
#endregion
///
/// Returns true if the parent is not null
/// parent != null
///
/// Target VisualElement
public static bool HasParent(this T target) where T : VisualElement =>
target.parent != null;
///
/// Returns TRUE if the parent is not null and if style.display is set to DisplayStyle.Flex
/// parent != null
/// target.style.display.value == DisplayStyle.Flex
///
/// Target VisualElement
public static bool IsVisible(this T target) where T : VisualElement =>
target.HasParent() && target.style.display.value == DisplayStyle.Flex;
///
/// Set the VisualElement to the enabled state. A disabled VisualElement does not receive most events
/// SetEnabled(true)
///
/// Target VisualElement
public static T EnableElement(this T target) where T : VisualElement
{
target.SetEnabled(true);
return target;
}
///
/// Set the VisualElement to the disabled state. A disabled VisualElement does not receive most events
/// SetEnabled(false)
///
/// Target VisualElement
public static T DisableElement(this T target) where T : VisualElement
{
target.SetEnabled(false);
return target;
}
///
/// Returns TRUE if the VisualElement is enabled locally and if is also enabled in its own hierarchy
/// enabledSelf and enabledInHierarchy
///
/// Target VisualElement
public static bool IsEnabled(this T target) where T : VisualElement =>
target.enabledSelf && target.enabledInHierarchy;
///
/// Replace this VisualElement with another one (while preserving its place in the hierarchy and its children).
/// Returns a reference to the other VisualElement
///
/// Target VisualElement
/// VisualElement that will replace the current one (
// ReSharper disable once InconsistentNaming
public static O ReplaceWith(this T target, O other) where T : VisualElement where O : VisualElement
{
if (target.childCount > 0) //check if the target has children
{
//target has children -> move them to the replacer element
var children = new List(target.Children());
foreach (VisualElement child in children)
other.AddChild(child);
}
if (!target.HasParent()) return other;
VisualElement parent = target.parent; //get the target parent
int targetIndexInParentHierarchy = parent.IndexOf(target); //get the 'child index' of the target inside the parent's hierarchy
target.RemoveFromHierarchy(); //remove the target from its parent hierarchy
parent.Insert(targetIndexInParentHierarchy, other); //insert the element back to the parent at the initial target's index
return other; //return a reference to the element
}
public static bool IsFocused(this T target) where T : VisualElement =>
target.focusable && target.focusController.focusedElement == target;
}
}