public TweenProcess(string description, EasingDelegate easingFunction, TimeSpan duration, Action <Interpolation> callback) { _description = description; _easingFunction = easingFunction; _durationInSeconds = duration.TotalSeconds; _callback = callback; }
public TweenProcess(string description, EasingDelegate easingFunction, TimeSpan duration, Action<Interpolation> callback) { _description = description; _easingFunction = easingFunction; _durationInSeconds = duration.TotalSeconds; _callback = callback; }
public static EasingDelegate Reverse(EasingDelegate easingFunction) { return(percentage => { return easingFunction(1.0 - percentage); }); }
public static EasingDelegate ConvertTo(EasingKind kind, EasingDelegate easingFunction) { switch (kind) { case EasingKind.EaseIn: return (percentage => { return easingFunction(percentage); }); case EasingKind.EaseOut: return (percentage => { return 1.0 - easingFunction(1.0 - percentage); }); case EasingKind.EaseInOut: return (percentage => { if (percentage >= 0.5) { return (1.0 - easingFunction((1.0 - percentage) * 2.0)) * 0.5 + 0.5; } else { return easingFunction(percentage * 2.0) * 0.5; } }); } throw new Exception("Unknown Easing Kind: " + kind.ToString()); }
public static EasingDelegate ConvertTo(EasingKind kind, EasingDelegate easingFunction) { switch (kind) { case EasingKind.EaseIn: return(percentage => { return easingFunction(percentage); }); case EasingKind.EaseOut: return(percentage => { return 1.0 - easingFunction(1.0 - percentage); }); case EasingKind.EaseInOut: return(percentage => { if (percentage >= 0.5) { return (1.0 - easingFunction((1.0 - percentage) * 2.0)) * 0.5 + 0.5; } else { return easingFunction(percentage * 2.0) * 0.5; } }); } throw new Exception("Unknown Easing Kind: " + kind.ToString()); }
private static void Register(EEasingMethod type, EasingDelegate inDelegate) { int index = ((int)type) * METHOD_COUNT; Methods[index] = inDelegate; Methods[index + 1] = CreateOut(inDelegate); Methods[index + 2] = CreateInOut(inDelegate); }
public MoveToComponent(Vector2 destination, TimeSpan duration, EasingDelegate easingX, EasingDelegate easingY, float augmentX, float augmentY) { _destination = destination; _duration = duration; _easingX = easingX; _easingY = easingY; _augmentX = augmentX; _augmentY = augmentY; _elapsedTime = TimeSpan.FromSeconds(0); }
private static EasingDelegate CreateInOut(EasingDelegate inDelegate) { return((float t) => { if (t < 0.5f) { return inDelegate(t * 2f) * 0.5f; } else { return 1f - inDelegate((1f - t) * 2f) * 0.5f; } }); }
IEnumerator TranslateCoroutine(float duration, Vector3 startPos, Vector3 endPos, EasingDelegate easingFunc, Action endAction) { float elapsedTime = 0; while (elapsedTime < duration) { float k = elapsedTime / duration; //transform.position = Vector3.Lerp(startPos, endPos, Mathf.Sin(k*Mathf.PI*.5f)); //transform.position = Vector3.Lerp(startPos, endPos, 1-Mathf.Pow(k, 4)); //transform.position = Vector3.Lerp(startPos, endPos, 1-Mathf.Pow(k-1, 2)); transform.position = Vector3.Lerp(startPos, endPos, easingFunc(k)); elapsedTime += Time.deltaTime; yield return(null); } transform.position = endPos; if (endAction != null) { endAction(); } //StartCoroutine(TranslateCoroutine(duration, endPos, startPos)); }
public MoveToComponent(Vector2 destination, TimeSpan duration, EasingDelegate easingX, EasingDelegate easingY) : this(destination, duration, easingX, easingY, 0, 0) { }
/// <summary> /// Updates an easing function. /// </summary> private void UpdateFunction() { m_easingFunction = Easing.EasingFunction.GetEasingFunction( m_category, m_easeType); }
private static EasingDelegate CreateOut(EasingDelegate inDelegate) { return((float t) => (1f - inDelegate(1f - t))); }
public TweenProcess(EasingDelegate easingFunction, TimeSpan duration, Action<Interpolation> callback) : this(string.Empty, easingFunction, duration, callback) { }
/// <summary> /// Animate a control property from its present value to a target one /// </summary> /// <param name="control">Target control</param> /// <param name="iEffect">Effect to apply</param> /// <param name="easing">Easing function to apply</param> /// <param name="valueToReach">Target value reached when animation completes</param> /// <param name="duration">Amount of time taken to reach the target value</param> /// <param name="delay">Amount of delay to apply before animation starts</param> /// <param name="reverse">If set to true, animation reaches target value and animates back to initial value. It takes 2*<paramref name="duration"/></param> /// <param name="loops">If reverse is set to true, indicates how many loops to perform. Negatives or zero mean infinite loop</param> /// <returns></returns> public static AnimationStatus Animate(Control control, IEffect iEffect, EasingDelegate easing, int valueToReach, int duration, int delay, bool reverse = false, int loops = 1) { try { //used to calculate animation frame based on how much time has effectively passed var stopwatch = new Stopwatch(); //used to cancel animation var cancelTokenSource = new CancellationTokenSource(); //used to access animation progress var animationStatus = new AnimationStatus(cancelTokenSource, stopwatch) { Effect = iEffect }; //This timer allows delayed start. Control's state checks and evaluations are delayed too. new System.Threading.Timer(state => { //is there anything to do here? int originalValue = iEffect.GetCurrentValue(control); if (originalValue == valueToReach) { animationStatus.IsCompleted = true; return; } //upper bound check int maxVal = iEffect.GetMaximumValue(control); if (valueToReach > maxVal) { string msg = "Value must be lesser than the maximum allowed. " + $"Max: {maxVal}, provided value: {valueToReach}"; throw new ArgumentException(msg, "valueToReach"); } //lower bound check int minVal = iEffect.GetMinimumValue(control); if (valueToReach < iEffect.GetMinimumValue(control)) { string msg = "Value must be greater than the minimum allowed. " + $"Min: {minVal}, provided value: {valueToReach}"; throw new ArgumentException(msg, "valueToReach"); } bool reversed = false; int performedLoops = 0; int actualValueChange = Math.Abs(originalValue - valueToReach); System.Timers.Timer animationTimer = new System.Timers.Timer(); double interval = (duration > actualValueChange) ? (duration / actualValueChange) : actualValueChange; // Fixes the short duration animation bug in long distance, avoiding animation crashes. if (interval > duration) { double newinterval = interval / duration; interval = newinterval; } animationTimer.Interval = interval; //adjust interval (naive, edge cases can mess up) //because of naive interval calculation this is required if (iEffect.Interaction == EffectInteractions.COLOR) { animationTimer.Interval = 10; } if (!control.IsDisposed) { //main animation timer tick animationTimer.Elapsed += (o, e2) => { if (!control.IsDisposed) { //cancellation support if (cancelTokenSource.Token.IsCancellationRequested) { animationStatus.IsCompleted = true; animationTimer.Stop(); stopwatch.Stop(); return; } //main logic bool increasing = originalValue < valueToReach; int minValue = Math.Min(originalValue, valueToReach); int maxValue = Math.Abs(valueToReach - originalValue); int newValue = (int)easing(stopwatch.ElapsedMilliseconds, minValue, maxValue, duration); if (!increasing) { newValue = originalValue + valueToReach - newValue - 1; } control.BeginInvoke(new MethodInvoker(() => { if (!control.IsDisposed && control.IsHandleCreated) { iEffect.SetValue(control, originalValue, valueToReach, newValue); bool timeout = stopwatch.ElapsedMilliseconds >= duration; if (timeout) { if (reverse && (!reversed || loops <= 0 || performedLoops < loops)) { reversed = !reversed; if (reversed) { performedLoops++; } int initialValue = originalValue; int finalValue = valueToReach; valueToReach = valueToReach == finalValue ? initialValue : finalValue; originalValue = valueToReach == finalValue ? initialValue : finalValue; stopwatch.Restart(); animationTimer.Start(); } else { animationStatus.IsCompleted = true; animationTimer.Stop(); stopwatch.Stop(); if (Animated != null) { Animated(control, animationStatus); } } } } else { if (Animated != null) { Animated(control, animationStatus); } } })); } }; } //start stopwatch.Start(); animationTimer.Start(); }, null, delay, Timeout.Infinite); return(animationStatus); } catch (Exception e) { if (!Debugger.IsAttached) { return(null); } Debug.Print($"Exception @ VisualEffects.Animator.Animate(). Message: {e.Message}"); Debug.Print($"Stack:\n{e.Message}"); } return(null); }
public GoToTrajectory(Vector2 origin, Vector2 destination, EasingDelegate easing = null) { Origin = origin; Destination = destination; Easing = easing; }
public static EasingDelegate Reverse(EasingDelegate easingFunction) { return (percentage => { return easingFunction(1.0 - percentage); }); }
/// <summary> /// Animates the specified control. /// </summary> /// <param name="control">The control.</param> /// <param name="iAnimation">The ianimation.</param> /// <param name="easing">The easing.</param> /// <param name="valueToReach">The value to reach.</param> /// <param name="duration">The duration.</param> /// <param name="delay">The delay.</param> /// <param name="reverse">if set to <c>true</c> [reverse].</param> /// <param name="loops">The loops.</param> /// <returns>AnimationStatus.</returns> public static AnimationStatus Animate(this Control control, IEffect iAnimation, EasingDelegate easing, int valueToReach, int duration, int delay, bool reverse = false, int loops = 1) { return(ZeroitVisAnim.Animate(control, iAnimation, easing, valueToReach, duration, delay, reverse, loops)); }
public TweenProcess(EasingDelegate easingFunction, TimeSpan duration, Action <Interpolation> callback) : this(string.Empty, easingFunction, duration, callback) { }