// --------------------------------------------------------------------------------------------------------------------
//
// 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 programatically or using the inspector.
///
public class UxrPositionTween : UxrGraphicTween
{
#region Inspector Properties/Serialized Fields
[SerializeField] private Vector2 _startPosition;
[SerializeField] private Vector2 _endPosition;
#endregion
#region Public Types & Data
///
/// Animation start position
///
public Vector2 StartPosition
{
get => _startPosition;
set => _startPosition = value;
}
///
/// Animation end position
///
public Vector2 EndPosition
{
get => _endPosition;
set => _endPosition = value;
}
#endregion
#region Public Methods
///
/// Creates and starts a position tweening animation for the of a Unity
/// UI component.
///
/// Target graphic
/// Start position
/// End position
/// 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 UxrPositionTween Animate(Graphic graphic, Vector2 startPosition, Vector2 endPosition, UxrInterpolationSettings settings, Action finishedCallback = null)
{
UxrPositionTween positionTween = graphic.GetOrAddComponent();
positionTween.StartPosition = startPosition;
positionTween.EndPosition = endPosition;
positionTween.InterpolationSettings = settings;
positionTween.FinishedCallback = finishedCallback;
positionTween.Restart();
return positionTween;
}
///
/// Creates and starts a position tweening animation for the of a Unity
/// UI component. The animation will start using an offset with respect to the initial graphic
/// position and will end in the initial graphic position.
///
/// Target graphic
/// Horizontal offset to the initial x position where the animation will start.
/// Vertical offset to the initial y position where the animation will start.
/// 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 UxrPositionTween MoveIn(Graphic graphic, float horizontalOffset, float verticalOffset, UxrInterpolationSettings settings, Action finishedCallback = null)
{
UxrPositionTween positionTween = graphic.GetOrAddComponent();
positionTween.RestoreOriginalValue();
positionTween.StartPosition = positionTween.TargetRectTransform.anchoredPosition + new Vector2(horizontalOffset, verticalOffset);
positionTween.EndPosition = positionTween.TargetRectTransform.anchoredPosition;
positionTween.InterpolationSettings = settings;
positionTween.FinishedCallback = finishedCallback;
positionTween.Restart();
return positionTween;
}
///
/// Creates and starts a position tweening animation for the of a Unity
/// UI component. The animation will start in the initial graphic position and end with an
/// offset with respect to the initial graphic position.
///
/// Target graphic
/// Horizontal offset to the initial x position where the animation will end.
/// Vertical offset to the initial y position where the animation will end.
/// 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 UxrPositionTween MoveOut(Graphic graphic, float horizontalOffset, float verticalOffset, UxrInterpolationSettings settings, Action finishedCallback = null)
{
UxrPositionTween positionTween = graphic.GetOrAddComponent();
positionTween.RestoreOriginalValue();
positionTween.StartPosition = positionTween.TargetRectTransform.anchoredPosition;
positionTween.EndPosition = positionTween.TargetRectTransform.anchoredPosition + new Vector2(horizontalOffset, verticalOffset);
positionTween.InterpolationSettings = settings;
positionTween.FinishedCallback = finishedCallback;
positionTween.Restart();
return positionTween;
}
#endregion
#region Protected Overrides UxrTween
///
protected override void RestoreOriginalValue()
{
RestoreAnchoredPosition();
}
///
protected override void Interpolate(float t)
{
TargetRectTransform.anchoredPosition = Vector2.Lerp(StartPosition, EndPosition, t);
}
#endregion
}
}