// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using System;
using UltimateXR.Animation.Interpolation;
using UltimateXR.Extensions.Unity;
using UnityEngine;
using UnityEngine.UI;
namespace UltimateXR.Animation.UI
{
///
/// Tweening component to animate the of an an component
/// programatically or using the inspector.
///
[RequireComponent(typeof(Image))]
public class UxrImageFillTween : UxrGraphicTween
{
#region Inspector Properties/Serialized Fields
[SerializeField] private float _startFillAmount;
[SerializeField] private float _endFillAmount;
#endregion
#region Public Types & Data
public Image TargetImage => GetCachedComponent();
///
/// Animation start fill amount
///
public float StartFillAmount
{
get => _startFillAmount;
set => _startFillAmount = value;
}
///
/// Animation end fill amount
///
public float EndFillAmount
{
get => _endFillAmount;
set => _endFillAmount = value;
}
#endregion
#region Public Methods
///
/// Creates and starts a tweening animation for the value in an
/// component.
///
/// Target image
/// Start fill amount
/// End fill amount
/// Interpolation settings that control the animation
/// Optional callback when the animation finished
///
/// Tweening component that will update itself automatically. Can be used to stop the animation prematurely or
/// change parameters on the fly.
///
public static UxrImageFillTween Animate(Image image, float startFillAmount, float endFillAmount, UxrInterpolationSettings settings, Action finishedCallback = null)
{
UxrImageFillTween imageFillTween = image.GetOrAddComponent();
imageFillTween.StartFillAmount = startFillAmount;
imageFillTween.EndFillAmount = endFillAmount;
imageFillTween.InterpolationSettings = settings;
imageFillTween.FinishedCallback = finishedCallback;
imageFillTween.Restart();
return imageFillTween;
}
#endregion
#region Protected Overrides UxrGraphicTween
///
protected override void StoreOriginalValue()
{
_originalFillAmount = TargetImage.fillAmount;
}
#endregion
#region Protected Overrides UxrTween
///
protected override void RestoreOriginalValue()
{
if (HasOriginalValueStored)
{
TargetImage.fillAmount = _originalFillAmount;
}
}
///
protected override void Interpolate(float t)
{
TargetImage.fillAmount = Mathf.Lerp(StartFillAmount, EndFillAmount, t);
}
#endregion
#region Private Types & Data
private float _originalFillAmount;
#endregion
}
}