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

Remove WithIgnoreTimeScale, Change scheduler specifications. #43

Merged
merged 12 commits into from
Jan 15, 2024
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using UnityEditor;

namespace LitMotion.Editor
{
internal sealed class EditorUpdateMotionScheduler : IMotionScheduler
{
public MotionHandle Schedule<TValue, TOptions, TAdapter>(in MotionData<TValue, TOptions> data, in MotionCallbackData callbackData)
public double Time => EditorApplication.timeSinceStartup;

public MotionHandle Schedule<TValue, TOptions, TAdapter>(ref MotionData<TValue, TOptions> data, ref MotionCallbackData callbackData)
where TValue : unmanaged
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
{
return MotionDispatcher.Schedule<TValue, TOptions, TAdapter>(data, callbackData, UpdateMode.EditorApplicationUpdate);
return EditorMotionDispatcher.Schedule<TValue, TOptions, TAdapter>(data, callbackData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static IObservable<TValue> ToObservable<TValue, TOptions, TAdapter>(this
var scheduler = builder.buffer.Scheduler;
var entity = builder.BuildMotionData();

builder.Schedule(scheduler, entity, callbacks);
builder.Schedule(scheduler, ref entity, ref callbacks);
return subject;
}

Expand Down
17 changes: 16 additions & 1 deletion src/LitMotion/Assets/LitMotion/Runtime/IMotionScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@ public interface IMotionScheduler
/// <param name="data">Motion data</param>
/// <param name="callbackData">Motion callback data</param>
/// <returns>Motion handle</returns>
MotionHandle Schedule<TValue, TOptions, TAdapter>(in MotionData<TValue, TOptions> data, in MotionCallbackData callbackData)
MotionHandle Schedule<TValue, TOptions, TAdapter>(ref MotionData<TValue, TOptions> data, ref MotionCallbackData callbackData)
where TValue : unmanaged
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>;

/// <summary>
/// Returns the current time.
/// </summary>
double Time { get; }
}

/// <summary>
/// Type of time used to play the motion
/// </summary>
public enum MotionTimeKind : byte
{
Time = 0,
UnscaledTime = 1,
Realtime = 2
}
}
4 changes: 2 additions & 2 deletions src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public struct MotionData<TValue, TOptions>
{
public MotionStatus Status;

public float Time;
public double StartTime;
public float Duration;

public Ease Ease;
public bool IgnoreTimeScale;
public MotionTimeKind TimeKind;
public float Delay;
public int Loops;
public LoopType LoopType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LitMotion
{
internal interface IUpdateRunner
{
public void Update(float deltaTime, float unscaledDeltaTime);
public void Update(double time, double unscaledTime, double realtime);
public void Reset();
}

Expand All @@ -24,7 +24,7 @@ public UpdateRunner(MotionStorage<TValue, TOptions, TAdapter> storage)

readonly MotionStorage<TValue, TOptions, TAdapter> storage;

public unsafe void Update(float deltaTime, float unscaledDeltaTime)
public unsafe void Update(double time, double unscaledTime, double realtime)
{
var count = storage.Count;
using var output = new NativeArray<TValue>(count, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
Expand All @@ -36,8 +36,9 @@ public unsafe void Update(float deltaTime, float unscaledDeltaTime)
var job = new MotionUpdateJob<TValue, TOptions, TAdapter>()
{
DataPtr = dataPtr,
DeltaTime = deltaTime,
UnscaledDeltaTime = unscaledDeltaTime,
Time = time,
UnscaledTime = unscaledTime,
Realtime = realtime,
Output = output,
CompletedIndexList = completedIndexList.AsParallelWriter()
};
Expand Down
19 changes: 16 additions & 3 deletions src/LitMotion/Assets/LitMotion/Runtime/ManualMotionDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static MotionStorage<TValue, TOptions, TAdapter> GetOrCreate()

static readonly MinimumList<IUpdateRunner> updateRunners = new();

/// <summary>
/// ManualMotionDispatcher time. It increases every time Update is called.
/// </summary>
public static double Time { get; set; }

/// <summary>
/// Ensures the storage capacity until it reaches at least `capacity`.
/// </summary>
Expand All @@ -45,14 +50,22 @@ public static void EnsureStorageCapacity<TValue, TOptions, TAdapter>(int capacit
/// Update all scheduled motions with MotionScheduler.Manual
/// </summary>
/// <param name="deltaTime">Delta time</param>
public static void Update(float deltaTime)
public static void Update(double deltaTime)
{
if (deltaTime < 0f) throw new ArgumentException("deltaTime must be 0 or higher.");

Time += deltaTime;
Update();
}

/// <summary>
/// Update all scheduled motions with MotionScheduler.Manual
/// </summary>
public static void Update()
{
var span = updateRunners.AsSpan();
for (int i = 0; i < span.Length; i++)
{
span[i].Update(deltaTime, deltaTime);
span[i].Update(Time, Time, Time);
}
}

Expand Down
31 changes: 8 additions & 23 deletions src/LitMotion/Assets/LitMotion/Runtime/MotionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static void Return(MotionBuilderBuffer<TValue, TOptions> buffer)
buffer.Version++;
buffer.Duration = default;
buffer.Ease = default;
buffer.IgnoreTimeScale = default;
buffer.Delay = default;
buffer.Loops = 1;
buffer.LoopType = default;
Expand All @@ -50,7 +49,6 @@ public static void Return(MotionBuilderBuffer<TValue, TOptions> buffer)

public float Duration;
public Ease Ease;
public bool IgnoreTimeScale;
public float Delay;
public int Loops = 1;
public LoopType LoopType;
Expand Down Expand Up @@ -100,19 +98,6 @@ public readonly MotionBuilder<TValue, TOptions, TAdapter> WithEase(Ease ease)
return this;
}

/// <summary>
/// Specify whether motion ignores time scale.
/// </summary>
/// <param name="ignoreTimeScale">If true, time scale will be ignored</param>
/// <returns>This builder to allow chaining multiple method calls.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly MotionBuilder<TValue, TOptions, TAdapter> WithIgnoreTimeScale(bool ignoreTimeScale = true)
{
CheckBuffer();
buffer.IgnoreTimeScale = ignoreTimeScale;
return this;
}

/// <summary>
/// Specify the delay time when the motion starts.
/// </summary>
Expand Down Expand Up @@ -216,7 +201,7 @@ public MotionHandle RunWithoutBinding()
var callbacks = BuildCallbackData();
var scheduler = buffer.Scheduler;
var data = BuildMotionData();
return Schedule(scheduler, data, callbacks);
return Schedule(scheduler, ref data, ref callbacks);
}

/// <summary>
Expand All @@ -231,7 +216,7 @@ public MotionHandle Bind(Action<TValue> action)
callbacks.OnCompleteAction = buffer.OnComplete;
var scheduler = buffer.Scheduler;
var data = BuildMotionData();
return Schedule(scheduler, data, callbacks);
return Schedule(scheduler, ref data, ref callbacks);
}

/// <summary>
Expand All @@ -247,7 +232,7 @@ public MotionHandle BindWithState<TState>(TState state, Action<TValue, TState> a
var callbacks = BuildCallbackData(state, action);
var scheduler = buffer.Scheduler;
var data = BuildMotionData();
return Schedule(scheduler, data, callbacks);
return Schedule(scheduler, ref data, ref callbacks);
}

/// <summary>
Expand All @@ -266,7 +251,7 @@ public MotionHandle BindWithState<TState1, TState2>(TState1 state1, TState2 stat
var callbacks = BuildCallbackData(state1, state2, action);
var scheduler = buffer.Scheduler;
var data = BuildMotionData();
return Schedule(scheduler, data, callbacks);
return Schedule(scheduler, ref data, ref callbacks);
}


Expand All @@ -288,7 +273,7 @@ public MotionHandle BindWithState<TState1, TState2, TState3>(TState1 state1, TSt
var callbacks = BuildCallbackData(state1, state2, state3, action);
var scheduler = buffer.Scheduler;
var data = BuildMotionData();
return Schedule(scheduler, data, callbacks);
return Schedule(scheduler, ref data, ref callbacks);
}

/// <summary>
Expand All @@ -305,7 +290,7 @@ public readonly MotionBuilder<TValue, TOptions, TAdapter> Preserve()
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal readonly MotionHandle Schedule(IMotionScheduler scheduler, in MotionData<TValue, TOptions> data, in MotionCallbackData callbackData)
internal readonly MotionHandle Schedule(IMotionScheduler scheduler, ref MotionData<TValue, TOptions> data, ref MotionCallbackData callbackData)
{
if (scheduler == null)
{
Expand All @@ -319,7 +304,7 @@ internal readonly MotionHandle Schedule(IMotionScheduler scheduler, in MotionDat
}
else
{
return scheduler.Schedule<TValue, TOptions, TAdapter>(data, callbackData);
return scheduler.Schedule<TValue, TOptions, TAdapter>(ref data, ref callbackData);
}
}

Expand All @@ -341,9 +326,9 @@ internal MotionData<TValue, TOptions> BuildMotionData()
StartValue = buffer.StartValue,
EndValue = buffer.EndValue,
Options = buffer.Options,
StartTime = buffer.Scheduler == null ? MotionScheduler.Update.Time : buffer.Scheduler.Time,
Duration = buffer.Duration,
Ease = buffer.Ease,
IgnoreTimeScale = buffer.IgnoreTimeScale,
Delay = buffer.Delay,
Loops = buffer.Loops,
LoopType = buffer.LoopType,
Expand Down
Loading