using UnityEngine;
using Lean.Common;
using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute;
namespace Lean.Touch
{
/// This component allows you to transform the current GameObject relative to the specified camera using a twist gesture.
[HelpURL(LeanTouch.HelpUrlPrefix + "LeanTwistRotate")]
[AddComponentMenu(LeanTouch.ComponentPathPrefix + "Twist Rotate")]
public class LeanTwistRotate : MonoBehaviour
{
/// The method used to find fingers to use with this component. See LeanFingerFilter documentation for more information.
public LeanFingerFilter Use = new LeanFingerFilter(true);
/// The camera we will be used to calculate relative rotations.
/// None/null = MainCamera.
public Camera Camera { set { _camera = value; } get { return _camera; } } [FSA("Camera")] [SerializeField] private Camera _camera;
/// Should the rotation be performed relative to the finger center?
public bool Relative { set { relative = value; } get { return relative; } } [FSA("Relative")] [SerializeField] private bool relative;
/// If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value.
/// -1 = Instantly change.
/// 1 = Slowly change.
/// 10 = Quickly change.
public float Damping { set { damping = value; } get { return damping; } } [FSA("Damping")] [FSA("Dampening")] [SerializeField] private float damping = -1.0f;
[SerializeField]
private Vector3 remainingTranslation;
[SerializeField]
private Quaternion remainingRotation = Quaternion.identity;
/// If you've set Use to ManuallyAddedFingers, then you can call this method to manually add a finger.
public void AddFinger(LeanFinger finger)
{
Use.AddFinger(finger);
}
/// If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove a finger.
public void RemoveFinger(LeanFinger finger)
{
Use.RemoveFinger(finger);
}
/// If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove all fingers.
public void RemoveAllFingers()
{
Use.RemoveAllFingers();
}
#if UNITY_EDITOR
protected virtual void Reset()
{
Use.UpdateRequiredSelectable(gameObject);
}
#endif
protected virtual void Awake()
{
Use.UpdateRequiredSelectable(gameObject);
}
protected virtual void Update()
{
// Store
var oldPosition = transform.localPosition;
var oldRotation = transform.localRotation;
// Get the fingers we want to use
var fingers = Use.UpdateAndGetFingers();
// Calculate the rotation values based on these fingers
var twistDegrees = LeanGesture.GetTwistDegrees(fingers);
if (twistDegrees != 0.0f)
{
if (relative == true)
{
var twistScreenCenter = LeanGesture.GetScreenCenter(fingers);
if (transform is RectTransform)
{
TranslateUI(twistDegrees, twistScreenCenter);
RotateUI(twistDegrees);
}
else
{
Translate(twistDegrees, twistScreenCenter);
Rotate(twistDegrees);
}
}
else
{
if (transform is RectTransform)
{
RotateUI(twistDegrees);
}
else
{
Rotate(twistDegrees);
}
}
}
// Increment
remainingTranslation += transform.localPosition - oldPosition;
remainingRotation *= Quaternion.Inverse(oldRotation) * transform.localRotation;
// Get t value
var factor = LeanHelper.GetDampenFactor(damping, Time.deltaTime);
// Dampen remainingDelta
var newRemainingTranslation = Vector3.Lerp(remainingTranslation, Vector3.zero, factor);
var newRemainingRotation = Quaternion.Slerp(remainingRotation, Quaternion.identity, factor);
// Shift this transform by the change in delta
transform.localPosition = oldPosition + remainingTranslation - newRemainingTranslation;
transform.localRotation = oldRotation * Quaternion.Inverse(newRemainingRotation) * remainingRotation;
// Update remainingDelta with the dampened value
remainingTranslation = newRemainingTranslation;
remainingRotation = newRemainingRotation;
}
protected virtual void TranslateUI(float twistDegrees, Vector2 twistScreenCenter)
{
var camera = _camera;
if (camera == null)
{
var canvas = transform.GetComponentInParent