using System;
using GTweens.TweenBehaviours;
using GTweens.Tweens;
namespace GTweens.Builders;
///
/// Builder class for creating sequences of tweens.
///
public sealed class GTweenSequenceBuilder
{
readonly SequenceTweenBehaviour _sequenceTweenBehaviour;
readonly GTween _gTween;
bool _creatingGroupTween;
GroupTweenBehaviour? _groupTweenBehaviour;
GTweenSequenceBuilder()
{
_sequenceTweenBehaviour = new SequenceTweenBehaviour();
_gTween = new GTween(_sequenceTweenBehaviour);
}
///
/// Creates a new instance of the .
///
/// A new instance of the builder.
public static GTweenSequenceBuilder New()
{
return new GTweenSequenceBuilder();
}
///
/// Adds the given tween to the end of the Sequence. This tween will play after all the previous tweens have finished.
///
/// The GTween to append to the sequence.
/// The current instance of the builder.
public GTweenSequenceBuilder Append(GTween gTween)
{
_creatingGroupTween = false;
_sequenceTweenBehaviour.Add(gTween);
return this;
}
///
/// Inserts the given tween at the same time position of the last tween added to the Sequence.
/// This tween will play at the same time as the previous tween.
///
/// The GTween to join with the sequence.
/// The current instance of the builder.
public GTweenSequenceBuilder Join(GTween gTween)
{
if (_creatingGroupTween)
{
_groupTweenBehaviour!.Add(gTween);
return this;
}
_creatingGroupTween = true;
_groupTweenBehaviour = new GroupTweenBehaviour();
if (_sequenceTweenBehaviour.Tweens.Count > 0)
{
GTween previousTween = _sequenceTweenBehaviour.Tweens[^1];
_sequenceTweenBehaviour.Remove(previousTween);
_groupTweenBehaviour.Add(previousTween);
}
_groupTweenBehaviour.Add(gTween);
_sequenceTweenBehaviour.Add(new GTween(_groupTweenBehaviour));
return this;
}
///
/// Appends a callback action to the end of the sequence.
///
/// The callback action to append.
/// Whether to call the callback if the tween is asked to complete instantly.
/// The current instance of the builder.
public GTweenSequenceBuilder AppendCallback(Action callback, bool callIfCompletingInstantly = true)
{
CallbackTweenBehaviour callbackTweenBehaviour = new(callback, callIfCompletingInstantly);
Append(new GTween(callbackTweenBehaviour));
return this;
}
///
/// Inserts the given callback at the same time position of the last tween added to the Sequence.
/// This tween will play at the same time as the previous tween.
///
/// The callback action to append.
/// Whether to call the callback if the tween is asked to complete instantly.
/// The current instance of the builder.
public GTweenSequenceBuilder JoinCallback(Action callback, bool callIfCompletingInstantly = true)
{
CallbackTweenBehaviour callbackTweenBehaviour = new(callback, callIfCompletingInstantly);
Join(new GTween(callbackTweenBehaviour));
return this;
}
///
/// Appends a time delay to the end of the sequence.
///
/// The duration of the time delay in seconds.
/// The current instance of the builder.
public GTweenSequenceBuilder AppendTime(float timeSeconds)
{
WaitTimeTweenBehaviour timeTweenBehaviour = new(timeSeconds);
Append(new GTween(timeTweenBehaviour));
return this;
}
///
/// Inserts the given time delay at the same time position of the last tween added to the Sequence.
/// This tween will play at the same time as the previous tween.
///
/// The duration of the time delay in seconds.
/// The current instance of the builder.
public GTweenSequenceBuilder JoinTime(float timeSeconds)
{
WaitTimeTweenBehaviour timeTweenBehaviour = new(timeSeconds);
Join(new GTween(timeTweenBehaviour));
return this;
}
///
/// Provides a new GTweenSequenceBuilder for building a sequence, and then adds it to the end of the sequence.
///
/// An action that defines the nested sequence using a new GTweenSequenceBuilder.
/// The current instance of the builder.
public GTweenSequenceBuilder AppendSequence(Action createSequence)
{
GTweenSequenceBuilder sequenceBuilder = New();
createSequence.Invoke(sequenceBuilder);
Append(sequenceBuilder.Build());
return this;
}
///
/// Provides a new GTweenSequenceBuilder for building a sequence, and then inserts it
/// at the same time position of the last tween added to the Sequence.
///
/// An action that defines the nested sequence using a new GTweenSequenceBuilder.
/// The current instance of the builder.
public GTweenSequenceBuilder JoinSequence(Action createSequence)
{
GTweenSequenceBuilder sequenceBuilder = New();
createSequence.Invoke(sequenceBuilder);
Join(sequenceBuilder.Build());
return this;
}
///
/// Builds and returns the final GTween representing the sequence.
///
/// The GTween representing the built sequence.
public GTween Build() => _gTween;
}