using System;
using Unity.Collections;
using UnityEngine;
namespace LitMotion
{
///
/// Provides additional Bind methods for MotionBuilder.
///
public static class MotionBuilderExtensions
{
///
/// Create a motion data and bind it to IProgress
///
/// The type of value to animate
/// The type of special parameters given to the motion data
/// The type of adapter that support value animation
/// This builder
/// Target object that implements IProgress
/// Handle of the created motion data.
public static MotionHandle BindToProgress(this MotionBuilder builder, IProgress progress)
where TValue : unmanaged
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(progress);
return builder.BindWithState(progress, static (x, progress) => progress.Report(x));
}
///
/// Create a motion data and bind it to Debug.unityLogger
///
/// The type of value to animate
/// 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 BindToUnityLogger(this MotionBuilder builder)
where TValue : unmanaged
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
return builder.Bind(static x => Debug.unityLogger.Log(x));
}
///
/// Create a motion data and bind it to UnityEngine.ILogger
///
/// The type of value to animate
/// 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 BindToUnityLogger(this MotionBuilder builder, ILogger logger)
where TValue : unmanaged
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Error.IsNull(logger);
return builder.BindWithState(logger, static (x, logger) => logger.Log(x));
}
///
/// Specifies the rounding format for decimal values when animating integer types.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Rounding mode
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithRoundingMode(this MotionBuilder builder, RoundingMode roundingMode)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
builder.buffer.Data.Options.RoundingMode = roundingMode;
return builder;
}
///
/// Specify the frequency of vibration.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Frequency
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithFrequency(this MotionBuilder builder, int frequency)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
builder.buffer.Data.Options.Frequency = frequency;
return builder;
}
///
/// Specify the vibration damping ratio.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Damping ratio
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithDampingRatio(this MotionBuilder builder, float dampingRatio)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
builder.buffer.Data.Options.DampingRatio = dampingRatio;
return builder;
}
///
/// Specify the frequency of vibration.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Frequency
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithFrequency(this MotionBuilder builder, int frequency)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
builder.buffer.Data.Options.Frequency = frequency;
return builder;
}
///
/// Specify the vibration damping ratio.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Damping ratio
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithDampingRatio(this MotionBuilder builder, float dampingRatio)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
builder.buffer.Data.Options.DampingRatio = dampingRatio;
return builder;
}
///
/// Specify the random number seed that determines the shake motion value.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Random number seed
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithRandomSeed(this MotionBuilder builder, uint seed)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
builder.buffer.Data.Options.RandomState = new Unity.Mathematics.Random(seed);
return builder;
}
///
/// Enable support for Rich Text tags.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Whether to support Rich Text tags
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithRichText(this MotionBuilder builder, bool richTextEnabled = true)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
builder.buffer.Data.Options.RichTextEnabled = richTextEnabled;
return builder;
}
///
/// Specify the random number seed used to display scramble characters.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Rrandom number seed
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithRandomSeed(this MotionBuilder builder, uint seed)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
builder.buffer.Data.Options.RandomState = new Unity.Mathematics.Random(seed);
return builder;
}
///
/// Fill in the parts that are not yet displayed with random strings.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Type of characters used for blank padding
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithScrambleChars(this MotionBuilder builder, ScrambleMode scrambleMode)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
if (scrambleMode == ScrambleMode.Custom) throw new ArgumentException("ScrambleMode.Custom cannot be specified explicitly. Use WithScrambleMode(FixedString64Bytes) instead.");
builder.buffer.Data.Options.ScrambleMode = scrambleMode;
return builder;
}
///
/// Fill in the parts that are not yet displayed with random strings.
///
/// The type of value to animate
/// The type of adapter that support value animation
/// This builder
/// Characters used for blank padding
/// This builder to allow chaining multiple method calls.
public static MotionBuilder WithScrambleChars(this MotionBuilder builder, FixedString64Bytes customScrambleChars)
where TValue : unmanaged
where TAdapter : unmanaged, IMotionAdapter
{
builder.buffer.Data.Options.ScrambleMode = ScrambleMode.Custom;
builder.buffer.Data.Options.CustomScrambleChars = customScrambleChars;
return builder;
}
}
}