#if LITMOTION_SUPPORT_UIELEMENTS
using Unity.Collections;
using UnityEngine;
using UnityEngine.UIElements;
#if LITMOTION_SUPPORT_ZSTRING
using Cysharp.Text;
#endif
namespace LitMotion.Extensions
{
///
/// Provides binding extension methods for UIElements.
///
public static class LitMotionUIToolkitExtensions
{
#region VisualElement
///
/// Create a motion data and bind it to VisualElement.style.left
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleLeft(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.left = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.right
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleRight(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.right = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.top
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleTop(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.top = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.bottom
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleBottom(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.bottom = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.width
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleWidth(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.width = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.height
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleHeight(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.height = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.color
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleColor(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.color = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.color.r
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleColorR(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
var c = target.style.color.value;
c.r = x;
target.style.color = c;
});
}
///
/// Create a motion data and bind it to VisualElement.style.color.g
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleColorG(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
var c = target.style.color.value;
c.g = x;
target.style.color = c;
});
}
///
/// Create a motion data and bind it to VisualElement.style.color.b
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleColorB(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
var c = target.style.color.value;
c.b = x;
target.style.color = c;
});
}
///
/// Create a motion data and bind it to VisualElement.style.color.a
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleColorA(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
var c = target.style.color.value;
c.a = x;
target.style.color = c;
});
}
///
/// Create a motion data and bind it to VisualElement.style.backgroundColor
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleBackgroundColor(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.backgroundColor = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.backgroundColor.r
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleBackgroundColorR(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
var c = target.style.backgroundColor.value;
c.r = x;
target.style.backgroundColor = c;
});
}
///
/// Create a motion data and bind it to VisualElement.style.backgroundColor.g
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleBackgroundColorG(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
var c = target.style.backgroundColor.value;
c.g = x;
target.style.backgroundColor = c;
});
}
///
/// Create a motion data and bind it to VisualElement.style.backgroundColor.b
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleBackgroundColorB(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
var c = target.style.backgroundColor.value;
c.b = x;
target.style.backgroundColor = c;
});
}
///
/// Create a motion data and bind it to VisualElement.style.backgroundColor.a
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleBackgroundColorA(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
var c = target.style.backgroundColor.value;
c.a = x;
target.style.backgroundColor = c;
});
}
///
/// Create a motion data and bind it to VisualElement.style.opacity
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleOpacity(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.opacity = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.fontSize
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleFontSize(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.fontSize = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.wordSpacing
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleWordSpacing(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.wordSpacing = x;
});
}
///
/// Create a motion data and bind it to VisualElement.style.translate
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleTranslate(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.translate = new Translate(x.x, x.y, x.z);
});
}
///
/// Create a motion data and bind it to VisualElement.style.translate
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleTranslate(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.translate = new Translate(x.x, x.y);
});
}
///
/// Create a motion data and bind it to VisualElement.style.rotate
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleRotate(this MotionBuilder builder, VisualElement visualElement, AngleUnit angleUnit = AngleUnit.Degree)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, (x, target) =>
{
target.style.rotate = new Rotate(new Angle(x, angleUnit));
});
}
///
/// Create a motion data and bind it to VisualElement.style.scale
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleScale(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.scale = new Scale(x);
});
}
///
/// Create a motion data and bind it to VisualElement.style.transformOrigin
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleTransformOrigin(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.transformOrigin = new TransformOrigin(x.x, x.y, x.z);
});
}
///
/// Create a motion data and bind it to VisualElement.style.transformOrigin
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToStyleTransformOrigin(this MotionBuilder builder, VisualElement visualElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(visualElement);
return builder.BindWithState(visualElement, static (x, target) =>
{
target.style.transformOrigin = new TransformOrigin(x.x, x.y);
});
}
#endregion
#region AbstractProgressBar
///
/// Create a motion data and bind it to AbstractProgressBar.value
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToProgressBar(this MotionBuilder builder, AbstractProgressBar progressBar)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(progressBar);
return builder.BindWithState(progressBar, static (x, target) =>
{
target.value = x;
});
}
#endregion
#region TextElement
///
/// Create a motion data and bind it to TextElement.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, static (x, target) =>
{
target.text = x.ConvertToString();
});
}
///
/// Create a motion data and bind it to TextElement.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, static (x, target) =>
{
target.text = x.ConvertToString();
});
}
///
/// Create a motion data and bind it to TextElement.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, static (x, target) =>
{
target.text = x.ConvertToString();
});
}
///
/// Create a motion data and bind it to TextElement.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, static (x, target) =>
{
target.text = x.ConvertToString();
});
}
///
/// Create a motion data and bind it to TextElement.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, static (x, target) =>
{
target.text = x.ConvertToString();
});
}
///
/// Create a motion data and bind it to Text.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, static (x, target) =>
{
target.text = x.ToString();
});
}
///
/// Create a motion data and bind it to Text.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Format string
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement, string format)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, format, static (x, target, f) =>
{
#if LITMOTION_SUPPORT_ZSTRING
target.text = ZString.Format(f, x);
#else
target.text = string.Format(f, x);
#endif
});
}
///
/// Create a motion data and bind it to Text.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, static (x, target) =>
{
target.text = x.ToString();
});
}
///
/// Create a motion data and bind it to Text.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Format string
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement, string format)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, format, static (x, target, format) =>
{
#if LITMOTION_SUPPORT_ZSTRING
target.text = ZString.Format(format, x);
#else
target.text = string.Format(format, x);
#endif
});
}
///
/// Create a motion data and bind it to Text.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, static (x, target) =>
{
target.text = x.ToString();
});
}
///
/// Create a motion data and bind it to Text.text
///
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
///
/// Format string
/// Handle of the created motion data.
public static MotionHandle BindToText(this MotionBuilder builder, TextElement textElement, string format)
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(textElement);
return builder.BindWithState(textElement, format, static (x, target, format) =>
{
#if LITMOTION_SUPPORT_ZSTRING
target.text = ZString.Format(format, x);
#else
target.text = string.Format(format, x);
#endif
});
}
#endregion
}
}
#endif