/* INFINITY CODE 2018-2019 */ /* http://www.infinity-code.com */ using InfinityCode.uPano.InteractiveElements; using UnityEngine; namespace InfinityCode.uPano.Actions { /// /// Base class for animated Interactive Element actions /// /// Type of AnimatedAction /// Type of animated value public abstract class AnimatedAction : InteractiveElementAction where T: AnimatedAction { /// /// The action must be animated? /// public bool animated = true; /// /// Animation curve /// public AnimationCurve curve = AnimationCurve.Linear(0, 0, 1, 1); /// /// Delay of the animation /// public float delay = 0; /// /// Duration of the animation /// public float duration = 0.2f; protected static AnimatedAction activeAnimation; protected bool started; protected float time; protected InteractiveElement element; protected U initialValue; public float totalDuration { get { return delay + duration; } } public override void Invoke(InteractiveElement element) { StopPreviousAnimation(); this.element = element; if (!animated || totalDuration <= 0) SetFixedValue(); else { activeAnimation = this; time = 0; StoreInitialValue(); started = true; } } protected abstract void SetAnimatedValue(float f); protected abstract void SetFixedValue(); protected virtual void StopPreviousAnimation() { if (activeAnimation == null) return; activeAnimation.SetFixedValue(); activeAnimation.started = false; activeAnimation = null; } protected abstract void StoreInitialValue(); protected virtual void Update() { if (!started) return; time += Time.deltaTime; if (time >= totalDuration) { time = totalDuration; started = false; activeAnimation = null; } float f; if (duration > 0) f = curve.Evaluate((time - delay) / duration); else f = time < delay? 0: 1; if (f < 0) f = 0; SetAnimatedValue(f); } } }