using System; using System.Threading; using System.Threading.Tasks; using Godot; using GTweens.Delegates; using GTweens.Extensions; using GTweens.TweenBehaviours; using GTweens.Tweeners; using GTweens.Tweens; using GTweensGodot.Contexts; using GTweensGodot.Tweeners; namespace GTweensGodot.Extensions; public static class GTweenGodotExtensions { public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Tweener.Getter to, float duration, ValidationDelegates.Validation validation ) { InterpolationTweenBehaviour tweenBehaviour = new InterpolationTweenBehaviour(); tweenBehaviour.Add(new GodotVector2Tweener(getter, setter, to, duration, validation)); return new GTween(tweenBehaviour); } public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Vector2 to, float duration, ValidationDelegates.Validation validation ) => Tween(getter, setter, () => to, duration, validation); public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Vector2 to, float duration ) => Tween(getter, setter, to, duration, ValidationExtensions.AlwaysValid); public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Tweener.Getter to, float duration, ValidationDelegates.Validation validation ) { InterpolationTweenBehaviour tweenBehaviour = new InterpolationTweenBehaviour(); tweenBehaviour.Add(new GodotVector2ITweener(getter, setter, to, duration, validation)); return new GTween(tweenBehaviour); } public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Vector2I to, float duration, ValidationDelegates.Validation validation ) => Tween(getter, setter, () => to, duration, validation); public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Vector2I to, float duration ) => Tween(getter, setter, to, duration, ValidationExtensions.AlwaysValid); public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Tweener.Getter to, float duration, ValidationDelegates.Validation validation ) { InterpolationTweenBehaviour tweenBehaviour = new InterpolationTweenBehaviour(); tweenBehaviour.Add(new GodotVector3Tweener(getter, setter, to, duration, validation)); return new GTween(tweenBehaviour); } public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Vector3 to, float duration, ValidationDelegates.Validation validation ) => Tween(getter, setter, () => to, duration, validation); public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Vector3 to, float duration ) => Tween(getter, setter, to, duration, ValidationExtensions.AlwaysValid); public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Tweener.Getter to, float duration, ValidationDelegates.Validation validation ) { InterpolationTweenBehaviour tweenBehaviour = new InterpolationTweenBehaviour(); tweenBehaviour.Add(new GodotColorTweener(getter, setter, to, duration, validation)); return new GTween(tweenBehaviour); } public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Color to, float duration, ValidationDelegates.Validation validation ) => Tween(getter, setter, () => to, duration, validation); public static GTween Tween( Tweener.Getter getter, Tweener.Setter setter, Color to, float duration ) => Tween(getter, setter, to, duration, ValidationExtensions.AlwaysValid); /// /// Sets the easing function for a GTween using a specified Curve. /// /// The GTween to set the easing function for. /// The Curve representing the desired easing behavior. /// The modified GTween with the specified easing function. /// Thrown if is null. public static GTween SetEasing(this GTween gTween, Curve curve) { if (curve == null) { throw new ArgumentNullException( $"Tried to {nameof(SetEasing)} on a {nameof(GTween)} with a null {nameof(Curve)}" ); } return gTween.SetEasing(curve.ToEasingDelegate()); } /// /// Plays a GTween. This tween will be paused when GetTree().Paused is set to true. /// /// The GTween to play. public static void Play(this GTween tween) { GodotGTweensContext.Instance.PausableContext.Play(tween); } /// /// Plays a GTween. This tween will not be paused when GetTree().Paused is set to true. /// /// The GTween to play. public static void PlayUnpausable(this GTween tween) { GodotGTweensContext.Instance.UnpausableContext.Play(tween); } /// /// Plays a GTween. This tween will be paused when GetTree().Paused is set to true. /// /// The GTween to play. /// If set to true, sets the tween as completed after playing, making it reach the end instantly. public static void Play(this GTween tween, bool instantly) { tween.Play(); if (instantly) { tween.Complete(); } } /// /// Plays a GTween. This tween will not be paused when GetTree().Paused is set to true. /// /// The GTween to play. /// If set to true, sets the tween as completed after playing, making it reach the end instantly. public static void PlayUnpausable(this GTween tween, bool instantly) { tween.PlayUnpausable(); if (instantly) { tween.Complete(); } } /// /// Asynchronously plays a GTween and awaits its completion or cancellation. /// If the cancellationToken cancellation is requested, the tween will be killed. /// This tween will be paused when GetTree().Paused is set to true. /// /// The GTween to play. /// A token to cancel the GTween's execution. /// A Task representing the asynchronous operation. public static Task PlayAsync(this GTween gTween, CancellationToken cancellationToken) { gTween.Play(); cancellationToken.Register(gTween.Kill); return gTween.AwaitCompleteOrKill(cancellationToken); } /// /// Asynchronously plays a GTween and awaits its completion or cancellation. /// If the cancellationToken cancellation is requested, the tween will be killed. /// This tween will not be paused when GetTree().Paused is set to true. /// /// The GTween to play. /// A token to cancel the GTween's execution. /// A Task representing the asynchronous operation. public static Task PlayUnpausableAsync(this GTween gTween, CancellationToken cancellationToken) { gTween.PlayUnpausable(); cancellationToken.Register(gTween.Kill); return gTween.AwaitCompleteOrKill(cancellationToken); } /// /// Asynchronously plays a GTween and awaits its completion or cancellation. /// If the cancellationToken cancellation is requested, the tween will be killed. /// This tween will be paused when GetTree().Paused is set to true. /// /// The GTween to play. /// If set to true, sets the tween as completed after playing, making it reach the end instantly. /// A token to cancel the GTween's execution. /// A Task representing the asynchronous operation. public static Task PlayAsync(this GTween gTween, bool instantly, CancellationToken cancellationToken) { if (!instantly) { return gTween.PlayAsync(cancellationToken); } gTween.Play(); gTween.Complete(); return Task.CompletedTask; } /// /// Asynchronously plays a GTween and awaits its completion or cancellation. /// If the cancellationToken cancellation is requested, the tween will be killed. /// This tween will not be paused when GetTree().Paused is set to true. /// /// The GTween to play. /// If set to true, sets the tween as completed after playing, making it reach the end instantly. /// A token to cancel the GTween's execution. /// A Task representing the asynchronous operation. public static Task PlayUnpausableAsync(this GTween gTween, bool instantly, CancellationToken cancellationToken) { if (!instantly) { return gTween.PlayUnpausableAsync(cancellationToken); } gTween.Play(); gTween.Complete(); return Task.CompletedTask; } /// /// Asynchronously plays a GTween and awaits its completion or cancellation. /// If the cancellationToken cancellation is requested, the tween will be killed. /// This tween will be paused when GetTree().Paused is set to true. /// /// The GTween to play. /// A token to complete the GTween's execution. /// A token to cancel the GTween's execution. /// A Task representing the asynchronous operation. public static Task PlayAsync(this GTween gTween, CancellationToken completeToken, CancellationToken cancellationToken) { gTween.Play(); cancellationToken.Register(gTween.Kill); completeToken.Register(gTween.Complete); return gTween.AwaitCompleteOrKill(cancellationToken); } /// /// Asynchronously plays a GTween and awaits its completion or cancellation. /// If the cancellationToken cancellation is requested, the tween will be killed. /// This tween will not be paused when GetTree().Paused is set to true. /// /// The GTween to play. /// A token to complete the GTween's execution. /// A token to cancel the GTween's execution. /// A Task representing the asynchronous operation. public static Task PlayUnpausableAsync(this GTween gTween, CancellationToken completeToken, CancellationToken cancellationToken) { gTween.PlayUnpausable(); cancellationToken.Register(gTween.Kill); completeToken.Register(gTween.Complete); return gTween.AwaitCompleteOrKill(cancellationToken); } }