// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit.Utilities
{
///
/// Ease settings and functionality for animation with curves
///
[System.Serializable]
public class Easing
{
///
/// basic ease curves for quick settings
///
public enum BasicEaseCurves { Linear, EaseIn, EaseOut, EaseInOut }
///
/// Is the ease enabled?
///
public bool Enabled = false;
///
/// The animation curve to use for the ease - default should be linear
///
public AnimationCurve Curve = AnimationCurve.Linear(0, 1, 1, 1);
///
/// The amount of time the ease should run in seconds
///
public float LerpTime = 0.5f;
private float timer = 0.5f;
private Keyframe[] cachedKeys;
public Easing()
{
Stop();
}
///
/// Create Easing object with copied internal properties
///
/// Copy of current Easing instance
public Easing Copy()
{
return new Easing()
{
Curve = this.Curve,
Enabled = this.Enabled,
LerpTime = this.LerpTime,
};
}
///
/// Update the ease each frame or on Update
///
public void OnUpdate()
{
if (timer < LerpTime)
{
timer = Mathf.Min(timer + Time.deltaTime, LerpTime);
}
}
///
/// start the ease if enabled
///
public void Start()
{
timer = 0;
if (!Enabled)
{
timer = LerpTime;
}
}
///
/// Is the ease currently running?
///
public bool IsPlaying()
{
return timer < LerpTime;
}
///
/// stop the ease
///
public void Stop()
{
timer = LerpTime;
}
///
/// get the linear ease value
///
public float GetLinear()
{
return timer / LerpTime;
}
///
/// get the ease value based on the animation curve
///
public float GetCurved()
{
return IsLinear() ? GetLinear() : Curve.Evaluate(GetLinear());
}
protected bool IsLinear()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
// If we're in the editor and we're not playing
// always update the cached keys to reflect recent changes
cachedKeys = Curve.keys;
}
#endif
if (cachedKeys == null)
{
cachedKeys = Curve.keys;
}
if (cachedKeys.Length > 1)
{
return (cachedKeys[0].value == 1 && cachedKeys[1].value == 1);
}
return false;
}
///
/// set the animation curve using a preset
///
public void SetCurve(BasicEaseCurves curve)
{
AnimationCurve animation = AnimationCurve.Linear(0, 1, 1, 1);
switch (curve)
{
case BasicEaseCurves.EaseIn:
animation = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1, 2.5f, 0));
break;
case BasicEaseCurves.EaseOut:
animation = new AnimationCurve(new Keyframe(0, 0, 0, 2.5f), new Keyframe(1, 1));
break;
case BasicEaseCurves.EaseInOut:
animation = AnimationCurve.EaseInOut(0, 0, 1, 1);
break;
default:
break;
}
Curve = animation;
// Update the cached keys
cachedKeys = Curve.keys;
}
}
}