Added GTWeen

This commit is contained in:
Marco 2025-02-24 11:52:50 +01:00
commit 2036e4e748
152 changed files with 5889 additions and 7 deletions

View file

@ -0,0 +1,38 @@
using System;
using GTweens.Enums;
namespace GTweens.TweenBehaviours
{
public sealed class CallbackTweenBehaviour : TweenBehaviour
{
readonly Action _action;
readonly bool _callIfCompletingInstantly;
public CallbackTweenBehaviour(Action action, bool callIfCompletingInstantly)
{
_action = action;
_callIfCompletingInstantly = callIfCompletingInstantly;
}
public override void Start(bool isCompletingInstantly)
{
bool canCall = !isCompletingInstantly || _callIfCompletingInstantly;
if (canCall)
{
_action?.Invoke();
}
MarkFinished();
}
public override void Reset(bool kill, ResetMode loopResetMode)
{
MarkUnfinished();
}
public override float GetDuration() => 0f;
public override float GetElapsed() => 0f;
public override bool GetLoopable() => false;
}
}

View file

@ -0,0 +1 @@
uid://b4nvwfr8t2hvu

View file

@ -0,0 +1,166 @@
using System.Collections.Generic;
using GTweens.Easings;
using GTweens.Enums;
using GTweens.Extensions;
using GTweens.Tweens;
namespace GTweens.TweenBehaviours
{
public sealed class GroupTweenBehaviour : TweenBehaviour
{
public IReadOnlyList<GTween> Tweens => _tweens;
readonly List<GTween> _tweens = new();
readonly List<GTween> _playingTweens = new();
bool _durationCalculated;
float _cachedCalculatedDuration;
public override void Start(bool isCompletingInstantly)
{
StartTweens(isCompletingInstantly);
}
public override void Tick(float deltaTime)
{
for (int i = _playingTweens.Count - 1; i >= 0; --i)
{
GTween gTween = _playingTweens[i];
gTween.Tick(deltaTime);
if (!gTween.IsPlaying)
{
_playingTweens.RemoveAt(i);
}
}
if (_playingTweens.Count == 0)
{
MarkFinished();
}
}
public override void Kill()
{
foreach (GTween tween in _playingTweens)
{
tween.Kill();
}
_playingTweens.Clear();
MarkFinished();
}
public override void Complete()
{
foreach (GTween tween in _playingTweens)
{
if (tween.IsCompleted)
{
continue;
}
if (!tween.IsPlaying)
{
tween.Start(isCompletingInstantly: true);
}
tween.Complete();
}
_playingTweens.Clear();
MarkFinished();
}
public override void Reset(bool kill, ResetMode resetMode)
{
for (int i = _tweens.Count - 1; i >= 0; --i)
{
GTween gTween = _tweens[i];
gTween.Reset(kill, resetMode);
}
MarkUnfinished();
}
public override void SetEasing(EasingDelegate easingFunction)
{
foreach (GTween tween in _tweens)
{
tween.SetEasing(easingFunction);
}
}
public override float GetDuration()
{
if(_durationCalculated)
{
return _cachedCalculatedDuration;
}
_durationCalculated = true;
_cachedCalculatedDuration = 0.0f;
foreach (GTween tween in _tweens)
{
_cachedCalculatedDuration += tween.GetDuration();
}
return _cachedCalculatedDuration;
}
public override float GetElapsed()
{
float totalDuration = 0.0f;
foreach (GTween tween in _tweens)
{
totalDuration += tween.GetElapsed();
}
return totalDuration;
}
public void Add(GTween gTween)
{
if(gTween.IsPlayingOrCompletedOrNested())
{
return;
}
gTween.IsNested = true;
_tweens.Add(gTween);
_durationCalculated = false;
}
void StartTweens(bool isCompletingInstantly)
{
_playingTweens.Clear();
_playingTweens.AddRange(_tweens);
for (int i = _playingTweens.Count - 1; i >= 0; --i)
{
GTween gTween = _playingTweens[i];
gTween.Start(isCompletingInstantly);
if (!gTween.IsPlaying)
{
_playingTweens.RemoveAt(i);
}
}
if (_playingTweens.Count == 0)
{
MarkFinished();
}
}
}
}

View file

@ -0,0 +1 @@
uid://b0sfk7ps1fav6

View file

@ -0,0 +1,22 @@
using GTweens.Easings;
using GTweens.Enums;
namespace GTweens.TweenBehaviours;
public interface ITweenBehaviour
{
float GetDuration();
float GetElapsed();
float GetRemaining();
bool GetLoopable();
void Start(bool isCompletingInstantly);
void Tick(float deltaTime);
void Kill();
void Complete();
void Reset(bool kill, ResetMode loopResetMode);
void SetEasing(EasingDelegate easingFunction);
bool GetFinished();
}

View file

@ -0,0 +1 @@
uid://ceieq47mwyq5q

View file

@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using GTweens.Easings;
using GTweens.Enums;
using GTweens.Tweeners;
namespace GTweens.TweenBehaviours
{
public sealed class InterpolationTweenBehaviour : TweenBehaviour
{
readonly List<ITweener> _tweeners = new();
readonly List<ITweener> _playingTweeners = new();
bool _durationCalculated;
float _cachedCalculatedDuration;
public override void Start(bool isCompletingInstantly)
{
StartTweeners();
}
public override void Tick(float deltaTime)
{
for (int i = _playingTweeners.Count - 1; i >= 0; --i)
{
ITweener tweener = _playingTweeners[i];
tweener.Tick(deltaTime);
if (!tweener.IsPlaying)
{
_playingTweeners.RemoveAt(i);
}
}
if (_playingTweeners.Count == 0)
{
MarkFinished();
}
}
public override void Kill()
{
foreach (ITweener tweener in _playingTweeners)
{
tweener.Kill();
}
_playingTweeners.Clear();
MarkFinished();
}
public override void Complete()
{
foreach (ITweener tweener in _playingTweeners)
{
if (!tweener.IsPlaying)
{
continue;
}
tweener.Complete();
}
_playingTweeners.Clear();
MarkFinished();
}
public override void Reset(bool kill, ResetMode resetMode)
{
for (int i = _tweeners.Count - 1; i >= 0; --i)
{
ITweener tweener = _tweeners[i];
tweener.Reset(resetMode);
}
MarkUnfinished();
}
public override void SetEasing(EasingDelegate easingFunction)
{
foreach (ITweener tweener in _tweeners)
{
tweener.SetEasing(easingFunction);
}
}
public override float GetDuration()
{
if(_durationCalculated)
{
return _cachedCalculatedDuration;
}
_durationCalculated = true;
_cachedCalculatedDuration = 0.0f;
foreach (ITweener tweener in _tweeners)
{
_cachedCalculatedDuration += tweener.Duration;
}
return _cachedCalculatedDuration;
}
public override float GetElapsed()
{
float totalElapsed = 0.0f;
foreach (ITweener tweener in _tweeners)
{
totalElapsed += tweener.Elapsed;
}
return totalElapsed;
}
public void Add(ITweener tweener)
{
if (tweener == null)
{
throw new ArgumentNullException(
$"Tried to {nameof(Add)} a null {nameof(ITweener)} on {nameof(InterpolationTweenBehaviour)}"
);
}
if (tweener.IsPlaying)
{
throw new ArgumentNullException(
$"Tried to {nameof(Add)} a {nameof(ITweener)} on {nameof(InterpolationTweenBehaviour)} but it was already playing"
);
}
if (_tweeners.Contains(tweener))
{
throw new ArgumentNullException(
$"Tried to {nameof(Add)} a {nameof(ITweener)} on {nameof(InterpolationTweenBehaviour)} but it was already added"
);
}
_tweeners.Add(tweener);
_durationCalculated = false;
}
void StartTweeners()
{
_playingTweeners.Clear();
_playingTweeners.AddRange(_tweeners);
for (int i = _playingTweeners.Count - 1; i >= 0; --i)
{
ITweener tweener = _playingTweeners[i];
tweener.Start();
if (!tweener.IsPlaying)
{
_playingTweeners.RemoveAt(i);
}
}
if (_playingTweeners.Count == 0)
{
MarkFinished();
}
}
}
}

View file

@ -0,0 +1 @@
uid://cx1regaakoijq

View file

@ -0,0 +1,189 @@
using System.Collections.Generic;
using GTweens.Easings;
using GTweens.Enums;
using GTweens.Extensions;
using GTweens.Tweens;
namespace GTweens.TweenBehaviours
{
public sealed class SequenceTweenBehaviour : TweenBehaviour
{
public IReadOnlyList<GTween> Tweens => _tweens;
readonly List<GTween> _tweens = new();
readonly List<GTween> _playingTweens = new();
bool _durationCalculated;
float _cachedCalculatedDuration;
public override void Start(bool isCompletingInstantly)
{
StartTweens(isCompletingInstantly);
}
public override void Tick(float deltaTime)
{
if (_playingTweens.Count == 0)
{
MarkFinished();
return;
}
GTween gTween = _playingTweens[0];
gTween.Tick(deltaTime);
if (gTween.IsPlaying)
{
return;
}
_playingTweens.RemoveAt(0);
if (_playingTweens.Count > 0)
{
GTween nextGTween = _playingTweens[0];
nextGTween.Start();
}
else
{
Tick(deltaTime);
}
}
public override void Kill()
{
foreach (GTween tween in _playingTweens)
{
tween.Kill();
}
_playingTweens.Clear();
MarkFinished();
}
public override void Complete()
{
foreach (GTween tween in _tweens)
{
if(tween.IsCompleted)
{
continue;
}
if (!tween.IsPlaying)
{
tween.Start(isCompletingInstantly: true);
}
tween.Complete();
}
_playingTweens.Clear();
MarkFinished();
}
public override void Reset(bool kill, ResetMode resetMode)
{
for (int i = _tweens.Count - 1; i >= 0; --i)
{
GTween gTween = _tweens[i];
gTween.Reset(kill, resetMode);
}
MarkUnfinished();
}
public override void SetEasing(EasingDelegate easingFunction)
{
foreach (GTween tween in _tweens)
{
tween.SetEasing(easingFunction);
}
}
public override float GetDuration()
{
if(_durationCalculated)
{
return _cachedCalculatedDuration;
}
_durationCalculated = true;
_cachedCalculatedDuration = 0.0f;
foreach (GTween tween in _tweens)
{
_cachedCalculatedDuration += tween.GetDuration();
}
return _cachedCalculatedDuration;
}
public override float GetElapsed()
{
float totalDuration = 0.0f;
foreach (GTween tween in _tweens)
{
totalDuration += tween.GetElapsed();
}
return totalDuration;
}
public void Add(GTween gTween)
{
if (gTween.IsPlayingOrCompletedOrNested())
{
return;
}
gTween.IsNested = true;
_tweens.Add(gTween);
_durationCalculated = false;
}
public void Remove(GTween gTween)
{
if (gTween.IsPlayingOrCompleted())
{
return;
}
bool found = _tweens.Remove(gTween);
if (!found)
{
return;
}
gTween.IsNested = false;
_durationCalculated = false;
}
void StartTweens(bool isCompletingInstantly)
{
_playingTweens.Clear();
_playingTweens.AddRange(_tweens);
if (_playingTweens.Count > 0)
{
GTween gTween = _playingTweens[0];
gTween.Start(isCompletingInstantly);
}
else
{
MarkFinished();
}
}
}
}

View file

@ -0,0 +1 @@
uid://dm1lb1r6xblco

View file

@ -0,0 +1,34 @@
using System;
using GTweens.Easings;
using GTweens.Enums;
namespace GTweens.TweenBehaviours;
public abstract class TweenBehaviour : ITweenBehaviour
{
bool _finished;
public bool GetFinished() => _finished;
protected void MarkFinished() => _finished = true;
protected void MarkUnfinished() => _finished = false;
public float GetRemaining()
{
float duration = GetDuration();
float elapsed = GetElapsed();
return Math.Max(duration - elapsed, 0f);
}
public abstract float GetDuration();
public abstract float GetElapsed();
public virtual bool GetLoopable() => true;
public virtual void Start(bool isCompletingInstantly) { }
public virtual void Tick(float deltaTime) { }
public virtual void Kill() { }
public virtual void Complete() { }
public virtual void Reset(bool kill, ResetMode loopResetMode) { }
public virtual void SetEasing(EasingDelegate easingFunction) { }
}

View file

@ -0,0 +1 @@
uid://rx0cbwgbajym

View file

@ -0,0 +1,53 @@
using GTweens.Enums;
namespace GTweens.TweenBehaviours
{
public sealed class WaitTimeTweenBehaviour : TweenBehaviour
{
readonly float _durationSeconds;
float _elapsedSeconds;
public WaitTimeTweenBehaviour(float durationSeconds)
{
_durationSeconds = durationSeconds;
}
public override void Start(bool isCompletingInstantly)
{
_elapsedSeconds = 0f;
}
public override void Tick(float deltaTime)
{
_elapsedSeconds += deltaTime;
if (_elapsedSeconds >= _durationSeconds)
{
MarkFinished();
}
}
public override void Complete()
{
_elapsedSeconds = _durationSeconds;
MarkFinished();
}
public override void Reset(bool kill, ResetMode loopResetMode)
{
_elapsedSeconds = 0f;
MarkUnfinished();
}
public override float GetDuration()
{
return _durationSeconds;
}
public override float GetElapsed()
{
return _elapsedSeconds;
}
}
}

View file

@ -0,0 +1 @@
uid://m7stdrq84q6r