Skip to content

Instantly share code, notes, and snippets.

@Duckers
Created July 31, 2015 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Duckers/c02f50c63a0d68e22683 to your computer and use it in GitHub Desktop.
Save Duckers/c02f50c63a0d68e22683 to your computer and use it in GitHub Desktop.
using Uno;
namespace Fuse.Animations
{
public abstract class Tween
{
public static double Time
{
get { return Uno.Time.FrameTime; }
}
public event Action<Tween> Updated;
public event Action<Tween> Started;
public event Action<Tween> Completed;
public float Duration { get; private set; }
public EasingFunction EasingFunction { get; private set; }
public Action DoneCallback { get; private set; }
protected void Init(float duration, EasingFunction easing = null, Action doneCallback = null)
{
this.DoneCallback = doneCallback;
this.Duration = duration;
this.EasingFunction = easing ?? EasingFunctions.Linear;
}
public float TweenTime { get; private set; }
void SetTweenInternal(float t)
{
TweenTime = t;
SetTween(t);
}
protected abstract void SetTween(float t);
public void Stop()
{
if (IsPlaying)
{
IsPlaying = false;
if (DoneCallback != null) DoneCallback();
Fuse.UpdateManager.RemoveAction(Update);
}
}
public void Update()
{
if (IsPlaying)
{
if (Updated != null) Updated(this);
if ( (float)(Time - StartTime) >= Duration)
{
SetTweenInternal(1);
if (Completed != null) Completed(this);
Stop();
}
else
{
var t = EasingFunction( (float)(Time - StartTime) / Duration);
SetTweenInternal(t);
}
}
}
public double StartTime { get; set; }
public bool IsPlaying { get; private set; }
public static Tween Start(Action<float> setter, float startValue, float endValue, float duration, EasingFunction easing = null, Action doneCallback = null)
{
var t = new TweenFloat(setter, startValue, endValue, duration, easing, doneCallback);
t.Start();
return t;
}
public static Tween Start(Action<float2> setter, float2 startValue, float2 endValue, float duration, EasingFunction easing = null, Action doneCallback = null)
{
var t = new TweenFloat2(setter, startValue, endValue, duration, easing, doneCallback);
t.Start();
return t;
}
public static Tween Start(Action<float3> setter, float3 startValue, float3 endValue, float duration, EasingFunction easing = null, Action doneCallback = null)
{
var t = new TweenFloat3(setter, startValue, endValue, duration, easing, doneCallback);
t.Start();
return t;
}
public static Tween Start(Action<float4> setter, float4 startValue, float4 endValue, float duration, EasingFunction easing = null, Action doneCallback = null)
{
var t = new TweenFloat4(setter, startValue, endValue, duration, easing, doneCallback);
t.Start();
return t;
}
public void Start()
{
Fuse.UpdateManager.AddAction(Update);
StartTime = Time;
if (Started != null) Started(this);
IsPlaying = true;
SetTween(0);
}
}
abstract class TweenBase<T>: Tween
{
public Action<T> Setter { get; set; }
public T StartValue { get; set; }
public T EndValue { get; set; }
protected TweenBase( Action<T> setter, T startValue, T endValue, float duration, EasingFunction easing = null, Action doneCallback = null)
{
Init(duration, easing, doneCallback); // workaound because of refcount error stripping : base()-call
this.Setter = setter;
this.StartValue = startValue;
this.EndValue = endValue;
}
}
class TweenFloat: TweenBase<float>
{
public TweenFloat(Action<float> setter, float startValue, float endValue, float duration, EasingFunction easing = null, Action doneCallback = null)
: base(setter, startValue, endValue, duration, easing, doneCallback)
{
}
protected override void SetTween(float t)
{
Setter(StartValue + (EndValue-StartValue) * t);
}
}
class TweenFloat2: TweenBase<float2>
{
public TweenFloat2(Action<float2> setter, float2 startValue, float2 endValue, float duration, EasingFunction easing = null, Action doneCallback = null)
: base(setter, startValue, endValue, duration, easing, doneCallback)
{
}
protected override void SetTween(float t)
{
Setter(StartValue + (EndValue-StartValue) * t);
}
}
class TweenFloat3: TweenBase<float3>
{
public TweenFloat3(Action<float3> setter, float3 startValue, float3 endValue, float duration, EasingFunction easing = null, Action doneCallback = null)
: base(setter, startValue, endValue, duration, easing, doneCallback)
{
}
protected override void SetTween(float t)
{
Setter(StartValue + (EndValue-StartValue) * t);
}
}
class TweenFloat4: TweenBase<float4>
{
public TweenFloat4(Action<float4> setter, float4 startValue, float4 endValue, float duration, EasingFunction easing = null, Action doneCallback = null)
: base(setter, startValue, endValue, duration, easing, doneCallback)
{
}
protected override void SetTween(float t)
{
Setter(StartValue + (EndValue-StartValue) * t);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment