/// <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> /// 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(Vector2 fromValue, Vector2 toValue, LerpBehaviors behavior) { _ = behavior; from = fromValue; to = toValue; range = new Vector2(to.X - from.X, to.Y - from.Y); }
/// <summary> /// Whether this tween handles rotation. /// </summary> /// <returns>A reference to this.</returns> public ITweenable Rotation(RotationUnit unit = RotationUnit.Degrees) { behavior |= LerpBehaviors.Rotation; behavior |= (unit == RotationUnit.Degrees) ? LerpBehaviors.RotationDegrees : LerpBehaviors.RotationRadians; return(this); }
/// <summary> /// Initialize. /// </summary> /// <param name="fromValue">The fromValue.</param> /// <param name="toValue">The toValue.</param> /// <param name="behavior">The behavior.</param> public void Initialize(PointF fromValue, PointF toValue, LerpBehaviors behavior) { _ = behavior; from = fromValue; to = toValue; range = new PointF(to.X - from.X, to.Y - from.Y); }
/// <summary> /// Initialize. /// </summary> /// <param name="fromValue">The fromValue.</param> /// <param name="toValue">The toValue.</param> /// <param name="behavior">The behavior.</param> public void Initialize(SizeF fromValue, SizeF toValue, LerpBehaviors behavior) { _ = behavior; from = fromValue; to = toValue; range = new SizeF(to.Width - from.Width, to.Height - from.Height); }
/// <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) { _ = behavior; from = (SizeF)fromValue; to = (SizeF)toValue; range = new SizeF(to.Width - from.Width, to.Height - from.Height); }
/// <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> /// Initializes a new instance of the <see cref="Tween"/> class. /// </summary> /// <param name="target">The target.</param> /// <param name="duration">The duration.</param> /// <param name="delay">The delay.</param> /// <param name="parent">The parent.</param> internal Tween(object?target, float duration, float delay, Tweener parent) { ease = null; begin = null; update = null; complete = null; Target = target ?? default !; this.duration = duration; this.delay = delay; this.parent = parent; Remover = parent; firstUpdate = true; varHash = new(); vars = new(); lerpers = new(); start = new(); end = new(); behavior = LerpBehaviors.None; }
/// <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 = (Vector2)fromValue; to = (Vector2)toValue; range = new Vector2(to.X - from.X, to.Y - from.Y); }
/// <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 = (PointF)fromValue; to = (PointF)toValue; range = new PointF(to.X - from.X, to.Y - from.Y); }
/// <summary> /// Whether tweened values should be rounded to integer values. /// </summary> /// <returns>A reference to this.</returns> public ITweenable Round() { behavior |= LerpBehaviors.Round; return(this); }
/// <summary> /// Sets the tween to reverse every other time it repeats. Repeating must be enabled for this to have any effect. /// </summary> /// <returns>A reference to this.</returns> public ITweenable Reflect() { behavior |= LerpBehaviors.Reflect; return(this); }