using System; using Unity.Collections; namespace LitMotion { /// /// Provides additional Bind methods for MotionBuilder. /// public static class MotionBuilderExtensions { /// /// Create motion and bind it to a specific object. Unlike the regular Bind method, it avoids allocation by closure by passing an object. /// /// Type of state /// Motion state /// Action that handles binding /// Handle of the created motion data. public unsafe static MotionHandle Bind(this MotionBuilder builder, TState state, Action action) where TValue : unmanaged where TOptions : unmanaged, IMotionOptions where TAdapter : unmanaged, IMotionAdapter where TState : struct { return builder.Bind(Box.Create(state), action, (value, state, action) => action(value, state.Value)); } /// /// 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 { var options = builder.buffer.Options; options.RoundingMode = roundingMode; builder.buffer.Options = options; 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 { var options = builder.buffer.Options; options.Frequency = frequency; builder.buffer.Options = options; 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 { var options = builder.buffer.Options; options.DampingRatio = dampingRatio; builder.buffer.Options = options; 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 { var options = builder.buffer.Options; options.Frequency = frequency; builder.buffer.Options = options; 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 { var options = builder.buffer.Options; options.DampingRatio = dampingRatio; builder.buffer.Options = options; 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 { var options = builder.buffer.Options; options.RandomSeed = seed; builder.buffer.Options = options; 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 { var options = builder.buffer.Options; options.RichTextEnabled = richTextEnabled; builder.buffer.Options = options; 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 { var options = builder.buffer.Options; options.RandomSeed = seed; builder.buffer.Options = options; 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."); var options = builder.buffer.Options; options.ScrambleMode = scrambleMode; builder.buffer.Options = options; 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 { var options = builder.buffer.Options; options.ScrambleMode = ScrambleMode.Custom; options.CustomScrambleChars = customScrambleChars; builder.buffer.Options = options; return builder; } } }