-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dubit/development
Initial port of source code from duck
- Loading branch information
Showing
105 changed files
with
4,116 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace DUCK.Tween | ||
{ | ||
/// <summary> | ||
/// AbstractAnimation is an abstraction for all animations to derive from | ||
/// </summary> | ||
public abstract class AbstractAnimation | ||
{ | ||
/// <summary> | ||
/// A boolean value which is true if the animation is currently playing | ||
/// </summary> | ||
public bool IsPlaying { get; protected set; } | ||
|
||
/// <summary> | ||
/// A boolean value which is true if the animation is currently looping | ||
/// </summary> | ||
public bool IsLooping { get; protected set; } | ||
|
||
/// <summary> | ||
/// A boolean value which is true if the animation is currently paused | ||
/// </summary> | ||
public bool IsPaused { get; protected set; } | ||
|
||
/// <summary> | ||
/// Is the animation valid? i.e. Does it still have the target reference? | ||
/// If this is set to false calling Play will end it instantly and notify the callbacks; | ||
/// If it's in a sequence it will be automatically removed. | ||
/// </summary> | ||
public abstract bool IsValid { get; } | ||
|
||
private Action onCompleteCallback; | ||
private Action onAbortCallback; | ||
|
||
/// <summary> | ||
/// Simply play the animation | ||
/// </summary> | ||
public void Play() | ||
{ | ||
Play(null); | ||
} | ||
|
||
/// <summary> | ||
/// Starts playback of the animation | ||
/// </summary> | ||
/// <param name="onComplete">An optional callback invoked when the animation is complete</param> | ||
/// <param name="onAbort">An optional callback invoked if the animation is aborted</param> | ||
public virtual void Play(Action onComplete, Action onAbort = null) | ||
{ | ||
if (!IsValid) | ||
{ | ||
Debug.LogError("Cannot play animation: Invalid!"); | ||
return; | ||
} | ||
|
||
onCompleteCallback = onComplete; | ||
onAbortCallback = onAbort; | ||
IsPlaying = true; | ||
} | ||
|
||
/// <summary> | ||
/// Starts playback of the animation with number of repeats | ||
/// </summary> | ||
/// <param name="repeat">The repeat times for the animation. 0 (or less than -1) means no repeat; -1 means infinite loop.</param> | ||
/// <param name="onRepeat">An optional callback invoked on each repeat is complete</param> | ||
/// <param name="onAllComplete">An optional callback invoked when all repeats are finished (will call once only)</param> | ||
/// <param name="onAbort">An optional callback invoked if the animation is aborted</param> | ||
public void Play(int repeat, Action onRepeat = null, Action onAllComplete = null, Action onAbort = null) | ||
{ | ||
if (!IsValid) | ||
{ | ||
Debug.LogError("Cannot play animation: Invalid!"); | ||
return; | ||
} | ||
|
||
IsLooping = repeat > 0 || repeat == -1; | ||
|
||
Action onRepeatComplete = delegate | ||
{ | ||
if (onRepeat != null) | ||
{ | ||
onRepeat(); | ||
} | ||
|
||
if (IsLooping) | ||
{ | ||
Play(repeat == -1 ? -1 : --repeat, onRepeat, onAllComplete, onAbort); | ||
} | ||
// This will only occur when the user called FastForward(). | ||
// Abort an animation wouldn't get this delegate called anyway. | ||
else if (onAllComplete != null) | ||
{ | ||
onAllComplete(); | ||
} | ||
}; | ||
|
||
Play(IsLooping ? onRepeatComplete : onAllComplete, onAbort); | ||
} | ||
|
||
/// <summary> | ||
/// Aborts the current animation at it's current place | ||
/// </summary> | ||
public virtual void Abort() | ||
{ | ||
IsPlaying = false; | ||
IsLooping = false; | ||
IsPaused = false; | ||
|
||
if (onAbortCallback != null) | ||
{ | ||
onAbortCallback(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Fastforwards the animation to the end and stops it, leaving the state of the object | ||
/// </summary> | ||
public virtual void FastForward() | ||
{ | ||
IsPlaying = false; | ||
IsLooping = false; | ||
IsPaused = false; | ||
NotifyAnimationComplete(); | ||
} | ||
|
||
/// <summary> | ||
/// Pause the animation | ||
/// </summary> | ||
public virtual void Pause() | ||
{ | ||
IsPaused = true; | ||
} | ||
|
||
/// <summary> | ||
/// Resume the paused animation. | ||
/// You should not resume a stopped animation. | ||
/// </summary> | ||
public virtual void Resume() | ||
{ | ||
if (!IsPlaying) | ||
{ | ||
Debug.LogError("You should not resume an animation which isn't playing!"); | ||
} | ||
IsPaused = false; | ||
} | ||
|
||
protected void NotifyAnimationComplete() | ||
{ | ||
IsPlaying = IsLooping; | ||
|
||
if (onCompleteCallback != null) | ||
{ | ||
onCompleteCallback(); | ||
} | ||
} | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace DUCK.Tween | ||
{ | ||
/// <summary> | ||
/// AbstractColorFadeAnimation is an abstraction for all color fade animations to derive from | ||
/// </summary> | ||
public abstract class AbstractColorFadeAnimation : TimedAnimation | ||
{ | ||
private readonly Color from; | ||
private readonly Color to; | ||
|
||
protected AbstractColorFadeAnimation(GameObject target, Color from, Color to, float duration = 1f, Func<float, float> easingFunction = null) | ||
: base(target, duration, easingFunction) | ||
{ | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
protected override void Refresh(float progress) | ||
{ | ||
SetColor(Interpolate(from, to, progress)); | ||
} | ||
|
||
protected abstract void SetColor(Color color); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace DUCK.Tween | ||
{ | ||
/// <summary> | ||
/// AbstractFadeAnimation is an abstraction for all fade animations to derive from | ||
/// </summary> | ||
public abstract class AbstractFadeAnimation : TimedAnimation | ||
{ | ||
private readonly float from; | ||
private readonly float to; | ||
|
||
protected AbstractFadeAnimation(GameObject target, float from, float to, float duration = 1f, Func<float, float> easingFunction = null) | ||
: base(target, duration, easingFunction) | ||
{ | ||
this.from = from; | ||
this.to = to; | ||
} | ||
|
||
protected override void Refresh(float progress) | ||
{ | ||
SetAlpha(Interpolate(from, to, progress)); | ||
} | ||
|
||
protected abstract void SetAlpha(float alpha); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.