Skip to content

Commit

Permalink
Merge pull request #127 from AnnulusGames/feature-with-bind-on-schedule
Browse files Browse the repository at this point in the history
Add: WithBindOnSchedule
  • Loading branch information
AnnulusGames committed May 12, 2024
2 parents b2bcc26 + 96872fe commit ee6b3f4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
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.

0 comments on commit ee6b3f4

Please sign in to comment.