/// <summary> /// Creates a new Interpolator. /// </summary> /// <param name="start">The starting value.</param> /// <param name="end">The ending value.</param> /// <param name="length">The length of time, in seconds, to perform the interpolation.</param> /// <param name="scale">A method to perform</param> /// <param name="step">An optional callback to invoke when the Interpolator is updated.</param> /// <param name="completed">An optional callback to invoke when the Interpolator completes.</param> /// <returns>The Interpolator instance.</returns> public static Interpolator Create( float start, float end, float length, InterpolatorScaleDelegate scale, InterpolatorEaseDirection direction, Action <Interpolator> step, Action <Interpolator> completed) { if (length <= 0f) { throw new ArgumentException("length must be greater than zero"); } if (scale == null) { throw new ArgumentNullException("scale"); } var i = Interpolators.New(); i.Start = start; i.End = end; i._range = end - start; i._step = step; i._completed = completed; i._scale = scale; i._speed = 1f / length; i._direction = direction; return(i); }
private static float ExponentialInterpolation(float progress, InterpolatorEaseDirection direction) { switch (direction) { case InterpolatorEaseDirection.In: return((progress == 0f) ? 0f : (float)Math.Pow(2f, 10f * (progress - 1f))); case InterpolatorEaseDirection.Out: return((progress == 1f) ? 0f : -(float)Math.Pow(2f, (-10f * progress) + 1f)); case InterpolatorEaseDirection.InOut: progress *= 2f; if (progress < 1f) { return(0.5f * (float)Math.Pow(2f, 10f * (progress - 1))); } return(0.5f * -(float)Math.Pow(2f, -10f * (progress - 1)) + 2f); default: return((progress == 0f) ? 0f : (float)Math.Pow(2f, 10f * (progress - 1f))); } }
private static float QuadraticInterpolation(float progress, InterpolatorEaseDirection direction) { switch (direction) { case InterpolatorEaseDirection.In: return(progress * progress); case InterpolatorEaseDirection.Out: return(-progress * (progress - 2f)); case InterpolatorEaseDirection.InOut: progress *= 2f; if (progress < 1f) { return(0.5f * progress * progress); } progress--; return(-0.5f * (progress * (progress - 2f) - 1f)); default: return(progress * progress); } }
private static float BackInterpolation(float progress, InterpolatorEaseDirection direction) { switch (direction) { case InterpolatorEaseDirection.In: return(progress * progress * (2.70158f * progress - 1.70158f)); case InterpolatorEaseDirection.Out: progress--; return(progress * progress * (2.70158f * progress + 1.70158f) + 1f); case InterpolatorEaseDirection.InOut: progress *= 2f; if (progress < 1f) { return(0.5f * progress * progress * (3.59491f * progress - 2.59491f)); } progress -= 2; return(0.5f * progress * progress * (3.59491f * progress - 2.59491f) + 2f); default: return(progress * progress * (2.70158f * progress - 1.70158f)); } }
private static float CircularInterpolation(float progress, InterpolatorEaseDirection direction) { switch (direction) { case InterpolatorEaseDirection.In: return(-(float)(Math.Sqrt(1f - progress * progress) - 1f)); case InterpolatorEaseDirection.Out: progress--; return((float)Math.Sqrt(1f - progress * progress)); case InterpolatorEaseDirection.InOut: progress *= 2f; if (progress < 1) { return(-0.5f * (float)Math.Sqrt(1f - progress * progress) - 1f); } progress -= 2; return(0.5f * (float)Math.Sqrt(1f - progress * progress) + 1f); default: return(-(float)(Math.Sqrt(1f - progress * progress) - 1f)); } }
private static float LinearInterpolation(float progress, InterpolatorEaseDirection direction) { return(progress); }
private static float BackInterpolation(float progress, InterpolatorEaseDirection direction) { switch (direction) { case InterpolatorEaseDirection.In: return progress * progress * (2.70158f * progress - 1.70158f); case InterpolatorEaseDirection.Out: progress--; return progress * progress * (2.70158f * progress + 1.70158f) + 1f; case InterpolatorEaseDirection.InOut: progress *= 2f; if (progress < 1f) return 0.5f * progress * progress * (3.59491f * progress - 2.59491f); progress -= 2; return 0.5f * progress * progress * (3.59491f * progress - 2.59491f) + 2f; default: return progress * progress * (2.70158f * progress - 1.70158f); } }
private static float CircularInterpolation(float progress, InterpolatorEaseDirection direction) { switch (direction) { case InterpolatorEaseDirection.In: return -(float)(Math.Sqrt(1f - progress * progress) - 1f); case InterpolatorEaseDirection.Out: progress--; return (float)Math.Sqrt(1f - progress * progress); case InterpolatorEaseDirection.InOut: progress *= 2f; if (progress < 1) return -0.5f * (float)Math.Sqrt(1f - progress * progress) - 1f; progress -= 2; return 0.5f * (float)Math.Sqrt(1f - progress * progress) + 1f; default: return -(float)(Math.Sqrt(1f - progress * progress) - 1f); } }
private static float ExponentialInterpolation(float progress, InterpolatorEaseDirection direction) { switch (direction) { case InterpolatorEaseDirection.In: return (progress == 0f) ? 0f : (float)Math.Pow(2f, 10f * (progress - 1f)); case InterpolatorEaseDirection.Out: return (progress == 1f) ? 0f : -(float)Math.Pow(2f, (-10f * progress) + 1f); case InterpolatorEaseDirection.InOut: progress *= 2f; if (progress < 1f) return 0.5f * (float)Math.Pow(2f, 10f * (progress - 1)); return 0.5f * -(float)Math.Pow(2f, -10f * (progress - 1)) + 2f; default: return (progress == 0f) ? 0f : (float)Math.Pow(2f, 10f * (progress - 1f)); } }
private static float QuinticInterpolation(float progress, InterpolatorEaseDirection direction) { switch (direction) { case InterpolatorEaseDirection.In: return progress * progress * progress * progress * progress; case InterpolatorEaseDirection.Out: progress--; return (progress * progress * progress * progress * progress - 1f); case InterpolatorEaseDirection.InOut: progress *= 2f; if (progress < 1) return 0.5f * progress * progress * progress * progress * progress; progress -= 2; return -0.5f * (progress * progress * progress * progress * progress - 2f); default: return progress * progress * progress * progress * progress; } }
private static float LinearInterpolation(float progress, InterpolatorEaseDirection direction) { return progress; }
/// <summary> /// Creates a new Interpolator. /// </summary> /// <param name="start">The starting value.</param> /// <param name="end">The ending value.</param> /// <param name="length">The length of time, in seconds, to perform the interpolation.</param> /// <param name="scale">A method to perform</param> /// <param name="step">An optional callback to invoke when the Interpolator is updated.</param> /// <param name="completed">An optional callback to invoke when the Interpolator completes.</param> /// <returns>The Interpolator instance.</returns> public static Interpolator Create( float start, float end, float length, InterpolatorScaleDelegate scale, InterpolatorEaseDirection direction, Action<Interpolator> step, Action<Interpolator> completed) { if (length <= 0f) throw new ArgumentException("length must be greater than zero"); if (scale == null) throw new ArgumentNullException("scale"); var i = Interpolators.New(); i.Start = start; i.End = end; i._range = end - start; i._step = step; i._completed = completed; i._scale = scale; i._speed = 1f / length; i._direction = direction; return i; }