From 1f0208ffdb2f5681e7bf5301cd5bd8fe522beb66 Mon Sep 17 00:00:00 2001 From: GravityScriptsV2 <95504918+GravityScriptsV2@users.noreply.github.com> Date: Sun, 26 Nov 2023 18:37:25 -0800 Subject: [PATCH 1/3] TinyTween --- LemonUI.SHVDN3/TinyTween/FloatTween.cs | 23 ++ LemonUI.SHVDN3/TinyTween/ITween.cs | 52 ++++ LemonUI.SHVDN3/TinyTween/LerpFunc.cs | 12 + LemonUI.SHVDN3/TinyTween/ScaleFunc.cs | 9 + LemonUI.SHVDN3/TinyTween/ScaleFuncs.cs | 287 +++++++++++++++++++++++ LemonUI.SHVDN3/TinyTween/StopBehavior.cs | 17 ++ LemonUI.SHVDN3/TinyTween/Tween.cs | 172 ++++++++++++++ LemonUI.SHVDN3/TinyTween/TweenState.cs | 21 ++ 8 files changed, 593 insertions(+) create mode 100644 LemonUI.SHVDN3/TinyTween/FloatTween.cs create mode 100644 LemonUI.SHVDN3/TinyTween/ITween.cs create mode 100644 LemonUI.SHVDN3/TinyTween/LerpFunc.cs create mode 100644 LemonUI.SHVDN3/TinyTween/ScaleFunc.cs create mode 100644 LemonUI.SHVDN3/TinyTween/ScaleFuncs.cs create mode 100644 LemonUI.SHVDN3/TinyTween/StopBehavior.cs create mode 100644 LemonUI.SHVDN3/TinyTween/Tween.cs create mode 100644 LemonUI.SHVDN3/TinyTween/TweenState.cs diff --git a/LemonUI.SHVDN3/TinyTween/FloatTween.cs b/LemonUI.SHVDN3/TinyTween/FloatTween.cs new file mode 100644 index 0000000..4d0d0ce --- /dev/null +++ b/LemonUI.SHVDN3/TinyTween/FloatTween.cs @@ -0,0 +1,23 @@ +namespace LemonUI.TinyTween +{ + /// + /// Represents a tween for floating-point values. + /// + public class FloatTween : Tween + { + private static readonly LerpFunc LerpFunc = LerpFloat; + + private static float LerpFloat(float start, float end, float progress) + { + return start + ((end - start) * progress); + } + + /// + /// Initializes a new instance of the FloatTween class. + /// + public FloatTween() + : base(LerpFunc) + { + } + } +} diff --git a/LemonUI.SHVDN3/TinyTween/ITween.cs b/LemonUI.SHVDN3/TinyTween/ITween.cs new file mode 100644 index 0000000..f81df23 --- /dev/null +++ b/LemonUI.SHVDN3/TinyTween/ITween.cs @@ -0,0 +1,52 @@ +namespace LemonUI.TinyTween +{ + /// + /// Represents an interface for a tween with different stop behaviors. + /// + public interface ITween + { + /// + /// Gets the current state of the tween. + /// + TweenState State { get; } + /// + /// Pauses the tween. + /// + void Pause(); + /// + /// Resumes the paused tween. + /// + void Resume(); + /// + /// Stops the tween with the specified stop behavior. + /// + /// The stop behavior of the tween. + void Stop(StopBehavior stopBehavior); + /// + /// Updates the tween with the elapsed time. + /// + /// The elapsed time since the last update. + void Update(float elapsedTime); + } + + /// + /// Represents an interface for a typed tween with different stop behaviors. + /// + /// The type of value being tweened. + public interface ITween : ITween + where T : struct + { + /// + /// Gets the current value of the tween. + /// + T CurrentValue { get; } + /// + /// Starts the tween with the specified start and end values, duration, and scale function. + /// + /// The starting value of the tween. + /// The ending value of the tween. + /// The duration of the tween. + /// The scale function to be used for the tween. + void Start(T start, T end, float duration, ScaleFunc scaleFunc); + } +} diff --git a/LemonUI.SHVDN3/TinyTween/LerpFunc.cs b/LemonUI.SHVDN3/TinyTween/LerpFunc.cs new file mode 100644 index 0000000..2dced37 --- /dev/null +++ b/LemonUI.SHVDN3/TinyTween/LerpFunc.cs @@ -0,0 +1,12 @@ +namespace LemonUI.TinyTween +{ + /// + /// Represents a delegate for a lerp function used in a tween. + /// + /// The type of value being lerped. + /// The starting value of the lerp. + /// The ending value of the lerp. + /// The progress of the lerp. + /// The lerped value based on the progress. + public delegate T LerpFunc(T start, T end, float progress); +} diff --git a/LemonUI.SHVDN3/TinyTween/ScaleFunc.cs b/LemonUI.SHVDN3/TinyTween/ScaleFunc.cs new file mode 100644 index 0000000..7c2d89f --- /dev/null +++ b/LemonUI.SHVDN3/TinyTween/ScaleFunc.cs @@ -0,0 +1,9 @@ +namespace LemonUI.TinyTween +{ + /// + /// Represents a delegate for a scale function used in a tween. + /// + /// The progress of the tween. + /// The scaled value based on the progress. + public delegate float ScaleFunc(float progress); +} diff --git a/LemonUI.SHVDN3/TinyTween/ScaleFuncs.cs b/LemonUI.SHVDN3/TinyTween/ScaleFuncs.cs new file mode 100644 index 0000000..f2f5b93 --- /dev/null +++ b/LemonUI.SHVDN3/TinyTween/ScaleFuncs.cs @@ -0,0 +1,287 @@ +using System; + +namespace LemonUI.TinyTween +{ + /// + /// Provides a collection of scale functions for use in tweens. + /// + public static class ScaleFuncs + { + #region Fields + /// + /// Represents the value of Pi. + /// + public const float Pi = (float)Math.PI; + /// + /// Represents half of the value of Pi. + /// + public const float HalfPi = (float)Math.PI / 2; + /// + /// Represents a linear scale function. + /// + public static readonly ScaleFunc Linear = LinearImpl; + /// + /// Represents a quadratic ease-in scale function. + /// + public static readonly ScaleFunc QuadraticEaseIn = QuadraticEaseInImpl; + /// + /// Represents a quadratic ease-out scale function. + /// + public static readonly ScaleFunc QuadraticEaseOut = QuadraticEaseOutImpl; + /// + /// Represents a quadratic ease-in-out scale function. + /// + public static readonly ScaleFunc QuadraticEaseInOut = QuadraticEaseInOutImpl; + /// + /// Represents a cubic ease-in scale function. + /// + public static readonly ScaleFunc CubicEaseIn = CubicEaseInImpl; + /// + /// Represents a cubic ease-out scale function. + /// + public static readonly ScaleFunc CubicEaseOut = CubicEaseOutImpl; + /// + /// Represents a cubic ease-in-out scale function. + /// + public static readonly ScaleFunc CubicEaseInOut = CubicEaseInOutImpl; + /// + /// Represents a quartic ease-in scale function. + /// + public static readonly ScaleFunc QuarticEaseIn = QuarticEaseInImpl; + /// + /// Represents a quartic ease-out scale function. + /// + public static readonly ScaleFunc QuarticEaseOut = QuarticEaseOutImpl; + /// + /// Represents a quartic ease-in-out scale function. + /// + public static readonly ScaleFunc QuarticEaseInOut = QuarticEaseInOutImpl; + /// + /// Represents a quintic ease-in scale function. + /// + public static readonly ScaleFunc QuinticEaseIn = QuinticEaseInImpl; + /// + /// Represents a quintic ease-out scale function. + /// + public static readonly ScaleFunc QuinticEaseOut = QuinticEaseOutImpl; + /// + /// Represents a quintic ease-in-out scale function. + /// + public static readonly ScaleFunc QuinticEaseInOut = QuinticEaseInOutImpl; + /// + /// Represents a sine ease-in scale function. + /// + public static readonly ScaleFunc SineEaseIn = SineEaseInImpl; + /// + /// Represents a sine ease-out scale function. + /// + public static readonly ScaleFunc SineEaseOut = SineEaseOutImpl; + /// + /// Represents a sine ease-in-out scale function. + /// + public static readonly ScaleFunc SineEaseInOut = SineEaseInOutImpl; + #endregion + + #region Functions + /// + /// Returns the progress as is, resulting in a linear scale. + /// + /// The progress value between 0 and 1. + /// The linear scale value. + public static float LinearImpl(float progress) + { + return progress; + } + + /// + /// Applies quadratic ease-in to the progress. + /// + /// The progress value between 0 and 1. + /// The eased-in scale value. + public static float QuadraticEaseInImpl(float progress) + { + return EaseInPower(progress, 2); + } + + /// + /// Applies quadratic ease-out to the progress. + /// + /// The progress value between 0 and 1. + /// The eased-out scale value. + public static float QuadraticEaseOutImpl(float progress) + { + return EaseOutPower(progress, 2); + } + + /// + /// Applies quadratic ease-in-out to the progress. + /// + /// The progress value between 0 and 1. + /// The eased-in-out scale value. + public static float QuadraticEaseInOutImpl(float progress) + { + return EaseInOutPower(progress, 2); + } + + /// + /// Applies cubic ease-in to the progress. + /// + /// The progress value between 0 and 1. + /// The eased-in scale value. + public static float CubicEaseInImpl(float progress) + { + return EaseInPower(progress, 3); + } + + /// + /// Applies cubic ease-out to the progress. + /// + /// The progress value between 0 and 1. + /// The eased-out scale value. + public static float CubicEaseOutImpl(float progress) + { + return EaseOutPower(progress, 3); + } + + /// + /// Applies cubic ease-in-out to the progress. + /// + /// The progress value between 0 and 1. + /// The eased-in-out scale value. + public static float CubicEaseInOutImpl(float progress) + { + return EaseInOutPower(progress, 3); + } + + /// + /// Applies quartic ease-in to the progress. + /// + /// The progress value between 0 and 1. + /// Returns the progress value modified with quadratic ease-in. + public static float QuarticEaseInImpl(float progress) + { + return EaseInPower(progress, 4); + } + + /// + /// Applies quartic ease-out to the progress. + /// + /// The progress value between 0 and 1. + /// Returns the progress value modified with quadratic ease-out. + public static float QuarticEaseOutImpl(float progress) + { + return EaseOutPower(progress, 4); + } + + /// + /// Applies quartic ease-in-out to the progress. + /// + /// The progress value between 0 and 1. + /// Returns the progress value modified with quadratic ease-in-out. + public static float QuarticEaseInOutImpl(float progress) + { + return EaseInOutPower(progress, 4); + } + + /// + /// Applies quintic ease-in to the progress. + /// + /// The progress value between 0 and 1. + /// Returns the progress value modified with quintic ease-in. + public static float QuinticEaseInImpl(float progress) + { + return EaseInPower(progress, 5); + } + + /// + /// Applies quintic ease-out to the progress. + /// + /// The progress value between 0 and 1. + /// Returns the progress value modified with quintic ease-out. + public static float QuinticEaseOutImpl(float progress) + { + return EaseOutPower(progress, 5); + } + + /// + /// Applies quintic ease-in-out to the progress. + /// + /// The progress value between 0 and 1. + /// Returns the progress value modified with quintic ease-in-out. + public static float QuinticEaseInOutImpl(float progress) + { + return EaseInOutPower(progress, 5); + } + + /// + /// Applies ease-in power to the progress with the specified power. + /// + /// The progress value between 0 and 1. + /// The power value for the ease-out function. + /// The modified progress value with ease-in power. + public static float EaseInPower(float progress, int power) + { + return (float)Math.Pow(progress, power); + } + + /// + /// Applies ease-out power to the progress with the specified power. + /// + /// The progress value between 0 and 1. + /// The power value for the ease-out function. + /// The modified progress value with ease-out power. + public static float EaseOutPower(float progress, int power) + { + int sign = (power % 2 != 0) ? 1 : -1; + return (float)(sign * (Math.Pow(progress - 1f, power) + sign)); + } + + /// + /// Applies ease-in-out power to the progress with the specified power. + /// + /// The progress value between 0 and 1. + /// The power value for the ease-out function. + /// The modified progress value with ease-in-out power. + 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))); + } + + /// + /// Applies sine ease-in to the progress. + /// + /// The progress value between 0 and 1. + /// The modified progress value with sine ease-in. + public static float SineEaseInImpl(float progress) + { + return (float)Math.Sin((progress * (float)Math.PI / 2f) - ((float)Math.PI / 2f)) + 1f; + } + + /// + /// Applies sine ease-out to the progress. + /// + /// The progress value between 0 and 1. + /// The modified progress value with sine ease-out. + public static float SineEaseOutImpl(float progress) + { + return (float)Math.Sin(progress * (float)Math.PI / 2f); + } + + /// + /// Applies sine ease-in-out to the progress. + /// + /// The progress value between 0 and 1. + /// The modified progress value with sine ease-in-out. + public static float SineEaseInOutImpl(float progress) + { + return (float)(Math.Sin((progress * (float)Math.PI) - ((float)Math.PI / 2f)) + 1.0) / 2f; + } + #endregion + } +} diff --git a/LemonUI.SHVDN3/TinyTween/StopBehavior.cs b/LemonUI.SHVDN3/TinyTween/StopBehavior.cs new file mode 100644 index 0000000..222181a --- /dev/null +++ b/LemonUI.SHVDN3/TinyTween/StopBehavior.cs @@ -0,0 +1,17 @@ +namespace LemonUI.TinyTween +{ + /// + /// Enumeration representing different stop behaviors of a tween. + /// + public enum StopBehavior + { + /// + /// Represents the "as is" stop behavior of a tween. + /// + AsIs, + /// + /// Represents the "force complete" stop behavior of a tween. + /// + ForceComplete + } +} diff --git a/LemonUI.SHVDN3/TinyTween/Tween.cs b/LemonUI.SHVDN3/TinyTween/Tween.cs new file mode 100644 index 0000000..02fe76c --- /dev/null +++ b/LemonUI.SHVDN3/TinyTween/Tween.cs @@ -0,0 +1,172 @@ +using System; + +namespace LemonUI.TinyTween +{ + /// + /// Represents a generic tween with different stop behaviors. + /// + /// The type of value being tweened. + public class Tween : ITween, ITween + where T : struct + { + #region Fields + /// + /// The lerp function used for the tween. + /// + private readonly LerpFunc lerpFunc; + /// + /// The current time of the tween. + /// + private float currentTime; + /// + /// The duration of the tween. + /// + private float duration; + /// + /// The scale function used for the tween. + /// + private ScaleFunc scaleFunc; + /// + /// The state of the tween. + /// + private TweenState state; + /// + /// The starting value of the tween. + /// + private T start; + /// + /// The ending value of the tween. + /// + private T end; + /// + /// The current value of the tween. + /// + private T value; + /// + /// Gets the current time of the tween. + /// + public float CurrentTime => currentTime; + /// + /// Gets the duration of the tween. + /// + public float Duration => duration; + /// + /// Gets the state of the tween. + /// + public TweenState State => state; + /// + /// Gets the starting value of the tween. + /// + public T StartValue => start; + /// + /// Gets the ending value of the tween. + /// + public T EndValue => end; + /// + /// Gets the current value of the tween. + /// + public T CurrentValue => value; + #endregion + + #region Constructors + /// + /// Initializes a new instance of the Tween class with the specified lerp function. + /// + /// The lerp function used for the tween. + public Tween(LerpFunc lerpFunc) + { + this.lerpFunc = lerpFunc; + state = TweenState.Stopped; + } + #endregion + + #region Functions + /// + /// Starts the tween with the specified start and end values, duration, and scale function. + /// + /// The starting value of the tween. + /// The ending value of the tween. + /// The duration of the tween. + /// The scale function to be used for the tween. + /// Thrown when the duration is less than or equal to 0. + /// Thrown when the scaleFunc is null. + public void Start(T start, T end, float duration, ScaleFunc scaleFunc) + { + if (duration <= 0f) + { + throw new ArgumentException("duration must be greater than 0"); + } + if (scaleFunc == null) + { + throw new ArgumentNullException("scaleFunc"); + } + currentTime = 0f; + this.duration = duration; + this.scaleFunc = scaleFunc; + state = TweenState.Running; + this.start = start; + this.end = end; + UpdateValue(); + } + + /// + /// Pauses the tween if it is currently running. + /// + public void Pause() + { + if (state == TweenState.Running) + { + state = TweenState.Paused; + } + } + + /// + /// Resumes the tween if it is currently paused. + /// + public void Resume() + { + if (state == TweenState.Paused) + { + state = TweenState.Running; + } + } + + /// + /// Stops the tween with the specified stop behavior. + /// + /// The stop behavior to apply. + public void Stop(StopBehavior stopBehavior) + { + state = TweenState.Stopped; + if (stopBehavior == StopBehavior.ForceComplete) + { + currentTime = duration; + UpdateValue(); + } + } + + /// + /// Updates the tween with the specified elapsed time. + /// + /// The elapsed time since the last update. + public void Update(float elapsedTime) + { + if (state == TweenState.Running) + { + currentTime += elapsedTime; + if (currentTime >= duration) + { + currentTime = duration; + state = TweenState.Stopped; + } + UpdateValue(); + } + } + + private void UpdateValue() + { + value = lerpFunc(start, end, scaleFunc(currentTime / duration)); + } + #endregion + } +} \ No newline at end of file diff --git a/LemonUI.SHVDN3/TinyTween/TweenState.cs b/LemonUI.SHVDN3/TinyTween/TweenState.cs new file mode 100644 index 0000000..48fa5ec --- /dev/null +++ b/LemonUI.SHVDN3/TinyTween/TweenState.cs @@ -0,0 +1,21 @@ +namespace LemonUI.TinyTween +{ + /// + /// Enumeration representing different states of a tween. + /// + public enum TweenState + { + /// + /// Represents the running state of a tween. + /// + Running, + /// + /// Represents the paused state of a tween. + /// + Paused, + /// + /// Represents the stopped state of a tween. + /// + Stopped + } +} From 880b640dee17c6ca342b8cad624ca2b560ec5ef0 Mon Sep 17 00:00:00 2001 From: GravityScriptsV2 <95504918+GravityScriptsV2@users.noreply.github.com> Date: Sun, 26 Nov 2023 18:46:15 -0800 Subject: [PATCH 2/3] Fixed Location --- {LemonUI.SHVDN3 => LemonUI}/TinyTween/FloatTween.cs | 0 {LemonUI.SHVDN3 => LemonUI}/TinyTween/ITween.cs | 0 {LemonUI.SHVDN3 => LemonUI}/TinyTween/LerpFunc.cs | 0 {LemonUI.SHVDN3 => LemonUI}/TinyTween/ScaleFunc.cs | 0 {LemonUI.SHVDN3 => LemonUI}/TinyTween/ScaleFuncs.cs | 0 {LemonUI.SHVDN3 => LemonUI}/TinyTween/StopBehavior.cs | 0 {LemonUI.SHVDN3 => LemonUI}/TinyTween/Tween.cs | 0 {LemonUI.SHVDN3 => LemonUI}/TinyTween/TweenState.cs | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename {LemonUI.SHVDN3 => LemonUI}/TinyTween/FloatTween.cs (100%) rename {LemonUI.SHVDN3 => LemonUI}/TinyTween/ITween.cs (100%) rename {LemonUI.SHVDN3 => LemonUI}/TinyTween/LerpFunc.cs (100%) rename {LemonUI.SHVDN3 => LemonUI}/TinyTween/ScaleFunc.cs (100%) rename {LemonUI.SHVDN3 => LemonUI}/TinyTween/ScaleFuncs.cs (100%) rename {LemonUI.SHVDN3 => LemonUI}/TinyTween/StopBehavior.cs (100%) rename {LemonUI.SHVDN3 => LemonUI}/TinyTween/Tween.cs (100%) rename {LemonUI.SHVDN3 => LemonUI}/TinyTween/TweenState.cs (100%) diff --git a/LemonUI.SHVDN3/TinyTween/FloatTween.cs b/LemonUI/TinyTween/FloatTween.cs similarity index 100% rename from LemonUI.SHVDN3/TinyTween/FloatTween.cs rename to LemonUI/TinyTween/FloatTween.cs diff --git a/LemonUI.SHVDN3/TinyTween/ITween.cs b/LemonUI/TinyTween/ITween.cs similarity index 100% rename from LemonUI.SHVDN3/TinyTween/ITween.cs rename to LemonUI/TinyTween/ITween.cs diff --git a/LemonUI.SHVDN3/TinyTween/LerpFunc.cs b/LemonUI/TinyTween/LerpFunc.cs similarity index 100% rename from LemonUI.SHVDN3/TinyTween/LerpFunc.cs rename to LemonUI/TinyTween/LerpFunc.cs diff --git a/LemonUI.SHVDN3/TinyTween/ScaleFunc.cs b/LemonUI/TinyTween/ScaleFunc.cs similarity index 100% rename from LemonUI.SHVDN3/TinyTween/ScaleFunc.cs rename to LemonUI/TinyTween/ScaleFunc.cs diff --git a/LemonUI.SHVDN3/TinyTween/ScaleFuncs.cs b/LemonUI/TinyTween/ScaleFuncs.cs similarity index 100% rename from LemonUI.SHVDN3/TinyTween/ScaleFuncs.cs rename to LemonUI/TinyTween/ScaleFuncs.cs diff --git a/LemonUI.SHVDN3/TinyTween/StopBehavior.cs b/LemonUI/TinyTween/StopBehavior.cs similarity index 100% rename from LemonUI.SHVDN3/TinyTween/StopBehavior.cs rename to LemonUI/TinyTween/StopBehavior.cs diff --git a/LemonUI.SHVDN3/TinyTween/Tween.cs b/LemonUI/TinyTween/Tween.cs similarity index 100% rename from LemonUI.SHVDN3/TinyTween/Tween.cs rename to LemonUI/TinyTween/Tween.cs diff --git a/LemonUI.SHVDN3/TinyTween/TweenState.cs b/LemonUI/TinyTween/TweenState.cs similarity index 100% rename from LemonUI.SHVDN3/TinyTween/TweenState.cs rename to LemonUI/TinyTween/TweenState.cs From b596fa000ccc9392bc341b306f05ecb64cb2b823 Mon Sep 17 00:00:00 2001 From: GravityExploitz <95504918+GravityScriptsV2@users.noreply.github.com> Date: Tue, 5 Dec 2023 13:52:53 -0800 Subject: [PATCH 3/3] Update ScaleFuncs.cs --- LemonUI/TinyTween/ScaleFuncs.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/LemonUI/TinyTween/ScaleFuncs.cs b/LemonUI/TinyTween/ScaleFuncs.cs index f2f5b93..6345313 100644 --- a/LemonUI/TinyTween/ScaleFuncs.cs +++ b/LemonUI/TinyTween/ScaleFuncs.cs @@ -9,14 +9,6 @@ public static class ScaleFuncs { #region Fields /// - /// Represents the value of Pi. - /// - public const float Pi = (float)Math.PI; - /// - /// Represents half of the value of Pi. - /// - public const float HalfPi = (float)Math.PI / 2; - /// /// Represents a linear scale function. /// public static readonly ScaleFunc Linear = LinearImpl;