using UnityEngine;
using Lean.Common;
using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute;
namespace Lean.Touch
{
/// This component allows you to rotate the current GameObject around the specified axis using finger twists.
[HelpURL(LeanTouch.HelpUrlPrefix + "LeanTwistRotateAxis")]
[AddComponentMenu(LeanTouch.ComponentPathPrefix + "Twist Rotate Axis")]
public class LeanTwistRotateAxis : 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 axis of rotation.
public Vector3 Axis { set { axis = value; } get { return axis; } } [FSA("Axis")] [SerializeField] private Vector3 axis = Vector3.down;
/// Rotate locally or globally?
public Space Space { set { space = value; } get { return space; } } [FSA("Sensitivity")] [SerializeField] private Space space = Space.Self;
/// The sensitivity of the rotation.
/// 1 = Default.
/// 2 = Double.
public float Sensitivity { set { sensitivity = value; } get { return sensitivity; } } [FSA("Sensitivity")] [SerializeField] private float sensitivity = 1.0f;
/// 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()
{
// Get the fingers we want to use
var fingers = Use.UpdateAndGetFingers();
// Calculate the rotation values based on these fingers
var twistDegrees = LeanGesture.GetTwistDegrees(fingers) * sensitivity;
// Perform rotation
transform.Rotate(axis, twistDegrees, space);
}
}
}
#if UNITY_EDITOR
namespace Lean.Touch.Editor
{
using TARGET = LeanTwistRotateAxis;
[UnityEditor.CanEditMultipleObjects]
[UnityEditor.CustomEditor(typeof(TARGET), true)]
public class LeanTwistRotateAxis_Editor : LeanEditor
{
protected override void OnInspector()
{
TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts);
Draw("Use");
Draw("axis", "The axis of rotation.");
Draw("space", "Rotate locally or globally?");
Draw("sensitivity", "The sensitivity of the rotation.\n\n1 = Default.\n\n2 = Double.");
}
}
}
#endif