diff --git a/osu.Framework/Graphics/TransformSequenceExtensions.cs b/osu.Framework/Graphics/TransformSequenceExtensions.cs index d6d3da8f35..f1c76c21dc 100644 --- a/osu.Framework/Graphics/TransformSequenceExtensions.cs +++ b/osu.Framework/Graphics/TransformSequenceExtensions.cs @@ -518,13 +518,13 @@ public static TransformSequence TransformBindableTo(this public static TransformSequence Loop(this TransformSequence t, double pause, int numIters, params TransformSequence.Generator[] childGenerators) where T : Drawable { - TransformSequenceBranch branch = t.CreateBranch(); + var branch = t.CreateBranch(); foreach (var gen in childGenerators) branch.Commit(branch.Head.Continue(gen)); branch.Commit(branch.Head.Loop(pause, numIters)); - return t.MergedWith(branch); + return branch.Merge(); } [Obsolete("For compatibility use only, replacement: X().And().Y().Z().Loop(pause)")] @@ -541,12 +541,13 @@ public static TransformSequence Loop(this TransformSequence t, params T public static TransformSequence Then(this TransformSequence t, double delay, params TransformSequence.Generator[] childGenerators) where T : Drawable { - TransformSequenceBranch branch = t.CreateBranch(); + var branch = t.CreateBranch(); + branch.Commit(branch.Head.Delay(delay)); foreach (var gen in childGenerators) branch.Commit(branch.Head.Continue(gen)); - return t.MergedWith(branch); + return branch.Merge(); } [Obsolete("For compatibility use only, replacement: X().And().Then().Y().Z()")] diff --git a/osu.Framework/Graphics/TransformableExtensions.cs b/osu.Framework/Graphics/TransformableExtensions.cs index dea6c629e0..b776dc3c23 100644 --- a/osu.Framework/Graphics/TransformableExtensions.cs +++ b/osu.Framework/Graphics/TransformableExtensions.cs @@ -715,27 +715,25 @@ public static TransformSequence TransformBindableTo(this public static TransformSequence Animate(this T transformable, params TransformSequence.Generator[] childGenerators) where T : Drawable { - TransformSequence outer = TransformSequence.Create(transformable); - TransformSequenceBranch branch = outer.CreateBranch(); + var branch = TransformSequence.Create(transformable).CreateBranch(); foreach (var gen in childGenerators) branch.Commit(branch.Head.Continue(gen)); - return outer.MergedWith(branch); + return branch.Merge(); } [Obsolete("For compatibility use only, replacement: X.Y().Z().Loop(pause, numIters)")] public static TransformSequence Loop(this T transformable, double pause, int numIters, params TransformSequence.Generator[] childGenerators) where T : Drawable { - TransformSequence outer = TransformSequence.Create(transformable); - TransformSequenceBranch branch = outer.CreateBranch(); + var branch = TransformSequence.Create(transformable).CreateBranch(); foreach (var gen in childGenerators) branch.Commit(branch.Head.Continue(gen)); branch.Commit(branch.Head.Loop(pause, numIters)); - return outer.MergedWith(branch); + return branch.Merge(); } [Obsolete("For compatibility use only, replacement: X.Y().Z().Loop(pause)")] diff --git a/osu.Framework/Graphics/Transforms/TransformSequence.cs b/osu.Framework/Graphics/Transforms/TransformSequence.cs index 23a4a3a726..37a7ef8b03 100644 --- a/osu.Framework/Graphics/Transforms/TransformSequence.cs +++ b/osu.Framework/Graphics/Transforms/TransformSequence.cs @@ -10,13 +10,6 @@ namespace osu.Framework.Graphics.Transforms { - internal static class TransformSequenceHelpers - { - private static ulong id = 1; - - public static ulong GetNextId() => Interlocked.Increment(ref id); - } - public readonly struct TransformSequence where T : class, ITransformable { @@ -89,9 +82,12 @@ public static TransformSequence Create(T target) /// The transform. public static TransformSequence Create(Transform transform) { - TransformSequenceException.ThrowIfInvalidTransform(transform); + if (transform.Target is null) + throwTargetIsNull(); + + if (transform.Target is not T target) + throwTargetTypeMismatch(typeof(T), transform.Target.GetType()); - T target = (T)transform.Target; TransformSequence sequence = Create(target); transform.SequenceID = sequence.id; @@ -107,6 +103,14 @@ public static TransformSequence Create(Transform transform) length = sequence.length + 1, transform = transform }; + + [DoesNotReturn] + static void throwTargetIsNull() + => throw new ArgumentException("Transform target cannot be null."); + + [DoesNotReturn] + static void throwTargetTypeMismatch(Type expected, Type actual) + => throw new ArgumentException($"Transform target was expected to be of type '{expected.ReadableName()}' but was '{actual.ReadableName()}'."); } /// @@ -157,28 +161,6 @@ public TransformSequence Delay(double delay) => this with currentTime = currentTime + delay }; - /// - /// Creates an empty branch from the current time. - /// - public TransformSequenceBranch CreateBranch() => new TransformSequenceBranch(this with - { - startTime = currentTime, - currentTime = currentTime, - endTime = currentTime, - length = 0 - }); - - /// - /// Continues with the result of merging a branch into the current sequence. - /// - /// The branch to merge. - public TransformSequence MergedWith(TransformSequenceBranch branch) => this with - { - endTime = Math.Max(endTime, branch.Head.endTime), - length = length + branch.Head.length, - transform = branch.Head.transform - }; - /// /// Repeats all added transforms indefinitely. /// @@ -249,6 +231,17 @@ public TransformSequence Loop(double pause, int numIters) return this; } + /// + /// Creates an empty branch from the current time. + /// + public Branch CreateBranch() => new Branch(this with + { + startTime = currentTime, + currentTime = currentTime, + endTime = currentTime, + length = 0 + }); + public TransformSequence Finally(Action function) { OnComplete(function); @@ -277,6 +270,41 @@ private TransformSequenceEventHandler getOrCreateEventHandler() return handler; } + public ref struct Branch + { + /// + /// The current head. + /// + public TransformSequence Head { get; private set; } + + /// + /// The sequence which this branch is based off. + /// + private readonly TransformSequence root; + + internal Branch(TransformSequence root) + { + this.root = root; + Head = root; + } + + /// + /// Appends a commit. + /// + /// The new head. + public void Commit(TransformSequence head) => Head = head; + + /// + /// Continues with the result of merging this branch into the original sequence. + /// + public TransformSequence Merge() => root with + { + endTime = Math.Max(root.endTime, Head.endTime), + length = root.length + Head.length, + transform = Head.transform + }; + } + /// /// A delegate that generates a new on a given . /// @@ -285,37 +313,10 @@ private TransformSequenceEventHandler getOrCreateEventHandler() public delegate TransformSequence Generator(T origin); } - public ref struct TransformSequenceBranch(TransformSequence head) - where T : class, ITransformable - { - public TransformSequence Head { get; private set; } = head; - - public void Commit(TransformSequence head) => Head = head; - } - - public class TransformSequenceException : Exception + internal static class TransformSequenceHelpers { - public TransformSequenceException(string message) - : base(message) - { - } - - public static void ThrowIfInvalidTransform(Transform transform) - where T : class, ITransformable - { - if (transform.Target is null) - throwTargetIsNull(); - - if (transform.Target is not T) - throwTargetTypeMismatch(typeof(T), transform.Target.GetType()); - } - - [DoesNotReturn] - private static void throwTargetIsNull() - => throw new TransformSequenceException("Transform target cannot be null."); + private static ulong id = 1; - [DoesNotReturn] - private static void throwTargetTypeMismatch(Type expected, Type actual) - => throw new TransformSequenceException($"Transform target was expected to be of type '{expected.ReadableName()}' but was '{actual.ReadableName()}'."); + public static ulong GetNextId() => Interlocked.Increment(ref id); } }