Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: WithBindOnSchedule #127

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/LitMotion/Assets/LitMotion/Runtime/MotionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static void Return(MotionBuilderBuffer<TValue, TOptions> buffer)
buffer.AnimationCurve = default;
buffer.Scheduler = default;
buffer.IsPreserved = default;
buffer.BindOnSchedule = default;

if (buffer.Version != ushort.MaxValue)
{
Expand All @@ -47,6 +48,7 @@ public static void Return(MotionBuilderBuffer<TValue, TOptions> buffer)
public ushort Version;
public MotionBuilderBuffer<TValue, TOptions> NextNode;
public bool IsPreserved;
public bool BindOnSchedule;

public MotionData<TValue, TOptions> Data = MotionData<TValue, TOptions>.Default;
public MotionCallbackData CallbackData = MotionCallbackData.Default;
Expand Down Expand Up @@ -187,6 +189,19 @@ public readonly MotionBuilder<TValue, TOptions, TAdapter> WithCancelOnError(bool
return this;
}

/// <summary>
/// Bind values when scheduling the motion.
/// </summary>
/// <param name="bindOnSchedule">Whether to bind on sheduling</param>
/// <returns>This builder to allow chaining multiple method calls.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly MotionBuilder<TValue, TOptions, TAdapter> WithBindOnSchedule(bool bindOnSchedule = true)
{
CheckBuffer();
buffer.BindOnSchedule = bindOnSchedule;
return this;
}

/// <summary>
/// Specifies the scheduler that schedule the motion.
/// </summary>
Expand Down Expand Up @@ -299,6 +314,22 @@ public readonly MotionBuilder<TValue, TOptions, TAdapter> Preserve()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal MotionHandle Schedule(IMotionScheduler scheduler, ref MotionData<TValue, TOptions> data, ref MotionCallbackData callbackData)
{
if (buffer.BindOnSchedule && callbackData.UpdateAction != null)
{
callbackData.InvokeUnsafe(
default(TAdapter).Evaluate(
ref data.StartValue,
ref data.EndValue,
ref data.Options,
new() { Progress = data.Core.Ease switch
{
Ease.CustomAnimationCurve => data.Core.AnimationCurve.Evaluate(0f),
_ => EaseUtility.Evaluate(0f, data.Core.Ease)
}
}
));
}

MotionHandle handle;

if (scheduler == null)
Expand Down
47 changes: 47 additions & 0 deletions src/LitMotion/Assets/LitMotion/Tests/Runtime/BindOnScheduleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using NUnit.Framework;
using UnityEngine;

namespace LitMotion.Tests.Runtime
{
public class BindOnScheduleTest
{
[Test]
public void Test_BindOnSchedule()
{
var value = 0f;

var motion = LMotion.Create(1f, 0f, 1f)
.Bind(x => value = x);

motion.Cancel();

Assert.That(value, Is.EqualTo(0f));

value = 0f;

motion = LMotion.Create(1f, 0f, 1f)
.WithBindOnSchedule()
.Bind(x => value = x);

motion.Cancel();

Assert.That(value, Is.EqualTo(1f));
}

[Test]
public void Test_BindOnSchedule_AnimationCurve()
{
var curve = AnimationCurve.EaseInOut(0f, 1f, 1f, 0f);

var value = 0f;
var motion = LMotion.Create(0f, 1f, 1f)
.WithEase(curve)
.WithBindOnSchedule()
.Bind(x => value = x);

motion.Cancel();

Assert.That(value, Is.EqualTo(1f));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.