MotionSequenceBuilder.cs
Runtime/Sequences/MotionSequenceBuilder.cs
1 using System;
2 using System.Collections.Generic;
3 using MeatMotion.Adapters;
4
5 namespace MeatMotion
6 {
7 public struct MotionSequenceBuilder : IDisposable
8 {
9 sealed class State
10 {
11 internal MotionWorld World;
12 internal readonly bool LitLifecycle;
13 internal List<SequenceItem> Items = new();
14 internal double Tail, LastStart, Duration;
15 internal bool Scheduling;
16 internal State(MotionWorld world, bool litLifecycle) { World = world; LitLifecycle = litLifecycle; }
17 }
18
19 readonly State state;
20 internal MotionSequenceBuilder(MotionWorld scheduler, bool litLifecycle = false)
21 => state = new State(scheduler, litLifecycle);
22
23 public readonly MotionSequenceBuilder Append(MotionHandle handle)
24 {
25 CheckOpen();
26 var node = Add(state.Tail, handle);
27 state.LastStart = state.Tail;
28 state.Tail += node.Duration;
29 return this;
30 }
31
32 public readonly MotionSequenceBuilder AppendInterval(float interval)
33 {
34 CheckOpen();
35 if (!state.LitLifecycle) MotionWorld.CheckTime(interval, nameof(interval));
36 state.LastStart = state.Tail;
37 state.Tail += interval;
38 state.Duration = Math.Max(state.Duration, state.Tail);
39 return this;
40 }
41
42 public readonly MotionSequenceBuilder Join(MotionHandle handle)
43 {
44 CheckOpen();
45 Add(state.LastStart, handle);
46 return this;
47 }
48
49 public readonly MotionSequenceBuilder Insert(float position, MotionHandle handle) => Insert((double)position, handle);
50 public readonly MotionSequenceBuilder Insert(double position, MotionHandle handle)
51 {
52 CheckOpen();
53 if (!state.LitLifecycle) MotionWorld.CheckTime(position, nameof(position));
54 Add(position, handle);
55 return this;
56 }
57
58 readonly MotionNode Add(double position, MotionHandle handle)
59 {
60 var node = handle.RequireRoot();
61 if (double.IsInfinity(node.Duration)) throw new ArgumentException("An infinite motion cannot be added to a sequence.", nameof(handle));
62 if (node.Started) throw new ArgumentException("Motion has already started.", nameof(handle));
63 if (!state.LitLifecycle)
64 {
65 var world = state.World;
66 if (world != null && node.World != world &&
67 (world.AutomaticPhase == null || world.AutomaticPhase != node.World.AutomaticPhase))
68 throw new ArgumentException("Motions must share a scheduler.");
69 state.World ??= node.World;
70 }
71 state.Items.Add(new SequenceItem(position, handle, node, state.Items.Count));
72 node.World.DetachRoot(node);
73 node.Owned = true;
74 state.Duration = Math.Max(state.Duration, position + node.Duration);
75 return node;
76 }
77
78 public MotionHandle Run() => Run(null);
79
80 public MotionHandle Run(Action<MotionBuilder<double, NoOptions, DoubleMotionAdapter>> configuration)
81 {
82 CheckOpen();
83 state.Scheduling = true;
84 try
85 {
86 var ordered = state.Items.ToArray();
87 Array.Sort(ordered, (a, b) => a.Start == b.Start ? a.Order.CompareTo(b.Order) : a.Start.CompareTo(b.Start));
88 MotionNode node;
89 if (configuration == null && !state.LitLifecycle)
90 node = (state.World ?? MotionWorld.Default).Rent(state.Duration);
91 else
92 {
93 using var clock = MMotion.Create(0d, state.Duration, (float)state.Duration);
94 if (state.World != null) clock.WithScheduler(state.World);
95 configuration?.Invoke(clock);
96 var timeline = new Timeline();
97 node = clock.Bind(timeline, static (t, owner) => owner.Parent?.SeekChildren(t)).Node;
98 timeline.Parent = node;
99 }
100 node.Children = ordered;
101 node.CancelChildrenOnComplete = state.LitLifecycle;
102 state.Items = null;
103 return configuration == null && !state.LitLifecycle
104 ? MotionDebugger.Scheduled(node, state.World) : new MotionHandle(node);
105 }
106 finally { state.Scheduling = false; }
107 }
108
109 sealed class Timeline { internal MotionNode Parent; }
110
111 readonly void CheckOpen()
112 {
113 if (state?.Items == null || state.Scheduling) throw new InvalidOperationException("Sequence builder is uninitialized or consumed.");
114 }
115
116 public void Dispose()
117 {
118 if (state != null && !state.LitLifecycle && state.Items == null) return;
119 CheckOpen();
120 var pending = state.Items;
121 state.Items = null;
122 if (state.LitLifecycle) return;
123 foreach (var item in pending)
124 if (item.Handle.IsActive()) item.Handle.Node.Release(false);
125 }
126 }
127 }