Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TinyTween #136

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions LemonUI/TinyTween/FloatTween.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace LemonUI.TinyTween
{
/// <summary>
/// Represents a tween for floating-point values.
/// </summary>
public class FloatTween : Tween<float>
{
private static readonly LerpFunc<float> LerpFunc = LerpFloat;

private static float LerpFloat(float start, float end, float progress)
{
return start + ((end - start) * progress);
}

/// <summary>
/// Initializes a new instance of the FloatTween class.
/// </summary>
public FloatTween()
: base(LerpFunc)
{
}
}
}
52 changes: 52 additions & 0 deletions LemonUI/TinyTween/ITween.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace LemonUI.TinyTween
{
/// <summary>
/// Represents an interface for a tween with different stop behaviors.
/// </summary>
public interface ITween
{
/// <summary>
/// Gets the current state of the tween.
/// </summary>
TweenState State { get; }
/// <summary>
/// Pauses the tween.
/// </summary>
void Pause();
/// <summary>
/// Resumes the paused tween.
/// </summary>
void Resume();
/// <summary>
/// Stops the tween with the specified stop behavior.
/// </summary>
/// <param name="stopBehavior">The stop behavior of the tween.</param>
void Stop(StopBehavior stopBehavior);
/// <summary>
/// Updates the tween with the elapsed time.
/// </summary>
/// <param name="elapsedTime">The elapsed time since the last update.</param>
void Update(float elapsedTime);
}

/// <summary>
/// Represents an interface for a typed tween with different stop behaviors.
/// </summary>
/// <typeparam name="T">The type of value being tweened.</typeparam>
public interface ITween<T> : ITween
where T : struct
{
/// <summary>
/// Gets the current value of the tween.
/// </summary>
T CurrentValue { get; }
/// <summary>
/// Starts the tween with the specified start and end values, duration, and scale function.
/// </summary>
/// <param name="start">The starting value of the tween.</param>
/// <param name="end">The ending value of the tween.</param>
/// <param name="duration">The duration of the tween.</param>
/// <param name="scaleFunc">The scale function to be used for the tween.</param>
void Start(T start, T end, float duration, ScaleFunc scaleFunc);
}
}
12 changes: 12 additions & 0 deletions LemonUI/TinyTween/LerpFunc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace LemonUI.TinyTween
{
/// <summary>
/// Represents a delegate for a lerp function used in a tween.
/// </summary>
/// <typeparam name="T">The type of value being lerped.</typeparam>
/// <param name="start">The starting value of the lerp.</param>
/// <param name="end">The ending value of the lerp.</param>
/// <param name="progress">The progress of the lerp.</param>
/// <returns>The lerped value based on the progress.</returns>
public delegate T LerpFunc<T>(T start, T end, float progress);
}
9 changes: 9 additions & 0 deletions LemonUI/TinyTween/ScaleFunc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace LemonUI.TinyTween
{
/// <summary>
/// Represents a delegate for a scale function used in a tween.
/// </summary>
/// <param name="progress">The progress of the tween.</param>
/// <returns>The scaled value based on the progress.</returns>
public delegate float ScaleFunc(float progress);
}
279 changes: 279 additions & 0 deletions LemonUI/TinyTween/ScaleFuncs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
using System;

namespace LemonUI.TinyTween
{
/// <summary>
/// Provides a collection of scale functions for use in tweens.
/// </summary>
public static class ScaleFuncs
{
#region Fields
/// <summary>
/// Represents a linear scale function.
/// </summary>
public static readonly ScaleFunc Linear = LinearImpl;
/// <summary>
/// Represents a quadratic ease-in scale function.
/// </summary>
public static readonly ScaleFunc QuadraticEaseIn = QuadraticEaseInImpl;
/// <summary>
/// Represents a quadratic ease-out scale function.
/// </summary>
public static readonly ScaleFunc QuadraticEaseOut = QuadraticEaseOutImpl;
/// <summary>
/// Represents a quadratic ease-in-out scale function.
/// </summary>
public static readonly ScaleFunc QuadraticEaseInOut = QuadraticEaseInOutImpl;
/// <summary>
/// Represents a cubic ease-in scale function.
/// </summary>
public static readonly ScaleFunc CubicEaseIn = CubicEaseInImpl;
/// <summary>
/// Represents a cubic ease-out scale function.
/// </summary>
public static readonly ScaleFunc CubicEaseOut = CubicEaseOutImpl;
/// <summary>
/// Represents a cubic ease-in-out scale function.
/// </summary>
public static readonly ScaleFunc CubicEaseInOut = CubicEaseInOutImpl;
/// <summary>
/// Represents a quartic ease-in scale function.
/// </summary>
public static readonly ScaleFunc QuarticEaseIn = QuarticEaseInImpl;
/// <summary>
/// Represents a quartic ease-out scale function.
/// </summary>
public static readonly ScaleFunc QuarticEaseOut = QuarticEaseOutImpl;
/// <summary>
/// Represents a quartic ease-in-out scale function.
/// </summary>
public static readonly ScaleFunc QuarticEaseInOut = QuarticEaseInOutImpl;
/// <summary>
/// Represents a quintic ease-in scale function.
/// </summary>
public static readonly ScaleFunc QuinticEaseIn = QuinticEaseInImpl;
/// <summary>
/// Represents a quintic ease-out scale function.
/// </summary>
public static readonly ScaleFunc QuinticEaseOut = QuinticEaseOutImpl;
/// <summary>
/// Represents a quintic ease-in-out scale function.
/// </summary>
public static readonly ScaleFunc QuinticEaseInOut = QuinticEaseInOutImpl;
/// <summary>
/// Represents a sine ease-in scale function.
/// </summary>
public static readonly ScaleFunc SineEaseIn = SineEaseInImpl;
/// <summary>
/// Represents a sine ease-out scale function.
/// </summary>
public static readonly ScaleFunc SineEaseOut = SineEaseOutImpl;
/// <summary>
/// Represents a sine ease-in-out scale function.
/// </summary>
public static readonly ScaleFunc SineEaseInOut = SineEaseInOutImpl;
#endregion

#region Functions
/// <summary>
/// Returns the progress as is, resulting in a linear scale.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The linear scale value.</returns>
public static float LinearImpl(float progress)
{
return progress;
}

/// <summary>
/// Applies quadratic ease-in to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The eased-in scale value.</returns>
public static float QuadraticEaseInImpl(float progress)
{
return EaseInPower(progress, 2);
}

/// <summary>
/// Applies quadratic ease-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The eased-out scale value.</returns>
public static float QuadraticEaseOutImpl(float progress)
{
return EaseOutPower(progress, 2);
}

/// <summary>
/// Applies quadratic ease-in-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The eased-in-out scale value.</returns>
public static float QuadraticEaseInOutImpl(float progress)
{
return EaseInOutPower(progress, 2);
}

/// <summary>
/// Applies cubic ease-in to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The eased-in scale value.</returns>
public static float CubicEaseInImpl(float progress)
{
return EaseInPower(progress, 3);
}

/// <summary>
/// Applies cubic ease-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The eased-out scale value.</returns>
public static float CubicEaseOutImpl(float progress)
{
return EaseOutPower(progress, 3);
}

/// <summary>
/// Applies cubic ease-in-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The eased-in-out scale value.</returns>
public static float CubicEaseInOutImpl(float progress)
{
return EaseInOutPower(progress, 3);
}

/// <summary>
/// Applies quartic ease-in to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>Returns the progress value modified with quadratic ease-in.</returns>
public static float QuarticEaseInImpl(float progress)
{
return EaseInPower(progress, 4);
}

/// <summary>
/// Applies quartic ease-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>Returns the progress value modified with quadratic ease-out.</returns>
public static float QuarticEaseOutImpl(float progress)
{
return EaseOutPower(progress, 4);
}

/// <summary>
/// Applies quartic ease-in-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>Returns the progress value modified with quadratic ease-in-out.</returns>
public static float QuarticEaseInOutImpl(float progress)
{
return EaseInOutPower(progress, 4);
}

/// <summary>
/// Applies quintic ease-in to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>Returns the progress value modified with quintic ease-in.</returns>
public static float QuinticEaseInImpl(float progress)
{
return EaseInPower(progress, 5);
}

/// <summary>
/// Applies quintic ease-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>Returns the progress value modified with quintic ease-out.</returns>
public static float QuinticEaseOutImpl(float progress)
{
return EaseOutPower(progress, 5);
}

/// <summary>
/// Applies quintic ease-in-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>Returns the progress value modified with quintic ease-in-out.</returns>
public static float QuinticEaseInOutImpl(float progress)
{
return EaseInOutPower(progress, 5);
}

/// <summary>
/// Applies ease-in power to the progress with the specified power.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <param name="power">The power value for the ease-out function.</param>
/// <returns>The modified progress value with ease-in power.</returns>
public static float EaseInPower(float progress, int power)
{
return (float)Math.Pow(progress, power);
}

/// <summary>
/// Applies ease-out power to the progress with the specified power.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <param name="power">The power value for the ease-out function.</param>
/// <returns>The modified progress value with ease-out power.</returns>
public static float EaseOutPower(float progress, int power)
{
int sign = (power % 2 != 0) ? 1 : -1;
return (float)(sign * (Math.Pow(progress - 1f, power) + sign));
}

/// <summary>
/// Applies ease-in-out power to the progress with the specified power.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <param name="power">The power value for the ease-out function.</param>
/// <returns>The modified progress value with ease-in-out power.</returns>
public static float EaseInOutPower(float progress, int power)
{
progress *= 2f;
if (progress < 1f)
{
return (float)Math.Pow(progress, power) / 2f;
}
int sign = (power % 2 != 0) ? 1 : -1;
return (float)(sign / 2.0 * (Math.Pow(progress - 2f, power) + (sign * 2)));
}

/// <summary>
/// Applies sine ease-in to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The modified progress value with sine ease-in.</returns>
public static float SineEaseInImpl(float progress)
{
return (float)Math.Sin((progress * (float)Math.PI / 2f) - ((float)Math.PI / 2f)) + 1f;
}

/// <summary>
/// Applies sine ease-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The modified progress value with sine ease-out.</returns>
public static float SineEaseOutImpl(float progress)
{
return (float)Math.Sin(progress * (float)Math.PI / 2f);
}

/// <summary>
/// Applies sine ease-in-out to the progress.
/// </summary>
/// <param name="progress">The progress value between 0 and 1.</param>
/// <returns>The modified progress value with sine ease-in-out.</returns>
public static float SineEaseInOutImpl(float progress)
{
return (float)(Math.Sin((progress * (float)Math.PI) - ((float)Math.PI / 2f)) + 1.0) / 2f;
}
#endregion
}
}
17 changes: 17 additions & 0 deletions LemonUI/TinyTween/StopBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace LemonUI.TinyTween
{
/// <summary>
/// Enumeration representing different stop behaviors of a tween.
/// </summary>
public enum StopBehavior
{
/// <summary>
/// Represents the "as is" stop behavior of a tween.
/// </summary>
AsIs,
/// <summary>
/// Represents the "force complete" stop behavior of a tween.
/// </summary>
ForceComplete
}
}
Loading
Loading