/// <summary> /// Initialize. /// </summary> /// <param name="fromValue">The fromValue.</param> /// <param name="toValue">The toValue.</param> /// <param name="behavior">The behavior.</param> public void Initialize(object fromValue, object toValue, LerpBehaviors behavior) { from = Convert.ToSingle(fromValue, CultureInfo.InvariantCulture); to = Convert.ToSingle(toValue, CultureInfo.InvariantCulture); range = to - from; if (behavior.HasFlag(LerpBehaviors.Rotation)) { var angle = from; if (behavior.HasFlag(LerpBehaviors.RotationRadians)) { angle *= Degree; } if (angle < 0d) { angle = 360f + angle; } var r = angle + range; var d = r - angle; var a = Abs(d); range = a >= 180f ? (360f - a) * (d > 0f ? -1f : 1f) : d; } }
/// <summary> /// Initialize. /// </summary> /// <param name="fromValue">The fromValue.</param> /// <param name="toValue">The toValue.</param> /// <param name="behavior">The behavior.</param> public void Initialize(float fromValue, float toValue, LerpBehaviors behavior) { from = fromValue; to = toValue; range = to - from; if (behavior.HasFlag(LerpBehaviors.Rotation)) { var angle = from; if (behavior.HasFlag(LerpBehaviors.RotationRadians)) { angle *= Degree; } if (angle < 0d) { angle = 360f + angle; } var r = angle + range; var d = r - angle; var a = Abs(d); range = a >= 180f ? (360f - a) * (d > 0f ? -1f : 1f) : d; } }
/// <summary> /// The interpolate. /// </summary> /// <param name="t">The t.</param> /// <param name="currentValue">The currentValue.</param> /// <param name="behavior">The behavior.</param> /// <returns>The <see cref="object"/>.</returns> public object Interpolate(float t, object currentValue, LerpBehaviors behavior) { var value = from + (range * t); if (behavior.HasFlag(LerpBehaviors.Rotation)) { if (behavior.HasFlag(LerpBehaviors.RotationRadians)) { value *= Degree; } value %= 360f; if (value < 0) { value += 360f; } if (behavior.HasFlag(LerpBehaviors.RotationRadians)) { value *= Radian; } } if (behavior.HasFlag(LerpBehaviors.Round)) { value = Round(value); } var type = currentValue?.GetType() ?? default; return(Convert.ChangeType(value, type !, CultureInfo.InvariantCulture)); }
/// <summary> /// The interpolate. /// </summary> /// <param name="t">The t.</param> /// <param name="currentValue">The currentValue.</param> /// <param name="behavior">The behavior.</param> /// <returns>The <see cref="object"/>.</returns> public object Interpolate(float t, object currentValue, LerpBehaviors behavior) { var i = from.X + (range.X * t); var j = from.Y + (range.Y * t); if (behavior.HasFlag(LerpBehaviors.Round)) { i = Round(i); j = Round(j); } var current = (Vector2)currentValue; return(new Vector2( (Abs(range.X) < float.Epsilon) ? current.X : i, (Abs(range.Y) < float.Epsilon) ? current.Y : j)); }
/// <summary> /// The interpolate. /// </summary> /// <param name="t">The t.</param> /// <param name="currentValue">The currentValue.</param> /// <param name="behavior">The behavior.</param> /// <returns>The <see cref="object"/>.</returns> public object Interpolate(float t, object currentValue, LerpBehaviors behavior) { var x = from.X + (range.X * t); var y = from.Y + (range.Y * t); if (behavior.HasFlag(LerpBehaviors.Round)) { x = Round(x); y = Round(y); } var current = (PointF)currentValue; return(new PointF( (Abs(range.X) < float.Epsilon) ? current.X : x, (Abs(range.Y) < float.Epsilon) ? current.X : y)); }
/// <summary> /// The interpolate. /// </summary> /// <param name="t">The t.</param> /// <param name="currentValue">The currentValue.</param> /// <param name="behavior">The behavior.</param> /// <returns>The <see cref="object"/>.</returns> public object Interpolate(float t, object currentValue, LerpBehaviors behavior) { var width = from.Width + (range.Width * t); var height = from.Height + (range.Height * t); if (behavior.HasFlag(LerpBehaviors.Round)) { width = Round(width); height = Round(height); } var current = (SizeF)currentValue; return(new SizeF( (Abs(range.Width) < float.Epsilon) ? current.Width : width, (Abs(range.Height) < float.Epsilon) ? current.Height : height)); }
/// <summary> /// Update. /// </summary> /// <param name="elapsed">The elapsed.</param> public void UpdateTween(float elapsed) { if (firstUpdate) { firstUpdate = false; var i = vars.Count; while (i-- > 0) { if (start?[i] is not null && end?[i] is not null) { lerpers[i]?.Initialize(start[i] !, end[i] !, behavior); } } } else { if (Paused) { return; } if (delay > 0) { delay -= elapsed; if (delay > 0) { return; } } if (time == 0 && timesRepeated == 0 && begin is not null) { begin(); } time += elapsed; var setTimeTo = time; var t = time / duration; var doComplete = false; if (time >= duration) { if (repeatCount != 0) { setTimeTo = 0; delay = repeatDelay; timesRepeated++; if (repeatCount > 0) { --repeatCount; } doComplete |= repeatCount < 0; } else { time = duration; t = 1; Remover.Remove(this); doComplete = true; } } if (ease is not null) { t = ease(t); } var i = vars.Count; while (i-- > 0) { if (vars?[i] is not null && vars[i]?.Value is not null) { vars[i] !.Value = lerpers[i]?.Interpolate(t, vars[i] !.Value !, behavior); } } time = setTimeTo; // If the timer is zero here, we just restarted. // If reflect mode is on, flip start to end if (time == 0 && behavior.HasFlag(LerpBehaviors.Reflect)) { Reverse(); } update?.Invoke(); if (doComplete && complete is not null) { complete(); } } }