MotionHandle.cs
Runtime/MotionHandle.cs
1 using System;
2
3 namespace MeatMotion
4 {
5 public struct MotionHandle : IEquatable<MotionHandle>
6 {
7 public int StorageId, Index, Version;
8 internal MotionHandle(MotionNode node)
9 {
10 StorageId = node.World.StorageId;
11 Index = node.Slot;
12 Version = node.Version;
13 }
14
15 internal readonly MotionNode Node => MotionWorld.Resolve(this);
16 public static readonly MotionHandle None = default;
17 public override readonly string ToString() => $"MotionHandle`{StorageId} ({Index}:{Version})";
18 public readonly bool Equals(MotionHandle other) => StorageId == other.StorageId && Index == other.Index && Version == other.Version;
19 public override readonly bool Equals(object obj) => obj is MotionHandle other && Equals(other);
20 public override readonly int GetHashCode() => HashCode.Combine(Index, Version, StorageId);
21 public static bool operator ==(MotionHandle a, MotionHandle b) => a.Equals(b);
22 public static bool operator !=(MotionHandle a, MotionHandle b) => !a.Equals(b);
23 public readonly bool IsActive() => Node?.Active == true;
24 public readonly bool IsPlaying() => Node is { Active: true, Ended: false };
25 internal readonly MotionNode RequireValid() => Node ?? throw new InvalidOperationException("Motion has been destroyed or no longer exists.");
26 internal readonly MotionNode RequireRoot()
27 {
28 var node = RequireValid();
29 if (!node.Active) throw new InvalidOperationException("Motion has already been canceled or completed.");
30 if (node.Owned) throw new InvalidOperationException("Cannot access the motion in sequence.");
31 return node;
32 }
33 readonly MotionNode ReadRoot()
34 {
35 var node = RequireValid();
36 if (node.Owned) throw new InvalidOperationException("Cannot access the motion in sequence.");
37 return node;
38 }
39 public readonly double TotalDuration => RequireValid().Duration;
40 public readonly float Duration
41 {
42 get { var node = RequireValid(); return node.Data.Timed ? node.Timing.Length : (float)node.Duration; }
43 }
44 public readonly float Delay { get { var node = RequireValid(); return node.Data.Timed ? node.Timing.Delay : 0f; } }
45 public readonly int Loops { get { var node = RequireValid(); return node.Data.Timed ? node.Timing.Loops : 1; } }
46 public readonly int CompletedLoops => ReadRoot().CompletedLoops;
47 public float PlaybackSpeed
48 {
49 get => ReadRoot().Speed;
50 set
51 {
52 if (float.IsNaN(value) || float.IsInfinity(value)) throw new ArgumentOutOfRangeException(nameof(value));
53 var node = RequireRoot();
54 node.Speed = value;
55 node.Data.SpecialPlayback = node.Preserved || value != 1f;
56 node.World.InvalidatePage(node.RootIndex);
57 }
58 }
59 public MotionHandle Preserve()
60 {
61 var node = RequireRoot();
62 node.Preserved = node.Data.SpecialPlayback = true;
63 node.World.InvalidatePage(node.RootIndex);
64 return this;
65 }
66 public double Time
67 {
68 get => RequireValid().Time;
69 set
70 {
71 if (double.IsNaN(value) || double.IsInfinity(value)) throw new ArgumentOutOfRangeException(nameof(value));
72 var node = RequireRoot();
73 node.World.InvalidatePage(node.RootIndex);
74 node.Seek(value);
75 }
76 }
77 public void Complete()
78 {
79 var n = RequireRoot();
80 if (n.Ended) throw new InvalidOperationException("Motion has already been canceled or completed.");
81 if (double.IsInfinity(n.Duration)) throw new InvalidOperationException("Cannot complete an infinite motion.");
82 n.World.InvalidatePage(n.RootIndex);
83 n.Seek(n.Duration, force: true);
84 }
85 public void Cancel() => RequireRoot().Release(false);
86 public bool TryComplete()
87 {
88 if (!IsPlaying() || Node.Owned || double.IsInfinity(Node.Duration)) return false;
89 Complete();
90 return true;
91 }
92 public bool TryCancel()
93 {
94 if (!IsActive() || Node.Owned) return false;
95 Cancel();
96 return true;
97 }
98 }
99 }