// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using System;
using UnityEngine;
namespace UltimateXR.Animation.Interpolation
{
///
/// Describes the different parameters of an interpolation.
///
[Serializable]
public class UxrInterpolationSettings
{
#region Inspector Properties/Serialized Fields
[SerializeField] private float _durationSeconds;
[SerializeField] private float _delaySeconds;
[SerializeField] private UxrEasing _easing;
[SerializeField] private UxrLoopMode _loopMode;
[SerializeField] private float _loopedDurationSeconds;
[SerializeField] private bool _useUnscaledTime;
[SerializeField] private bool _delayUsingEndValue;
#endregion
#region Public Types & Data
///
/// Gets or sets the interpolation duration in seconds. In looped interpolations it tells the duration of a single
/// loop.
///
public float DurationSeconds
{
get => _durationSeconds;
set => _durationSeconds = value;
}
///
/// Gets or sets the interpolation delay in seconds. The delay is usually relative to the time the object that it uses
/// was enabled and specifies an initial waiting time before the actual interpolation will start.
///
public float DelaySeconds
{
get => _delaySeconds;
set => _delaySeconds = value;
}
///
/// Gets or sets the easing function to use by the interpolation.
///
public UxrEasing Easing
{
get => _easing;
set => _easing = value;
}
///
/// Gets or sets if and how to loop the interpolation.
///
public UxrLoopMode LoopMode
{
get => _loopMode;
set => _loopMode = value;
}
///
/// Gets or sets the total animation duration in interpolations that use looping. The duration of a single loop is
/// described by . A negative value tells to loop indefinitely.
///
public float LoopedDurationSeconds
{
get => _loopedDurationSeconds;
set => _loopedDurationSeconds = value;
}
///
/// Gets or sets whether to use unscaled time () or regular time
/// when interpolating.
/// Regular time is affected by , which is normally used to pause the application or
/// simulate slow motion effects.
///
public bool UseUnscaledTime
{
get => _useUnscaledTime;
set => _useUnscaledTime = value;
}
///
/// Gets or sets whether to use the interpolation end value during the initial delay, if there is a
/// value specified.
/// By default the interpolation uses the start value during the initial delay.
///
public bool DelayUsingEndValue
{
get => _delayUsingEndValue;
set => _delayUsingEndValue = value;
}
#endregion
#region Constructors & Finalizer
///
/// Default constructor
///
public UxrInterpolationSettings()
{
_durationSeconds = 0.0f;
_delaySeconds = 0.0f;
_easing = UxrEasing.Linear;
_loopMode = UxrLoopMode.None;
_loopedDurationSeconds = -1.0f;
_useUnscaledTime = false;
_delayUsingEndValue = false;
}
///
/// UxrInterpolationSettings constructor.
///
///
/// The duration in seconds the interpolation will be applied. If a loopMode was specified, it tells the duration of a
/// single loop.
///
/// The delay in seconds before the interpolation
/// The type of interpolation used.
/// The type of looping used.
///
/// If loopMode is not LoopMode.None this parameter will tell how many seconds the total duration of
/// the interpolation will last and durationSeconds will tell the duration of each loop. A negative value means it will
/// loop forever.
///
///
/// Tells whether to use the real timer value (true) or the scaled
/// value (false) which is affected by .
///
///
/// Tells whether to use the interpolation end value during the delay, if there is a
/// specified. By default it's false, which means the interpolation start value is
/// used during the delay.
///
public UxrInterpolationSettings(float durationSeconds,
float delaySeconds = 0.0f,
UxrEasing easing = UxrEasing.Linear,
UxrLoopMode loopMode = UxrLoopMode.None,
float loopedDurationSeconds = -1.0f,
bool useUnscaledTime = false,
bool delayUsingEndValue = false)
{
_durationSeconds = durationSeconds;
_delaySeconds = delaySeconds;
_easing = easing;
_loopMode = loopMode;
_loopedDurationSeconds = loopedDurationSeconds;
_useUnscaledTime = useUnscaledTime;
_delayUsingEndValue = delayUsingEndValue;
}
#endregion
#region Public Methods
///
/// Gets the T value used for linear interpolations given a time value.
///
/// Time value
/// The interpolation t value required to linearly interpolate using the current parameters
public float GetInterpolationFactor(float time)
{
return UxrInterpolator.Interpolate(0.0f, 1.0f, _durationSeconds, _delaySeconds, time, _easing, _loopMode, _loopedDurationSeconds, _delayUsingEndValue);
}
///
/// Checks if the given time has surpassed the interpolation duration.
///
/// Time value
/// Boolean telling whether the interpolation has finished
public bool CheckInterpolationHasFinished(float time)
{
if (LoopMode == UxrLoopMode.None && time > DelaySeconds + DurationSeconds)
{
return true;
}
if (LoopMode != UxrLoopMode.None && time > DelaySeconds + LoopedDurationSeconds && LoopedDurationSeconds >= 0.0f)
{
return true;
}
return false;
}
#endregion
}
}